PageRenderTime 70ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_jce/classes/installer.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 275 lines | 138 code | 55 blank | 82 comment | 36 complexity | 8fe89d5deb3da51b27373915004dcccd MD5 | raw file
  1. <?php
  2. /**
  3. * @package JCE
  4. * @copyright Copyright (c) 2009-2013 Ryan Demmer. All rights reserved.
  5. * @license GNU/GPL 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  6. * JCE is free software. This version may have been modified pursuant
  7. * to the GNU General Public License, and as distributed it includes or
  8. * is derivative of works licensed under the GNU General Public License or
  9. * other free or open source software licenses.
  10. */
  11. defined('_JEXEC') or die('RESTRICTED');
  12. jimport('joomla.installer.installer');
  13. /**
  14. * Plugins Component Controller
  15. *
  16. * @package Joomla
  17. * @subpackage Plugins
  18. */
  19. class WFInstaller extends JObject {
  20. protected $_adapters = null;
  21. public $installer = null;
  22. public function __construct() {
  23. $installer = JInstaller::getInstance();
  24. $this->installer = $installer;
  25. }
  26. /**
  27. * Returns the global Installer object, only creating it
  28. * if it doesn't already exist.
  29. *
  30. * @return object An installer object
  31. */
  32. public static function getInstance() {
  33. static $instance;
  34. if (!isset($instance)) {
  35. $instance = new WFInstaller;
  36. }
  37. return $instance;
  38. }
  39. /**
  40. * Set an installer adapter by name
  41. *
  42. * @access public
  43. * @param string $name Adapter name
  44. * @param object $adapter Installer adapter object
  45. * @return boolean True if successful
  46. */
  47. public function setAdapter($name, $adapter = null) {
  48. if (!is_object($adapter)) {
  49. $adapter = $this->getAdapter($name);
  50. }
  51. $this->_adapters[$name] = $adapter;
  52. return true;
  53. }
  54. /**
  55. * Get a JCE installer adapter
  56. * @param string $name adapter name eg: plugin.
  57. * @return $adapter instance
  58. */
  59. public function getAdapter($type) {
  60. // Try to load the adapter object
  61. require_once(dirname(dirname(__FILE__)) . '/adapters/' . strtolower($type) . '.php');
  62. $class = 'WFInstaller' . ucfirst($type);
  63. if (!class_exists($class)) {
  64. return false;
  65. }
  66. $adapter = new $class($this);
  67. // set parent as JInstaller instance
  68. $adapter->parent = $this->installer;
  69. return $adapter;
  70. }
  71. public function install($path) {
  72. if ($path && JFolder::exists($path)) {
  73. $this->installer->setPath('source', $path);
  74. } else {
  75. $this->installer->abort(JText::_('JLIB_INSTALLER_ABORT_NOINSTALLPATH'));
  76. return false;
  77. }
  78. if (!$this->setupInstall()) {
  79. $this->installer->abort(JText::_('JLIB_INSTALLER_ABORT_DETECTMANIFEST'));
  80. return false;
  81. }
  82. // Load the adapter(s) for the install manifest
  83. $type = (string) $this->installer->manifest->attributes()->type;
  84. if ($type == 'extension') {
  85. $type = 'plugin';
  86. }
  87. if (is_object($this->_adapters[$type])) {
  88. // Add the languages from the package itself
  89. if (method_exists($this->_adapters[$type], 'loadLanguage')) {
  90. $this->_adapters[$type]->loadLanguage($path);
  91. }
  92. // Run the install
  93. $ret = $this->_adapters[$type]->install();
  94. $this->set('name', $this->installer->get('name'));
  95. $this->set('version', $this->installer->get('version'));
  96. $this->set('message', $this->installer->get('description'));
  97. $this->set('extension.message', $this->installer->get('extension.message'));
  98. return $ret;
  99. }
  100. }
  101. /**
  102. * Package uninstallation method
  103. *
  104. * @param string $type Package type
  105. * @param mixed $identifier Package identifier for adapter
  106. *
  107. * @return boolean True if successful
  108. */
  109. public function uninstall($type, $identifier) {
  110. if (!isset($this->_adapters[$type]) || !is_object($this->_adapters[$type])) {
  111. if (!$this->setAdapter($type)) {
  112. // We failed to get the right adapter
  113. return false;
  114. }
  115. }
  116. if (is_object($this->_adapters[$type])) {
  117. // Run the uninstall
  118. return $this->_adapters[$type]->uninstall($identifier);
  119. }
  120. return false;
  121. }
  122. /**
  123. * Prepare for installation: this method sets the installation directory, finds
  124. * and checks the installation file and verifies the installation type.
  125. *
  126. * @return boolean True on success
  127. */
  128. public function setupInstall() {
  129. // We need to find the installation manifest file
  130. if (!$this->findManifest()) {
  131. return false;
  132. }
  133. // Load the adapter(s) for the install manifest
  134. $type = (string) $this->installer->manifest->attributes()->type;
  135. if ($type == 'extension') {
  136. $type = 'plugin';
  137. }
  138. // Lazy load the adapter
  139. if (!isset($this->_adapters[$type]) || !is_object($this->_adapters[$type])) {
  140. $adapter = $this->getAdapter($type);
  141. if (!$adapter) {
  142. return false;
  143. }
  144. if (!$this->setAdapter($type, $adapter)) {
  145. return false;
  146. }
  147. }
  148. return true;
  149. }
  150. public function getManifest() {
  151. if (!is_object($this->installer->manifest)) {
  152. $this->findManifest();
  153. }
  154. return $this->installer->manifest;
  155. }
  156. /**
  157. * Tries to find the package manifest file
  158. *
  159. * @return boolean True on success, False on error
  160. */
  161. public function findManifest() {
  162. jimport('joomla.filesystem.folder');
  163. // Get an array of all the XML files from the installation directory
  164. $xmlfiles = JFolder::files($this->installer->getPath('source'), '.xml$', 1, true);
  165. // If at least one XML file exists
  166. if (!empty($xmlfiles)) {
  167. foreach ($xmlfiles as $file) {
  168. // Is it a valid Joomla installation manifest file?
  169. $manifest = $this->isManifest($file);
  170. if (!is_null($manifest)) {
  171. // If the root method attribute is set to upgrade, allow file overwrite
  172. if ((string) $manifest->attributes()->method == 'upgrade') {
  173. if (method_exists($this->installer, 'setUpgrade')) {
  174. $this->installer->setUpgrade(true);
  175. }
  176. $this->installer->setOverwrite(true);
  177. }
  178. // If the overwrite option is set, allow file overwriting
  179. if ((string) $manifest->attributes()->overwrite == 'true') {
  180. $this->installer->setOverwrite(true);
  181. }
  182. // Set the manifest object and path
  183. $this->installer->manifest = $manifest;
  184. $this->installer->setPath('manifest', $file);
  185. // Set the installation source path to that of the manifest file
  186. $this->installer->setPath('source', dirname($file));
  187. return true;
  188. }
  189. }
  190. // None of the XML files found were valid install files
  191. JError::raiseWarning(JText::_('WF_INSTALLER_MANIFEST_INVALID'));
  192. return false;
  193. } else {
  194. // No XML files were found in the install folder
  195. JError::raiseWarning(JText::_('WF_INSTALLER_MANIFEST_LOAD_ERROR'));
  196. return false;
  197. }
  198. }
  199. /**
  200. * Is the XML file a valid Joomla installation manifest file.
  201. *
  202. * @param string $file An xmlfile path to check
  203. *
  204. * @return mixed A SimpleXMLElement, or null if the file failed to parse
  205. */
  206. public function isManifest($file) {
  207. $xml = simplexml_load_file($file);
  208. // If we cannot load the XML file return null
  209. if (!$xml) {
  210. return null;
  211. }
  212. // Check for a valid XML root tag.
  213. if ($xml->getName() != 'extension' && $xml->getName() != 'install') {
  214. return null;
  215. }
  216. // Valid manifest file return the object
  217. return $xml;
  218. }
  219. }
  220. ?>