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         $this->inner = ('yes' == $xmlobject->baseAttribute('inner')) ? 1 : 0;
00068         if ('template' == $this->type) {
00069             $currentDefault = aliroTemplateHandler::getInstance()->getDefaultTemplateProperty('formalname', (2 == $this->admin));
00070             if (!$currentDefault OR $currentDefault == $this->formalname) $this->default_template = '1';
00071         }
00072         foreach (array('author', 'version', 'authoremail', 'authorurl', 'description', 'creationdate') as $field) {
00073             $this->$field = $purifier->purify((string) $xmlobject->getXML($field));
00074         }
00075         $this->date = $this->creationdate;
00076         unset($this->creationdate);
00077         foreach (array('adminclass', 'menuclass', 'exportclass') as $field) {
00078             $this->$field = $xmlobject->baseAttribute($field);
00079         }
00080         $this->class = $xmlobject->baseAttribute('userclass');
00081         $this->timestamp = date('Y-m-d');
00082         return false;
00083     }
00084 }
00085 
00086 class aliroExtensionHandler extends cachedSingleton  {
00087     protected static $instance = __CLASS__;
00088     private $extensions = array();
00089     private $extensionsByType = array();
00090     private $templates = array();
00091 
00092     protected function __construct () {
00093         $results = aliroCoreDatabase::getInstance()->doSQLget("SELECT * FROM #__extensions", 'aliroExtension');
00094         if ($results) {
00095             foreach ($results as $extension) {
00096                 $this->extensions[$extension->formalname] = $extension;
00097                 if ($extension->type == 'template') $this->templates[] = $extension->formalname;
00098             }
00099             // Sort by formal name (unique)
00100             ksort($this->extensions);
00101             foreach ($this->extensions as $extension) $this->extensionsByType[$extension->type][$extension->formalname] = $extension;
00102         }
00103     }
00104 
00105     public static function getInstance () {
00106         return is_object(self::$instance) ? self::$instance : (self::$instance = parent::getCachedSingleton(self::$instance));
00107     }
00108 
00109     public function checkStarterPack () {
00110         if (0 == count($this->extensions)) {
00111             $starterpack = criticalInfo::getInstance()->admin_absolute_path.'/starterpack/';
00112             $dir = new aliroDirectory($starterpack);
00113             foreach ($dir->listAll() as $package) if ('index.html' != $package) {
00114                 $installer = new aliroInstaller();
00115                 $installer->installfromfile($starterpack.$package);
00116             }
00117         }
00118     }
00119 
00120     public function getTemplateExtensions () {
00121         $result = array();
00122         foreach ($this->templates as $templatename) $result[$templatename] = $this->extensions[$templatename];
00123         return $result;
00124     }
00125 
00126     public function removeExtensions ($formalnames, $isUpgrade=false) {
00127         $extlist = implode("', '", (array) $formalnames);
00128         foreach ((array) $formalnames as $formalname) {
00129             if ($extension = $this->getExtensionByName ($formalname)) {
00130                 $handler = aliroExtensionHandler::getExtensionTypeHandler($extension->type);
00131                 if (!$isUpgrade) $this->uninstallExtension($handler, $extension);
00132                 $this->removeExtensionFiles($handler, $extension);
00133             }
00134         }
00135         $database = aliroCoreDatabase::getInstance();
00136         $this->deleteFromTable($extlist, 'extensions', 'formalname', $database);
00137         $this->deleteFromTable($extlist, 'components', 'option', $database);
00138         if (!$isUpgrade) $this->deleteFromTable($extlist, 'modules', 'module', $database);
00139         $this->deleteFromTable($extlist, 'mambots', 'element', $database);
00140         $this->deleteFromTable($extlist, 'classmap', 'formalname', $database);
00141         if (!$isUpgrade) $database->doSQL("DELETE FROM `#__menu` WHERE `component` != '' AND `component` IN ('$extlist')");
00142         $database->doSQL("DELETE FROM `#__admin_menu` WHERE `component` != '' AND `component` IN ('$extlist')");
00143         $database->doSQL("DELETE `#__modules_menu` FROM `#__modules_menu` LEFT JOIN `#__modules` ON `moduleid`=`id` WHERE `id` IS NULL");
00144         aliroMenuHandler::getInstance()->clearCache();
00145         $this->clearCache();
00146     }
00147 
00148     private function deleteFromTable ($extlist, $table, $fieldname, $database) {
00149         $database->doSQL("DELETE FROM `#__$table` WHERE `$fieldname` IN ('$extlist')");
00150     }
00151 
00152     private function removeExtensionFiles ($handler, $extension) {
00153         if ($handler) {
00154             $handler->remove($extension->formalname, $extension->admin);
00155             $handler->clearCache();
00156         }
00157     }
00158 
00159     private function uninstallExtension ($handler, $extension) {
00160         if ('component' == $extension->type AND file_exists(_ALIRO_ABSOLUTE_PATH.$extension->xmlfile)) {
00161             $installer = new aliroExtensionInstaller(_ALIRO_ABSOLUTE_PATH.$extension->xmlfile);
00162             $installer->removeComponent($handler, $extension);
00163         }
00164     }
00165 
00166     public function getExtensions ($type='') {
00167         if ($type) return isset($this->extensionsByType[$type]) ? $this->extensionsByType[$type] : array();
00168         return $this->extensions;
00169     }
00170 
00171     public function getExtensionByName ($formalname) {
00172         return isset($this->extensions[$formalname]) ? $this->extensions[$formalname] : null;
00173     }
00174 
00175     public function getXMLFileName ($formalname) {
00176         if ($ext = $this->getExtensionByName ($formalname)) return $ext->xmlfile;
00177         return '';
00178     }
00179 
00180     public static function getExtensionTypeHandler ($type) {
00181         $prettytype = strtoupper(substr($type,0,1)).strtolower(substr($type,1));
00182         $handlername = 'aliro'.$prettytype.'Handler';
00183         if (aliro::getInstance()->classExists($handlername)) return call_user_func(array($handlername, 'getInstance'));
00184         else return null;
00185     }
00186 
00187 }
00188 
00189 abstract class aliroCommonExtHandler extends cachedSingleton {
00190 
00191     // Provided mainly for the installer
00192     public function getPath ($formalname, $admin) {
00193         return criticalInfo::getInstance()->absolute_path.$this->getRelativePath($formalname, $admin);
00194     }
00195 
00196     // Provided mainly for the installer
00197     public function getRelativePath ($formalname, $admin) {
00198         $extradir = (_ALIRO_ADMIN_SIDE == $admin) ? criticalInfo::getInstance()->admin_dir : '';
00199         return $extradir.$this->extensiondir.$formalname;
00200     }
00201 
00202     // Provided mainly for the installer
00203     public function getClassPath ($formalname, $admin) {
00204         return criticalInfo::getInstance()->class_base.$this->getRelativePath($formalname, $admin);
00205     }
00206 
00207     // Mainly for the installer - overrides default method
00208     public function getXMLRelativePath ($formalname, $admin) {
00209         return $this->getRelativePath ($formalname, $admin);
00210     }
00211 
00212     // Mainly for the installer - overrides default method
00213     public function getXMLPath ($formalname, $admin) {
00214         return criticalInfo::getInstance()->absolute_path.$this->getXMLRelativePath ($formalname, $admin);
00215     }
00216 
00217     // Provided mainly for the installer
00218     public function createDirectory ($formalname, $admin) {
00219         $dir = new aliroDirectory ($this->getPath($formalname, $admin));
00220         return $dir->createFresh();
00221     }
00222 
00223     // Provided for uninstaller, but not currently used 
00224     public function remove ($formalname) {
00225         $info = criticalInfo::getInstance();
00226         $this->deleteDirectory ($this->getPath($formalname, _ALIRO_USER_SIDE));
00227         $this->deleteDirectory ($this->getPath($formalname, _ALIRO_ADMIN_SIDE));
00228         $this->clearCache();
00229     }
00230 
00231     private function deleteDirectory ($path) {
00232         $dir = new aliroDirectory($path);
00233         $dir->deleteAll();
00234     }
00235 
00236     public function clearCache ($immediate=false) {
00237         aliroExtensionHandler::getInstance()->clearCache(true);
00238         parent::clearCache($immediate);
00239     }
00240 
00241 }
00242 
00243 abstract class aliroCommonExtBase extends aliroDatabaseRow {
00244     protected $xmlobject = null;
00245 
00246     public function getXMLObject () {
00247         if (is_null($this->xmlobject)) {
00248             $field = $this->formalfield;
00249             $formalname = $this->$field;
00250             $extension = aliroExtensionHandler::getInstance()->getExtensionByName ($formalname);
00251             $this->xmlobject = simplexml_load_file(aliroCore::getInstance()->getCfg('absolute_path').$extension->xmlfile);
00252         }
00253         return $this->xmlobject;
00254     }
00255 
00256 }

Generated on Wed May 14 13:01:56 2008 for ALIRO by  doxygen 1.5.5