Source for file download.php
Documentation is available at download.php
* @package Joomla.Administrator
* @subpackage com_joomlaupdate
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* Smart download helper. Automatically uses cURL or URL fopen() wrappers to
* @package Joomla.Administrator
* Downloads from a URL and saves the result as a local file
* @param string $url The URL to download from
* @param string $target The file path to download to
* @return bool True on success
public static function download($url, $target)
// Make sure the target does not exist
// Try to open the output file for writing
$fp =
@fopen($target, 'wb');
// The file can not be opened for writing. Let's try a hack.
if ( self::chmod($target, 511) )
$fp =
@fopen($target, 'wb');
// First try to download directly to file if $fp !== false
$adapters =
self::getAdapters();
while (!empty($adapters) &&
($result ===
false))
// Run the current download method
$result =
self::$method($url, $fp);
// Check if we have a download
// The download is complete, close the file pointer
// If the filesize is not at least 1 byte, we consider it failed.
$fp =
@fopen($target, 'wb');
// If we have no download, close the file pointer
// Delete the target file if it exists
// Download and write using JFile::write();
$result =
JFile::write($target, self::downloadAndReturn($url));
* Downloads from a URL and returns the result as a string
* @param string $url The URL to download from
* @return mixed Result string on success, false on failure
$adapters =
self::getAdapters();
while (!empty($adapters) &&
($result ===
false))
// Run the current download method
$result =
self::$method($url, null);
* Does the server support PHP's cURL extension?
* @return bool True if it is supported
private static function hasCURL()
$result =
function_exists('curl_init');
* Downloads the contents of a URL and writes them to disk (if $fp is not null)
* or returns them as a string (if $fp is null)
* @param string $url The URL to download from
* @param resource $fp The file pointer to download to. Omit to return the contents.
* @param boolean $nofollow Should we follow 301/302/307 redirection HTTP headers?
* @return bool|stringFalse on failure, true on success ($fp not null) or the URL contents (if $fp is null)
private static function &getCURL($url, $fp =
null, $nofollow =
false)
if ( !@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1) &&
!$nofollow )
// Safe Mode is enabled. We have to fetch the headers and
// parse any redirections present in there.
foreach ($lines as $line)
if (substr($line, 0, 9) ==
"Location:")
return self::getCURL($newURL, $fp);
return self::getCURL($newURL, $fp, true);
* Does the server support URL fopen() wrappers?
private static function hasFOPEN()
// If we are not allowed to use ini_get, we assume that URL fopen is
if (!function_exists('ini_get'))
$result =
ini_get('allow_url_fopen');
* Download from a URL using URL fopen() wrappers
* @param string $url The URL to download from
* @param resource $fp The file pointer to download to; leave null to return the d/l file as a string
* @return bool|stringFalse on failure, true on success ($fp not null) or the URL contents (if $fp is null)
private static function &getFOPEN($url, $fp =
null)
// Open the URL for reading
$opts['http']['user_agent'] =
'Joomla/' .
JVERSION;
$ih =
@fopen($url, 'r', false, $context);
// PHP 4 way (actually, it's just a fallback)
// If fopen() fails, abort
while (!feof($ih) &&
$result)
$contents =
fread($ih, 4096);
$result =
@fwrite($fp, $contents);
elseif ( $result ===
true )
* Detect and return available download "adapters" (not really adapters, as
* we don't follow the Adapter pattern, yet)
private static function getAdapters()
// Detect available adapters
* Change the permissions of a file, optionally using FTP
* @param string $path Absolute path to file
* @param int $mode Permissions, e.g. 0755
* @return boolean True on success
private static function chmod($path, $mode)
if ( ($mode <
0600) ||
($mode >
0777) )
// Check to make sure the path valid and clean
if ($ftpOptions['enabled'] ==
1)
// Connect the FTP client
$ftpOptions['host'], $ftpOptions['port'], null,
$ftpOptions['user'], $ftpOptions['pass']
if (@chmod($path, $mode))
elseif ($ftpOptions['enabled'] ==
1)
// Translate path and delete
// FTP connector throws an error
$ret =
$ftp->chmod($path, $mode);
Documentation generated on Tue, 19 Nov 2013 15:01:40 +0100 by phpDocumentor 1.4.3