/plugins/extension/joomla/joomla.php

https://github.com/pjwiseman/joomla-cms · PHP · 263 lines · 143 code · 26 blank · 94 comment · 14 complexity · 8d1c701332305361ff51b4f0489f0b91 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Plugin
  4. * @subpackage Extension.Joomla
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. /**
  11. * Joomla! master extension plugin.
  12. *
  13. * @since 1.6
  14. */
  15. class PlgExtensionJoomla extends JPlugin
  16. {
  17. /**
  18. * @var integer Extension Identifier
  19. * @since 1.6
  20. */
  21. private $eid = 0;
  22. /**
  23. * @var JInstaller Installer object
  24. * @since 1.6
  25. */
  26. private $installer = null;
  27. /**
  28. * Load the language file on instantiation.
  29. *
  30. * @var boolean
  31. * @since 3.1
  32. */
  33. protected $autoloadLanguage = true;
  34. /**
  35. * Adds an update site to the table if it doesn't exist.
  36. *
  37. * @param string $name The friendly name of the site
  38. * @param string $type The type of site (e.g. collection or extension)
  39. * @param string $location The URI for the site
  40. * @param boolean $enabled If this site is enabled
  41. *
  42. * @return void
  43. *
  44. * @since 1.6
  45. */
  46. private function addUpdateSite($name, $type, $location, $enabled)
  47. {
  48. $db = JFactory::getDbo();
  49. // Look if the location is used already; doesn't matter what type you can't have two types at the same address, doesn't make sense
  50. $query = $db->getQuery(true)
  51. ->select('update_site_id')
  52. ->from('#__update_sites')
  53. ->where('location = ' . $db->quote($location));
  54. $db->setQuery($query);
  55. $update_site_id = (int) $db->loadResult();
  56. // If it doesn't exist, add it!
  57. if (!$update_site_id)
  58. {
  59. $query->clear()
  60. ->insert('#__update_sites')
  61. ->columns(array($db->quoteName('name'), $db->quoteName('type'), $db->quoteName('location'), $db->quoteName('enabled')))
  62. ->values($db->quote($name) . ', ' . $db->quote($type) . ', ' . $db->quote($location) . ', ' . (int) $enabled);
  63. $db->setQuery($query);
  64. if ($db->execute())
  65. {
  66. // Link up this extension to the update site
  67. $update_site_id = $db->insertid();
  68. }
  69. }
  70. // Check if it has an update site id (creation might have faileD)
  71. if ($update_site_id)
  72. {
  73. // Look for an update site entry that exists
  74. $query->clear()
  75. ->select('update_site_id')
  76. ->from('#__update_sites_extensions')
  77. ->where('update_site_id = ' . $update_site_id)
  78. ->where('extension_id = ' . $this->eid);
  79. $db->setQuery($query);
  80. $tmpid = (int) $db->loadResult();
  81. if (!$tmpid)
  82. {
  83. // Link this extension to the relevant update site
  84. $query->clear()
  85. ->insert('#__update_sites_extensions')
  86. ->columns(array($db->quoteName('update_site_id'), $db->quoteName('extension_id')))
  87. ->values($update_site_id . ', ' . $this->eid);
  88. $db->setQuery($query);
  89. $db->execute();
  90. }
  91. }
  92. }
  93. /**
  94. * Handle post extension install update sites
  95. *
  96. * @param JInstaller $installer Installer object
  97. * @param integer $eid Extension Identifier
  98. *
  99. * @return void
  100. *
  101. * @since 1.6
  102. */
  103. public function onExtensionAfterInstall($installer, $eid )
  104. {
  105. if ($eid)
  106. {
  107. $this->installer = $installer;
  108. $this->eid = $eid;
  109. // After an install we only need to do update sites
  110. $this->processUpdateSites();
  111. }
  112. }
  113. /**
  114. * Handle extension uninstall
  115. *
  116. * @param JInstaller $installer Installer instance
  117. * @param integer $eid Extension id
  118. * @param integer $result Installation result
  119. *
  120. * @return void
  121. *
  122. * @since 1.6
  123. */
  124. public function onExtensionAfterUninstall($installer, $eid, $result)
  125. {
  126. if ($eid)
  127. {
  128. // Wipe out any update_sites_extensions links
  129. $db = JFactory::getDbo();
  130. $query = $db->getQuery(true)
  131. ->delete('#__update_sites_extensions')
  132. ->where('extension_id = ' . $eid);
  133. $db->setQuery($query);
  134. $db->execute();
  135. // Delete any unused update sites
  136. $query->clear()
  137. ->select('update_site_id')
  138. ->from('#__update_sites_extensions');
  139. $db->setQuery($query);
  140. $results = $db->loadColumn();
  141. if (is_array($results))
  142. {
  143. // So we need to delete the update sites and their associated updates
  144. $updatesite_delete = $db->getQuery(true);
  145. $updatesite_delete->delete('#__update_sites');
  146. $updatesite_query = $db->getQuery(true);
  147. $updatesite_query->select('update_site_id')
  148. ->from('#__update_sites');
  149. // If we get results back then we can exclude them
  150. if (count($results))
  151. {
  152. $updatesite_query->where('update_site_id NOT IN (' . implode(',', $results) . ')');
  153. $updatesite_delete->where('update_site_id NOT IN (' . implode(',', $results) . ')');
  154. }
  155. // So let's find what update sites we're about to nuke and remove their associated extensions
  156. $db->setQuery($updatesite_query);
  157. $update_sites_pending_delete = $db->loadColumn();
  158. if (is_array($update_sites_pending_delete) && count($update_sites_pending_delete))
  159. {
  160. // Nuke any pending updates with this site before we delete it
  161. // TODO: investigate alternative of using a query after the delete below with a query and not in like above
  162. $query->clear()
  163. ->delete('#__updates')
  164. ->where('update_site_id IN (' . implode(',', $update_sites_pending_delete) . ')');
  165. $db->setQuery($query);
  166. $db->execute();
  167. }
  168. // Note: this might wipe out the entire table if there are no extensions linked
  169. $db->setQuery($updatesite_delete);
  170. $db->execute();
  171. }
  172. // Last but not least we wipe out any pending updates for the extension
  173. $query->clear()
  174. ->delete('#__updates')
  175. ->where('extension_id = ' . $eid);
  176. $db->setQuery($query);
  177. $db->execute();
  178. }
  179. }
  180. /**
  181. * After update of an extension
  182. *
  183. * @param JInstaller $installer Installer object
  184. * @param integer $eid Extension identifier
  185. *
  186. * @return void
  187. *
  188. * @since 1.6
  189. */
  190. public function onExtensionAfterUpdate($installer, $eid)
  191. {
  192. if ($eid)
  193. {
  194. $this->installer = $installer;
  195. $this->eid = $eid;
  196. // Handle any update sites
  197. $this->processUpdateSites();
  198. }
  199. }
  200. /**
  201. * Processes the list of update sites for an extension.
  202. *
  203. * @return void
  204. *
  205. * @since 1.6
  206. */
  207. private function processUpdateSites()
  208. {
  209. $manifest = $this->installer->getManifest();
  210. $updateservers = $manifest->updateservers;
  211. if ($updateservers)
  212. {
  213. $children = $updateservers->children();
  214. }
  215. else
  216. {
  217. $children = array();
  218. }
  219. if (count($children))
  220. {
  221. foreach ($children as $child)
  222. {
  223. $attrs = $child->attributes();
  224. $this->addUpdateSite($attrs['name'], $attrs['type'], trim($child), true);
  225. }
  226. }
  227. else
  228. {
  229. $data = trim((string) $updateservers);
  230. if (strlen($data))
  231. {
  232. // We have a single entry in the update server line, let us presume this is an extension line
  233. $this->addUpdateSite(JText::_('PLG_EXTENSION_JOOMLA_UNKNOWN_SITE'), 'extension', $data, true);
  234. }
  235. }
  236. }
  237. }