[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/installer/adapters/ -> plugin.php (source)

   1  <?php
   2  /**
   3   * @version        $Id:plugin.php 6961 2007-03-15 16:06:53Z tcp $
   4   * @package        Joomla.Framework
   5   * @subpackage    Installer
   6   * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7   * @license        GNU/GPL, see LICENSE.php
   8   * Joomla! is free software. This version may have been modified pursuant
   9   * to the GNU General Public License, and as distributed it includes or
  10   * is derivative of works licensed under the GNU General Public License or
  11   * other free or open source software licenses.
  12   * See COPYRIGHT.php for copyright notices and details.
  13   */
  14  
  15  // Check to ensure this file is within the rest of the framework
  16  defined('JPATH_BASE') or die();
  17  
  18  /**
  19   * Plugin installer
  20   *
  21   * @package        Joomla.Framework
  22   * @subpackage    Installer
  23   * @since        1.5
  24   */
  25  class JInstallerPlugin extends JObject
  26  {
  27      /**
  28       * Constructor
  29       *
  30       * @access    protected
  31       * @param    object    $parent    Parent object [JInstaller instance]
  32       * @return    void
  33       * @since    1.5
  34       */
  35  	function __construct(&$parent)
  36      {
  37          $this->parent =& $parent;
  38      }
  39  
  40      /**
  41       * Custom install method
  42       *
  43       * @access    public
  44       * @return    boolean    True on success
  45       * @since    1.5
  46       */
  47  	function install()
  48      {
  49          // Get a database connector object
  50          $db =& $this->parent->getDBO();
  51  
  52          // Get the extension manifest object
  53          $manifest =& $this->parent->getManifest();
  54          $this->manifest =& $manifest->document;
  55  
  56          /**
  57           * ---------------------------------------------------------------------------------------------
  58           * Manifest Document Setup Section
  59           * ---------------------------------------------------------------------------------------------
  60           */
  61  
  62          // Set the extensions name
  63          $name =& $this->manifest->getElementByPath('name');
  64          $name = JFilterInput::clean($name->data(), 'string');
  65          $this->set('name', $name);
  66  
  67          // Get the component description
  68          $description = & $this->manifest->getElementByPath('description');
  69          if (is_a($description, 'JSimpleXMLElement')) {
  70              $this->parent->set('message', $description->data());
  71          } else {
  72              $this->parent->set('message', '' );
  73          }
  74  
  75          /*
  76           * Backward Compatability
  77           * @todo Deprecate in future version
  78           */
  79          $type = $this->manifest->attributes('type');
  80  
  81          // Set the installation path
  82          $element =& $this->manifest->getElementByPath('files');
  83          if (is_a($element, 'JSimpleXMLElement') && count($element->children())) {
  84              $files = $element->children();
  85              foreach ($files as $file) {
  86                  if ($file->attributes($type)) {
  87                      $pname = $file->attributes($type);
  88                      break;
  89                  }
  90              }
  91          }
  92          $group = $this->manifest->attributes('group');
  93          if (!empty ($pname) && !empty($group)) {
  94              $this->parent->setPath('extension_root', JPATH_ROOT.DS.'plugins'.DS.$group);
  95          } else {
  96              $this->parent->abort(JText::_('Plugin').' '.JText::_('Install').': '.JText::_('No plugin file specified'));
  97              return false;
  98          }
  99  
 100          /**
 101           * ---------------------------------------------------------------------------------------------
 102           * Filesystem Processing Section
 103           * ---------------------------------------------------------------------------------------------
 104           */
 105  
 106          // If the plugin directory does not exist, lets create it
 107          $created = false;
 108          if (!file_exists($this->parent->getPath('extension_root'))) {
 109              if (!$created = JFolder::create($this->parent->getPath('extension_root'))) {
 110                  $this->parent->abort(JText::_('Plugin').' '.JText::_('Install').': '.JText::_('Failed to create directory').': "'.$this->parent->getPath('extension_root').'"');
 111                  return false;
 112              }
 113          }
 114  
 115          /*
 116           * If we created the plugin directory and will want to remove it if we
 117           * have to roll back the installation, lets add it to the installation
 118           * step stack
 119           */
 120          if ($created) {
 121              $this->parent->pushStep(array ('type' => 'folder', 'path' => $this->parent->getPath('extension_root')));
 122          }
 123  
 124          // Copy all necessary files
 125          if ($this->parent->parseFiles($element, -1) === false) {
 126              // Install failed, roll back changes
 127              $this->parent->abort();
 128              return false;
 129          }
 130  
 131          // Parse optional tags -- media and language files for plugins go in admin app
 132          $this->parent->parseMedia($this->manifest->getElementByPath('media'), 1);
 133          $this->parent->parseLanguages($this->manifest->getElementByPath('languages'), 1);
 134  
 135          /**
 136           * ---------------------------------------------------------------------------------------------
 137           * Database Processing Section
 138           * ---------------------------------------------------------------------------------------------
 139           */
 140  
 141          // Check to see if a plugin by the same name is already installed
 142          $query = 'SELECT `id`' .
 143                  ' FROM `#__plugins`' .
 144                  ' WHERE folder = '.$db->Quote($group) .
 145                  ' AND element = '.$db->Quote($pname);
 146          $db->setQuery($query);
 147          if (!$db->Query()) {
 148              // Install failed, roll back changes
 149              $this->parent->abort(JText::_('Plugin').' '.JText::_('Install').': '.$db->stderr(true));
 150              return false;
 151          }
 152          $id = $db->loadResult();
 153  
 154          // Was there a module already installed with the same name?
 155          if ($id) {
 156  
 157              if (!$this->parent->getOverwrite())
 158              {
 159                  // Install failed, roll back changes
 160                  $this->parent->abort(JText::_('Plugin').' '.JText::_('Install').': '.JText::_('Plugin').' "'.$pname.'" '.JText::_('already exists!'));
 161                  return false;
 162              }
 163  
 164          } else {
 165              $row =& JTable::getInstance('plugin');
 166              $row->name = $this->get('name');
 167              $row->ordering = 0;
 168              $row->folder = $group;
 169              $row->iscore = 0;
 170              $row->access = 0;
 171              $row->client_id = 0;
 172              $row->element = $pname;
 173              $row->params = $this->parent->getParams();
 174  
 175              // Editor plugins are published by default
 176              if ($group == 'editors') {
 177                  $row->published = 1;
 178              }
 179  
 180              if (!$row->store()) {
 181                  // Install failed, roll back changes
 182                  $this->parent->abort(JText::_('Plugin').' '.JText::_('Install').': '.$db->stderr(true));
 183                  return false;
 184              }
 185  
 186              // Since we have created a plugin item, we add it to the installation step stack
 187              // so that if we have to rollback the changes we can undo it.
 188              $this->parent->pushStep(array ('type' => 'plugin', 'id' => $row->id));
 189          }
 190  
 191          /**
 192           * ---------------------------------------------------------------------------------------------
 193           * Finalization and Cleanup Section
 194           * ---------------------------------------------------------------------------------------------
 195           */
 196  
 197          // Lastly, we will copy the manifest file to its appropriate place.
 198          if (!$this->parent->copyManifest(-1)) {
 199              // Install failed, rollback changes
 200              $this->parent->abort(JText::_('Plugin').' '.JText::_('Install').': '.JText::_('Could not copy setup file'));
 201              return false;
 202          }
 203  
 204          // Load plugin language file
 205          $lang =& JFactory::getLanguage();
 206          $lang->load('plg_'.$group.'_'.$pname);
 207  
 208          return true;
 209      }
 210  
 211      /**
 212       * Custom uninstall method
 213       *
 214       * @access    public
 215       * @param    int        $cid    The id of the plugin to uninstall
 216       * @param    int        $clientId    The id of the client (unused)
 217       * @return    boolean    True on success
 218       * @since    1.5
 219       */
 220  	function uninstall($id, $clientId )
 221      {
 222          // Initialize variables
 223          $row    = null;
 224          $retval = true;
 225          $db        =& $this->parent->getDBO();
 226  
 227          // First order of business will be to load the module object table from the database.
 228          // This should give us the necessary information to proceed.
 229          $row = & JTable::getInstance('plugin');
 230          if ( !$row->load((int) $id) ) {
 231              JError::raiseWarning(100, JText::_('ERRORUNKOWNEXTENSION'));
 232              return false;
 233          }
 234  
 235          // Is the plugin we are trying to uninstall a core one?
 236          // Because that is not a good idea...
 237          if ($row->iscore) {
 238              JError::raiseWarning(100, JText::_('Plugin').' '.JText::_('Uninstall').': '.JText::sprintf('WARNCOREPLUGIN', $row->name)."<br />".JText::_('WARNCOREPLUGIN2'));
 239              return false;
 240          }
 241  
 242          // Get the plugin folder so we can properly build the plugin path
 243          if (trim($row->folder) == '') {
 244              JError::raiseWarning(100, JText::_('Plugin').' '.JText::_('Uninstall').': '.JText::_('Folder field empty, cannot remove files'));
 245              return false;
 246          }
 247  
 248          // Set the plugin root path
 249          $this->parent->setPath('extension_root', JPATH_ROOT.DS.'plugins'.DS.$row->folder);
 250  
 251          // Because plugins don't have their own folders we cannot use the standard method of finding an installation manifest
 252          $manifestFile = JPATH_ROOT.DS.'plugins'.DS.$row->folder.DS.$row->element.'.xml';
 253          if (file_exists($manifestFile))
 254          {
 255              $xml =& JFactory::getXMLParser('Simple');
 256  
 257              // If we cannot load the xml file return null
 258              if (!$xml->loadFile($manifestFile)) {
 259                  JError::raiseWarning(100, JText::_('Plugin').' '.JText::_('Uninstall').': '.JText::_('Could not load manifest file'));
 260                  return false;
 261              }
 262  
 263              /*
 264               * Check for a valid XML root tag.
 265               * @todo: Remove backwards compatability in a future version
 266               * Should be 'install', but for backward compatability we will accept 'mosinstall'.
 267               */
 268              $root =& $xml->document;
 269              if ($root->name() != 'install' && $root->name() != 'mosinstall') {
 270                  JError::raiseWarning(100, JText::_('Plugin').' '.JText::_('Uninstall').': '.JText::_('Invalid manifest file'));
 271                  return false;
 272              }
 273  
 274              // Remove the plugin files
 275              $this->parent->removeFiles($root->getElementByPath('images'), -1);
 276              $this->parent->removeFiles($root->getElementByPath('files'), -1);
 277              JFile::delete($manifestFile);
 278  
 279              // Remove all media and languages as well
 280              $this->parent->removeFiles($root->getElementByPath('media'));
 281              $this->parent->removeFiles($root->getElementByPath('languages'), 1);
 282          } else {
 283              JError::raiseWarning(100, 'Plugin Uninstall: Manifest File invalid or not found');
 284              return false;
 285          }
 286  
 287          // Now we will no longer need the plugin object, so lets delete it
 288          $row->delete($row->id);
 289          unset ($row);
 290  
 291          // If the folder is empty, let's delete it
 292          $files = JFolder::files($this->parent->getPath('extension_root'));
 293          if (!count($files)) {
 294              JFolder::delete($this->parent->getPath('extension_root'));
 295          }
 296  
 297          return $retval;
 298      }
 299  
 300      /**
 301       * Custom rollback method
 302       *     - Roll back the plugin item
 303       *
 304       * @access    public
 305       * @param    array    $arg    Installation step to rollback
 306       * @return    boolean    True on success
 307       * @since    1.5
 308       */
 309  	function _rollback_plugin($arg)
 310      {
 311          // Get database connector object
 312          $db =& $this->parent->getDBO();
 313  
 314          // Remove the entry from the #__plugins table
 315          $query = 'DELETE' .
 316                  ' FROM `#__plugins`' .
 317                  ' WHERE id='.(int)$arg['id'];
 318          $db->setQuery($query);
 319          return ($db->query() !== false);
 320      }
 321  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1