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 PimPluginSetup {
24: 25: 26:
27: const PLUGIN_XML_FILENAME = "plugin.xml";
28: 29: 30:
31: const SQL_PREFIX = "!PREFIX!";
32:
33: 34: 35: 36: 37: 38: 39:
40: public static $mode = 0;
41:
42: 43: 44:
45: protected static $_GuiPage;
46:
47: 48: 49:
50: protected static ;
51:
52: 53: 54: 55: 56: 57: 58:
59: private static $_updateSqlFileExist = false;
60:
61: 62: 63: 64: 65: 66:
67: public static $XmlGeneral;
68:
69: 70: 71: 72: 73:
74: public static $XmlRequirements;
75:
76: 77: 78: 79: 80:
81: public static $XmlDependencies;
82:
83: 84: 85: 86: 87:
88: public static $XmlArea;
89:
90: 91: 92: 93: 94:
95: public static $XmlActions;
96:
97: 98: 99: 100: 101:
102: public static $XmlFrames;
103:
104: 105: 106: 107: 108:
109: public static $XmlNavMain;
110:
111: 112: 113: 114: 115:
116: public static $XmlNavSub;
117:
118: 119: 120: 121: 122:
123: public static $XmlContentType;
124:
125: 126: 127: 128: 129:
130: protected static $_pluginId = 0;
131:
132: 133: 134: 135: 136:
137: protected static $_pluginName;
138:
139:
140:
141: 142: 143: 144: 145: 146: 147:
148: public static function setMode($mode) {
149: switch ($mode) {
150: case 'extracted':
151: self::$mode = 1;
152: break;
153: case 'uploaded':
154: self::$mode = 2;
155: break;
156: case 'uninstall':
157: self::$mode = 3;
158: break;
159: case 'update':
160: self::$mode = 4;
161: break;
162: }
163: }
164:
165: 166: 167: 168: 169: 170: 171:
172: public function setPageClass($page) {
173: return self::$_GuiPage = $page;
174: }
175:
176: 177: 178: 179: 180:
181: protected function _setUpdateSqlFileExist($value) {
182: self::$_updateSqlFileExist = cSecurity::toBoolean($value);
183: }
184:
185: 186: 187: 188: 189: 190: 191:
192: protected static function ($tempArchiveNewPath, $tempArchiveName) {
193: return self::$_PimPluginArchiveExtractor = new PimPluginArchiveExtractor($tempArchiveNewPath, $tempArchiveName);
194: }
195:
196: 197: 198: 199: 200:
201: private function _setXml($xml) {
202:
203:
204: self::$XmlGeneral = $xml->general;
205:
206:
207: self::$XmlRequirements = $xml->requirements;
208:
209:
210: self::$XmlDependencies = $xml->dependencies;
211:
212:
213: self::$XmlArea = $xml->contenido->areas;
214:
215:
216: self::$XmlActions = $xml->contenido->actions;
217:
218:
219: self::$XmlFrames = $xml->contenido->frames;
220:
221:
222: self::$XmlNavMain = $xml->contenido->nav_main;
223:
224:
225: self::$XmlNavSub = $xml->contenido->nav_sub;
226:
227:
228: self::$XmlContentType = $xml->content_types;
229: }
230:
231: 232: 233: 234: 235: 236: 237:
238: public function setPluginId($pluginId = 0) {
239: return self::$_pluginId = $pluginId;
240: }
241:
242: 243: 244: 245: 246: 247: 248:
249: public function setPluginName($pluginName = '') {
250: return self::$_pluginName = $pluginName;
251: }
252:
253: 254: 255: 256: 257:
258: public static function getMode() {
259: return self::$mode;
260: }
261:
262: 263: 264: 265: 266:
267: protected static function _getPluginId() {
268: return self::$_pluginId;
269: }
270:
271: 272: 273: 274: 275:
276: protected static function _getPluginName() {
277: return self::$_pluginName;
278: }
279:
280: 281: 282: 283: 284:
285: protected function _getUpdateSqlFileExist() {
286: return self::$_updateSqlFileExist;
287: }
288:
289:
290:
291: 292: 293: 294: 295: 296:
297: public function checkXml() {
298:
299: $cfg = cRegistry::getConfig();
300:
301: if (self::getMode() == 1) {
302: $XmlData = file_get_contents($cfg['path']['contenido'] . $cfg['path']['plugins'] . cSecurity::escapeString($_GET['pluginFoldername']) . DIRECTORY_SEPARATOR . self::PLUGIN_XML_FILENAME);
303: } elseif (self::getMode() == 2 || self::getMode() == 4) {
304:
305:
306:
307: $tempArchiveNewPath = $cfg['path']['frontend'] . DIRECTORY_SEPARATOR . $cfg['path']['temp'];
308:
309:
310: if (!cDirHandler::exists($tempArchiveNewPath)) {
311: $success = cDirHandler::create($tempArchiveNewPath);
312:
313:
314: if (!$success) {
315: self::error(sprintf(i18n('Plugin Manager can not found a temporary CONTENIDO directory. Also it is not possible to create a temporary directory at <em>%s</em>. You have to create it manualy.', 'pim'), $tempArchiveNewPath));
316: return;
317: }
318: }
319:
320:
321: $tempArchiveName = cSecurity::escapeString($_FILES['package']['name']);
322:
323:
324: move_uploaded_file($_FILES['package']['tmp_name'], $tempArchiveNewPath . $tempArchiveName);
325:
326:
327: try {
328: self::_setPimPluginArchiveExtractor($tempArchiveNewPath, $tempArchiveName);
329: } catch (cException $e) {
330: self::$_PimPluginArchiveExtractor->destroyTempFiles();
331: }
332:
333:
334: $this->checkZip();
335:
336:
337: $XmlData = self::$_PimPluginArchiveExtractor->extractArchiveFileToVariable(self::PLUGIN_XML_FILENAME);
338: }
339:
340:
341: if ($this->validXml($XmlData) === true) {
342: $this->_setXml(simplexml_load_string($XmlData));
343: } else {
344: self::error(i18n('Invalid Xml document. Please contact the plugin author.', 'pim'));
345: return;
346: }
347: }
348:
349: 350: 351: 352: 353: 354: 355: 356: 357:
358: public function checkDependencies() {
359:
360:
361: $cfg = cRegistry::getConfig();
362: $pluginsDir = $cfg['path']['contenido'] . $cfg['path']['plugins'];
363:
364:
365: $this->_PimPluginCollection->setWhere('idplugin', self::_getPluginId());
366: $this->_PimPluginCollection->query();
367: $pimPluginSql = $this->_PimPluginCollection->next();
368: $uuidUninstall = $pimPluginSql->get('uuid');
369:
370:
371: $this->_PimPluginCollection->resetQuery();
372:
373:
374: $dirs = cDirHandler::read($pluginsDir);
375: foreach ($dirs as $dirname) {
376:
377:
378: if (!cFileHandler::exists($pluginsDir . $dirname . DIRECTORY_SEPARATOR . self::PLUGIN_XML_FILENAME)) {
379: continue;
380: }
381:
382:
383: $tempXmlContent = cFileHandler::read($pluginsDir . $dirname . DIRECTORY_SEPARATOR . self::PLUGIN_XML_FILENAME);
384:
385:
386: $tempXml = simplexml_load_string($tempXmlContent);
387:
388: $dependenciesCount = count($tempXml->dependencies);
389: for ($i = 0; $i < $dependenciesCount; $i++) {
390:
391:
392: $depend = cSecurity::escapeString($tempXml->dependencies->depend[$i]);
393:
394:
395: if ($depend == "") {
396: continue;
397: }
398:
399:
400: foreach ($tempXml->dependencies->depend[$i]->attributes() as $key => $value) {
401:
402:
403: if ($key == "uuid") {
404: $uuidTemp = cSecurity::escapeString($value);
405: }
406: }
407:
408:
409:
410: if ($uuidTemp === $uuidUninstall) {
411:
412: $this->_PimPluginCollection->setWhere('uuid', $tempXml->general->uuid);
413: $this->_PimPluginCollection->setWhere('active', '1');
414: $this->_PimPluginCollection->query();
415:
416: if ($this->_PimPluginCollection->count() != 0) {
417: self::setPluginName($tempXml->general->plugin_name);
418: return false;
419: }
420: }
421: }
422: }
423:
424: return true;
425: }
426:
427: 428: 429: 430: 431:
432: private function checkZip() {
433: if (cString::getPartOfString($_FILES['package']['name'], -4) != ".zip") {
434: self::error(i18n('Plugin Manager accepts only Zip archives', 'pim'));
435: }
436: }
437:
438: 439: 440: 441: 442:
443: private function validXml($xml) {
444:
445:
446: $dom = new DomDocument();
447: $dom->loadXML($xml);
448:
449:
450: if ($dom->schemaValidate('plugins' . DIRECTORY_SEPARATOR . 'pim' . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'plugin_info.xsd')) {
451: return true;
452: } else {
453: return false;
454: }
455: }
456:
457: 458: 459: 460: 461: 462: 463: 464:
465: protected static function error($message = '') {
466:
467:
468: $session = cRegistry::getSession();
469:
470:
471: if (self::getMode() == 2) {
472: self::$_PimPluginArchiveExtractor->destroyTempFiles();
473: }
474:
475:
476: $pimError = new cGuiPage('pim_error', 'pim');
477: $pimError->set('s', 'BACKLINK', $session->url('main.php?area=pim&frame=4'));
478: $pimError->set('s', 'LANG_BACKLINK', i18n('Back to Plugin Manager', 'pim'));
479: $pimError->displayError($message);
480: $pimError->render();
481: exit();
482: }
483:
484: 485: 486: 487: 488:
489: protected static function info($message = '') {
490: self::$_GuiPage->displayOk($message);
491: }
492:
493: }
494: