1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17:
18: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
19:
20: cInclude('includes', 'functions.api.string.php');
21: cInclude('includes', 'functions.con.php');
22:
23: 24: 25: 26: 27: 28: 29: 30:
31: class cModuleSynchronizer extends cModuleHandler {
32:
33: 34: 35: 36: 37:
38: private $_lastIdMod = 0;
39:
40: 41: 42: 43: 44: 45: 46: 47:
48: private function _syncModule($dir, $oldModulName, $newModulName) {
49: global $client;
50:
51: if ($this->_isExistInTable($oldModulName, $client) == false) {
52:
53: $this->_addModul($newModulName, $client);
54: cRegistry::appendLastOkMessage(sprintf(i18n('Module %s successfully synchronized'), $newModulName));
55: } else {
56:
57: if ($oldModulName != $newModulName) {
58: $this->_updateModulnameInDb($oldModulName, $newModulName, $client);
59: }
60: }
61: }
62:
63: 64: 65: 66: 67: 68: 69:
70: private function _renameFiles($dir, $oldModulName, $newModulName) {
71: if (cFileHandler::exists($dir . $newModulName . '/' . $this->_directories['php'] . $oldModulName . '_input.php') == true) {
72: rename($dir . $newModulName . '/' . $this->_directories['php'] . $oldModulName . '_input.php', $dir . $newModulName . '/' . $this->_directories['php'] . $newModulName . '_input.php');
73: }
74:
75: if (cFileHandler::exists($dir . $newModulName . '/' . $this->_directories['php'] . $oldModulName . '_output.php') == true) {
76: rename($dir . $newModulName . '/' . $this->_directories['php'] . $oldModulName . '_output.php', $dir . $newModulName . '/' . $this->_directories['php'] . $newModulName . '_output.php');
77: }
78:
79: if (cFileHandler::exists($dir . $newModulName . '/' . $this->_directories['css'] . $oldModulName . '.css') == true) {
80: rename($dir . $newModulName . '/' . $this->_directories['css'] . $oldModulName . '.css', $dir . $newModulName . '/' . $this->_directories['css'] . $newModulName . '.css');
81: }
82:
83: if (cFileHandler::exists($dir . $newModulName . '/' . $this->_directories['js'] . $oldModulName . '.js') == true) {
84: rename($dir . $newModulName . '/' . $this->_directories['js'] . $oldModulName . '.js', $dir . $newModulName . '/' . $this->_directories['js'] . $newModulName . '.js');
85: }
86: }
87:
88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101:
102: private function _renameFileAndDir($dir, $dirNameOld, $dirNameNew, $client) {
103: if (rename($dir . $dirNameOld, $dir . $dirNameNew) == FALSE) {
104: return false;
105: } else {
106: $this->_renameFiles($dir, $dirNameOld, $dirNameNew);
107: }
108: return true;
109: }
110:
111: 112: 113: 114: 115: 116: 117:
118: public function compareFileAndModuleTimestamp() {
119: global $cfg, $cfgClient;
120:
121: $synchLock = 0;
122:
123: $sql = sprintf('SELECT UNIX_TIMESTAMP(mod1.lastmodified) AS lastmodified,mod1.idclient,description,type, mod1.name, mod1.alias, mod1.idmod FROM %s AS mod1 WHERE mod1.idclient = %s', $this->_cfg['tab']['mod'], $this->_client);
124: $notification = new cGuiNotification();
125:
126: $db = cRegistry::getDb();
127: $db->query($sql);
128: $retIdMod = 0;
129:
130: while ($db->nextRecord()) {
131: $showMessage = false;
132:
133: $modulePath = $cfgClient[$db->f('idclient')]['module']['path'] . $db->f('alias') . '/';
134: $modulePHP = $modulePath . $this->_directories['php'] . $db->f('alias');
135:
136: $lastmodified = $db->f('lastmodified');
137:
138: $lastmodInput = $lastmodOutput = 0;
139:
140: if (cFileHandler::exists($modulePHP . '_input.php')) {
141: $lastmodInput = filemtime($modulePHP . '_input.php');
142: }
143:
144: if (cFileHandler::exists($modulePHP . '_output.php')) {
145: $lastmodOutput = filemtime($modulePHP . '_output.php');
146: }
147:
148: if (cFileHandler::exists($modulePath . "info.xml")) {
149: $lastModInfo = filemtime($modulePath . "info.xml");
150: if ($lastModInfo > $lastmodified) {
151: $modInfo = cXmlBase::xmlStringToArray(cFileHandler::read($modulePath . "info.xml"));
152: $mod = new cApiModule($db->f("idmod"));
153: if ($modInfo["description"] != $mod->get("description")) {
154: $mod->set("description", $modInfo["description"]);
155: $this->setLastModified($lastModInfo, $db->f('idmod'));
156: }
157: if ($modInfo["type"] != $mod->get("type")) {
158: $mod->set("type", $modInfo["type"]);
159: $this->setLastModified($lastModInfo, $db->f('idmod'));
160: }
161:
162: if ($modInfo["name"] != $mod->get("name")) {
163: $mod->set("name", $modInfo["name"]);
164: $this->setLastModified($lastModInfo, $db->f('idmod'));
165: }
166:
167: if ($modInfo["alias"] != $mod->get("alias")) {
168: $mod->set("alias", $modInfo["alias"]);
169: $this->setLastModified($lastModInfo, $db->f('idmod'));
170: }
171: $mod->store();
172: $synchLock = 1;
173: $showMessage = true;
174: }
175: }
176:
177: $lastmodabsolute = max($lastmodInput, $lastmodOutput);
178: if ($lastmodified < $lastmodabsolute) {
179:
180: $synchLock = 1;
181: $this->setLastModified($lastmodabsolute, $db->f('idmod'));
182: conGenerateCodeForAllArtsUsingMod($db->f('idmod'));
183: $showMessage = true;
184: }
185:
186: if (($idmod = $this->_synchronizeFilesystemAndDb($db)) != 0) {
187: $retIdMod = $idmod;
188: }
189:
190: if ($showMessage) {
191: cRegistry::appendLastOkMessage(sprintf(i18n('Module %s successfully synchronized'), $db->f('name')));
192: }
193: }
194:
195: if ($synchLock == 0) {
196: cRegistry::addInfoMessage(i18n('All modules are already synchronized'));
197: }
198:
199:
200: return $retIdMod;
201: }
202:
203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213:
214: private function _synchronizeFilesystemAndDb($db) {
215: $returnIdMod = 0;
216: $this->initWithDatabaseRow($db);
217:
218: if ($this->modulePathExists() == false) {
219: $modul = new cApiModule($db->f('idmod'));
220: $returnIdMod = $db->f('idmod');
221: if ($modul->moduleInUse($db->f('idmod')) == true) {
222:
223: if ($this->createModule() == false) {
224: $notification = new cGuiNotification();
225: $notification->displayNotification('error', i18n("Can not create module") . " " . $db->f('name'));
226: }
227: } else {
228:
229: $sql = sprintf('DELETE FROM %s WHERE idmod = %s AND idclient = %s', $this->_cfg['tab']['mod'], $db->f('idmod'), $this->_client);
230: $myDb = cRegistry::getDb();
231: $myDb->query($sql);
232: }
233: }
234: return $returnIdMod;
235: }
236:
237: 238: 239: 240: 241: 242: 243: 244: 245:
246: public function synchronize() {
247: global $cfg, $cfgClient;
248:
249:
250: $dir = $cfgClient[$this->_client]['module']['path'];
251:
252: if (is_dir($dir)) {
253: if (false !== ($handle = cDirHandler::read($dir))) {
254: foreach ($handle as $file) {
255: if (false === cFileHandler::fileNameBeginsWithDot($file) && is_dir($dir . $file . '/')) {
256: $newFile = cString::cleanURLCharacters($file);
257:
258: if ($newFile == $file) {
259: $this->_syncModule($dir, $file, $newFile);
260: } else {
261: if (is_dir($dir . $newFile)) {
262:
263:
264: $newDirName = $newFile . substr(md5(time() . rand(0, time())), 0, 4);
265:
266:
267: if ($this->_renameFileAndDir($dir, $file, $newDirName, $this->_client) != false) {
268: $this->_syncModule($dir, $file, $newDirName);
269: }
270: } else {
271:
272: if ($this->_renameFileAndDir($dir, $file, $newFile, $this->_client) != false) {
273: $this->_syncModule($dir, $file, $newFile);
274: }
275: }
276: }
277: }
278: }
279: }
280: }
281:
282:
283: return $this->_lastIdMod;
284: }
285:
286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297:
298: private function _isExistInTable($alias, $idclient) {
299: $db = cRegistry::getDb();
300:
301:
302: $sql = sprintf("SELECT * FROM %s WHERE alias='%s' AND idclient=%s", $this->_cfg['tab']['mod'], $alias, $idclient);
303:
304: $db->query($sql);
305:
306:
307: if ($db->nextRecord()) {
308: return true;
309: } else {
310: return false;
311: }
312: }
313:
314: 315: 316: 317: 318: 319: 320: 321: 322: 323:
324: private function _updateModulnameInDb($oldName, $newName, $idclient) {
325: $db = cRegistry::getDb();
326:
327:
328: $sql = sprintf("SELECT * FROM %s WHERE alias='%s' AND idclient=%s", $this->_cfg['tab']['mod'], $oldName, $idclient);
329:
330: $db->query($sql);
331:
332:
333: if ($db->nextRecord()) {
334: $sqlUpdateName = sprintf("UPDATE %s SET alias='%s' WHERE idmod=%s", $this->_cfg['tab']['mod'], $newName, $db->f('idmod'));
335: $db->query($sqlUpdateName);
336: return;
337: }
338: }
339:
340: 341: 342: 343: 344: 345: 346: 347:
348: private function _addModul($name, $idclient) {
349:
350: $oModColl = new cApiModuleCollection();
351: $oMod = $oModColl->create($name, $idclient, $name);
352: if (is_object($oMod)) {
353:
354: $this->_lastIdMod = $oMod->get('idmod');
355: }
356: }
357:
358: 359: 360: 361: 362: 363: 364: 365:
366: public function setLastModified($timestamp, $idmod) {
367: $oMod = new cApiModule((int) $idmod);
368: if ($oMod->isLoaded()) {
369: $oMod->set('lastmodified', date('Y-m-d H:i:s', $timestamp));
370: $oMod->store();
371: }
372: }
373:
374: }
375: