PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_installer/models/languages.php

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 393 lines | 188 code | 64 blank | 141 comment | 13 complexity | 138643926eadefa4717d851f6e6ee4db MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_installer
  5. * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. defined('_JEXEC') or die;
  9. jimport('joomla.updater.update');
  10. /**
  11. * Languages Installer Model
  12. *
  13. * @since 2.5.7
  14. */
  15. class InstallerModelLanguages extends JModelList
  16. {
  17. /**
  18. * @var integer Extension ID of the en-GB language pack.
  19. * @since 3.4
  20. */
  21. private $enGbExtensionId = 0;
  22. /**
  23. * @var integer Upate Site ID of the en-GB language pack.
  24. * @since 3.4
  25. */
  26. private $updateSiteId = 0;
  27. /**
  28. * Constructor override, defines a white list of column filters.
  29. *
  30. * @param array $config An optional associative array of configuration settings.
  31. *
  32. * @since 2.5.7
  33. */
  34. public function __construct($config = array())
  35. {
  36. if (empty($config['filter_fields']))
  37. {
  38. $config['filter_fields'] = array(
  39. 'update_id', 'update_id',
  40. 'name', 'name',
  41. );
  42. }
  43. parent::__construct($config);
  44. // Get the extension_id of the en-GB package.
  45. $db = $this->getDbo();
  46. $extQuery = $db->getQuery(true);
  47. $extType = 'language';
  48. $extElem = 'en-GB';
  49. $extQuery->select($db->quoteName('extension_id'))
  50. ->from($db->quoteName('#__extensions'))
  51. ->where($db->quoteName('type') . ' = ' . $db->quote($extType))
  52. ->where($db->quoteName('element') . ' = ' . $db->quote($extElem))
  53. ->where($db->quoteName('client_id') . ' = 0');
  54. $db->setQuery($extQuery);
  55. $extId = (int) $db->loadResult();
  56. // Get the update_site_id for the en-GB package if extension_id found before.
  57. if ($extId)
  58. {
  59. $this->enGbExtensionId = $extId;
  60. $siteQuery = $db->getQuery(true);
  61. $siteQuery->select($db->quoteName('update_site_id'))
  62. ->from($db->quoteName('#__update_sites_extensions'))
  63. ->where($db->quoteName('extension_id') . ' = ' . $extId);
  64. $db->setQuery($siteQuery);
  65. $siteId = (int) $db->loadResult();
  66. if ($siteId)
  67. {
  68. $this->updateSiteId = $siteId;
  69. }
  70. }
  71. }
  72. /**
  73. * Method to get the available languages database query.
  74. *
  75. * @return JDatabaseQuery The database query
  76. *
  77. * @since 2.5.7
  78. */
  79. protected function _getListQuery()
  80. {
  81. $db = $this->getDbo();
  82. $query = $db->getQuery(true);
  83. // Select the required fields from the updates table.
  84. $query->select($db->quoteName(array('update_id', 'name', 'version', 'detailsurl', 'type')))
  85. ->from($db->quoteName('#__updates'));
  86. /*
  87. * This where clause will limit to language updates only.
  88. * If no update site exists, set the where clause so
  89. * no available languages will be found later with the
  90. * query returned by this function here.
  91. */
  92. if ($this->updateSiteId)
  93. {
  94. $query->where($db->quoteName('update_site_id') . ' = ' . $this->updateSiteId);
  95. }
  96. else
  97. {
  98. $query->where($db->quoteName('update_site_id') . ' = -1');
  99. }
  100. // This where clause will avoid to list languages already installed.
  101. $query->where($db->quoteName('extension_id') . ' = 0');
  102. // Filter by search in title
  103. $search = $this->getState('filter.search');
  104. if (!empty($search))
  105. {
  106. $search = $db->quote('%' . str_replace(' ', '%', $db->escape(trim($search), true) . '%'));
  107. $query->where('(LOWER(name) LIKE ' . strtolower($search) . ')');
  108. }
  109. // Add the list ordering clause.
  110. $listOrder = $this->state->get('list.ordering');
  111. $orderDirn = $this->state->get('list.direction');
  112. $query->order($db->escape($listOrder) . ' ' . $db->escape($orderDirn));
  113. return $query;
  114. }
  115. /**
  116. * Method to get a store id based on model configuration state.
  117. *
  118. * @param string $id A prefix for the store id.
  119. *
  120. * @return string A store id.
  121. *
  122. * @since 2.5.7
  123. */
  124. protected function getStoreId($id = '')
  125. {
  126. // Compile the store id.
  127. $id .= ':' . $this->getState('filter.search');
  128. return parent::getStoreId($id);
  129. }
  130. /**
  131. * Method to auto-populate the model state.
  132. *
  133. * Note. Calling getState in this method will result in recursion.
  134. *
  135. * @param string $ordering list order
  136. * @param string $direction direction in the list
  137. *
  138. * @return void
  139. *
  140. * @since 2.5.7
  141. */
  142. protected function populateState($ordering = 'name', $direction = 'asc')
  143. {
  144. $app = JFactory::getApplication();
  145. $value = $app->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
  146. $this->setState('filter.search', $value);
  147. $this->setState('extension_message', $app->getUserState('com_installer.extension_message'));
  148. parent::populateState($ordering, $direction);
  149. }
  150. /**
  151. * Enable languages update server
  152. *
  153. * @return boolean
  154. *
  155. * @since 3.4
  156. */
  157. protected function enableUpdateSite()
  158. {
  159. // If no update site, return false.
  160. if (!$this->updateSiteId)
  161. {
  162. return false;
  163. }
  164. // Try to enable the update site, return false if some RuntimeException
  165. $db = $this->getDbo();
  166. $query = $db->getQuery(true)
  167. ->update('#__update_sites')
  168. ->set('enabled = 1')
  169. ->where('update_site_id = ' . $this->updateSiteId);
  170. $db->setQuery($query);
  171. try
  172. {
  173. $db->execute();
  174. }
  175. catch (RuntimeException $e)
  176. {
  177. $this->setError($e->getMessage());
  178. return false;
  179. }
  180. return true;
  181. }
  182. /**
  183. * Method to find available languages in the Accredited Languages Update Site.
  184. *
  185. * @param int $cache_timeout time before refreshing the cached updates
  186. *
  187. * @return bool
  188. *
  189. * @since 2.5.7
  190. */
  191. public function findLanguages($cache_timeout = 0)
  192. {
  193. if (!$this->enableUpdateSite())
  194. {
  195. return false;
  196. }
  197. if (!$this->enGbExtensionId)
  198. {
  199. return false;
  200. }
  201. $updater = JUpdater::getInstance();
  202. /*
  203. * The following function call uses the extension_id of the en-GB package.
  204. * In #__update_sites_extensions you should have this extension_id linked
  205. * to the Accredited Translations Repo.
  206. */
  207. $updater->findUpdates(array($this->enGbExtensionId), $cache_timeout);
  208. return true;
  209. }
  210. /**
  211. * Install languages in the system.
  212. *
  213. * @param array $lids array of language ids selected in the list
  214. *
  215. * @return bool
  216. *
  217. * @since 2.5.7
  218. */
  219. public function install($lids)
  220. {
  221. $app = JFactory::getApplication();
  222. // Loop through every selected language
  223. foreach ($lids as $id)
  224. {
  225. $installer = new JInstaller;
  226. // Loads the update database object that represents the language.
  227. $language = JTable::getInstance('update');
  228. $language->load($id);
  229. // Get the url to the XML manifest file of the selected language.
  230. $remote_manifest = $this->_getLanguageManifest($id);
  231. if (!$remote_manifest)
  232. {
  233. // Could not find the url, the information in the update server may be corrupt.
  234. $message = JText::sprintf('COM_INSTALLER_MSG_LANGUAGES_CANT_FIND_REMOTE_MANIFEST', $language->name);
  235. $message .= ' ' . JText::_('COM_INSTALLER_MSG_LANGUAGES_TRY_LATER');
  236. $app->enqueueMessage($message);
  237. continue;
  238. }
  239. // Based on the language XML manifest get the url of the package to download.
  240. $package_url = $this->_getPackageUrl($remote_manifest);
  241. if (!$package_url)
  242. {
  243. // Could not find the url , maybe the url is wrong in the update server, or there is not internet access
  244. $message = JText::sprintf('COM_INSTALLER_MSG_LANGUAGES_CANT_FIND_REMOTE_PACKAGE', $language->name);
  245. $message .= ' ' . JText::_('COM_INSTALLER_MSG_LANGUAGES_TRY_LATER');
  246. $app->enqueueMessage($message);
  247. continue;
  248. }
  249. // Download the package to the tmp folder.
  250. $package = $this->_downloadPackage($package_url);
  251. // Install the package
  252. if (!$installer->install($package['dir']))
  253. {
  254. // There was an error installing the package.
  255. $message = JText::sprintf('COM_INSTALLER_INSTALL_ERROR', $language->name);
  256. $message .= ' ' . JText::_('COM_INSTALLER_MSG_LANGUAGES_TRY_LATER');
  257. $app->enqueueMessage($message);
  258. continue;
  259. }
  260. // Package installed successfully.
  261. $app->enqueueMessage(JText::sprintf('COM_INSTALLER_INSTALL_SUCCESS', $language->name));
  262. // Cleanup the install files in tmp folder.
  263. if (!is_file($package['packagefile']))
  264. {
  265. $config = JFactory::getConfig();
  266. $package['packagefile'] = $config->get('tmp_path') . '/' . $package['packagefile'];
  267. }
  268. JInstallerHelper::cleanupInstall($package['packagefile'], $package['extractdir']);
  269. // Delete the installed language from the list.
  270. $language->delete($id);
  271. }
  272. }
  273. /**
  274. * Gets the manifest file of a selected language from a the language list in a update server.
  275. *
  276. * @param int $uid the id of the language in the #__updates table
  277. *
  278. * @return string
  279. *
  280. * @since 2.5.7
  281. */
  282. protected function _getLanguageManifest($uid)
  283. {
  284. $instance = JTable::getInstance('update');
  285. $instance->load($uid);
  286. return $instance->detailsurl;
  287. }
  288. /**
  289. * Finds the url of the package to download.
  290. *
  291. * @param string $remote_manifest url to the manifest XML file of the remote package
  292. *
  293. * @return string|bool
  294. *
  295. * @since 2.5.7
  296. */
  297. protected function _getPackageUrl( $remote_manifest )
  298. {
  299. $update = new JUpdate;
  300. $update->loadFromXml($remote_manifest);
  301. $package_url = trim($update->get('downloadurl', false)->_data);
  302. return $package_url;
  303. }
  304. /**
  305. * Download a language package from a URL and unpack it in the tmp folder.
  306. *
  307. * @param string $url hola
  308. *
  309. * @return array|bool Package details or false on failure
  310. *
  311. * @since 2.5.7
  312. */
  313. protected function _downloadPackage($url)
  314. {
  315. // Download the package from the given URL.
  316. $p_file = JInstallerHelper::downloadPackage($url);
  317. // Was the package downloaded?
  318. if (!$p_file)
  319. {
  320. JError::raiseWarning('', JText::_('COM_INSTALLER_MSG_INSTALL_INVALID_URL'));
  321. return false;
  322. }
  323. $config = JFactory::getConfig();
  324. $tmp_dest = $config->get('tmp_path');
  325. // Unpack the downloaded package file.
  326. $package = JInstallerHelper::unpack($tmp_dest . '/' . $p_file);
  327. return $package;
  328. }
  329. }