aliroExtensionHandler.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*******************************************************************************
00004  * Aliro - the modern, accessible content management system
00005  *
00006  * Aliro is open source software, free to use, and licensed under GPL.
00007  * You can find the full licence at http://www.gnu.org/copyleft/gpl.html GNU/GPL
00008  *
00009  * The author freely draws attention to the fact that Aliro derives from Mambo,
00010  * software that is controlled by the Mambo Foundation.  However, this section
00011  * of code is totally new.  If it should contain any fragments that are similar
00012  * to Mambo, please bear in mind (1) there are only so many ways to do things
00013  * and (2) the author of Aliro is also the author and copyright owner for large
00014  * parts of Mambo 4.6.
00015  *
00016  * Tribute should be paid to all the developers who took Mambo to the stage
00017  * it had reached at the time Aliro was created.  It is a feature rich system
00018  * that contains a good deal of innovation.
00019  *
00020  * Your attention is also drawn to the fact that Aliro relies on other items of
00021  * open source software, which is very much in the spirit of open source.  Aliro
00022  * wishes to give credit to those items of code.  Please refer to
00023  * http://aliro.org/credits for details.  The credits are not included within
00024  * the Aliro package simply to avoid providing a marker that allows hackers to
00025  * identify the system.
00026  *
00027  * Copyright in this code is strictly reserved by its author, Martin Brampton.
00028  * If it seems appropriate, the copyright will be vested in the Aliro Organisation
00029  * at a suitable time.
00030  *
00031  * Copyright (c) 2007 Martin Brampton
00032  *
00033  * http://aliro.org
00034  *
00035  * counterpoint@aliro.org
00036  *
00037  * aliroExtension is the data class for an extension, corresponding to a row
00038  * in the extensions table.
00039  *
00040  * aliroExtensionHandler knows all about the various installed extensions in
00041  * the system.  Anything not integral to the core - components, modules, mambots,
00042  * templates - are counted as extensions.  It is a cached singleton class and
00043  * uses common code the implement the object cache.
00044  *
00045  * aliroCommonExtHandler is an abstract class that is the base for various
00046  * different extension handlers, aliroComponentHandler, aliroModuleHandler
00047  * and aliroMambotHandler.
00048  *
00049  */
00050 
00051 class aliroExtension extends aliroDatabaseRow  {
00052     private static $legalTypes = array('component', 'module', 'mambot', 'plugin', 'template', 'language', 'patch');
00053     protected $DBclass = 'aliroCoreDatabase';
00054     protected $tableName = '#__extensions';
00055     protected $rowKey = 'id';
00056 
00057     public function populateFromXML ($xmlobject) {
00058         $purifier = new HTMLPurifier();
00059         $this->name = $purifier->purify((string) $xmlobject->getXML('name'));
00060         $this->type = $xmlobject->baseAttribute('type');
00061         if (!in_array($this->type, self::$legalTypes)) return T_('has no valid type');
00062         if ('plugin' == $this->type) $extension->type = 'mambot';
00063         $this->formalname = $purifier->purify((string) $xmlobject->getXML('formalname'));
00064         if (!$this->formalname AND 'component' == strtolower($this->type)) $this->formalname = 'com_'.str_replace(' ', '', strtolower($this->name));
00065         if (!$this->formalname) return T_('has no formal name');
00066         $this->admin = ('administrator' == $xmlobject->baseAttribute('client')) ? 2 : 1;
00067         if ('template' == $this->type) {
00068             $currentDefault = aliroTemplateHandler::getInstance()->getDefaultTemplateProperty('formalname', (2 == $this->admin));
00069             if (!$currentDefault OR $currentDefault == $this->formalname) $this->default_template = '1';
00070         }
00071         foreach (array('author', 'version', 'authoremail', 'authorurl', 'description', 'creationdate') as $field) {
00072             $this->$field = $purifier->purify((string) $xmlobject->getXML($field));
00073         }
00074         $this->date = $this->creationdate;
00075         unset($this->creationdate);
00076         foreach (array('adminclass', 'menuclass', 'exportclass') as $field) {
00077             $this->$field = $xmlobject->baseAttribute($field);
00078         }
00079         $this->class = $xmlobject->baseAttribute('userclass');
00080         $this->timestamp = date('Y-m-d');
00081         return false;
00082     }
00083 }
00084 
00085 class aliroExtensionHandler extends cachedSingleton  {
00086     protected static $instance = __CLASS__;
00087     private $extensions = array();
00088     private $extensionsByType = array();
00089     private $templates = array();
00090 
00091     protected function __construct () {
00092         $results = aliroCoreDatabase::getInstance()->doSQLget("SELECT * FROM #__extensions", 'aliroExtension');
00093         if ($results) {
00094             foreach ($results as $extension) {
00095                 $this->extensions[$extension->formalname] = $extension;
00096                 if ($extension->type == 'template') $this->templates[] = $extension->formalname;
00097             }
00098             // Sort by formal name (unique)
00099             ksort($this->extensions);
00100             foreach ($this->extensions as $extension) $this->extensionsByType[$extension->type][$extension->formalname] = $extension;
00101         }
00102     }
00103 
00104     public static function getInstance () {
00105         return is_object(self::$instance) ? self::$instance : (self::$instance = parent::getCachedSingleton(self::$instance));
00106     }
00107 
00108     public function checkStarterPack () {
00109         if (0 == count($this->extensions)) {
00110             $starterpack = criticalInfo::getInstance()->admin_absolute_path.'/starterpack/';
00111             $dir = new aliroDirectory($starterpack);
00112             foreach ($dir->listAll() as $package) if ('index.html' != $package) {
00113                 $installer = new aliroInstaller();
00114                 $installer->installfromfile($starterpack.$package);
00115             }
00116         }
00117     }
00118 
00119     public function getTemplateExtensions () {
00120         $result = array();
00121         foreach ($this->templates as $templatename) $result[$templatename] = $this->extensions[$templatename];
00122         return $result;
00123     }
00124 
00125     public function removeExtensions ($formalnames, $isUpgrade=false) {
00126         $extlist = implode("', '", (array) $formalnames);
00127         foreach ((array) $formalnames as $formalname) {
00128             if ($extension = $this->getExtensionByName ($formalname)) {
00129                 $handler = aliroExtensionHandler::getExtensionTypeHandler($extension->type);
00130                 if (!$isUpgrade) $this->uninstallExtension($handler, $extension);
00131                 $this->removeExtensionFiles($handler, $extension);
00132             }
00133         }
00134         $database = aliroCoreDatabase::getInstance();
00135         $this->deleteFromTable($extlist, 'extensions', 'formalname', $database);
00136         $this->deleteFromTable($extlist, 'components', 'option', $database);
00137         $this->deleteFromTable($extlist, 'modules', 'module', $database);
00138         $this->deleteFromTable($extlist, 'mambots', 'element', $database);
00139         $this->deleteFromTable($extlist, 'classmap', 'formalname', $database);
00140         if (!$isUpgrade) $database->doSQL("DELETE FROM `#__menu` WHERE `component` != '' AND `component` IN ('$extlist')");
00141         $database->doSQL("DELETE FROM `#__admin_menu` WHERE `component` != '' AND `component` IN ('$extlist')");
00142         $database->doSQL("DELETE `#__modules_menu` FROM `#__modules_menu` LEFT JOIN `#__modules` ON `moduleid`=`id` WHERE `id` IS NULL");
00143         aliroMenuHandler::getInstance()->clearCache();
00144         $this->clearCache();
00145     }
00146 
00147     private function deleteFromTable ($extlist, $table, $fieldname, $database) {
00148         $database->doSQL("DELETE FROM `#__$table` WHERE `$fieldname` IN ('$extlist')");
00149     }
00150 
00151     private function removeExtensionFiles ($handler, $extension) {
00152         if ($handler) {
00153             $handler->remove($extension->formalname, $extension->admin);
00154             $handler->clearCache();
00155         }
00156     }
00157 
00158     private function uninstallExtension ($handler, $extension) {
00159         if ('component' == $extension->type AND file_exists(_ALIRO_ABSOLUTE_PATH.$extension->xmlfile)) {
00160             $installer = new aliroExtensionInstaller(_ALIRO_ABSOLUTE_PATH.$extension->xmlfile);
00161             $installer->removeComponent($handler, $extension);
00162         }
00163     }
00164 
00165     public function getExtensions ($type='') {
00166         if ($type) return isset($this->extensionsByType[$type]) ? $this->extensionsByType[$type] : array();
00167         return $this->extensions;
00168     }
00169 
00170     public function getExtensionByName ($formalname) {
00171         return isset($this->extensions[$formalname]) ? $this->extensions[$formalname] : null;
00172     }
00173 
00174     public function getXMLFileName ($formalname) {
00175         if ($ext = $this->getExtensionByName ($formalname)) return $ext->xmlfile;
00176         return '';
00177     }
00178 
00179     public static function getExtensionTypeHandler ($type) {
00180         $prettytype = strtoupper(substr($type,0,1)).strtolower(substr($type,1));
00181         $handlername = 'aliro'.$prettytype.'Handler';
00182         if (aliro::getInstance()->classExists($handlername)) return call_user_func(array($handlername, 'getInstance'));
00183         else return null;
00184     }
00185 
00186 }
00187 
00188 abstract class aliroCommonExtHandler extends cachedSingleton {
00189 
00190     // Provided mainly for the installer
00191     public function getPath ($formalname, $admin) {
00192         return criticalInfo::getInstance()->absolute_path.$this->getRelativePath($formalname, $admin);
00193     }
00194 
00195     // Provided mainly for the installer
00196     public function getRelativePath ($formalname, $admin) {
00197         $extradir = (_ALIRO_ADMIN_SIDE == $admin) ? criticalInfo::getInstance()->admin_dir : '';
00198         return $extradir.$this->extensiondir.$formalname;
00199     }
00200 
00201     // Provided mainly for the installer
00202     public function getClassPath ($formalname, $admin) {
00203         return criticalInfo::getInstance()->class_base.$this->getRelativePath($formalname, $admin);
00204     }
00205 
00206     // Mainly for the installer - overrides default method
00207     public function getXMLRelativePath ($formalname, $admin) {
00208         return $this->getRelativePath ($formalname, $admin);
00209     }
00210 
00211     // Mainly for the installer - overrides default method
00212     public function getXMLPath ($formalname, $admin) {
00213         return criticalInfo::getInstance()->absolute_path.$this->getXMLRelativePath ($formalname, $admin);
00214     }
00215 
00216     // Provided mainly for the installer
00217     public function createDirectory ($formalname, $admin) {
00218         $dir = new aliroDirectory ($this->getPath($formalname, $admin));
00219         return $dir->createFresh();
00220     }
00221 
00222     // Provided for uninstaller, but not currently used 
00223     public function remove ($formalname) {
00224         $info = criticalInfo::getInstance();
00225         $this->deleteDirectory ($this->getPath($formalname, _ALIRO_USER_SIDE));
00226         $this->deleteDirectory ($this->getPath($formalname, _ALIRO_ADMIN_SIDE));
00227         $this->clearCache();
00228     }
00229 
00230     private function deleteDirectory ($path) {
00231         $dir = new aliroDirectory($path);
00232         $dir->deleteAll();
00233     }
00234 
00235     public function clearCache ($immediate=false) {
00236         aliroExtensionHandler::getInstance()->clearCache(true);
00237         parent::clearCache($immediate);
00238     }
00239 
00240 }
00241 
00242 abstract class aliroCommonExtBase extends aliroDatabaseRow {
00243     protected $xmlobject = null;
00244 
00245     public function getXMLObject () {
00246         if (is_null($this->xmlobject)) {
00247             $field = $this->formalfield;
00248             $formalname = $this->$field;
00249             $extension = aliroExtensionHandler::getInstance()->getExtensionByName ($formalname);
00250             $this->xmlobject = simplexml_load_file(aliroCore::getInstance()->getCfg('absolute_path').$extension->xmlfile);
00251         }
00252         return $this->xmlobject;
00253     }
00254 
00255 }

Generated on Thu Apr 17 13:03:26 2008 for ALIRO by  doxygen 1.5.5