1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: 17: 18: 19: 20: 21: 22:
23: class PimPluginSetupInstall extends PimPluginSetup {
24:
25:
26:
27:
28: private $PluginFoldername;
29:
30:
31: private $PluginInstalledAreas = array();
32:
33:
34:
35: protected $_PimPluginCollection;
36:
37:
38: protected $_PimPluginRelationsCollection;
39:
40:
41: protected $_ApiAreaCollection;
42:
43:
44: protected $_ApiActionCollection;
45:
46:
47: protected $_ApiFileCollection;
48:
49:
50: protected $_ApiFrameFileCollection;
51:
52:
53: protected $_ApiNavMainCollection;
54:
55:
56: protected $_ApiNavSubCollection;
57:
58:
59: protected $_ApiTypeCollection;
60:
61:
62: 63: 64: 65: 66: 67:
68: private function _setPluginFoldername($foldername) {
69: return $this->PluginFoldername = cSecurity::escapeString($foldername);
70: }
71:
72: 73: 74: 75: 76:
77: private function _setPimPluginCollection() {
78: return $this->_PimPluginCollection = new PimPluginCollection();
79: }
80:
81: 82: 83: 84: 85:
86: private function _setPimPluginRelationsCollection() {
87: return $this->_PimPluginRelationsCollection = new PimPluginRelationsCollection();
88: }
89:
90: 91: 92: 93: 94:
95: private function _setApiAreaCollection() {
96: return $this->_ApiAreaCollection = new cApiAreaCollection();
97: }
98:
99: 100: 101: 102: 103:
104: private function _setApiActionCollection() {
105: return $this->_ApiActionCollection = new cApiActionCollection();
106: }
107:
108: 109: 110: 111: 112:
113: private function _setApiFileCollection() {
114: return $this->_ApiFileCollection = new cApiFileCollection();
115: }
116:
117: 118: 119: 120: 121:
122: private function _setApiFrameFileCollection() {
123: return $this->_ApiFrameFileCollection = new cApiFrameFileCollection();
124: }
125:
126: 127: 128: 129: 130:
131: private function _setApiNavMainCollection() {
132: return $this->_ApiNavMainCollection = new cApiNavMainCollection();
133: }
134:
135: 136: 137: 138: 139:
140: private function _setApiNavSubCollection() {
141: return $this->_ApiNavSubCollection = new cApiNavSubCollection();
142: }
143:
144: 145: 146: 147: 148:
149: private function _setApiTypeCollection() {
150: return $this->_ApiTypeCollection = new cApiTypeCollection();
151: }
152:
153: 154: 155: 156: 157:
158: protected function _getPluginFoldername() {
159: return $this->PluginFoldername;
160: }
161:
162: 163: 164: 165: 166:
167: protected function _getInstalledAreas() {
168: return $this->PluginInstalledAreas;
169: }
170:
171: 172: 173: 174: 175:
176: protected function _getNavMainId($navm = '') {
177:
178: if (!$navm) {
179: return false;
180: }
181:
182: $this->_ApiNavMainCollection->setWhere('name', cSecurity::escapeString($navm));
183: $this->_ApiNavMainCollection->query();
184:
185: if ($this->_ApiNavMainCollection->count() == 0) {
186: return false;
187: } else {
188: $entry = $this->_ApiNavMainCollection->next();
189: return $entry->get('idnavm');
190: }
191: }
192:
193:
194: 195: 196:
197: public function __construct() {
198:
199:
200:
201: $this->_setPimPluginCollection();
202: $this->_setPimPluginRelationsCollection();
203:
204:
205: $this->_setApiAreaCollection();
206: $this->_setApiActionCollection();
207: $this->_setApiFileCollection();
208: $this->_setApiFrameFileCollection();
209: $this->_setApiNavMainCollection();
210: $this->_setApiNavSubCollection();
211: $this->_setApiTypeCollection();
212: }
213:
214: 215: 216:
217: public function install() {
218:
219:
220: $this->_installCheckUuid();
221:
222:
223: $this->_installCheckRequirements();
224:
225:
226: $this->_installCheckDependencies();
227:
228:
229: $this->_installAddPlugin();
230:
231:
232: $this->_installFillAreas();
233:
234:
235: $this->_installAddAreas();
236:
237:
238: $this->_installAddActions();
239:
240:
241: $this->_installAddFrames();
242:
243:
244: $this->_installAddNavMain();
245:
246:
247: $this->_installAddNavSub();
248:
249:
250: if (PimPluginSetup::_getUpdateSqlFileExist() === false) {
251: $this->_installAddSpecificSql();
252: }
253:
254:
255: $this->_installAddContentTypes();
256:
257:
258: $this->_installAddModules();
259:
260:
261: if (parent::getMode() == 2) {
262: $this->_installAddDir();
263: }
264:
265:
266:
267: if (parent::getMode() <= 2) {
268: parent::info(i18n('The plugin has been successfully installed. To apply the changes please login into backend again.', 'pim'));
269: }
270: }
271:
272: 273: 274:
275: private function _installCheckUuid() {
276: $this->_PimPluginCollection->setWhere('uuid', parent::$XmlGeneral->uuid);
277: $this->_PimPluginCollection->query();
278: if ($this->_PimPluginCollection->count() > 0) {
279: parent::error(i18n('You can install this plugin only for one time.', 'pim'));
280: }
281: }
282:
283: 284: 285:
286: private function _installCheckRequirements() {
287:
288:
289: if (version_compare(CON_VERSION, parent::$XmlRequirements->contenido->attributes()->minversion, '<')) {
290: parent::error(sprintf(i18n('You have to install CONTENIDO <strong>%s</strong> or higher to install this plugin!', 'pim'), parent::$XmlRequirements->contenido->attributes()->minversion));
291: }
292:
293:
294: if (parent::$XmlRequirements->contenido->attributes()->maxversion) {
295:
296: if (version_compare(CON_VERSION, parent::$XmlRequirements->contenido->attributes()->maxversion, '>')) {
297: parent::error(sprintf(i18n('Your current CONTENIDO version is to new - max CONTENIDO version: %s', 'pim'), parent::$XmlRequirements->contenido->attributes()->maxversion));
298: }
299: }
300:
301:
302: if (version_compare(phpversion(), parent::$XmlRequirements->attributes()->php, '<')) {
303: parent::error(sprintf(i18n('You have to install PHP <strong>%s</strong> or higher to install this plugin!', 'pim'), parent::$XmlRequirements->attributes()->php));
304: }
305:
306:
307: if (count(parent::$XmlRequirements->extension) != 0) {
308:
309: for ($i = 0; $i < count(parent::$XmlRequirements->extension); $i++) {
310:
311: if (!extension_loaded(parent::$XmlRequirements->extension[$i]->attributes()->name)) {
312: parent::error(sprintf(i18n('The plugin could not find the PHP extension <strong>%s</strong>. Because this is required by the plugin, it can not be installed.', 'pim'), parent::$XmlRequirements->extension[$i]->attributes()->name));
313: }
314: }
315: }
316:
317:
318: if (count(parent::$XmlRequirements->class) != 0) {
319:
320: for ($i = 0; $i < count(parent::$XmlRequirements->class); $i++) {
321:
322: if (!class_exists(parent::$XmlRequirements->class[$i]->attributes()->name)) {
323: parent::error(sprintf(i18n('The plugin could not find the class <strong>%s</strong>. Because this is required by the plugin, it can not be installed.', 'pim'), parent::$XmlRequirements->class[$i]->attributes()->name));
324: }
325: }
326: }
327:
328:
329: if (count(parent::$XmlRequirements->function) != 0) {
330:
331: for ($i = 0; $i < count(parent::$XmlRequirements->function); $i++) {
332:
333: if (!function_exists(parent::$XmlRequirements->function[$i]->attributes()->name)) {
334: parent::error(sprintf(i18n('The plugin could not find the function <strong>%s</strong>. Because this is required by the plugin, it can not be installed.', 'pim'), parent::$XmlRequirements->function[$i]->attributes()->name));
335: }
336: }
337: }
338: }
339:
340: 341: 342:
343: private function _installCheckDependencies() {
344:
345: $dependenciesCount = count(parent::$XmlDependencies);
346: for ($i = 0; $i < $dependenciesCount; $i++) {
347:
348: $attributes = array();
349:
350:
351: foreach (parent::$XmlDependencies->depend[$i]->attributes() as $key => $value) {
352: $attributes[$key] = $value;
353: }
354:
355:
356: $depend = cSecurity::escapeString(parent::$XmlDependencies->depend[$i]);
357:
358: if ($depend == "") {
359: return true;
360: }
361:
362:
363: $attributes = array(
364: 'uuid' => cSecurity::escapeString($attributes['uuid']),
365: 'minversion' => cSecurity::escapeString($attributes['min_version']),
366: 'maxversion' => cSecurity::escapeString($attributes['max_version'])
367: );
368:
369:
370: $this->_PimPluginCollection->setWhere('uuid', $attributes['uuid']);
371: $this->_PimPluginCollection->setWhere('active', '1');
372: $this->_PimPluginCollection->query();
373: if ($this->_PimPluginCollection->count() == 0) {
374: parent::error(sprintf(i18n('This plugin required the plugin <strong>%s</strong>.', 'pim'), $depend));
375: }
376:
377: $plugin = $this->_PimPluginCollection->next();
378:
379:
380: if (parent::$XmlDependencies->depend[$i]->attributes()->minversion) {
381:
382: if (version_compare($plugin->get("version"), parent::$XmlDependencies->depend[$i]->attributes()->minversion, '<')) {
383: parent::error(sprintf(i18n('You have to install<strong>%s %</strong> or higher to install this plugin!', 'pim'), $depend, parent::$XmlDependencies->depend[$i]->attributes()->minversion));
384: }
385: }
386:
387:
388: if (parent::$XmlDependencies->depend[$i]->attributes()->maxversion) {
389:
390: if (version_compare($plugin->get("version"), parent::$XmlDependencies->depend[$i]->attributes()->maxversion, '>')) {
391: parent::error(sprintf(i18n('You have to install <strong>%s %s</strong> or lower to install this plugin!', 'pim'), $depend, parent::$XmlDependencies->depend[$i]->attributes()->maxversion));
392: }
393: }
394:
395: }
396:
397: }
398:
399: 400: 401:
402: private function _installAddPlugin() {
403:
404: $pimPlugin = $this->_PimPluginCollection->create(parent::$XmlGeneral->plugin_name, parent::$XmlGeneral->description, parent::$XmlGeneral->author, parent::$XmlGeneral->copyright, parent::$XmlGeneral->mail, parent::$XmlGeneral->website, parent::$XmlGeneral->version, parent::$XmlGeneral->plugin_foldername, parent::$XmlGeneral->uuid, parent::$XmlGeneral->attributes()->active);
405:
406:
407: $pluginId = $pimPlugin->get('idplugin');
408:
409:
410: parent::setPluginId($pluginId);
411:
412:
413: $this->_setPluginFoldername(parent::$XmlGeneral->plugin_foldername);
414: }
415:
416: 417: 418:
419: private function _installFillAreas() {
420: $this->_ApiAreaCollection->select(NULL, NULL, 'name');
421: while (($areas = $this->_ApiAreaCollection->next()) !== false) {
422: $this->PluginInstalledAreas[] = $areas->get('name');
423: }
424: }
425:
426: 427: 428:
429: private function _installAddAreas() {
430:
431: $elements = parent::$XmlArea->area;
432: if (empty($elements)) {
433: return;
434: }
435:
436:
437: $pluginId = parent::_getPluginId();
438:
439: foreach ($elements as $element) {
440:
441:
442: $attributes = array();
443:
444:
445: foreach ($element->attributes() as $key => $value) {
446: $attributes[$key] = $value;
447: }
448:
449:
450: $area = cSecurity::escapeString($element);
451:
452:
453: $attributes = array(
454: 'parent' => cSecurity::escapeString($attributes['parent']),
455: 'relevant' => cSecurity::toInteger($attributes['relevant']),
456: 'menuless' => cSecurity::toInteger($attributes['menuless'])
457: );
458:
459:
460: if (empty($attributes['parent'])) {
461: $attributes['parent'] = 0;
462: }
463:
464: if (empty($attributes['relevant'])) {
465: $attributes['relevant'] = 1;
466: }
467:
468:
469: $item = $this->_ApiAreaCollection->create($area, $attributes['parent'], $attributes['relevant'], 1, $attributes['menuless']);
470:
471:
472: $this->_PimPluginRelationsCollection->create($item->get('idarea'), $pluginId, 'area');
473:
474:
475: $this->PluginInstalledAreas[] = $area;
476: }
477: }
478:
479: 480: 481:
482: private function _installAddActions() {
483:
484: $elements = parent::$XmlActions->action;
485: if (empty($elements)) {
486: return;
487: }
488:
489:
490: $attributes = array();
491:
492:
493: $pluginId = parent::_getPluginId();
494:
495: foreach ($elements as $element) {
496:
497:
498: foreach ($element->attributes() as $key => $value) {
499: $attributes[$key] = $value;
500: }
501:
502:
503: if (empty($attributes['relevant'])) {
504: $attributes['relevant'] = 1;
505: }
506:
507:
508: $attributes = array(
509: 'area' => cSecurity::escapeString($attributes['area']),
510: 'relevant' => cSecurity::toInteger($attributes['relevant'])
511: );
512:
513:
514: $action = cSecurity::escapeString($element);
515:
516:
517: if (!in_array($attributes['area'], $this->_getInstalledAreas())) {
518: parent::error(sprintf(i18n('Defined area <strong>%s</strong> are not found on your CONTENIDO installation. Please contact your plugin author.', 'pim'), $attributes['area']));
519: }
520:
521:
522: $item = $this->_ApiActionCollection->create($attributes['area'], $action, '', '', '', $attributes['relevant']);
523:
524:
525: $this->_PimPluginRelationsCollection->create($item->get('idaction'), $pluginId, 'action');
526: }
527: }
528:
529: 530: 531:
532: private function _installAddFrames() {
533:
534: $elements = parent::$XmlFrames->frame;
535: if (empty($elements)) {
536: return;
537: }
538:
539:
540: $attributes = array();
541:
542:
543: $pluginId = parent::_getPluginId();
544:
545: foreach ($elements as $element) {
546:
547:
548: foreach ($element->attributes() as $sKey => $sValue) {
549: $attributes[$sKey] = cSecurity::escapeString($sValue);
550: }
551:
552:
553: if (!in_array($attributes['area'], $this->_getInstalledAreas())) {
554: parent::error(sprintf(i18n('Defined area <strong>%s</strong> are not found on your CONTENIDO installation. Please contact your plugin author.', 'pim'), $attributes['area']));
555: }
556:
557:
558: $file = $this->_ApiFileCollection->create($attributes['area'], $attributes['name'], $attributes['filetype']);
559:
560:
561: if (!empty($attributes['frameId'])) {
562: $item = $this->_ApiFrameFileCollection->create($attributes['area'], $attributes['frameId'], $file->get('idfile'));
563:
564:
565: $this->_PimPluginRelationsCollection->create($item->get('idframefile'), $pluginId, 'framefl');
566: }
567: }
568: }
569:
570: 571: 572:
573: private function _installAddNavMain() {
574:
575: $elements = parent::$XmlNavMain->nav;
576: if (empty($elements)) {
577: return;
578: }
579:
580: $cfg = cRegistry::getConfig();
581: $db = cRegistry::getDb();
582:
583:
584: $attributes = array();
585:
586:
587: $pluginId = parent::_getPluginId();
588:
589:
590: $sql = 'SELECT MAX(idnavm) AS id FROM ' . $cfg['tab']['nav_main'];
591: $db->query($sql);
592:
593: if ($db->nextRecord()) {
594: $idnavm = $db->f('id');
595:
596:
597: if ($idnavm < 10000) {
598: $idnavm = 10000;
599: }
600: }
601:
602: foreach ($elements as $element) {
603:
604:
605: $location = cSecurity::escapeString($element);
606:
607:
608: foreach ($element->attributes() as $sKey => $sValue) {
609: $attributes[$sKey] = cSecurity::escapeString($sValue);
610: }
611:
612:
613: if (!$attributes['name']) {
614: $attributes['name'] = cString::toLowerCase($location);
615: $attributes['name'] = str_replace('/', '', $attributes['name']);
616: }
617:
618:
619: $idnavm = $idnavm + 10;
620:
621:
622: $idnavm = cString::getPartOfString($idnavm, 0, cString::getStringLength($idnavm) - 1);
623:
624:
625: $idnavm = cSecurity::toInteger($idnavm . 0);
626:
627:
628: $this->_ApiNavMainCollection->create($attributes['name'], $location, $idnavm);
629:
630:
631: $this->_PimPluginRelationsCollection->create($idnavm, $pluginId, 'navm');
632: }
633: }
634:
635: 636: 637:
638: private function _installAddNavSub() {
639:
640: $elements = parent::$XmlNavSub->nav;
641: if (empty($elements)) {
642: return;
643: }
644:
645:
646: $attributes = array();
647:
648:
649: $pluginId = parent::_getPluginId();
650:
651: foreach ($elements as $element) {
652:
653:
654: foreach ($element->attributes() as $key => $value) {
655: $attributes[$key] = $value;
656: }
657:
658:
659: $attributes['area'] = cSecurity::toString($attributes['area']);
660:
661:
662: if (!in_array($attributes['area'], $this->_getInstalledAreas())) {
663: parent::error(sprintf(i18n('Defined area <strong>%s</strong> are not found on your CONTENIDO installation. Please contact your plugin author.', 'pim'), $attributes['area']));
664: }
665:
666:
667: if (!preg_match('/[^a-zA-Z]/u', $attributes['navm'])) {
668:
669: $navm = $this->_getNavMainId($attributes['navm']);;
670: if ($navm === false) {
671: parent::error(sprintf(i18n('Can not find <strong>%s</strong> entry at nav_main table on your CONTENIDO installation. Please contact your plugin author.', 'pim'), $attributes['navm']));
672: } else {
673: $attributes['navm'] = $navm;
674: }
675: }
676:
677:
678: $item = $this->_ApiNavSubCollection->create($attributes['navm'], $attributes['area'], $attributes['level'], $element, 1);
679:
680:
681: $this->_PimPluginRelationsCollection->create($item->get('idnavs'), $pluginId, 'navs');
682: }
683: }
684:
685: 686: 687:
688: private function _installAddSpecificSql() {
689:
690: $cfg = cRegistry::getConfig();
691: $db = cRegistry::getDb();
692:
693: if (parent::getMode() == 1) {
694: $tempSqlFilename = $cfg['path']['contenido'] . $cfg['path']['plugins'] . $this->_getPluginFoldername() . DIRECTORY_SEPARATOR . 'plugin_install.sql';
695: } elseif (parent::getMode() == 2 || parent::getMode() == 4) {
696:
697: $tempSqlFilename = parent::$_PimPluginArchiveExtractor->extractArchiveFileToVariable('plugin_install.sql', 0);
698: }
699:
700:
701: if (!cFileHandler::exists($tempSqlFilename)) {
702: return;
703: }
704:
705: $tempSqlContent = cFileHandler::read($tempSqlFilename);
706: $tempSqlContent = str_replace("\r\n", "\n", $tempSqlContent);
707: $tempSqlContent = explode("\n", $tempSqlContent);
708: $tempSqlLines = count($tempSqlContent);
709:
710: $pattern = '/^(CREATE TABLE IF NOT EXISTS|INSERT INTO|UPDATE|ALTER TABLE) `?' . parent::SQL_PREFIX . '`?\b/';
711:
712: for ($i = 0; $i < $tempSqlLines; $i++) {
713: if (preg_match($pattern, $tempSqlContent[$i])) {
714: $tempSqlContent[$i] = str_replace(parent::SQL_PREFIX, $cfg['sql']['sqlprefix'] . '_pi', $tempSqlContent[$i]);
715: $db->query($tempSqlContent[$i]);
716: }
717: }
718: }
719:
720: 721: 722:
723: private function _installAddContentTypes() {
724:
725: $elements = parent::$XmlContentType->type;
726: if (empty($elements)) {
727: return;
728: }
729:
730:
731: $pluginId = parent::_getPluginId();
732:
733: $pattern = '/^CMS_.+/';
734:
735: foreach ($elements as $element) {
736:
737: $type = cSecurity::toString($element);
738:
739: if (preg_match($pattern, $type)) {
740:
741:
742: $item = $this->_ApiTypeCollection->create($type, '');
743:
744:
745: $this->_PimPluginRelationsCollection->create($item->get('idtype'), $pluginId, 'ctype');
746: }
747: }
748: }
749:
750: 751: 752:
753: private function _installAddModules() {
754:
755: $cfg = cRegistry::getConfig();
756: $module = new cApiModule();
757:
758:
759: $modulesPath = $cfg['path']['contenido'] . $cfg['path']['plugins'] . $this->_getPluginFoldername() . DIRECTORY_SEPARATOR . "modules" . DIRECTORY_SEPARATOR;
760:
761: if (!is_dir($modulesPath)) {
762: return;
763: }
764:
765: foreach (new DirectoryIterator($modulesPath) as $modulesFiles) {
766:
767: if (cString::getPartOfString($modulesFiles->getBasename(), -4) == ".zip") {
768:
769:
770: $module->import($modulesFiles->getBasename(), $modulesFiles->getBasename(), false);
771: }
772: }
773:
774: cDirHandler::recursiveRmdir($modulesPath);
775: }
776:
777: 778: 779:
780: private function _installAddDir() {
781:
782: $cfg = cRegistry::getConfig();
783:
784:
785: $tempPluginDir = $cfg['path']['contenido'] . $cfg['path']['plugins'] . parent::$XmlGeneral->plugin_foldername . DIRECTORY_SEPARATOR;
786:
787:
788: try {
789: parent::$_PimPluginArchiveExtractor->setDestinationPath($tempPluginDir);
790: } catch (cException $e) {
791: parent::$_PimPluginArchiveExtractor->destroyTempFiles();
792: }
793:
794:
795: try {
796: parent::$_PimPluginArchiveExtractor->extractArchive();
797: } catch (cException $e) {
798: parent::$_PimPluginArchiveExtractor->destroyTempFiles();
799: }
800: }
801:
802: }
803: ?>