1
0
Fork 0
composer/src/Composer/Util/RemoteFilesystem.php

297 lines
10 KiB
PHP
Raw Normal View History

2012-02-14 10:25:00 +00:00
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Util;
2012-03-18 20:05:10 +00:00
use Composer\Composer;
2012-02-14 10:25:00 +00:00
use Composer\IO\IOInterface;
use Composer\Downloader\TransportException;
2012-02-14 10:25:00 +00:00
/**
* @author François Pluchino <francois.pluchino@opendisplay.com>
*/
class RemoteFilesystem
{
private $io;
private $firstCall;
2012-02-14 10:25:00 +00:00
private $bytesMax;
2012-02-17 10:50:36 +00:00
private $originUrl;
private $fileUrl;
2012-02-14 10:25:00 +00:00
private $fileName;
private $result;
private $progress;
2012-02-17 10:53:38 +00:00
private $lastProgress;
private $options;
2012-02-14 10:25:00 +00:00
/**
* Constructor.
*
2012-05-22 10:07:08 +00:00
* @param IOInterface $io The IO instance
2012-02-14 10:25:00 +00:00
*/
public function __construct(IOInterface $io, $options = array())
2012-02-14 10:25:00 +00:00
{
$this->io = $io;
$this->options = $options;
2012-02-14 10:25:00 +00:00
}
/**
* Copy the remote file in local.
*
2012-07-13 20:16:17 +00:00
* @param string $originUrl The origin URL
* @param string $fileUrl The file URL
* @param string $fileName the local filename
* @param boolean $progress Display the progression
* @param array $options Additional context options
*
2012-06-23 09:58:18 +00:00
* @return bool true
*/
public function copy($originUrl, $fileUrl, $fileName, $progress = true, $options = array())
{
$this->get($originUrl, $fileUrl, $options, $fileName, $progress);
return $this->result;
}
/**
* Get the content.
*
2012-07-13 20:16:17 +00:00
* @param string $originUrl The origin URL
* @param string $fileUrl The file URL
* @param boolean $progress Display the progression
* @param array $options Additional context options
*
2012-02-16 22:41:26 +00:00
* @return string The content
*/
public function getContents($originUrl, $fileUrl, $progress = true, $options = array())
{
$this->get($originUrl, $fileUrl, $options, null, $progress);
return $this->result;
}
/**
* Get file content or copy action.
*
* @param string $originUrl The origin URL
* @param string $fileUrl The file URL
* @param array $additionalOptions context options
* @param string $fileName the local filename
* @param boolean $progress Display the progression
2012-02-14 10:25:00 +00:00
*
* @throws TransportException When the file could not be downloaded
2012-02-14 10:25:00 +00:00
*/
protected function get($originUrl, $fileUrl, $additionalOptions = array(), $fileName = null, $progress = true)
2012-02-14 10:25:00 +00:00
{
$this->bytesMax = 0;
$this->result = null;
2012-02-17 10:50:36 +00:00
$this->originUrl = $originUrl;
$this->fileUrl = $fileUrl;
$this->fileName = $fileName;
$this->progress = $progress;
2012-02-17 10:53:38 +00:00
$this->lastProgress = null;
2012-02-17 10:50:36 +00:00
$options = $this->getOptionsForUrl($originUrl, $additionalOptions);
2012-02-17 10:50:36 +00:00
$ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet')));
if ($this->progress) {
$this->io->write(" Downloading: <comment>connection...</comment>", false);
}
$errorMessage = '';
2012-10-18 15:09:23 +00:00
$errorCode = 0;
set_error_handler(function ($code, $msg) use (&$errorMessage) {
if ($errorMessage) {
$errorMessage .= "\n";
}
$errorMessage .= preg_replace('{^file_get_contents\(.*?\): }', '', $msg);
});
try {
$result = file_get_contents($fileUrl, false, $ctx);
} catch (\Exception $e) {
if ($e instanceof TransportException && !empty($http_response_header[0])) {
$e->setHeaders($http_response_header);
}
}
if ($errorMessage && !ini_get('allow_url_fopen')) {
$errorMessage = 'allow_url_fopen must be enabled in php.ini ('.$errorMessage.')';
}
restore_error_handler();
if (isset($e)) {
throw $e;
}
2012-03-09 22:44:10 +00:00
// fix for 5.4.0 https://bugs.php.net/bug.php?id=61336
2012-10-18 15:09:23 +00:00
if (!empty($http_response_header[0]) && preg_match('{^HTTP/\S+ ([45]\d\d)}i', $http_response_header[0], $match)) {
2012-03-09 22:44:10 +00:00
$result = false;
2012-10-18 15:09:23 +00:00
$errorCode = $match[1];
2012-03-09 22:44:10 +00:00
}
2012-03-18 20:05:10 +00:00
// decode gzip
if (false !== $result && extension_loaded('zlib') && substr($fileUrl, 0, 4) === 'http') {
2012-03-18 21:12:48 +00:00
$decode = false;
2012-03-18 20:05:10 +00:00
foreach ($http_response_header as $header) {
if (preg_match('{^content-encoding: *gzip *$}i', $header)) {
2012-03-18 21:12:48 +00:00
$decode = true;
continue;
} elseif (preg_match('{^HTTP/}i', $header)) {
$decode = false;
}
}
if ($decode) {
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$result = zlib_decode($result);
} else {
// work around issue with gzuncompress & co that do not work with all gzip checksums
$result = file_get_contents('compress.zlib://data:application/octet-stream;base64,'.base64_encode($result));
2012-03-18 20:05:10 +00:00
}
}
}
if ($this->progress) {
$this->io->overwrite(" Downloading: <comment>100%</comment>");
}
2012-03-18 20:05:10 +00:00
// handle copy command if download was successful
if (false !== $result && null !== $fileName) {
$errorMessage = '';
set_error_handler(function ($code, $msg) use (&$errorMessage) {
if ($errorMessage) {
$errorMessage .= "\n";
}
$errorMessage .= preg_replace('{^file_put_contents\(.*?\): }', '', $msg);
});
$result = (bool) file_put_contents($fileName, $result);
restore_error_handler();
if (false === $result) {
throw new TransportException('The "'.$fileUrl.'" file could not be written to '.$fileName.': '.$errorMessage);
}
2012-03-18 20:05:10 +00:00
}
// avoid overriding if content was loaded by a sub-call to get()
if (null === $this->result) {
$this->result = $result;
2012-02-17 10:50:36 +00:00
}
if (false === $this->result) {
2012-10-18 15:09:23 +00:00
$e = new TransportException('The "'.$fileUrl.'" file could not be downloaded: '.$errorMessage, $errorCode);
if (!empty($http_response_header[0])) {
$e->setHeaders($http_response_header);
}
throw $e;
2012-02-16 22:41:26 +00:00
}
2012-02-14 10:25:00 +00:00
}
/**
* Get notification action.
*
* @param integer $notificationCode The notification code
* @param integer $severity The severity level
* @param string $message The message
* @param integer $messageCode The message code
* @param integer $bytesTransferred The loaded size
* @param integer $bytesMax The total size
*/
protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
{
switch ($notificationCode) {
case STREAM_NOTIFY_FAILURE:
2012-03-18 21:12:48 +00:00
throw new TransportException('The "'.$this->fileUrl.'" file could not be downloaded ('.trim($message).')', $messageCode);
break;
2012-02-14 10:25:00 +00:00
case STREAM_NOTIFY_AUTH_REQUIRED:
if (401 === $messageCode) {
2012-02-14 10:25:00 +00:00
if (!$this->io->isInteractive()) {
$message = "The '" . $this->fileUrl . "' URL required authentication.\nYou must be using the interactive console";
2012-02-14 10:25:00 +00:00
throw new TransportException($message, 401);
2012-02-14 10:25:00 +00:00
}
2012-02-17 11:19:29 +00:00
$this->io->overwrite(' Authentication required (<info>'.parse_url($this->fileUrl, PHP_URL_HOST).'</info>):');
2012-02-14 10:25:00 +00:00
$username = $this->io->ask(' Username: ');
$password = $this->io->askAndHideAnswer(' Password: ');
$this->io->setAuthorization($this->originUrl, $username, $password);
$this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progress);
2012-02-14 10:25:00 +00:00
}
break;
2012-10-16 12:16:39 +00:00
case STREAM_NOTIFY_AUTH_RESULT:
if (403 === $messageCode) {
throw new TransportException($message, 403);
}
break;
2012-02-14 10:25:00 +00:00
case STREAM_NOTIFY_FILE_SIZE_IS:
if ($this->bytesMax < $bytesMax) {
$this->bytesMax = $bytesMax;
}
break;
case STREAM_NOTIFY_PROGRESS:
if ($this->bytesMax > 0 && $this->progress) {
2012-02-14 10:25:00 +00:00
$progression = 0;
if ($this->bytesMax > 0) {
$progression = round($bytesTransferred / $this->bytesMax * 100);
}
2012-02-17 10:53:38 +00:00
if ((0 === $progression % 5) && $progression !== $this->lastProgress) {
$this->lastProgress = $progression;
2012-02-14 10:25:00 +00:00
$this->io->overwrite(" Downloading: <comment>$progression%</comment>", false);
}
}
break;
default:
break;
}
}
protected function getOptionsForUrl($originUrl, $additionalOptions)
{
$headers = array(
sprintf(
'User-Agent: Composer/%s (%s; %s; PHP %s.%s.%s)',
Composer::VERSION === '@package_version@' ? 'source' : Composer::VERSION,
php_uname('s'),
php_uname('r'),
PHP_MAJOR_VERSION,
PHP_MINOR_VERSION,
PHP_RELEASE_VERSION
)
2012-06-01 12:05:24 +00:00
);
2012-03-18 20:05:10 +00:00
if (extension_loaded('zlib')) {
$headers[] = 'Accept-Encoding: gzip';
2012-03-18 20:05:10 +00:00
}
if ($this->io->hasAuthorization($originUrl)) {
$auth = $this->io->getAuthorization($originUrl);
$authStr = base64_encode($auth['username'] . ':' . $auth['password']);
$headers[] = 'Authorization: Basic '.$authStr;
}
$options = array_replace_recursive($this->options, $additionalOptions);
if (isset($options['http']['header']) && !is_array($options['http']['header'])) {
$options['http']['header'] = explode("\r\n", trim($options['http']['header'], "\r\n"));
}
foreach ($headers as $header) {
$options['http']['header'][] = $header;
}
return $options;
}
}