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: 
237:         $cfg = cRegistry::getConfig();
238: 
239:         if (self::getMode() == 1) { 
240:             $XmlData = file_get_contents($cfg['path']['contenido'] . $cfg['path']['plugins'] . cSecurity::escapeString($_GET['pluginFoldername']) . DIRECTORY_SEPARATOR . self::PLUGIN_XML_FILENAME);
241:         } elseif (self::getMode() == 2 || self::getMode() == 4) { 
242:                                                                   
243:                                                                   
244: 
245:             
246:             $tempArchiveNewPath = $cfg['path']['frontend'] . DIRECTORY_SEPARATOR . $cfg['path']['temp'];
247: 
248:             
249:             if (!cDirHandler::exists($tempArchiveNewPath)) {
250:                 $success = cDirHandler::create($tempArchiveNewPath);
251: 
252:                 
253:                 if (!$success) {
254:                     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));              }
255:             }
256: 
257:             
258:             $tempArchiveName = cSecurity::escapeString($_FILES['package']['name']);
259: 
260:             
261:             move_uploaded_file($_FILES['package']['tmp_name'], $tempArchiveNewPath . $tempArchiveName);
262: 
263:             
264:             try {
265:                 self::_setPimPluginArchiveExtractor($tempArchiveNewPath, $tempArchiveName);
266:             } catch (cException $e) {
267:                 self::$_PimPluginArchiveExtractor->destroyTempFiles();
268:             }
269: 
270:             
271:             $this->checkZip();
272: 
273:             
274:             $XmlData = self::$_PimPluginArchiveExtractor->extractArchiveFileToVariable(self::PLUGIN_XML_FILENAME);
275:         }
276: 
277:         
278:         if ($this->validXml($XmlData) === true) {
279:             $this->_setXml(simplexml_load_string($XmlData));
280:         } else {
281:             return self::error(i18n('Invalid Xml document. Please contact the plugin author.', 'pim'));
282:         }
283:     }
284: 
285:     286: 287: 288: 289: 290: 291: 
292:     public function checkDependencies() {
293: 
294:         
295:         $cfg = cRegistry::getConfig();
296:         $pluginsDir = $cfg['path']['contenido'] . $cfg['path']['plugins'];
297: 
298:         
299:         $this->_PimPluginCollection->setWhere('idplugin', self::_getPluginId());
300:         $this->_PimPluginCollection->query();
301:         $pimPluginSql = $this->_PimPluginCollection->next();
302:         $uuidUninstall = $pimPluginSql->get('uuid');
303: 
304:         
305:         $this->_PimPluginCollection->resetQuery();
306: 
307:         
308:         $dirs = cDirHandler::read($pluginsDir);
309:         foreach ($dirs as $dirname) {
310: 
311:             
312:             if (!cFileHandler::exists($pluginsDir . $dirname . DIRECTORY_SEPARATOR . self::PLUGIN_XML_FILENAME)) {
313:                 continue;
314:             }
315: 
316:             
317:             $tempXmlContent = cFileHandler::read($pluginsDir . $dirname . DIRECTORY_SEPARATOR . self::PLUGIN_XML_FILENAME);
318: 
319:             
320:             $tempXml = simplexml_load_string($tempXmlContent);
321: 
322:             $dependenciesCount = count($tempXml->dependencies);
323:             for ($i = 0; $i < $dependenciesCount; $i++) {
324: 
325:                 
326:                 $depend = cSecurity::escapeString($tempXml->dependencies->depend[$i]);
327: 
328:                 
329:                 if ($depend == "") {
330:                     continue;
331:                 }
332: 
333:                 
334:                 foreach ($tempXml->dependencies->depend[$i]->attributes() as $key => $value) {
335: 
336:                     
337:                     if ($key  == "uuid") {
338:                         $uuidTemp = cSecurity::escapeString($value);
339:                     }
340:                 }
341: 
342:                 
343:                 
344:                 if ($uuidTemp === $uuidUninstall) {
345: 
346:                     $this->_PimPluginCollection->setWhere('uuid', $tempXml->general->uuid);
347:                     $this->_PimPluginCollection->setWhere('active', '1');
348:                     $this->_PimPluginCollection->query();
349: 
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 (cString::getPartOfString($_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:         
379:         $dom = new DomDocument();
380:         $dom->loadXML($xml);
381: 
382:         
383:         if ($dom->schemaValidate('plugins' . DIRECTORY_SEPARATOR . 'pim' . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'plugin_info.xsd')) {
384:             return true;
385:         } else {
386:             return false;
387:         }
388:     }
389: 
390:     391: 392: 393: 
394:     protected static function error($message = '') {
395: 
396:         
397:         $session = cRegistry::getSession();
398: 
399:         
400:         if (self::getMode() == 2) {
401:             self::$_PimPluginArchiveExtractor->destroyTempFiles();
402:         }
403: 
404:         
405:         $pimError = new cGuiPage('pim_error', 'pim');
406:         $pimError->set('s', 'BACKLINK', $session->url('main.php?area=pim&frame=4'));
407:         $pimError->set('s', 'LANG_BACKLINK', i18n('Back to Plugin Manager', 'pim'));
408:         $pimError->displayError($message);
409:         $pimError->render();
410:         exit();
411:     }
412: 
413:     414: 415: 416: 
417:     protected static function info($message = '') {
418:         return self::$_GuiPage->displayOk($message);
419:     }
420: 
421: }
422: 
423: ?>