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