PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/installer/adapters/library.php

https://github.com/chalosalvador/GDS
PHP | 389 lines | 235 code | 43 blank | 111 comment | 27 complexity | 3316965277fc9ce5e9410a3c625d4549 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Installer
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.installer.librarymanifest');
  11. jimport('joomla.base.adapterinstance');
  12. /**
  13. * Library installer
  14. *
  15. * @package Joomla.Platform
  16. * @subpackage Installer
  17. * @since 11.1
  18. */
  19. class JInstallerLibrary extends JAdapterInstance
  20. {
  21. /**
  22. * Custom loadLanguage method
  23. *
  24. * @param string $path the path where to find language files
  25. * @since 11.1
  26. */
  27. public function loadLanguage($path=null)
  28. {
  29. $source = $this->parent->getPath('source');
  30. if (!$source) {
  31. $this->parent->setPath('source', JPATH_PLATFORM . '/'.$this->parent->extension->element);
  32. }
  33. $this->manifest = $this->parent->getManifest();
  34. $extension = 'lib_' . strtolower(JFilterInput::getInstance()->clean((string)$this->manifest->name, 'cmd'));
  35. $name = strtolower((string)$this->manifest->libraryname);
  36. $lang = JFactory::getLanguage();
  37. $source = $path ? $path : JPATH_PLATFORM . "/$name";
  38. $lang->load($extension . '.sys', $source, null, false, false)
  39. || $lang->load($extension . '.sys', JPATH_SITE, null, false, false)
  40. || $lang->load($extension . '.sys', $source, $lang->getDefault(), false, false)
  41. || $lang->load($extension . '.sys', JPATH_SITE, $lang->getDefault(), false, false);
  42. }
  43. /**
  44. * Custom install method
  45. *
  46. * @return boolean True on success
  47. * @since 11.1
  48. */
  49. public function install()
  50. {
  51. // Get the extension manifest object
  52. $this->manifest = $this->parent->getManifest();
  53. // Manifest Document Setup Section
  54. // Set the extensions name
  55. $name = JFilterInput::getInstance()->clean((string)$this->manifest->name, 'string');
  56. $element = str_replace('.xml','',basename($this->parent->getPath('manifest')));
  57. $this->set('name', $name);
  58. $this->set('element', $element);
  59. $db = $this->parent->getDbo();
  60. $db->setQuery('SELECT extension_id FROM #__extensions WHERE type="library" AND element = "'. $element .'"');
  61. $result = $db->loadResult();
  62. if ($result)
  63. {
  64. // Already installed, can we upgrade?
  65. if ($this->parent->getOverwrite() || $this->parent->getUpgrade())
  66. {
  67. // We can upgrade, so uninstall the old one
  68. $installer = new JInstaller(); // we don't want to compromise this instance!
  69. $installer->uninstall('library', $result);
  70. }
  71. else
  72. {
  73. // Abort the install, no upgrade possible
  74. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_LIB_INSTALL_ALREADY_INSTALLED'));
  75. return false;
  76. }
  77. }
  78. // Get the libraries description
  79. $description = (string)$this->manifest->description;
  80. if ($description) {
  81. $this->parent->set('message', JText::_($description));
  82. }
  83. else {
  84. $this->parent->set('message', '');
  85. }
  86. // Set the installation path
  87. $group = (string)$this->manifest->libraryname;
  88. if ( ! $group) {
  89. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_LIB_INSTALL_NOFILE'));
  90. return false;
  91. }
  92. else
  93. {
  94. $this->parent->setPath('extension_root', JPATH_PLATFORM . '/' . implode(DS,explode('/',$group)));
  95. }
  96. // Filesystem Processing Section
  97. // If the plugin directory does not exist, let's create it
  98. $created = false;
  99. if (!file_exists($this->parent->getPath('extension_root')))
  100. {
  101. if (!$created = JFolder::create($this->parent->getPath('extension_root')))
  102. {
  103. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_LIB_INSTALL_FAILED_TO_CREATE_DIRECTORY', $this->parent->getPath('extension_root')));
  104. return false;
  105. }
  106. }
  107. // If we created the plugin directory and will want to remove it if we
  108. // have to roll back the installation, lets add it to the installation
  109. // step stack
  110. if ($created) {
  111. $this->parent->pushStep(array ('type' => 'folder', 'path' => $this->parent->getPath('extension_root')));
  112. }
  113. // Copy all necessary files
  114. if ($this->parent->parseFiles($this->manifest->files, -1) === false)
  115. {
  116. // Install failed, roll back changes
  117. $this->parent->abort();
  118. return false;
  119. }
  120. // Parse optional tags
  121. $this->parent->parseLanguages($this->manifest->languages);
  122. $this->parent->parseMedia($this->manifest->media);
  123. // Extension Registration
  124. $row = JTable::getInstance('extension');
  125. $row->name = $this->get('name');
  126. $row->type = 'library';
  127. $row->element = $this->get('element');
  128. $row->folder = ''; // There is no folder for modules
  129. $row->enabled = 1;
  130. $row->protected = 0;
  131. $row->access = 1;
  132. $row->client_id = 0;
  133. $row->params = $this->parent->getParams();
  134. $row->custom_data = ''; // custom data
  135. $row->manifest_cache = $this->parent->generateManifestCache();
  136. if (!$row->store())
  137. {
  138. // Install failed, roll back changes
  139. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_LIB_INSTALL_ROLLBACK', $db->stderr(true)));
  140. return false;
  141. }
  142. // Finalization and Cleanup Section
  143. // Lastly, we will copy the manifest file to its appropriate place.
  144. $manifest = Array();
  145. $manifest['src'] = $this->parent->getPath('manifest');
  146. $manifest['dest'] = JPATH_MANIFESTS . '/libraries/' . basename($this->parent->getPath('manifest'));
  147. if (!$this->parent->copyFiles(array($manifest), true))
  148. {
  149. // Install failed, rollback changes
  150. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_LIB_INSTALL_COPY_SETUP'));
  151. return false;
  152. }
  153. return $row->get('extension_id');
  154. }
  155. /**
  156. * Custom update method
  157. *
  158. * @return boolean True on success
  159. * @since 11.1
  160. */
  161. public function update()
  162. {
  163. // Since this is just files, an update removes old files
  164. // Get the extension manifest object
  165. $this->manifest = $this->parent->getManifest();
  166. // Manifest Document Setup Section
  167. // Set the extensions name
  168. $name = (string)$this->manifest->name;
  169. $name = JFilterInput::getInstance()->clean($name, 'string');
  170. $element = str_replace('.xml','',basename($this->parent->getPath('manifest')));
  171. $this->set('name', $name);
  172. $this->set('element', $element);
  173. $installer = new JInstaller(); // we don't want to compromise this instance!
  174. $db = $this->parent->getDbo();
  175. $db->setQuery('SELECT extension_id FROM #__extensions WHERE type="library" AND element = "'. $element .'"');
  176. $result = $db->loadResult();
  177. if ($result) {
  178. // Already installed, which would make sense
  179. $installer->uninstall('library', $result);
  180. }
  181. // Now create the new files
  182. return $this->install();
  183. }
  184. /**
  185. * Custom uninstall method
  186. *
  187. * @param string $id The id of the library to uninstall
  188. *
  189. * @return boolean True on success
  190. * @since 11.1
  191. */
  192. public function uninstall($id)
  193. {
  194. // Initialise variables.
  195. $retval = true;
  196. // First order of business will be to load the module object table from the database.
  197. // This should give us the necessary information to proceed.
  198. $row = JTable::getInstance('extension');
  199. if (!$row->load((int) $id) || !strlen($row->element))
  200. {
  201. JError::raiseWarning(100, JText::_('ERRORUNKOWNEXTENSION'));
  202. return false;
  203. }
  204. // Is the library we are trying to uninstall a core one?
  205. // Because that is not a good idea...
  206. if ($row->protected)
  207. {
  208. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_LIB_UNINSTALL_WARNCORELIBRARY'));
  209. return false;
  210. }
  211. $manifestFile = JPATH_MANIFESTS . '/libraries/' . $row->element .'.xml';
  212. // Because libraries may not have their own folders we cannot use the standard method of finding an installation manifest
  213. if (file_exists($manifestFile))
  214. {
  215. $manifest = new JLibraryManifest($manifestFile);
  216. // Set the plugin root path
  217. $this->parent->setPath('extension_root', JPATH_PLATFORM . '/' . $manifest->libraryname);
  218. $xml = JFactory::getXML($manifestFile);
  219. // If we cannot load the XML file return null
  220. if ( ! $xml)
  221. {
  222. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_LIB_UNINSTALL_LOAD_MANIFEST'));
  223. return false;
  224. }
  225. // Check for a valid XML root tag.
  226. // TODO: Remove backwards compatability in a future version
  227. // Should be 'extension', but for backward compatability we will accept 'install'.
  228. if ($xml->getName() != 'install' && $xml->getName() != 'extension')
  229. {
  230. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_LIB_UNINSTALL_INVALID_MANIFEST'));
  231. return false;
  232. }
  233. $this->parent->removeFiles($xml->files, -1);
  234. JFile::delete($manifestFile);
  235. }
  236. else
  237. {
  238. // Remove this row entry since its invalid
  239. $row->delete($row->extension_id);
  240. unset($row);
  241. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_LIB_UNINSTALL_INVALID_NOTFOUND_MANIFEST'));
  242. return false;
  243. }
  244. // TODO: Change this so it walked up the path backwards so we clobber multiple empties
  245. // If the folder is empty, let's delete it
  246. if (JFolder::exists($this->parent->getPath('extension_root')))
  247. {
  248. if (is_dir($this->parent->getPath('extension_root')))
  249. {
  250. $files = JFolder::files($this->parent->getPath('extension_root'));
  251. if (!count($files)) {
  252. JFolder::delete($this->parent->getPath('extension_root'));
  253. }
  254. }
  255. }
  256. $this->parent->removeFiles($xml->languages);
  257. $row->delete($row->extension_id);
  258. unset($row);
  259. return $retval;
  260. }
  261. /**
  262. * Custom discover method
  263. *
  264. * @return array JExtension) list of extensions available
  265. * @since 11.1
  266. */
  267. public function discover()
  268. {
  269. $results = Array();
  270. $file_list = JFolder::files(JPATH_MANIFESTS . '/libraries','\.xml$');
  271. foreach ($file_list as $file)
  272. {
  273. $manifest_details = JApplicationHelper::parseXMLInstallFile(JPATH_MANIFESTS.'/libraries/'.$file);
  274. $file = JFile::stripExt($file);
  275. $extension = JTable::getInstance('extension');
  276. $extension->set('type', 'library');
  277. $extension->set('client_id', 0);
  278. $extension->set('element', $file);
  279. $extension->set('name', $file);
  280. $extension->set('state', -1);
  281. $extension->set('manifest_cache', json_encode($manifest_details));
  282. $results[] = $extension;
  283. }
  284. return $results;
  285. }
  286. /**
  287. * Custom discover_install method
  288. *
  289. * @param integer $id The id of the extension to install
  290. *
  291. * @return void
  292. * @since 11.1
  293. */
  294. public function discover_install()
  295. {
  296. /* Libraries are a strange beast, they are actually references to files
  297. * There are two parts to a library which are disjunct in their locations
  298. * 1) The manifest file (stored in /JPATH_MANIFESTS/libraries)
  299. * 2) The actual files (stored in /JPATH_PLATFORM/libraryname)
  300. * Thus installation of a library is the process of dumping files
  301. * in two different places. As such it is impossible to perform
  302. * any operation beyond mere registration of a library under the presumption
  303. * that the files exist in the appropriate location so that come uninstall
  304. * time they can be adequately removed.
  305. */
  306. $manifestPath = JPATH_MANIFESTS . '/libraries/' . $this->parent->extension->element . '.xml';
  307. $this->parent->manifest = $this->parent->isManifest($manifestPath);
  308. $this->parent->setPath('manifest', $manifestPath);
  309. $manifest_details = JApplicationHelper::parseXMLInstallFile($this->parent->getPath('manifest'));
  310. $this->parent->extension->manifest_cache = json_encode($manifest_details);
  311. $this->parent->extension->state = 0;
  312. $this->parent->extension->name = $manifest_details['name'];
  313. $this->parent->extension->enabled = 1;
  314. $this->parent->extension->params = $this->parent->getParams();
  315. if ($this->parent->extension->store()) {
  316. return true;
  317. }
  318. else
  319. {
  320. JError::raiseWarning(101, JText::_('JLIB_INSTALLER_ERROR_LIB_DISCOVER_STORE_DETAILS'));
  321. return false;
  322. }
  323. }
  324. /**
  325. * Refreshes the extension table cache
  326. * @return boolean result of operation, true if updated, false on failure
  327. * @since 11.1
  328. */
  329. public function refreshManifestCache()
  330. {
  331. // Need to find to find where the XML file is since we don't store this normally
  332. $manifestPath = JPATH_MANIFESTS . '/libraries/' . $this->parent->extension->element.'.xml';
  333. $this->parent->manifest = $this->parent->isManifest($manifestPath);
  334. $this->parent->setPath('manifest', $manifestPath);
  335. $manifest_details = JApplicationHelper::parseXMLInstallFile($this->parent->getPath('manifest'));
  336. $this->parent->extension->manifest_cache = json_encode($manifest_details);
  337. $this->parent->extension->name = $manifest_details['name'];
  338. try {
  339. return $this->parent->extension->store();
  340. }
  341. catch(JException $e) {
  342. JError::raiseWarning(101, JText::_('JLIB_INSTALLER_ERROR_LIB_REFRESH_MANIFEST_CACHE'));
  343. return false;
  344. }
  345. }
  346. }