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