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->_installAddPlugin();
207:
208:
209: $this->_installFillAreas();
210:
211:
212: $this->_installAddAreas();
213:
214:
215: $this->_installAddActions();
216:
217:
218: $this->_installAddFrames();
219:
220:
221: $this->_installAddNavMain();
222:
223:
224: $this->_installAddNavSub();
225:
226:
227: if (PimPluginSetup::_getUpdateSqlFileExist() == false) {
228: $this->_installAddSpecificSql();
229: }
230:
231:
232: $this->_installAddContentTypes();
233:
234:
235: $this->_installAddModules();
236:
237:
238: if (parent::getMode() == 2) {
239: $this->_installAddDir();
240: }
241:
242:
243:
244: if (parent::getMode() <= 2) {
245: parent::info(i18n('The plugin has been successfully installed. To apply the changes please login into backend again.', 'pim'));
246: }
247: }
248:
249: 250: 251:
252: private function _installCheckUuid() {
253: $this->_PimPluginCollection->setWhere('uuid', parent::$XmlGeneral->uuid);
254: $this->_PimPluginCollection->query();
255: if ($this->_PimPluginCollection->count() > 0) {
256: parent::error(i18n('You can install this plugin only for one time.', 'pim'));
257: }
258: }
259:
260: 261: 262:
263: private function _installCheckRequirements() {
264:
265:
266: $cfg = cRegistry::getConfig();
267:
268:
269: if (version_compare(CON_VERSION, parent::$XmlRequirements->contenido->attributes()->minversion, '<')) {
270: parent::error(i18n('You have to install CONTENIDO <strong>', 'pim') . parent::$XmlRequirements->contenido->attributes()->minversion . i18n('</strong> or higher to install this plugin!', 'pim'));
271: }
272:
273:
274: if (parent::$XmlRequirements->contenido->attributes()->maxversion) {
275:
276: if (version_compare(CON_VERSION, parent::$XmlRequirements->contenido->attributes()->maxversion, '>')) {
277: parent::error(i18n('Your current CONTENIDO version is to new - max CONTENIDO version: ' . parent::$XmlRequirements->contenido->attributes()->maxversion . '', 'pim'));
278: }
279: }
280:
281:
282: if (version_compare(phpversion(), parent::$XmlRequirements->attributes()->php, '<')) {
283: parent::error(i18n('You have to install PHP <strong>', 'pim') . parent::$XmlRequirements->attributes()->php . i18n('</strong> or higher to install this plugin!', 'pim'));
284: }
285:
286:
287: if (count(parent::$XmlRequirements->extension) != 0) {
288:
289: for ($i = 0; $i < count(parent::$XmlRequirements->extension); $i++) {
290:
291: if (!extension_loaded(parent::$XmlRequirements->extension[$i]->attributes()->name)) {
292: 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'));
293: }
294: }
295: }
296:
297:
298: if (count(parent::$XmlRequirements->class) != 0) {
299:
300: for ($i = 0; $i < count(parent::$XmlRequirements->class); $i++) {
301:
302: if (!class_exists(parent::$XmlRequirements->class[$i]->attributes()->name)) {
303: 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'));
304: }
305: }
306: }
307:
308:
309: if (count(parent::$XmlRequirements->function) != 0) {
310:
311: for ($i = 0; $i < count(parent::$XmlRequirements->function); $i++) {
312:
313: if (!function_exists(parent::$XmlRequirements->function[$i]->attributes()->name)) {
314: 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'));
315: }
316: }
317: }
318: }
319:
320: 321: 322:
323: private function _installAddPlugin() {
324:
325: $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);
326:
327:
328: $pluginId = $pimPlugin->get('idplugin');
329:
330:
331: parent::setPluginId($pluginId);
332:
333:
334: $this->_setPluginFoldername(parent::$XmlGeneral->plugin_foldername);
335: }
336:
337: 338: 339:
340: private function _installFillAreas() {
341: $oItem = $this->_ApiAreaCollection;
342: $this->_ApiAreaCollection->select(NULL, NULL, 'name');
343: while (($areas = $this->_ApiAreaCollection->next()) !== false) {
344: $this->PluginInstalledAreas[] = $areas->get('name');
345: }
346: }
347:
348: 349: 350:
351: private function _installAddAreas() {
352:
353:
354: $attributes = array();
355:
356:
357: $pluginId = parent::_getPluginId();
358:
359: $areaCount = count(parent::$XmlArea->area);
360: for ($i = 0; $i < $areaCount; $i++) {
361:
362: $attributes = array();
363:
364:
365: foreach (parent::$XmlArea->area[$i]->attributes() as $key => $value) {
366: $attributes[$key] = $value;
367: }
368:
369:
370: $area = cSecurity::escapeString(parent::$XmlArea->area[$i]);
371:
372:
373: $attributes = array(
374: 'parent' => cSecurity::escapeString($attributes['parent']),
375: 'menuless' => cSecurity::toInteger($attributes['menuless'])
376: );
377:
378:
379: if (empty($attributes['parent'])) {
380: $attributes['parent'] = 0;
381: }
382:
383:
384: $item = $this->_ApiAreaCollection->create($area, $attributes['parent'], 1, 1, $attributes['menuless']);
385:
386:
387: $this->_PimPluginRelationsCollection->create($item->get('idarea'), $pluginId, 'area');
388:
389:
390: $this->PluginInstalledAreas[] = $area;
391: }
392: }
393:
394: 395: 396:
397: private function _installAddActions() {
398:
399:
400: $attributes = array();
401:
402:
403: $pluginId = parent::_getPluginId();
404:
405: $actionCount = count(parent::$XmlActions->action);
406: for ($i = 0; $i < $actionCount; $i++) {
407:
408:
409: foreach (parent::$XmlActions->action[$i]->attributes() as $key => $value) {
410: $attributes[$key] = $value;
411: }
412:
413:
414: if (empty($attributes['relevant'])) {
415: $attributes['relevant'] = 1;
416: }
417:
418:
419: $attributes = array(
420: 'area' => cSecurity::escapeString($attributes['area']),
421: 'relevant' => cSecurity::toInteger($attributes['relevant'])
422: );
423:
424:
425: $action = cSecurity::escapeString(parent::$XmlActions->action[$i]);
426:
427:
428: if (!in_array($attributes['area'], $this->_getInstalledAreas())) {
429: parent::error(i18n('Defined area', 'pim') . ' <strong>' . $attributes['area'] . '</strong> ' . i18n('are not found on your CONTENIDO installation. Please contact your plugin author.', 'pim'));
430: }
431:
432:
433: $item = $this->_ApiActionCollection->create($attributes['area'], $action, '', '', '', $attributes['relevant']);
434:
435:
436: $this->_PimPluginRelationsCollection->create($item->get('idaction'), $pluginId, 'action');
437: }
438: }
439:
440: 441: 442:
443: private function _installAddFrames() {
444:
445:
446: $attributes = array();
447:
448: $frameCount = count(parent::$XmlFrames->frame);
449: for ($i = 0; $i < $frameCount; $i++) {
450:
451:
452: foreach (parent::$XmlFrames->frame[$i]->attributes() as $sKey => $sValue) {
453: $attributes[$sKey] = cSecurity::escapeString($sValue);
454: }
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: $file = $this->_ApiFileCollection->create($attributes['area'], $attributes['name'], $attributes['filetype']);
463:
464:
465: if (!empty($attributes['frameId'])) {
466: $this->_ApiFrameFileCollection->create($attributes['area'], $attributes['frameId'], $file->get('idfile'));
467: }
468: }
469: }
470:
471: 472: 473:
474: private function _installAddNavMain() {
475:
476:
477: $pluginId = parent::_getPluginId();
478:
479: $navCount = count(parent::$XmlNavMain->nav);
480: for ($i = 0; $i < $navCount; $i++) {
481:
482: $location = cSecurity::escapeString(parent::$XmlNavMain->nav[$i]);
483:
484:
485: $navMain = $this->_ApiNavMainCollection->create($location);
486:
487:
488: $this->_PimPluginRelationsCollection->create($navMain->get('idnavm'), $pluginId, 'navm');
489: }
490: }
491:
492: 493: 494:
495: private function _installAddNavSub() {
496:
497:
498: $attributes = array();
499:
500:
501: $pluginId = parent::_getPluginId();
502:
503: $navCount = count(parent::$XmlNavSub->nav);
504: for ($i = 0; $i < $navCount; $i++) {
505:
506:
507: foreach (parent::$XmlNavSub->nav[$i]->attributes() as $key => $value) {
508: $attributes[$key] = $value;
509: }
510:
511:
512: $attributes['area'] = cSecurity::toString($attributes['area']);
513:
514:
515: if (!in_array($attributes['area'], $this->_getInstalledAreas())) {
516: parent::error(i18n('Defined area', 'pim') . ' <strong>' . $attributes['area'] . '</strong> ' . i18n('are not found on your CONTENIDO installation. Please contact your plugin author.', 'pim'));
517: }
518:
519:
520: $item = $this->_ApiNavSubCollection->create($attributes['navm'], $attributes['area'], $attributes['level'], parent::$XmlNavSub->nav[$i], 1);
521:
522:
523: $this->_PimPluginRelationsCollection->create($item->get('idnavs'), $pluginId, 'navs');
524: }
525: }
526:
527: 528: 529:
530: private function _installAddSpecificSql() {
531: $cfg = cRegistry::getConfig();
532: $db = cRegistry::getDb();
533:
534: if (parent::getMode() == 1) {
535: $tempSqlFilename = $cfg['path']['contenido'] . $cfg['path']['plugins'] . $this->_getPluginFoldername() . DIRECTORY_SEPARATOR . 'plugin_install.sql';
536: } elseif (parent::getMode() == 2 || parent::getMode() == 4) {
537:
538:
539:
540:
541:
542:
543: $tempSqlFilename = parent::$_PimPluginArchiveExtractor->extractArchiveFileToVariable('plugin_install.sql', 0);
544: }
545:
546: if (!cFileHandler::exists($tempSqlFilename)) {
547: return;
548: }
549:
550: $tempSqlContent = cFileHandler::read($tempSqlFilename);
551: $tempSqlContent = str_replace("\r\n", "\n", $tempSqlContent);
552: $tempSqlContent = explode("\n", $tempSqlContent);
553: $tempSqlLines = count($tempSqlContent);
554:
555: $pattern = '/^(CREATE TABLE IF NOT EXISTS|INSERT INTO|UPDATE|ALTER TABLE) `?' . parent::SQL_PREFIX . '`?\b/';
556:
557: for ($i = 0; $i < $tempSqlLines; $i++) {
558: if (preg_match($pattern, $tempSqlContent[$i])) {
559: $tempSqlContent[$i] = str_replace(parent::SQL_PREFIX, $cfg['sql']['sqlprefix'] . '_pi', $tempSqlContent[$i]);
560: $db->query($tempSqlContent[$i]);
561: }
562: }
563: }
564:
565: 566: 567:
568: private function _installAddContentTypes() {
569:
570:
571: $pluginId = parent::_getPluginId();
572:
573: $pattern = '/^CMS_.+/';
574:
575: $typeCount = count(parent::$XmlContentType->type);
576: for ($i = 0; $i < $typeCount; $i++) {
577:
578: $type = cSecurity::toString(parent::$XmlContentType->type[$i]);
579:
580: if (preg_match($pattern, $type)) {
581:
582:
583: $item = $this->_ApiTypeCollection->create($type, '');
584:
585:
586: $this->_PimPluginRelationsCollection->create($item->get('idtype'), $pluginId, 'ctype');
587: }
588: }
589: }
590:
591: 592: 593:
594: private function _installAddModules() {
595: $cfg = cRegistry::getConfig();
596: $module = new cApiModule();
597:
598:
599: $modulesPath = $cfg['path']['contenido'] . $cfg['path']['plugins'] . $this->_getPluginFoldername() . DIRECTORY_SEPARATOR . "modules" . DIRECTORY_SEPARATOR;
600:
601: if (!is_dir($modulesPath)) {
602: return false;
603: }
604:
605: foreach (new DirectoryIterator($modulesPath) as $modulesFiles) {
606:
607: if (substr($modulesFiles->getBasename(), -4) == ".zip") {
608:
609:
610: $module->import($modulesFiles->getBasename(), $modulesFiles->getBasename(), false);
611: }
612: }
613:
614: cDirHandler::recursiveRmdir($modulesPath);
615: }
616:
617: 618: 619:
620: private function _installAddDir() {
621: $cfg = cRegistry::getConfig();
622:
623:
624: $tempPluginDir = $cfg['path']['contenido'] . $cfg['path']['plugins'] . parent::$XmlGeneral->plugin_foldername . DIRECTORY_SEPARATOR;
625:
626:
627: try {
628: parent::$_PimPluginArchiveExtractor->setDestinationPath($tempPluginDir);
629: } catch (cException $e) {
630: parent::$_PimPluginArchiveExtractor->destroyTempFiles();
631: }
632:
633:
634: try {
635: parent::$_PimPluginArchiveExtractor->extractArchive();
636: } catch (cException $e) {
637: parent::$_PimPluginArchiveExtractor->destroyTempFiles();
638: }
639: }
640:
641: }
642: ?>