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: public function getModulesInUse() {
122: global $cfg;
123:
124: $db = cRegistry::getDb();
125:
126: $sql = 'SELECT
127: c.idmod, c.idtpl, t.name
128: FROM
129: ' . $cfg['tab']['container'] . ' as c,
130: ' . $cfg['tab']['tpl'] . " as t
131: WHERE
132: t.idtpl = c.idtpl
133: GROUP BY c.idmod
134: ORDER BY t.name";
135: $db->query($sql);
136:
137: $aUsedTemplates = array();
138: if ($db->numRows() != 0) {
139: while ($db->nextRecord()) {
140: $aUsedTemplates[$db->f('idmod')][$db->f('idtpl')]['tpl_name'] = $db->f('name');
141: $aUsedTemplates[$db->f('idmod')][$db->f('idtpl')]['tpl_id'] = (int) $db->f('idtpl');
142: }
143: }
144:
145: return $aUsedTemplates;
146: }
147:
148: }
149:
150: 151: 152: 153: 154: 155:
156: class cApiModule extends Item {
157:
158: 159: 160:
161: private $_translationPatternText = '/mi18n([\s]*)\("((\\\\"|[^"])*)"(([\s]*),([\s]*)[^\),]+)*\)/';
162:
163: 164: 165:
166: private $_translationPatternBase = '/mi18n([\s]*)\(([\s]*)"/';
167:
168: 169: 170:
171: private $_translationReplacement = 'mi18n("';
172:
173: protected $_error;
174:
175: 176: 177: 178: 179:
180: protected $_packageStructure;
181:
182: 183: 184: 185:
186: private $aUsedTemplates = array();
187:
188: 189: 190: 191: 192:
193: public function __construct($mId = false) {
194: global $cfg, $cfgClient, $client;
195: parent::__construct($cfg['tab']['mod'], 'idmod');
196:
197:
198:
199:
200:
201: $this->setFilters(array(), array());
202:
203: if ($mId !== false) {
204: $this->loadByPrimaryKey($mId);
205: }
206:
207: if (isset($client) && $client != 0) {
208: $this->_packageStructure = array(
209: 'jsfiles' => $cfgClient[$client]['js']['path'],
210: 'tplfiles' => $cfgClient[$client]['tpl']['path'],
211: 'cssfiles' => $cfgClient[$client]['css']['path']
212: );
213: }
214: }
215:
216: 217: 218: 219: 220:
221: public function getTranslatedName() {
222: global $lang;
223:
224:
225: if (!$this->isLoaded()) {
226: return false;
227: }
228:
229: $modname = $this->getProperty('translated-name', $lang);
230:
231: if ($modname === false) {
232: return $this->get('name');
233: } else {
234: return $modname;
235: }
236: }
237:
238: 239: 240: 241: 242:
243: public function setTranslatedName($name) {
244: global $lang;
245: $this->setProperty('translated-name', $lang, $name);
246: }
247:
248: 249: 250: 251:
252: function parseModuleForStringsLoadFromFile($cfg, $client, $lang) {
253: global $client;
254:
255:
256: if (!$this->isLoaded()) {
257: return false;
258: }
259:
260:
261: $contenidoModuleHandler = new cModuleHandler($this->get('idmod'));
262: $code = $contenidoModuleHandler->readOutput() . ' ';
263: $code .= $contenidoModuleHandler->readInput();
264:
265:
266: $strings = array();
267:
268:
269: $varr = preg_split($this->_translationPatternBase, $code, -1);
270: array_shift($varr);
271:
272: if (count($varr) > 0) {
273: foreach ($varr as $key => $value) {
274:
275: $closing = strpos($value, '")');
276:
277: if ($closing === false) {
278: $closing = strpos($value, '" )');
279: }
280:
281: if ($closing !== false) {
282: $value = substr($value, 0, $closing) . '")';
283: }
284:
285:
286: $varr[$key] = $this->_translationReplacement . $value;
287:
288:
289: preg_match_all($this->_translationPatternText, $varr[$key], $results);
290:
291:
292: if (is_array($results[1]) && count($results[2]) > 0) {
293: $strings = array_merge($strings, $results[2]);
294: }
295:
296:
297: unset($results);
298: }
299: }
300:
301:
302:
303:
304:
305: if (is_array($cfg['translatable_content_types']) && count($cfg['translatable_content_types']) > 0) {
306:
307: foreach ($cfg['translatable_content_types'] as $sContentType) {
308:
309: if (file_exists($cfg['contenido']['path'] . 'classes/class.' . strtolower($sContentType) . '.php')) {
310: cInclude('classes', 'class.' . strtolower($sContentType) . '.php');
311:
312:
313:
314:
315: if (class_exists($sContentType) && method_exists($sContentType, 'addModuleTranslations') && preg_match('/' . strtoupper($sContentType) . '\[\d+\]/', $code)) {
316:
317: $strings = call_user_func(array(
318: $sContentType,
319: 'addModuleTranslations'
320: ), $strings);
321: }
322: }
323: }
324: }
325:
326:
327: return array_unique($strings);
328: }
329:
330: 331: 332: 333: 334:
335: public function parseModuleForStrings() {
336: if (!$this->isLoaded()) {
337: return false;
338: }
339:
340:
341:
342:
343:
344: $contenidoModuleHandler = new cModuleHandler($this->get('idmod'));
345: $code = $contenidoModuleHandler->readOutput() . ' ';
346: $code .= $contenidoModuleHandler->readInput();
347:
348:
349: $strings = array();
350:
351:
352: $varr = preg_split($this->_translationPatternBase, $code, -1);
353:
354: if (count($varr) > 1) {
355: foreach ($varr as $key => $value) {
356:
357: $closing = strpos($value, '")');
358:
359: if ($closing === false) {
360: $closing = strpos($value, '" )');
361: }
362:
363: if ($closing !== false) {
364: $value = substr($value, 0, $closing) . '")';
365: }
366:
367:
368: $varr[$key] = $this->_translationReplacement . $value;
369:
370:
371: preg_match_all($this->_translationPatternText, $varr[$key], $results);
372:
373:
374: if (is_array($results[1]) && count($results[2]) > 0) {
375: $strings = array_merge($strings, $results[2]);
376: }
377:
378:
379: unset($results);
380: }
381: }
382:
383: global $cfg;
384:
385:
386:
387:
388:
389: if (is_array($cfg['translatable_content_types']) && count($cfg['translatable_content_types']) > 0) {
390:
391: foreach ($cfg['translatable_content_types'] as $sContentType) {
392:
393: if (cFileHandler::exists($cfg['contenido']['path'] . 'classes/class.' . strtolower($sContentType) . '.php')) {
394: cInclude('classes', 'class.' . strtolower($sContentType) . '.php');
395:
396:
397:
398:
399: if (class_exists($sContentType) && method_exists($sContentType, 'addModuleTranslations') && preg_match('/' . strtoupper($sContentType) . '\[\d+\]/', $code)) {
400:
401: $strings = call_user_func(array(
402: $sContentType,
403: 'addModuleTranslations'
404: ), $strings);
405: }
406: }
407: }
408: }
409:
410:
411: return array_unique($strings);
412: }
413:
414: 415: 416: 417: 418:
419: public function moduleInUse($module, $bSetData = false) {
420: global $cfg;
421:
422: $db = cRegistry::getDb();
423:
424: $sql = 'SELECT
425: c.idmod, c.idtpl, t.name
426: FROM
427: ' . $cfg['tab']['container'] . ' as c,
428: ' . $cfg['tab']['tpl'] . " as t
429: WHERE
430: c.idmod = '" . cSecurity::toInteger($module) . "' AND
431: t.idtpl=c.idtpl
432: GROUP BY c.idtpl
433: ORDER BY t.name";
434: $db->query($sql);
435:
436: if ($db->numRows() == 0) {
437: return false;
438: } else {
439: $i = 0;
440:
441: if ($bSetData === true) {
442: while ($db->nextRecord()) {
443: $this->aUsedTemplates[$i]['tpl_name'] = $db->f('name');
444: $this->aUsedTemplates[$i]['tpl_id'] = (int) $db->f('idmod');
445: $i++;
446: }
447: }
448:
449: return true;
450: }
451: }
452:
453: 454: 455: 456: 457:
458: public function getUsedTemplates() {
459: return $this->aUsedTemplates;
460: }
461:
462: 463: 464: 465: 466:
467: public function isOldModule() {
468:
469: $scanKeywords = array(
470: '$cfgTab',
471: 'idside',
472: 'idsidelang'
473: );
474:
475: $input = $this->get('input');
476: $output = $this->get('output');
477:
478: foreach ($scanKeywords as $keyword) {
479: if (strstr($input, $keyword)) {
480: return true;
481: }
482: if (strstr($output, $keyword)) {
483: return true;
484: }
485: }
486: }
487:
488: public function getField($field) {
489: $value = parent::getField($field);
490:
491: switch ($field) {
492: case 'name':
493: if ($value == '') {
494: $value = i18n('- Unnamed module -');
495: }
496: }
497:
498: return ($value);
499: }
500:
501: public function store($bJustStore = false) {
502: if ($bJustStore) {
503:
504: $success = parent::store();
505: } else {
506: cInclude('includes', 'functions.con.php');
507:
508: $success = parent::store();
509:
510: conGenerateCodeForAllArtsUsingMod($this->get('idmod'));
511: }
512:
513: return $success;
514: }
515:
516: 517: 518: 519: 520: 521: 522:
523: private function _parseImportFile($sFile) {
524: $oXmlReader = new cXmlReader();
525: $oXmlReader->load($sFile);
526:
527: $aData = array();
528: $aInformation = array(
529: 'name',
530: 'description',
531: 'type',
532: 'input',
533: 'output',
534: 'alias'
535: );
536:
537: foreach ($aInformation as $sInfoName) {
538: $sPath = '/module/' . $sInfoName;
539:
540: $value = $oXmlReader->getXpathValue($sPath);
541: $aData[$sInfoName] = $value;
542: }
543:
544: return $aData;
545: }
546:
547: 548: 549: 550: 551:
552: private function _getModuleProperties($sFile) {
553: $ret = array();
554:
555: $aModuleData = $this->_parseImportFile($sFile);
556: if (count($aModuleData) > 0) {
557: foreach ($aModuleData as $key => $value) {
558:
559: if ($key != 'output' && $key != 'input') {
560: $ret[$key] = addslashes($value);
561: }
562: }
563: }
564:
565: return $ret;
566: }
567:
568: 569: 570: 571: 572: 573:
574: function import($sFile, $tempName, $show_notification = true) {
575: global $cfgClient, $db, $client, $cfg, $encoding, $lang;
576: $zip = new ZipArchive();
577: $notification = new cGuiNotification();
578: $contenidoModuleHandler = new cModuleHandler($this->get('idmod'));
579:
580:
581: $modulName = substr($sFile, 0, -4);
582:
583: $sModulePath = $cfgClient[$client]['module']['path'] . $modulName;
584:
585:
586: if (is_dir($sModulePath)) {
587: if ($show_notification == true) {
588: $notification->displayNotification('error', i18n('Module exist!'));
589: }
590:
591: return false;
592: }
593:
594: if ($zip->open($tempName)) {
595: if ($zip->extractTo($sModulePath)) {
596: $zip->close();
597:
598:
599: $modules = new cApiModuleCollection();
600:
601: $module = $modules->create($modulName);
602: $moduleProperties = $this->_getModuleProperties($sModulePath . '/info.xml');
603:
604:
605: foreach ($moduleProperties as $key => $value) {
606: $module->set($key, $value);
607: }
608:
609: $module->store();
610: } else {
611: if ($show_notification == true) {
612: $notification->displayNotification('error', i18n('Import failed, could not extract zip file!'));
613: }
614:
615: return false;
616: }
617: } else {
618: if ($show_notification == true) {
619: $notification->displayNotification('error', i18n('Could not open the zip file!'));
620: }
621:
622: return false;
623: }
624:
625: return true;
626: }
627:
628: 629: 630: 631: 632:
633: function importModuleFromXML($sFile) {
634: global $db, $cfgClient, $client, $cfg, $encoding, $lang;
635:
636: $inputOutput = array();
637: $notification = new cGuiNotification();
638:
639: $aModuleData = $this->_parseImportFile($sFile);
640: if (count($aModuleData) > 0) {
641: foreach ($aModuleData as $key => $value) {
642: if ($this->get($key) != $value) {
643:
644: if ($key == 'output' || $key == 'input') {
645: $inputOutput[$key] = $value;
646: } else {
647: $this->set($key, addslashes($value));
648: }
649: }
650: }
651:
652: $moduleAlias = cApiStrCleanURLCharacters($this->get('name'));
653:
654: if ($this->get('alias') == '') {
655: $this->set('alias', $moduleAlias);
656: }
657:
658: if (is_dir($cfgClient[$client]['module']['path'] . $moduleAlias)) {
659: $notification->displayNotification('error', i18n('Module exist!'));
660:
661: return false;
662: } else {
663:
664: $this->store();
665: $contenidoModuleHandler = new cModuleHandler($this->get('idmod'));
666: if (!$contenidoModuleHandler->createModule($inputOutput['input'], $inputOutput['output'])) {
667: $notification->displayNotification('error', i18n('Could not make a module!'));
668:
669: return false;
670: } else {
671:
672: $contenidoModuleHandler->saveInfoXML();
673: }
674: }
675: } else {
676: $notification->displayNotification('error', i18n('Could not parse xml file!'));
677:
678: return false;
679: }
680:
681: return true;
682: }
683:
684: 685: 686: 687: 688: 689: 690:
691: private function _addFolderToZip($dir, $zipArchive, $zipdir = '') {
692: if (is_dir($dir)) {
693: if ($dh = opendir($dir)) {
694:
695: if (!empty($zipdir)) {
696: $zipArchive->addEmptyDir($zipdir);
697: }
698:
699:
700: while (($file = readdir($dh)) !== false) {
701:
702: if (!is_file($dir . $file)) {
703:
704: if (($file !== '.') && ($file !== '..')) {
705: $this->_addFolderToZip($dir . $file . '/', $zipArchive, $zipdir . $file . '/');
706: }
707: } else {
708:
709: if ($zipArchive->addFile($dir . $file, $zipdir . $file) === false) {
710: $notification = new cGuiNotification();
711: $notification->displayNotification('error', sprintf(i18n('Could not add file %s to zip!'), $file));
712: }
713: }
714: }
715: }
716: }
717: }
718:
719: 720: 721:
722: public function export() {
723: $notification = new cGuiNotification();
724: $moduleHandler = new cModuleHandler($this->get('idmod'));
725:
726:
727: if ($moduleHandler->modulePathExists()) {
728: $zip = new ZipArchive();
729: $zipName = $this->get('alias') . '.zip';
730: if ($zip->open($zipName, ZipArchive::CREATE)) {
731: $this->_addFolderToZip($moduleHandler->getModulePath(), $zip);
732:
733: $zip->close();
734:
735: header('Content-Type: application/zip');
736: header('Content-Length: ' . filesize($zipName));
737: header("Content-Disposition: attachment; filename=\"$zipName\"");
738: readfile($zipName);
739:
740:
741: unlink($zipName);
742: } else {
743: $notification->displayNotification('error', i18n('Could not open the zip file!'));
744: }
745: } else {
746: $notification->displayNotification('error', i18n("Module don't exist on file system!"));
747: }
748: }
749:
750: 751: 752: 753: 754: 755: 756:
757: public function setField($name, $value, $bSafe = true) {
758: switch ($name) {
759: case 'deletable':
760: case 'static':
761: $value = ($value == 1)? 1 : 0;
762: break;
763: case 'idclient':
764: $value = (int) $value;
765: break;
766: }
767:
768: parent::setField($name, $value, $bSafe);
769: }
770:
771: }
772:
773: ?>