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