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