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:
432: $attributes = array();
433:
434:
435: $pluginId = parent::_getPluginId();
436:
437: $areaCount = count(parent::$XmlArea->area);
438: for ($i = 0; $i < $areaCount; $i++) {
439:
440: $attributes = array();
441:
442:
443: foreach (parent::$XmlArea->area[$i]->attributes() as $key => $value) {
444: $attributes[$key] = $value;
445: }
446:
447:
448: $area = cSecurity::escapeString(parent::$XmlArea->area[$i]);
449:
450:
451: $attributes = array(
452: 'parent' => cSecurity::escapeString($attributes['parent']),
453: 'relevant' => cSecurity::toInteger($attributes['relevant']),
454: 'menuless' => cSecurity::toInteger($attributes['menuless'])
455: );
456:
457:
458: if (empty($attributes['parent'])) {
459: $attributes['parent'] = 0;
460: }
461:
462: if (empty($attributes['relevant'])) {
463: $attributes['relevant'] = 1;
464: }
465:
466:
467: $item = $this->_ApiAreaCollection->create($area, $attributes['parent'], $attributes['relevant'], 1, $attributes['menuless']);
468:
469:
470: $this->_PimPluginRelationsCollection->create($item->get('idarea'), $pluginId, 'area');
471:
472:
473: $this->PluginInstalledAreas[] = $area;
474: }
475: }
476:
477: 478: 479:
480: private function _installAddActions() {
481:
482:
483: $attributes = array();
484:
485:
486: $pluginId = parent::_getPluginId();
487:
488: $actionCount = count(parent::$XmlActions->action);
489: for ($i = 0; $i < $actionCount; $i++) {
490:
491:
492: foreach (parent::$XmlActions->action[$i]->attributes() as $key => $value) {
493: $attributes[$key] = $value;
494: }
495:
496:
497: if (empty($attributes['relevant'])) {
498: $attributes['relevant'] = 1;
499: }
500:
501:
502: $attributes = array(
503: 'area' => cSecurity::escapeString($attributes['area']),
504: 'relevant' => cSecurity::toInteger($attributes['relevant'])
505: );
506:
507:
508: $action = cSecurity::escapeString(parent::$XmlActions->action[$i]);
509:
510:
511: if (!in_array($attributes['area'], $this->_getInstalledAreas())) {
512: parent::error(sprintf(i18n('Defined area <strong>%s</strong> are not found on your CONTENIDO installation. Please contact your plugin author.', 'pim'), $attributes['area']));
513: }
514:
515:
516: $item = $this->_ApiActionCollection->create($attributes['area'], $action, '', '', '', $attributes['relevant']);
517:
518:
519: $this->_PimPluginRelationsCollection->create($item->get('idaction'), $pluginId, 'action');
520: }
521: }
522:
523: 524: 525:
526: private function _installAddFrames() {
527:
528:
529: $attributes = array();
530:
531:
532: $pluginId = parent::_getPluginId();
533:
534: $frameCount = count(parent::$XmlFrames->frame);
535: for ($i = 0; $i < $frameCount; $i++) {
536:
537:
538: foreach (parent::$XmlFrames->frame[$i]->attributes() as $sKey => $sValue) {
539: $attributes[$sKey] = cSecurity::escapeString($sValue);
540: }
541:
542:
543: if (!in_array($attributes['area'], $this->_getInstalledAreas())) {
544: parent::error(sprintf(i18n('Defined area <strong>%s</strong> are not found on your CONTENIDO installation. Please contact your plugin author.', 'pim'), $attributes['area']));
545: }
546:
547:
548: $file = $this->_ApiFileCollection->create($attributes['area'], $attributes['name'], $attributes['filetype']);
549:
550:
551: if (!empty($attributes['frameId'])) {
552: $item = $this->_ApiFrameFileCollection->create($attributes['area'], $attributes['frameId'], $file->get('idfile'));
553:
554:
555: $this->_PimPluginRelationsCollection->create($item->get('idframefile'), $pluginId, 'framefl');
556: }
557: }
558: }
559:
560: 561: 562:
563: private function _installAddNavMain() {
564: $cfg = cRegistry::getConfig();
565: $db = cRegistry::getDb();
566:
567:
568: $attributes = array();
569:
570:
571: $pluginId = parent::_getPluginId();
572:
573:
574: $sql = 'SELECT MAX(idnavm) AS id FROM ' . $cfg['tab']['nav_main'];
575: $db->query($sql);
576:
577: if ($db->nextRecord()) {
578: $idnavm = $db->f('id');
579:
580:
581: if ($idnavm < 10000) {
582: $idnavm = 10000;
583: }
584: }
585:
586: $navCount = count(parent::$XmlNavMain->nav);
587: for ($i = 0; $i < $navCount; $i++) {
588:
589:
590: $location = cSecurity::escapeString(parent::$XmlNavMain->nav[$i]);
591:
592:
593: foreach (parent::$XmlNavMain->nav[$i]->attributes() as $sKey => $sValue) {
594: $attributes[$sKey] = cSecurity::escapeString($sValue);
595: }
596:
597:
598: if (!$attributes['name']) {
599: $attributes['name'] = strtolower($location);
600: $attributes['name'] = str_replace('/', '', $attributes['name']);
601: }
602:
603:
604: $idnavm = $idnavm + 10;
605:
606:
607: $idnavm = substr($idnavm, 0, strlen($idnavm) - 1);
608:
609:
610: $idnavm = cSecurity::toInteger($idnavm . 0);
611:
612:
613: $this->_ApiNavMainCollection->create($attributes['name'], $location, $idnavm);
614:
615:
616: $this->_PimPluginRelationsCollection->create($idnavm, $pluginId, 'navm');
617: }
618: }
619:
620: 621: 622:
623: private function _installAddNavSub() {
624:
625:
626: $attributes = array();
627:
628:
629: $pluginId = parent::_getPluginId();
630:
631: $navCount = count(parent::$XmlNavSub->nav);
632: for ($i = 0; $i < $navCount; $i++) {
633:
634:
635: foreach (parent::$XmlNavSub->nav[$i]->attributes() as $key => $value) {
636: $attributes[$key] = $value;
637: }
638:
639:
640: $attributes['area'] = cSecurity::toString($attributes['area']);
641:
642:
643: if (!in_array($attributes['area'], $this->_getInstalledAreas())) {
644: parent::error(sprintf(i18n('Defined area <strong>%s</strong> are not found on your CONTENIDO installation. Please contact your plugin author.', 'pim'), $attributes['area']));
645: }
646:
647:
648: if (!preg_match('/[^a-zA-Z]/u', $attributes['navm'])) {
649:
650: $navm = $this->_getNavMainId($attributes['navm']);;
651: if ($navm === false) {
652: 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']));
653: } else {
654: $attributes['navm'] = $navm;
655: }
656: }
657:
658:
659: $item = $this->_ApiNavSubCollection->create($attributes['navm'], $attributes['area'], $attributes['level'], parent::$XmlNavSub->nav[$i], 1);
660:
661:
662: $this->_PimPluginRelationsCollection->create($item->get('idnavs'), $pluginId, 'navs');
663: }
664: }
665:
666: 667: 668:
669: private function _installAddSpecificSql() {
670: $cfg = cRegistry::getConfig();
671: $db = cRegistry::getDb();
672: if (parent::getMode() == 1) {
673: $tempSqlFilename = $cfg['path']['contenido'] . $cfg['path']['plugins'] . $this->_getPluginFoldername() . DIRECTORY_SEPARATOR . 'plugin_install.sql';
674: } elseif (parent::getMode() == 2 || parent::getMode() == 4) {
675:
676:
677:
678:
679:
680:
681: $tempSqlFilename = parent::$_PimPluginArchiveExtractor->extractArchiveFileToVariable('plugin_install.sql', 0);
682: }
683:
684:
685: if (!cFileHandler::exists($tempSqlFilename)) {
686: return;
687: }
688:
689: $tempSqlContent = cFileHandler::read($tempSqlFilename);
690: $tempSqlContent = str_replace("\r\n", "\n", $tempSqlContent);
691: $tempSqlContent = explode("\n", $tempSqlContent);
692: $tempSqlLines = count($tempSqlContent);
693:
694: $pattern = '/^(CREATE TABLE IF NOT EXISTS|INSERT INTO|UPDATE|ALTER TABLE) `?' . parent::SQL_PREFIX . '`?\b/';
695:
696: for ($i = 0; $i < $tempSqlLines; $i++) {
697: if (preg_match($pattern, $tempSqlContent[$i])) {
698: $tempSqlContent[$i] = str_replace(parent::SQL_PREFIX, $cfg['sql']['sqlprefix'] . '_pi', $tempSqlContent[$i]);
699: $db->query($tempSqlContent[$i]);
700: }
701: }
702: }
703:
704: 705: 706:
707: private function _installAddContentTypes() {
708:
709:
710: $pluginId = parent::_getPluginId();
711:
712: $pattern = '/^CMS_.+/';
713:
714: $typeCount = count(parent::$XmlContentType->type);
715: for ($i = 0; $i < $typeCount; $i++) {
716:
717: $type = cSecurity::toString(parent::$XmlContentType->type[$i]);
718:
719: if (preg_match($pattern, $type)) {
720:
721:
722: $item = $this->_ApiTypeCollection->create($type, '');
723:
724:
725: $this->_PimPluginRelationsCollection->create($item->get('idtype'), $pluginId, 'ctype');
726: }
727: }
728: }
729:
730: 731: 732:
733: private function _installAddModules() {
734: $cfg = cRegistry::getConfig();
735: $module = new cApiModule();
736:
737:
738: $modulesPath = $cfg['path']['contenido'] . $cfg['path']['plugins'] . $this->_getPluginFoldername() . DIRECTORY_SEPARATOR . "modules" . DIRECTORY_SEPARATOR;
739:
740: if (!is_dir($modulesPath)) {
741: return false;
742: }
743:
744: foreach (new DirectoryIterator($modulesPath) as $modulesFiles) {
745:
746: if (substr($modulesFiles->getBasename(), -4) == ".zip") {
747:
748:
749: $module->import($modulesFiles->getBasename(), $modulesFiles->getBasename(), false);
750: }
751: }
752:
753: cDirHandler::recursiveRmdir($modulesPath);
754: }
755:
756: 757: 758:
759: private function _installAddDir() {
760: $cfg = cRegistry::getConfig();
761:
762:
763: $tempPluginDir = $cfg['path']['contenido'] . $cfg['path']['plugins'] . parent::$XmlGeneral->plugin_foldername . DIRECTORY_SEPARATOR;
764:
765:
766: try {
767: parent::$_PimPluginArchiveExtractor->setDestinationPath($tempPluginDir);
768: } catch (cException $e) {
769: parent::$_PimPluginArchiveExtractor->destroyTempFiles();
770: }
771:
772:
773: try {
774: parent::$_PimPluginArchiveExtractor->extractArchive();
775: } catch (cException $e) {
776: parent::$_PimPluginArchiveExtractor->destroyTempFiles();
777: }
778: }
779:
780: }
781: ?>