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