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