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