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;
|
|
|
|
|
|
|
|
use Composer\IO\IOInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author François Pluchino <francois.pluchino@opendisplay.com>
|
|
|
|
*/
|
|
|
|
class RemoteFilesystem
|
|
|
|
{
|
2012-02-15 12:11:29 +00:00
|
|
|
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;
|
2012-02-15 12:11:29 +00:00
|
|
|
private $content;
|
|
|
|
private $progess;
|
2012-02-17 10:53:38 +00:00
|
|
|
private $lastProgress;
|
2012-02-14 10:25:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param IOInterface $io The IO instance
|
|
|
|
*/
|
|
|
|
public function __construct(IOInterface $io)
|
|
|
|
{
|
|
|
|
$this->io = $io;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy the remote file in local.
|
|
|
|
*
|
2012-02-15 12:11:29 +00:00
|
|
|
* @param string $originUrl The orgin URL
|
|
|
|
* @param string $fileUrl The file URL
|
|
|
|
* @param string $fileName the local filename
|
|
|
|
* @param boolean $progess Display the progression
|
|
|
|
*/
|
|
|
|
public function copy($originUrl, $fileUrl, $fileName, $progess = true)
|
|
|
|
{
|
|
|
|
$this->get($originUrl, $fileUrl, $fileName, $progess);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the content.
|
|
|
|
*
|
|
|
|
* @param string $originUrl The orgin URL
|
|
|
|
* @param string $fileUrl The file URL
|
|
|
|
* @param boolean $progess Display the progression
|
|
|
|
*
|
2012-02-16 22:41:26 +00:00
|
|
|
* @return string The content
|
2012-02-15 12:11:29 +00:00
|
|
|
*/
|
|
|
|
public function getContents($originUrl, $fileUrl, $progess = true)
|
|
|
|
{
|
|
|
|
$this->get($originUrl, $fileUrl, null, $progess);
|
|
|
|
|
|
|
|
return $this->content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file content or copy action.
|
|
|
|
*
|
|
|
|
* @param string $originUrl The orgin URL
|
|
|
|
* @param string $fileUrl The file URL
|
|
|
|
* @param string $fileName the local filename
|
|
|
|
* @param boolean $progess Display the progression
|
2012-02-17 10:53:09 +00:00
|
|
|
* @param boolean $firstCall Whether this is the first attempt at fetching this resource
|
2012-02-14 10:25:00 +00:00
|
|
|
*
|
2012-02-16 22:41:26 +00:00
|
|
|
* @throws \RuntimeException When the file could not be downloaded
|
2012-02-14 10:25:00 +00:00
|
|
|
*/
|
2012-02-17 10:53:09 +00:00
|
|
|
protected function get($originUrl, $fileUrl, $fileName = null, $progess = true, $firstCall = true)
|
2012-02-14 10:25:00 +00:00
|
|
|
{
|
2012-02-17 10:53:09 +00:00
|
|
|
$this->firstCall = $firstCall;
|
2012-02-14 10:25:00 +00:00
|
|
|
$this->bytesMax = 0;
|
2012-02-17 10:50:36 +00:00
|
|
|
$this->content = null;
|
|
|
|
$this->originUrl = $originUrl;
|
|
|
|
$this->fileUrl = $fileUrl;
|
2012-02-15 12:11:29 +00:00
|
|
|
$this->fileName = $fileName;
|
2012-02-17 10:50:36 +00:00
|
|
|
$this->progress = $progess;
|
2012-02-17 10:53:38 +00:00
|
|
|
$this->lastProgress = null;
|
2012-02-17 10:50:36 +00:00
|
|
|
|
2012-02-15 12:11:29 +00:00
|
|
|
// add authorization in context
|
2012-02-16 17:11:01 +00:00
|
|
|
$options = array();
|
2012-02-17 10:50:36 +00:00
|
|
|
if ($this->io->hasAuthorization($originUrl)) {
|
2012-02-16 17:11:01 +00:00
|
|
|
$auth = $this->io->getAuthorization($originUrl);
|
|
|
|
$authStr = base64_encode($auth['username'] . ':' . $auth['password']);
|
2012-02-16 22:41:26 +00:00
|
|
|
$options['http']['header'] = "Authorization: Basic $authStr\r\n";
|
2012-02-17 11:13:56 +00:00
|
|
|
} elseif (null !== $this->io->getLastUsername()) {
|
2012-02-17 10:50:36 +00:00
|
|
|
$authStr = base64_encode($this->io->getLastUsername() . ':' . $this->io->getLastPassword());
|
|
|
|
$options['http'] = array('header' => "Authorization: Basic $authStr\r\n");
|
|
|
|
$this->io->setAuthorization($originUrl, $this->io->getLastUsername(), $this->io->getLastPassword());
|
|
|
|
}
|
|
|
|
|
|
|
|
$ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet')));
|
|
|
|
|
2012-02-15 12:11:29 +00:00
|
|
|
if ($this->progress) {
|
|
|
|
$this->io->overwrite(" Downloading: <comment>connection...</comment>", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $fileName) {
|
2012-02-16 22:41:26 +00:00
|
|
|
$result = @copy($fileUrl, $fileName, $ctx);
|
2012-02-15 12:11:29 +00:00
|
|
|
} else {
|
2012-02-16 22:41:26 +00:00
|
|
|
$result = @file_get_contents($fileUrl, false, $ctx);
|
|
|
|
$this->content = $result;
|
2012-02-17 10:50:36 +00:00
|
|
|
}
|
|
|
|
|
2012-02-15 12:11:29 +00:00
|
|
|
if ($this->progress) {
|
|
|
|
$this->io->overwrite(" Downloading", false);
|
|
|
|
}
|
2012-02-16 22:41:26 +00:00
|
|
|
|
|
|
|
if (false === $result) {
|
2012-02-17 11:19:29 +00:00
|
|
|
throw new \RuntimeException("The '$fileUrl' file could not be downloaded");
|
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_AUTH_REQUIRED:
|
|
|
|
case STREAM_NOTIFY_FAILURE:
|
|
|
|
// for private repository returning 404 error when the authorization is incorrect
|
|
|
|
$auth = $this->io->getAuthorization($this->originUrl);
|
2012-02-17 11:19:29 +00:00
|
|
|
$attemptAuthentication = $this->firstCall && 404 === $messageCode && null === $auth['username'];
|
2012-02-14 10:25:00 +00:00
|
|
|
|
|
|
|
if (404 === $messageCode && !$this->firstCall) {
|
|
|
|
throw new \RuntimeException("The '" . $this->fileUrl . "' URL not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->firstCall = false;
|
|
|
|
|
|
|
|
// get authorization informations
|
2012-02-17 11:19:29 +00:00
|
|
|
if (401 === $messageCode || $attemptAuthentication) {
|
2012-02-14 10:25:00 +00:00
|
|
|
if (!$this->io->isInteractive()) {
|
2012-02-15 12:11:29 +00:00
|
|
|
$mess = "The '" . $this->fileUrl . "' URL was not found";
|
2012-02-14 10:25:00 +00:00
|
|
|
|
2012-02-17 11:19:29 +00:00
|
|
|
if (401 === $code || $attemptAuthentication) {
|
|
|
|
$mess = "The '" . $this->fileUrl . "' URL required authentication.\nYou must be using the interactive console";
|
2012-02-14 10:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new \RuntimeException($mess);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2012-02-17 10:53:09 +00:00
|
|
|
$this->content = $this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progress, false);
|
2012-02-14 10:25:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STREAM_NOTIFY_FILE_SIZE_IS:
|
|
|
|
if ($this->bytesMax < $bytesMax) {
|
|
|
|
$this->bytesMax = $bytesMax;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STREAM_NOTIFY_PROGRESS:
|
2012-02-15 12:11:29 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|