PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/installer/adapters/plugin.php

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