1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora;
9:
10: use Aurora\System\Module\Settings;
11: use Composer\Script\Event;
12:
13: /**
14: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
15: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
16: * @copyright Copyright (c) 2019, Afterlogic Corp.
17: */
18: class Installer
19: {
20: /**
21: * NOTE! This method should be run from composer.
22: */
23: public static function checkConfigs()
24: {
25: $RED = "\033[1;31m";
26: $YELLOW = "\033[1;33m";
27: $GREEN = "\033[1;32m";
28: $NC = "\033[0m";
29:
30: $sBaseDir = dirname(dirname(__File__));
31: $sModulesDir = $sBaseDir . "/modules/";
32: $aModuleDirs = glob($sModulesDir . '*', GLOB_ONLYDIR);
33:
34: $aModulesOk = [];
35: $aModulesHasError = [];
36: $aModulesNoConfig = [];
37:
38: foreach ($aModuleDirs as $sModuleDir) {
39: $sModuleName = str_replace($sModulesDir, "", $sModuleDir);
40:
41: $sConfigFile = $sModuleDir . "/config.json";
42: if (file_exists($sConfigFile)) {
43: if (json_decode(file_get_contents($sConfigFile))) {
44: $aModulesOk[] = $sModuleName;
45: } else {
46: $aModulesHasError[] = $sModuleName;
47: }
48: } else {
49: $aModulesNoConfig[] = $sModuleName;
50: }
51: }
52:
53: echo $GREEN . count($aModuleDirs) . " modules were processed" . $NC . "\r\n\r\n";
54:
55: if (count($aModulesHasError) > 0) {
56: echo $YELLOW . "The following modules have syntax problems in the config file (" . count($aModulesHasError) . "):" . $NC . "\r\n";
57: echo $RED . implode("\r\n", $aModulesHasError) . $NC;
58: echo "\r\n\r\n";
59: }
60:
61: if (count($aModulesNoConfig) > 0) {
62: echo $YELLOW . "The following modules have no config files (" . count($aModulesNoConfig) . "):" . $NC . "\r\n";
63: echo $RED . implode("\r\n", $aModulesNoConfig) . $NC;
64: echo "\r\n\r\n";
65: }
66:
67: if (count($aModulesOk) > 0) {
68: echo $GREEN . count($aModulesOk) . " modules have no problem with config files" . $NC;
69: echo "\r\n";
70: }
71: }
72:
73: /**
74: * NOTE! This method should be run from composer.
75: */
76: public static function updateConfigs()
77: {
78: $sBaseDir = dirname(__File__);
79:
80: include_once $sBaseDir . '/autoload.php';
81:
82: $sMessage = "Configuration was updated successfully";
83: if (!\Aurora\System\Api::UpdateSettings()) {
84: $sMessage = "An error occurred while updating the configuration";
85: }
86:
87: echo $sMessage . "\r\n";
88: }
89:
90: /**
91: * NOTE! This method should be run from composer.
92: */
93: public static function preConfigForce($event)
94: {
95: self::preConfig($event);
96: }
97:
98: /**
99: * NOTE! This method should be run from composer.
100: */
101: public static function preConfigSafe($event)
102: {
103: $sBaseDir = dirname(__File__);
104:
105: //Checking that configuration files already exist
106: if (count(glob(dirname($sBaseDir) . "/data/settings/modules/*")) !== 0) {
107: echo "The config files are already exist \r\n";
108: return;
109: } else {
110: self::preConfig($event);
111: }
112: }
113:
114: /**
115: * NOTE! This method should be run from composer.
116: */
117: private static function preConfig($event)
118: {
119: $sConfigFilename = 'pre-config.json';
120: $sBaseDir = dirname(__File__);
121: $sMessage = "Configuration was updated successfully";
122:
123: $oExtra = $event->getComposer()->getPackage()->getExtra();
124:
125: if ($oExtra && isset($oExtra['aurora-installer-pre-config'])) {
126: $sConfigFilename = $oExtra['aurora-installer-pre-config'];
127: }
128:
129: $sConfigPath = dirname($sBaseDir) . '/' . $sConfigFilename;
130:
131: if (file_exists($sConfigPath)) {
132: $sPreConfig = file_get_contents($sConfigPath);
133: $oPreConfig = json_decode($sPreConfig, true);
134:
135: if ($oPreConfig) {
136: include_once $sBaseDir . '/autoload.php';
137:
138: \Aurora\System\Api::Init();
139:
140: if ($oPreConfig['modules']) {
141: $oModuleManager = \Aurora\System\Api::GetModuleManager();
142:
143: foreach ($oPreConfig['modules'] as $sModuleName => $oModuleConfig) {
144: if (!empty($sModuleName)) {
145: $oSettings = $oModuleManager->getModuleSettings($sModuleName);
146: if ($oSettings instanceof Settings) {
147: $oSettings->Load();
148: foreach ($oModuleConfig as $sConfigName => $mConfigValue) {
149: //overriding modules default configuration with pre-configuration data
150: $oProp = $oSettings->GetSettingsProperty($sConfigName);
151: if ($oProp) {
152: if (!empty($oProp->SpecType)) {
153: $mConfigValue = \Aurora\System\Enums\EnumConvert::FromXml($mConfigValue, $oProp->SpecType);
154: }
155: $oSettings->SetValue($sConfigName, $mConfigValue);
156: }
157: }
158: $oSettings->Save();
159: }
160: }
161: }
162: }
163: if ($oPreConfig['system']) {
164: $oSettings = &\Aurora\System\Api::GetSettings();
165: foreach ($oPreConfig['system'] as $mKey => $mSett) {
166: $oSettings->{$mKey} = $mSett;
167: }
168: $oSettings->Save();
169: }
170: } else {
171: $sMessage = "Invalid config file";
172: }
173: } else {
174: $sMessage = "Config file didn't found";
175: }
176:
177: echo $sMessage . "\r\n";
178: }
179: }
180: