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

/libraries/joomla/installer/helper.php

https://github.com/chrisinammo/arthurmcneil
PHP | 278 lines | 237 code | 7 blank | 34 comment | 3 complexity | 1c6654f869d52a1e64e83a14436b1002 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * @version $Id: helper.php 9978 2008-02-01 17:23:39Z ircmaxell $
  4. * @package Joomla.Framework
  5. * @subpackage Installer
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. jimport('joomla.filesystem.file');
  17. jimport('joomla.filesystem.folder');
  18. jimport('joomla.filesystem.archive');
  19. jimport('joomla.filesystem.path');
  20. /**
  21. * Installer helper class
  22. *
  23. * @static
  24. * @author Louis Landry <louis.landry@joomla.org>
  25. * @package Joomla.Framework
  26. * @subpackage Installer
  27. * @since 1.5
  28. */
  29. class JInstallerHelper
  30. {
  31. /**
  32. * Downloads a package
  33. *
  34. * @static
  35. * @param string URL of file to download
  36. * @param string Download target filename [optional]
  37. * @return mixed Path to downloaded package or boolean false on failure
  38. * @since 1.5
  39. */
  40. function downloadPackage($url, $target = false)
  41. {
  42. $config =& JFactory::getConfig();
  43. // Capture PHP errors
  44. $php_errormsg = 'Error Unknown';
  45. ini_set('track_errors', true);
  46. // Set user agent
  47. ini_set('user_agent', "Joomla! 1.5 Installer");
  48. // Open the remote server socket for reading
  49. $inputHandle = @ fopen($url, "r");
  50. $error = strstr($php_errormsg,'failed to open stream:');
  51. if (!$inputHandle) {
  52. JError::raiseWarning(42, JText::_('SERVER_CONNECT_FAILED').', '.$error);
  53. return false;
  54. }
  55. $meta_data = stream_get_meta_data($inputHandle);
  56. foreach ($meta_data['wrapper_data'] as $wrapper_data)
  57. {
  58. if (substr($wrapper_data, 0, strlen("Content-Disposition")) == "Content-Disposition") {
  59. $contentfilename = explode ("\"", $wrapper_data);
  60. $target = $contentfilename[1];
  61. }
  62. }
  63. // Set the target path if not given
  64. if (!$target) {
  65. $target = $config->getValue('config.tmp_path').DS.JInstallerHelper::getFilenameFromURL($url);
  66. } else {
  67. $target = $config->getValue('config.tmp_path').DS.basename($target);
  68. }
  69. // Initialize contents buffer
  70. $contents = null;
  71. while (!feof($inputHandle))
  72. {
  73. $contents .= fread($inputHandle, 4096);
  74. if ($contents == false) {
  75. JError::raiseWarning(44, 'Failed reading network resource: '.$php_errormsg);
  76. return false;
  77. }
  78. }
  79. // Write buffer to file
  80. JFile::write($target, $contents);
  81. // Close file pointer resource
  82. fclose($inputHandle);
  83. // Return the name of the downloaded package
  84. return basename($target);
  85. }
  86. /**
  87. * Unpacks a file and verifies it as a Joomla element package
  88. * Supports .gz .tar .tar.gz and .zip
  89. *
  90. * @static
  91. * @param string $p_filename The uploaded package filename or install directory
  92. * @return boolean True on success, False on error
  93. * @since 1.5
  94. */
  95. function unpack($p_filename)
  96. {
  97. // Path to the archive
  98. $archivename = $p_filename;
  99. // Temporary folder to extract the archive into
  100. $tmpdir = uniqid('install_');
  101. // Clean the paths to use for archive extraction
  102. $extractdir = JPath::clean(dirname($p_filename).DS.$tmpdir);
  103. $archivename = JPath::clean($archivename);
  104. // do the unpacking of the archive
  105. $result = JArchive::extract( $archivename, $extractdir);
  106. if ( $result === false ) {
  107. return false;
  108. }
  109. /*
  110. * Lets set the extraction directory and package file in the result array so we can
  111. * cleanup everything properly later on.
  112. */
  113. $retval['extractdir'] = $extractdir;
  114. $retval['packagefile'] = $archivename;
  115. /*
  116. * Try to find the correct install directory. In case the package is inside a
  117. * subdirectory detect this and set the install directory to the correct path.
  118. *
  119. * List all the items in the installation directory. If there is only one, and
  120. * it is a folder, then we will set that folder to be the installation folder.
  121. */
  122. $dirList = array_merge(JFolder::files($extractdir, ''), JFolder::folders($extractdir, ''));
  123. if (count($dirList) == 1)
  124. {
  125. if (JFolder::exists($extractdir.DS.$dirList[0]))
  126. {
  127. $extractdir = JPath::clean($extractdir.DS.$dirList[0]);
  128. }
  129. }
  130. /*
  131. * We have found the install directory so lets set it and then move on
  132. * to detecting the extension type.
  133. */
  134. $retval['dir'] = $extractdir;
  135. /*
  136. * Get the extension type and return the directory/type array on success or
  137. * false on fail.
  138. */
  139. if ($retval['type'] = JInstallerHelper::detectType($extractdir))
  140. {
  141. return $retval;
  142. } else
  143. {
  144. return false;
  145. }
  146. }
  147. /**
  148. * Method to detect the extension type from a package directory
  149. *
  150. * @static
  151. * @param string $p_dir Path to package directory
  152. * @return mixed Extension type string or boolean false on fail
  153. * @since 1.5
  154. */
  155. function detectType($p_dir)
  156. {
  157. // Search the install dir for an xml file
  158. $files = JFolder::files($p_dir, '\.xml$', 1, true);
  159. if (count($files) > 0)
  160. {
  161. foreach ($files as $file)
  162. {
  163. $xmlDoc = & JFactory::getXMLParser();
  164. $xmlDoc->resolveErrors(true);
  165. if (!$xmlDoc->loadXML($file, false, true))
  166. {
  167. // Free up memory from DOMIT parser
  168. unset ($xmlDoc);
  169. continue;
  170. }
  171. $root = & $xmlDoc->documentElement;
  172. if (!is_object($root) || ($root->getTagName() != "install" && $root->getTagName() != 'mosinstall'))
  173. {
  174. unset($xmlDoc);
  175. continue;
  176. }
  177. $type = $root->getAttribute('type');
  178. // Free up memory from DOMIT parser
  179. unset ($xmlDoc);
  180. return $type;
  181. }
  182. JError::raiseWarning(1, JText::_('ERRORNOTFINDJOOMLAXMLSETUPFILE'));
  183. // Free up memory from DOMIT parser
  184. unset ($xmlDoc);
  185. return false;
  186. } else
  187. {
  188. JError::raiseWarning(1, JText::_('ERRORNOTFINDXMLSETUPFILE'));
  189. return false;
  190. }
  191. }
  192. /**
  193. * Gets a file name out of a url
  194. *
  195. * @static
  196. * @param string $url URL to get name from
  197. * @return mixed String filename or boolean false if failed
  198. * @since 1.5
  199. */
  200. function getFilenameFromURL($url)
  201. {
  202. if (is_string($url)) {
  203. $parts = explode('/', $url);
  204. return $parts[count($parts) - 1];
  205. }
  206. return false;
  207. }
  208. /**
  209. * Clean up temporary uploaded package and unpacked extension
  210. *
  211. * @static
  212. * @param string $p_file Path to the uploaded package file
  213. * @param string $resultdir Path to the unpacked extension
  214. * @return boolean True on success
  215. * @since 1.5
  216. */
  217. function cleanupInstall($package, $resultdir)
  218. {
  219. $config =& JFactory::getConfig();
  220. // Does the unpacked extension directory exist?
  221. if (is_dir($resultdir)) {
  222. JFolder::delete($resultdir);
  223. }
  224. // Is the package file a valid file?
  225. if (is_file($package)) {
  226. JFile::delete($package);
  227. } elseif (is_file(JPath::clean($config->getValue('config.tmp_path').DS.$package))) {
  228. // It might also be just a base filename
  229. JFile::delete(JPath::clean($config->getValue('config.tmp_path').DS.$package));
  230. }
  231. }
  232. /**
  233. * Splits contents of a sql file into array of discreet queries
  234. * queries need to be delimited with end of statement marker ';'
  235. * @param string
  236. * @return array
  237. */
  238. function splitSql($sql)
  239. {
  240. $db =& JFactory::getDBO();
  241. return $db->splitSql($sql);
  242. }
  243. }