PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/joomla/installer/helper.php

https://gitlab.com/chuda/Web_CCPEL
PHP | 303 lines | 262 code | 14 blank | 27 comment | 7 complexity | 1566be1c86d104766661e7aecd594182 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Installer
  5. *
  6. * @copyright Copyright (C) 2005 - 2014 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.filesystem.file');
  11. jimport('joomla.filesystem.folder');
  12. jimport('joomla.filesystem.archive');
  13. jimport('joomla.filesystem.path');
  14. /**
  15. * Installer helper class
  16. *
  17. * @package Joomla.Platform
  18. * @subpackage Installer
  19. * @since 11.1
  20. */
  21. abstract class JInstallerHelper
  22. {
  23. /**
  24. * Downloads a package
  25. *
  26. * @param string $url URL of file to download
  27. * @param string $target Download target filename [optional]
  28. *
  29. * @return mixed Path to downloaded package or boolean false on failure
  30. *
  31. * @since 11.1
  32. */
  33. public static function downloadPackage($url, $target = false)
  34. {
  35. $config = JFactory::getConfig();
  36. // Capture PHP errors
  37. $php_errormsg = 'Error Unknown';
  38. $track_errors = ini_get('track_errors');
  39. ini_set('track_errors', true);
  40. // Set user agent
  41. $version = new JVersion;
  42. ini_set('user_agent', $version->getUserAgent('Installer'));
  43. $http = JHttpFactory::getHttp();
  44. // load installer plugins, and allow url and headers modification
  45. $headers = array();
  46. JPluginHelper::importPlugin('installer');
  47. $dispatcher = JDispatcher::getInstance();
  48. $results = $dispatcher->trigger('onInstallerBeforePackageDownload', array(&$url, &$headers));
  49. try
  50. {
  51. $response = $http->get($url, $headers);
  52. }
  53. catch (Exception $exc)
  54. {
  55. $response = null;
  56. }
  57. if (is_null($response))
  58. {
  59. JError::raiseWarning(42, JText::_('JLIB_INSTALLER_ERROR_DOWNLOAD_SERVER_CONNECT'));
  60. return false;
  61. }
  62. if (302 == $response->code && isset($response->headers['Location']))
  63. {
  64. return self::downloadPackage($response->headers['Location']);
  65. }
  66. elseif (200 != $response->code)
  67. {
  68. if ($response->body === '')
  69. {
  70. $response->body = $php_errormsg;
  71. }
  72. JError::raiseWarning(42, JText::sprintf('JLIB_INSTALLER_ERROR_DOWNLOAD_SERVER_CONNECT', $response->body));
  73. return false;
  74. }
  75. if (isset($response->headers['Content-Disposition']))
  76. {
  77. $contentfilename = explode("\"", $response->headers['Content-Disposition']);
  78. $target = $contentfilename[1];
  79. }
  80. // Set the target path if not given
  81. if (!$target)
  82. {
  83. $target = $config->get('tmp_path') . '/' . self::getFilenameFromURL($url);
  84. }
  85. else
  86. {
  87. $target = $config->get('tmp_path') . '/' . basename($target);
  88. }
  89. // Write buffer to file
  90. JFile::write($target, $response->body);
  91. // Restore error tracking to what it was before
  92. ini_set('track_errors', $track_errors);
  93. // bump the max execution time because not using built in php zip libs are slow
  94. @set_time_limit(ini_get('max_execution_time'));
  95. // Return the name of the downloaded package
  96. return basename($target);
  97. }
  98. /**
  99. * Unpacks a file and verifies it as a Joomla element package
  100. * Supports .gz .tar .tar.gz and .zip
  101. *
  102. * @param string $p_filename The uploaded package filename or install directory
  103. *
  104. * @return array Two elements: extractdir and packagefile
  105. *
  106. * @since 11.1
  107. */
  108. public static function unpack($p_filename)
  109. {
  110. // Path to the archive
  111. $archivename = $p_filename;
  112. // Temporary folder to extract the archive into
  113. $tmpdir = uniqid('install_');
  114. // Clean the paths to use for archive extraction
  115. $extractdir = JPath::clean(dirname($p_filename) . '/' . $tmpdir);
  116. $archivename = JPath::clean($archivename);
  117. // Do the unpacking of the archive
  118. $result = JArchive::extract($archivename, $extractdir);
  119. if ($result === false)
  120. {
  121. return false;
  122. }
  123. /*
  124. * Let's set the extraction directory and package file in the result array so we can
  125. * cleanup everything properly later on.
  126. */
  127. $retval['extractdir'] = $extractdir;
  128. $retval['packagefile'] = $archivename;
  129. /*
  130. * Try to find the correct install directory. In case the package is inside a
  131. * subdirectory detect this and set the install directory to the correct path.
  132. *
  133. * List all the items in the installation directory. If there is only one, and
  134. * it is a folder, then we will set that folder to be the installation folder.
  135. */
  136. $dirList = array_merge(JFolder::files($extractdir, ''), JFolder::folders($extractdir, ''));
  137. if (count($dirList) == 1)
  138. {
  139. if (JFolder::exists($extractdir . '/' . $dirList[0]))
  140. {
  141. $extractdir = JPath::clean($extractdir . '/' . $dirList[0]);
  142. }
  143. }
  144. /*
  145. * We have found the install directory so lets set it and then move on
  146. * to detecting the extension type.
  147. */
  148. $retval['dir'] = $extractdir;
  149. /*
  150. * Get the extension type and return the directory/type array on success or
  151. * false on fail.
  152. */
  153. if ($retval['type'] = self::detectType($extractdir))
  154. {
  155. return $retval;
  156. }
  157. else
  158. {
  159. return false;
  160. }
  161. }
  162. /**
  163. * Method to detect the extension type from a package directory
  164. *
  165. * @param string $p_dir Path to package directory
  166. *
  167. * @return mixed Extension type string or boolean false on fail
  168. *
  169. * @since 11.1
  170. */
  171. public static function detectType($p_dir)
  172. {
  173. // Search the install dir for an XML file
  174. $files = JFolder::files($p_dir, '\.xml$', 1, true);
  175. if (!count($files))
  176. {
  177. JError::raiseWarning(1, JText::_('JLIB_INSTALLER_ERROR_NOTFINDXMLSETUPFILE'));
  178. return false;
  179. }
  180. foreach ($files as $file)
  181. {
  182. if (!$xml = JFactory::getXML($file))
  183. {
  184. continue;
  185. }
  186. if ($xml->getName() != 'install' && $xml->getName() != 'extension')
  187. {
  188. unset($xml);
  189. continue;
  190. }
  191. $type = (string) $xml->attributes()->type;
  192. // Free up memory
  193. unset($xml);
  194. return $type;
  195. }
  196. JError::raiseWarning(1, JText::_('JLIB_INSTALLER_ERROR_NOTFINDJOOMLAXMLSETUPFILE'));
  197. // Free up memory.
  198. unset($xml);
  199. return false;
  200. }
  201. /**
  202. * Gets a file name out of a url
  203. *
  204. * @param string $url URL to get name from
  205. *
  206. * @return mixed String filename or boolean false if failed
  207. *
  208. * @since 11.1
  209. */
  210. public static function getFilenameFromURL($url)
  211. {
  212. if (is_string($url))
  213. {
  214. $parts = explode('/', $url);
  215. return $parts[count($parts) - 1];
  216. }
  217. return false;
  218. }
  219. /**
  220. * Clean up temporary uploaded package and unpacked extension
  221. *
  222. * @param string $package Path to the uploaded package file
  223. * @param string $resultdir Path to the unpacked extension
  224. *
  225. * @return boolean True on success
  226. *
  227. * @since 11.1
  228. */
  229. public static function cleanupInstall($package, $resultdir)
  230. {
  231. $config = JFactory::getConfig();
  232. // Does the unpacked extension directory exist?
  233. if (is_dir($resultdir))
  234. {
  235. JFolder::delete($resultdir);
  236. }
  237. // Is the package file a valid file?
  238. if (is_file($package))
  239. {
  240. JFile::delete($package);
  241. }
  242. elseif (is_file(JPath::clean($config->get('tmp_path') . '/' . $package)))
  243. {
  244. // It might also be just a base filename
  245. JFile::delete(JPath::clean($config->get('tmp_path') . '/' . $package));
  246. }
  247. }
  248. /**
  249. * Splits contents of a sql file into array of discreet queries.
  250. * Queries need to be delimited with end of statement marker ';'
  251. *
  252. * @param string $sql The SQL statement.
  253. *
  254. * @return array Array of queries
  255. *
  256. * @since 11.1
  257. */
  258. public static function splitSql($sql)
  259. {
  260. $db = JFactory::getDbo();
  261. return $db->splitSql($sql);
  262. }
  263. }