PageRenderTime 52ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/libraries/cms/installer/script.php

https://gitlab.com/ricardosanchez/prueba
PHP | 353 lines | 158 code | 40 blank | 155 comment | 30 complexity | 92bf83b56290563799dd2a72463a1e2b MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Libraries
  4. * @subpackage Helper
  5. *
  6. * @copyright Copyright (C) 2005 - 2016 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. jimport('joomla.filesystem.file');
  11. jimport('joomla.filesystem.folder');
  12. /**
  13. * Base install script for use by extensions providing helper methods for common behaviours.
  14. *
  15. * @since 3.6
  16. */
  17. class JInstallerScript
  18. {
  19. /**
  20. * The version number of the extension.
  21. *
  22. * @var string
  23. * @since 3.6
  24. */
  25. protected $release;
  26. /**
  27. * The table the parameters are stored in.
  28. *
  29. * @var string
  30. * @since 3.6
  31. */
  32. protected $paramTable;
  33. /**
  34. * The extension name. This should be set in the installer script.
  35. *
  36. * @var string
  37. * @since 3.6
  38. */
  39. protected $extension;
  40. /**
  41. * A list of files to be deleted
  42. *
  43. * @var array
  44. * @since 3.6
  45. */
  46. protected $deleteFiles = array();
  47. /**
  48. * A list of folders to be deleted
  49. *
  50. * @var array
  51. * @since 3.6
  52. */
  53. protected $deleteFolders = array();
  54. /**
  55. * A list of CLI script files to be copied to the cli directory
  56. *
  57. * @var array
  58. * @since 3.6
  59. */
  60. protected $cliScriptFiles = array();
  61. /**
  62. * Minimum PHP version required to install the extension
  63. *
  64. * @var string
  65. * @since 3.6
  66. */
  67. protected $minimumPhp;
  68. /**
  69. * Minimum Joomla! version required to install the extension
  70. *
  71. * @var string
  72. * @since 3.6
  73. */
  74. protected $minimumJoomla;
  75. /**
  76. * Allow downgrades of your extension
  77. *
  78. * Use at your own risk as if there is a change in functionality people may wish to downgrade.
  79. *
  80. * @var boolean
  81. * @since 3.6
  82. */
  83. protected $allowDowngrades = false;
  84. /**
  85. * Function called before extension installation/update/removal procedure commences
  86. *
  87. * @param string $type The type of change (install, update or discover_install, not uninstall)
  88. * @param JInstallerAdapter $parent The class calling this method
  89. *
  90. * @return boolean True on success
  91. *
  92. * @since 3.6
  93. */
  94. public function preflight($type, $parent)
  95. {
  96. // Check for the minimum PHP version before continuing
  97. if (!empty($this->minimumPhp) && !version_compare(PHP_VERSION, $this->minimumPhp, '>'))
  98. {
  99. JLog::add(JText::sprintf('JLIB_INSTALLER_MINIMUM_PHP', $this->minimumPhp), JLog::WARNING, 'jerror');
  100. }
  101. // Check for the minimum Joomla version before continuing
  102. if (!empty($this->minimumJoomla) && !version_compare(JVERSION, $this->minimumJoomla, '>'))
  103. {
  104. JLog::add(JText::sprintf('JLIB_INSTALLER_MINIMUM_JOOMLA', $this->minimumJoomla), JLog::WARNING, 'jerror');
  105. }
  106. // Extension manifest file version
  107. $this->release = $parent->get("manifest")->version;
  108. $extensionType = substr($this->extension, 0, 3);
  109. // Modules parameters are located in the module table - else in the extension table
  110. if ($extensionType === 'mod')
  111. {
  112. $this->paramTable = '#__modules';
  113. }
  114. else
  115. {
  116. $this->paramTable = '#__extensions';
  117. }
  118. // Abort if the extension being installed is not newer than the currently installed version
  119. if (strtolower($type) == 'update' && !$this->allowDowngrades)
  120. {
  121. $manifest = $this->getItemArray('manifest_cache', '#__extensions', 'element', JFactory::getDbo()->quote($this->extension));
  122. $oldRelease = $manifest['version'];
  123. if (version_compare($this->release, $oldRelease, '<'))
  124. {
  125. JFactory::getApplication()->enqueueMessage(JText::sprintf('JLIB_INSTALLER_INCORRECT_SEQUENCE', $oldRelease, $this->release), 'error');
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. /**
  132. * Gets each instance of a module in the #__modules table
  133. *
  134. * @param boolean $isModule True if the extension is a module as this can have multiple instances
  135. *
  136. * @return array An array of ID's of the extension
  137. *
  138. * @since 3.6
  139. */
  140. public function getInstances($isModule)
  141. {
  142. $db = JFactory::getDbo();
  143. $query = $db->getQuery(true);
  144. // Select the item(s) and retrieve the id
  145. $query->select($db->quoteName('id'));
  146. if ($isModule)
  147. {
  148. $query->from($db->quoteName('#__modules'))
  149. ->where($db->quoteName('module') . ' = ' . $db->quote($this->extension));
  150. }
  151. else
  152. {
  153. $query->from($db->quoteName('#__extensions'))
  154. ->where($db->quoteName('element') . ' = ' . $db->quote($this->extension));
  155. }
  156. // Set the query and obtain an array of id's
  157. return $db->setQuery($query)->loadColumn();
  158. }
  159. /**
  160. * Gets parameter value in the extensions row of the extension table
  161. *
  162. * @param string $name The name of the parameter to be retrieved
  163. * @param integer $id The id of the item in the Param Table
  164. *
  165. * @return string The parameter desired
  166. *
  167. * @since 3.6
  168. */
  169. public function getParam($name, $id = 0)
  170. {
  171. if (!is_int($id) || $id == 0)
  172. {
  173. // Return false if there is no item given
  174. return false;
  175. }
  176. $params = $this->getItemArray('params', $this->paramTable, 'id', $id);
  177. return $params[$name];
  178. }
  179. /**
  180. * Sets parameter values in the extensions row of the extension table. Note that the
  181. * this must be called separately for deleting and editing. Note if edit is called as a
  182. * type then if the param doesn't exist it will be created
  183. *
  184. * @param array $param_array The array of parameters to be added/edited/removed
  185. * @param string $type The type of change to be made to the param (edit/remove)
  186. * @param integer $id The id of the item in the relevant table
  187. *
  188. * @return boolean True on success
  189. *
  190. * @since 3.6
  191. */
  192. public function setParams($param_array = null, $type = 'edit', $id = 0)
  193. {
  194. if (!is_int($id) || $id == 0)
  195. {
  196. // Return false if there is no valid item given
  197. return false;
  198. }
  199. $params = $this->getItemArray('params', $this->paramTable, 'id', $id);
  200. if ($param_array)
  201. {
  202. foreach ($param_array as $name => $value)
  203. {
  204. if ($type == 'edit')
  205. {
  206. // Add or edit the new variable(s) to the existing params
  207. if (is_array($value))
  208. {
  209. // Convert an array into a json encoded string
  210. $params[(string) $name] = array_values($value);
  211. }
  212. else
  213. {
  214. $params[(string) $name] = (string) $value;
  215. }
  216. }
  217. elseif ($type == 'remove')
  218. {
  219. // Unset the parameter from the array
  220. unset($params[(string) $name]);
  221. }
  222. }
  223. }
  224. // Store the combined new and existing values back as a JSON string
  225. $paramsString = json_encode($params);
  226. $db = JFactory::getDbo();
  227. $query = $db->getQuery(true)
  228. ->update($db->quoteName($this->paramTable))
  229. ->set('params = ' . $db->quote($paramsString))
  230. ->where('id = ' . $id);
  231. // Update table
  232. $db->setQuery($query)->execute();
  233. return true;
  234. }
  235. /**
  236. * Builds a standard select query to produce better DRY code in this script.
  237. * This should produce a single unique cell which is json encoded - it will then
  238. * return an associated array with this data in.
  239. *
  240. * @param string $element The element to get from the query
  241. * @param string $table The table to search for the data in
  242. * @param string $column The column of the database to search from
  243. * @param mixed $identifier The integer id or the already quoted string
  244. *
  245. * @return array Associated array containing data from the cell
  246. *
  247. * @since 3.6
  248. */
  249. public function getItemArray($element, $table, $column, $identifier)
  250. {
  251. // Get the DB and query objects
  252. $db = JFactory::getDbo();
  253. // Build the query
  254. $query = $db->getQuery(true)
  255. ->select($db->quoteName($element))
  256. ->from($db->quoteName($table))
  257. ->where($db->quoteName($column) . ' = ' . $identifier);
  258. $db->setQuery($query);
  259. // Load the single cell and json_decode data
  260. return json_decode($db->loadResult(), true);
  261. }
  262. /**
  263. * Remove the files and folders in the given array from
  264. *
  265. * @return void
  266. *
  267. * @since 3.6
  268. */
  269. public function removeFiles()
  270. {
  271. if (!empty($this->deleteFiles))
  272. {
  273. foreach ($this->deleteFiles as $file)
  274. {
  275. if (file_exists(JPATH_ROOT . $file) && !JFile::delete(JPATH_ROOT . $file))
  276. {
  277. echo JText::sprintf('JLIB_INSTALLER_ERROR_FILE_FOLDER', $file) . '<br />';
  278. }
  279. }
  280. }
  281. if (!empty($this->deleteFolders))
  282. {
  283. foreach ($this->deleteFolders as $folder)
  284. {
  285. if (JFolder::exists(JPATH_ROOT . $folder) && !JFolder::delete(JPATH_ROOT . $folder))
  286. {
  287. echo JText::sprintf('JLIB_INSTALLER_ERROR_FILE_FOLDER', $folder) . '<br />';
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * Moves the CLI scripts into the CLI folder in the CMS
  294. *
  295. * @return void
  296. *
  297. * @since 3.6
  298. */
  299. public function moveCliFiles()
  300. {
  301. if (!empty($this->cliScriptFiles))
  302. {
  303. foreach ($this->cliScriptFiles as $file)
  304. {
  305. $name = basename($file);
  306. if (file_exists(JPATH_ROOT . $file) && !JFile::move(JPATH_ROOT . $file, JPATH_ROOT . '/cli/' . $name))
  307. {
  308. echo JText::sprintf('JLIB_INSTALLER_FILE_ERROR_MOVE', $name);
  309. }
  310. }
  311. }
  312. }
  313. }