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