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