1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22:
23: class cApiModuleCollection extends ItemCollection {
24:
25: 26: 27:
28: public function __construct() {
29: global $cfg;
30: parent::__construct($cfg['tab']['mod'], 'idmod');
31: $this->_setItemClass('cApiModule');
32: }
33:
34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
56: public function create($name, $idclient = NULL, $alias = '', $type = '',
57: $error = 'none', $description = '', $deletable = 0, $template = '',
58: $static = 0, $package_guid = '', $package_data = '', $author = '',
59: $created = '', $lastmodified = '') {
60: global $client, $auth;
61:
62: if (NULL === $idclient) {
63: $idclient = $client;
64: }
65:
66: if (empty($author)) {
67: $author = $auth->auth['uname'];
68: }
69: if (empty($created)) {
70: $created = date('Y-m-d H:i:s');
71: }
72: if (empty($lastmodified)) {
73: $lastmodified = date('Y-m-d H:i:s');
74: }
75:
76: $item = $this->createNewItem();
77:
78: $item->set('idclient', $idclient);
79: $item->set('name', $name);
80: $item->set('alias', $alias);
81: $item->set('type', $type);
82: $item->set('error', $error);
83: $item->set('description', $description);
84: $item->set('deletable', $deletable);
85: $item->set('template', $template);
86: $item->set('static', $static);
87: $item->set('package_guid', $package_guid);
88: $item->set('package_data', $package_data);
89: $item->set('author', $author);
90: $item->set('created', $created);
91: $item->set('lastmodified', $lastmodified);
92: $item->store();
93:
94: return $item;
95: }
96:
97: 98: 99: 100: 101: 102: 103:
104: public function getAllTypesByIdclient($idclient) {
105: $types = array();
106:
107: $sql = "SELECT type FROM `%s` WHERE idclient = %d GROUP BY type";
108: $sql = $this->db->prepare($sql, $this->table, $idclient);
109: $this->db->query($sql);
110: while ($this->db->nextRecord()) {
111: $types[] = $this->db->f('type');
112: }
113:
114: return $types;
115: }
116:
117: 118: 119: 120: 121: 122: 123: 124: 125:
126: public function getAllByIdclient($idclient, $oderBy = 'name') {
127: $records = array();
128:
129: if (!empty($oderBy)) {
130: $oderBy = ' ORDER BY ' . $this->db->escape($oderBy);
131: }
132: $sql = "SELECT * FROM `%s` WHERE idclient = %d{$oderBy}";
133: $sql = $this->db->prepare($sql, $this->table, $idclient);
134: $this->db->query($sql);
135: while ($this->db->nextRecord()) {
136: $records[$this->db->f('idmod')] = $this->db->toArray();
137: }
138:
139: return $records;
140: }
141:
142: 143: 144: 145: 146: 147:
148: public function getModulesInUse() {
149: global $cfg;
150:
151: $db = cRegistry::getDb();
152:
153: $sql = 'SELECT
154: c.idmod, c.idtpl, t.name
155: FROM
156: ' . $cfg['tab']['container'] . ' as c,
157: ' . $cfg['tab']['tpl'] . " as t
158: WHERE
159: t.idtpl = c.idtpl
160: GROUP BY c.idmod
161: ORDER BY t.name";
162: $db->query($sql);
163:
164: $aUsedTemplates = array();
165: if ($db->numRows() != 0) {
166: while ($db->nextRecord()) {
167: $aUsedTemplates[$db->f('idmod')][$db->f('idtpl')]['tpl_name'] = $db->f('name');
168: $aUsedTemplates[$db->f('idmod')][$db->f('idtpl')]['tpl_id'] = (int) $db->f('idtpl');
169: }
170: }
171:
172: return $aUsedTemplates;
173: }
174: }
175:
176: 177: 178: 179: 180: 181:
182: class cApiModule extends Item {
183:
184: 185: 186: 187: 188:
189: private $_translationPatternText = '/mi18n([\s]*)\("((\\\\"|[^"])*)"(([\s]*),([\s]*)[^\),]+)*\)/';
190:
191: 192: 193: 194: 195:
196: private $_translationPatternBase = '/mi18n([\s]*)\(([\s]*)"/';
197:
198: 199: 200: 201: 202:
203: private $_translationReplacement = 'mi18n("';
204:
205: 206: 207: 208:
209: protected $_error;
210:
211: 212: 213: 214: 215:
216: protected $_packageStructure;
217:
218: 219: 220: 221:
222: private $aUsedTemplates = array();
223:
224: 225: 226: 227: 228: 229:
230: public function __construct($mId = false) {
231: global $cfg, $cfgClient, $client;
232: parent::__construct($cfg['tab']['mod'], 'idmod');
233:
234:
235:
236:
237:
238: $this->setFilters(array(), array());
239:
240: if ($mId !== false) {
241: $this->loadByPrimaryKey($mId);
242: }
243:
244: if (isset($client) && $client != 0) {
245: $this->_packageStructure = array(
246: 'jsfiles' => $cfgClient[$client]['js']['path'],
247: 'tplfiles' => $cfgClient[$client]['tpl']['path'],
248: 'cssfiles' => $cfgClient[$client]['css']['path']
249: );
250: }
251: }
252:
253: 254: 255: 256: 257: 258:
259: public function getTranslatedName() {
260: global $lang;
261:
262:
263: if (!$this->isLoaded()) {
264: return false;
265: }
266:
267: $modname = $this->getProperty('translated-name', $lang);
268:
269: if ($modname === false) {
270: return $this->get('name');
271: } else {
272: return $modname;
273: }
274: }
275:
276: 277: 278: 279: 280: 281:
282: public function setTranslatedName($name) {
283: global $lang;
284: $this->setProperty('translated-name', $lang, $name);
285: }
286:
287: 288: 289: 290: 291: 292: 293: 294: 295:
296: function parseModuleForStringsLoadFromFile($cfg, $client, $lang) {
297: global $client;
298:
299:
300: if (!$this->isLoaded()) {
301: return false;
302: }
303:
304:
305: $contenidoModuleHandler = new cModuleHandler($this->get('idmod'));
306: $code = $contenidoModuleHandler->readOutput() . ' ';
307: $code .= $contenidoModuleHandler->readInput();
308:
309:
310: $strings = array();
311:
312:
313: $varr = preg_split($this->_translationPatternBase, $code, -1);
314: array_shift($varr);
315:
316: if (count($varr) > 0) {
317: foreach ($varr as $key => $value) {
318:
319: $closing = strpos($value, '")');
320:
321: if ($closing === false) {
322: $closing = strpos($value, '" )');
323: }
324:
325: if ($closing !== false) {
326: $value = substr($value, 0, $closing) . '")';
327: }
328:
329:
330: $varr[$key] = $this->_translationReplacement . $value;
331:
332:
333: preg_match_all($this->_translationPatternText, $varr[$key], $results);
334:
335:
336: if (is_array($results[1]) && count($results[2]) > 0) {
337: $strings = array_merge($strings, $results[2]);
338: }
339:
340:
341: unset($results);
342: }
343: }
344:
345:
346:
347:
348:
349: if (is_array($cfg['translatable_content_types']) && count($cfg['translatable_content_types']) > 0) {
350:
351: foreach ($cfg['translatable_content_types'] as $sContentType) {
352:
353: if (file_exists($cfg['contenido']['path'] . 'classes/class.' . strtolower($sContentType) . '.php')) {
354: cInclude('classes', 'class.' . strtolower($sContentType) . '.php');
355:
356:
357:
358:
359: if (class_exists($sContentType) && method_exists($sContentType, 'addModuleTranslations') && preg_match('/' . strtoupper($sContentType) . '\[\d+\]/', $code)) {
360:
361: $strings = call_user_func(array(
362: $sContentType,
363: 'addModuleTranslations'
364: ), $strings);
365: }
366: }
367: }
368: }
369:
370:
371: return array_unique($strings);
372: }
373:
374: 375: 376: 377: 378: 379:
380: public function parseModuleForStrings() {
381: if (!$this->isLoaded()) {
382: return false;
383: }
384:
385:
386:
387:
388:
389: $contenidoModuleHandler = new cModuleHandler($this->get('idmod'));
390: $code = $contenidoModuleHandler->readOutput() . ' ';
391: $code .= $contenidoModuleHandler->readInput();
392:
393:
394: $strings = array();
395:
396:
397: $varr = preg_split($this->_translationPatternBase, $code, -1);
398:
399: if (count($varr) > 1) {
400: foreach ($varr as $key => $value) {
401:
402: $closing = strpos($value, '")');
403:
404: if ($closing === false) {
405: $closing = strpos($value, '" )');
406: }
407:
408: if ($closing !== false) {
409: $value = substr($value, 0, $closing) . '")';
410: }
411:
412:
413: $varr[$key] = $this->_translationReplacement . $value;
414:
415:
416: preg_match_all($this->_translationPatternText, $varr[$key], $results);
417:
418:
419: if (is_array($results[1]) && count($results[2]) > 0) {
420: $strings = array_merge($strings, $results[2]);
421: }
422:
423:
424: unset($results);
425: }
426: }
427:
428: global $cfg;
429:
430:
431:
432:
433:
434: if (is_array($cfg['translatable_content_types']) && count($cfg['translatable_content_types']) > 0) {
435:
436: foreach ($cfg['translatable_content_types'] as $sContentType) {
437:
438: if (cFileHandler::exists($cfg['contenido']['path'] . 'classes/class.' . strtolower($sContentType) . '.php')) {
439: cInclude('classes', 'class.' . strtolower($sContentType) . '.php');
440:
441:
442:
443:
444: if (class_exists($sContentType) && method_exists($sContentType, 'addModuleTranslations') && preg_match('/' . strtoupper($sContentType) . '\[\d+\]/', $code)) {
445:
446: $strings = call_user_func(array(
447: $sContentType,
448: 'addModuleTranslations'
449: ), $strings);
450: }
451: }
452: }
453: }
454:
455:
456: return array_unique($strings);
457: }
458:
459: 460: 461: 462: 463: 464: 465: 466:
467: public function moduleInUse($module, $bSetData = false) {
468: global $cfg;
469:
470: $db = cRegistry::getDb();
471:
472: $sql = 'SELECT
473: c.idmod, c.idtpl, t.name
474: FROM
475: ' . $cfg['tab']['container'] . ' as c,
476: ' . $cfg['tab']['tpl'] . " as t
477: WHERE
478: c.idmod = '" . cSecurity::toInteger($module) . "' AND
479: t.idtpl=c.idtpl
480: GROUP BY c.idtpl
481: ORDER BY t.name";
482: $db->query($sql);
483:
484: if ($db->numRows() == 0) {
485: return false;
486: } else {
487: $i = 0;
488:
489: if ($bSetData === true) {
490: while ($db->nextRecord()) {
491: $this->aUsedTemplates[$i]['tpl_name'] = $db->f('name');
492: $this->aUsedTemplates[$i]['tpl_id'] = (int) $db->f('idmod');
493: $i++;
494: }
495: }
496:
497: return true;
498: }
499: }
500:
501: 502: 503: 504: 505: 506:
507: public function getUsedTemplates() {
508: return $this->aUsedTemplates;
509: }
510:
511: 512: 513: 514: 515: 516:
517: public function isOldModule() {
518:
519: $scanKeywords = array(
520: '$cfgTab',
521: 'idside',
522: 'idsidelang'
523: );
524:
525: $input = $this->get('input');
526: $output = $this->get('output');
527:
528: foreach ($scanKeywords as $keyword) {
529: if (strstr($input, $keyword)) {
530: return true;
531: }
532: if (strstr($output, $keyword)) {
533: return true;
534: }
535: }
536: }
537:
538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548:
549: public function getField($field, $bSafe = true) {
550: $value = parent::getField($field, $bSafe);
551:
552: switch ($field) {
553: case 'name':
554: if ($value == '') {
555: $value = i18n('- Unnamed module -');
556: }
557: }
558:
559: return $value;
560: }
561:
562: 563: 564: 565: 566: 567: 568: 569: 570: 571:
572: public function store($bJustStore = false) {
573: if ($bJustStore) {
574:
575: $success = parent::store();
576: } else {
577: cInclude('includes', 'functions.con.php');
578:
579: $success = parent::store();
580:
581: conGenerateCodeForAllartsUsingMod($this->get('idmod'));
582: }
583:
584: return $success;
585: }
586:
587: 588: 589: 590: 591: 592: 593: 594:
595: private function _parseImportFile($sFile) {
596: $oXmlReader = new cXmlReader();
597: $oXmlReader->load($sFile);
598:
599: $aData = array();
600: $aInformation = array(
601: 'name',
602: 'description',
603: 'type',
604: 'input',
605: 'output',
606: 'alias'
607: );
608:
609: foreach ($aInformation as $sInfoName) {
610: $sPath = '/module/' . $sInfoName;
611:
612: $value = $oXmlReader->getXpathValue($sPath);
613: $aData[$sInfoName] = $value;
614: }
615:
616: return $aData;
617: }
618:
619: 620: 621: 622: 623: 624: 625:
626: private function _getModuleProperties($sFile) {
627: $ret = array();
628:
629: $aModuleData = $this->_parseImportFile($sFile);
630: if (count($aModuleData) > 0) {
631: foreach ($aModuleData as $key => $value) {
632:
633: if ($key != 'output' && $key != 'input') {
634: $ret[$key] = addslashes($value);
635: }
636: }
637: }
638:
639: return $ret;
640: }
641:
642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652:
653: function import($sFile, $tempName, $show_notification = true) {
654: global $cfgClient, $db, $client, $cfg, $encoding, $lang;
655: $zip = new ZipArchive();
656: $notification = new cGuiNotification();
657:
658:
659:
660: $modulName = substr($sFile, 0, -4);
661:
662: $sModulePath = $cfgClient[$client]['module']['path'] . $modulName;
663: $sTempPath = $cfg['path']['contenido_temp'] . 'module_import_' . $modulName;
664:
665:
666: if (is_dir($sModulePath)) {
667: if ($show_notification == true) {
668: $notification->displayNotification('error', i18n('Module already exists!'));
669: }
670:
671: return false;
672: }
673:
674: if ($zip->open($tempName)) {
675: if ($zip->extractTo($sTempPath)) {
676: $zip->close();
677:
678:
679: if (cFileHandler::exists($sTempPath . '/info.xml')) {
680:
681:
682: $modules = new cApiModuleCollection();
683:
684: $module = $modules->create($modulName);
685: $moduleProperties = $this->_getModuleProperties($sTempPath . '/info.xml');
686:
687:
688: foreach ($moduleProperties as $key => $value) {
689: $module->set($key, $value);
690: }
691:
692: $module->store();
693: } else {
694: if ($show_notification == true) {
695: $notification->displayNotification('error', i18n('Import failed, could load module information!'));
696: }
697: return false;
698: }
699: } else {
700: if ($show_notification == true) {
701: $notification->displayNotification('error', i18n('Import failed, could not extract zip file!'));
702: }
703:
704: return false;
705: }
706: } else {
707: if ($show_notification == true) {
708: $notification->displayNotification('error', i18n('Could not open the zip file!'));
709: }
710:
711: return false;
712: }
713:
714:
715: cDirHandler::rename($sTempPath, $sModulePath);
716:
717: return true;
718: }
719:
720: 721: 722: 723: 724: 725: 726:
727: function importModuleFromXML($sFile) {
728: global $db, $cfgClient, $client, $cfg, $encoding, $lang;
729:
730: $inputOutput = array();
731: $notification = new cGuiNotification();
732:
733: $aModuleData = $this->_parseImportFile($sFile);
734: if (count($aModuleData) > 0) {
735: foreach ($aModuleData as $key => $value) {
736: if ($this->get($key) != $value) {
737:
738: if ($key == 'output' || $key == 'input') {
739: $inputOutput[$key] = $value;
740: } else {
741: $this->set($key, addslashes($value));
742: }
743: }
744: }
745:
746: $moduleName = cString::cleanURLCharacters($this->get('name'));
747: $moduleAlias = cString::cleanURLCharacters($this->get('alias'));
748:
749:
750: if ($this->get('alias') == '') {
751: $this->set('alias', $moduleName);
752: }
753:
754: if (is_dir($cfgClient[$client]['module']['path'] . $moduleAlias)) {
755: $notification->displayNotification('error', i18n('Module exist!'));
756: return false;
757: } else {
758:
759: $this->store();
760: $contenidoModuleHandler = new cModuleHandler($this->get('idmod'));
761: if (!$contenidoModuleHandler->createModule($inputOutput['input'], $inputOutput['output'])) {
762: $notification->displayNotification('error', i18n('Could not create a module!'));
763: return false;
764: } else {
765:
766: $contenidoModuleHandler->saveInfoXML();
767: }
768: }
769: } else {
770: $notification->displayNotification('error', i18n('Could not parse xml file!'));
771:
772: return false;
773: }
774:
775: return true;
776: }
777:
778: 779: 780: 781: 782: 783: 784: 785: 786:
787: private function _addFolderToZip($dir, $zipArchive, $zipdir = '') {
788: if (is_dir($dir)) {
789: if (false !== $handle = cDirHandler::read($dir)) {
790: if (!empty($zipdir)) {
791: $zipArchive->addEmptyDir($zipdir);
792: }
793:
794: foreach ($handle as $file) {
795:
796: if (!is_file($dir . $file)) {
797:
798: if (false === cFileHandler::fileNameIsDot($file)) {
799: $this->_addFolderToZip($dir . $file . '/', $zipArchive, $zipdir . $file . '/');
800: }
801: } else {
802:
803: if ($zipArchive->addFile($dir . $file, $zipdir . $file) === false) {
804: $notification = new cGuiNotification();
805: $notification->displayNotification('error', sprintf(i18n('Could not add file %s to zip!'), $file));
806: }
807: }
808: }
809: }
810: }
811: }
812:
813: 814: 815:
816: public function export() {
817: $notification = new cGuiNotification();
818: $moduleHandler = new cModuleHandler($this->get('idmod'));
819:
820:
821: if ($moduleHandler->modulePathExists()) {
822: $zip = new ZipArchive();
823: $zipName = $this->get('alias') . '.zip';
824: $cfg = cRegistry::getConfig();
825: $path = $cfg['path']['contenido_temp'];
826: if ($zip->open($path . $zipName, ZipArchive::CREATE)) {
827: $this->_addFolderToZip($moduleHandler->getModulePath(), $zip);
828:
829: $zip->close();
830:
831: header('Content-Type: application/zip');
832: header('Content-Length: ' . filesize($path . $zipName));
833: header("Content-Disposition: attachment; filename=\"$zipName\"");
834: readfile($path . $zipName);
835:
836:
837: unlink($path . $zipName);
838: } else {
839: $notification->displayNotification('error', i18n('Could not open the zip file!'));
840: }
841: } else {
842: $notification->displayNotification('error', i18n("Module don't exist on file system!"));
843: }
844: }
845:
846: 847: 848: 849: 850: 851: 852: 853: 854: 855:
856: public function setField($name, $value, $bSafe = true) {
857: switch ($name) {
858: case 'deletable':
859: case 'static':
860: $value = ($value == 1) ? 1 : 0;
861: break;
862: case 'idclient':
863: $value = (int) $value;
864: break;
865: }
866:
867: return parent::setField($name, $value, $bSafe);
868: }
869:
870: 871: 872: 873: 874: 875: 876: 877: 878: 879: 880:
881: public static function processContainerInputCode($containerNr, $containerCfg, $moduleInputCode) {
882: $containerConfigurations = array();
883: if (!empty($containerCfg)) {
884: $containerConfigurations = cApiContainerConfiguration::parseContainerValue($containerCfg);
885: }
886:
887: $CiCMS_Var = '$C' . $containerNr . 'CMS_VALUE';
888: $CiCMS_Values = array();
889:
890: foreach ($containerConfigurations as $key3 => $value3) {
891:
892: $tmp = conHtmlSpecialChars($value3);
893: $tmp = str_replace('\\', '\\\\', $tmp);
894:
895: $CiCMS_Values[] = $CiCMS_Var . '[' . $key3 . '] = "' . $tmp . '";';
896: $moduleInputCode = str_replace("\$CMS_VALUE[$key3]", $tmp, $moduleInputCode);
897: $moduleInputCode = str_replace("CMS_VALUE[$key3]", $tmp, $moduleInputCode);
898: }
899:
900: $moduleInputCode = str_replace('CMS_VALUE', $CiCMS_Var, $moduleInputCode);
901: $moduleInputCode = str_replace("\$" . $CiCMS_Var, $CiCMS_Var, $moduleInputCode);
902: $moduleInputCode = str_replace('CMS_VAR', 'C' . $containerNr . 'CMS_VAR', $moduleInputCode);
903:
904: $CiCMS_Values = implode("\n", $CiCMS_Values);
905:
906: return $CiCMS_Values . "\n" . $moduleInputCode;
907: }
908: }
909: