2012-01-10 14:44:13 +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\Repository\Vcs;
|
|
|
|
|
2012-01-16 13:14:15 +00:00
|
|
|
use Composer\IO\IOInterface;
|
|
|
|
|
2012-01-10 14:44:13 +00:00
|
|
|
/**
|
2012-01-17 13:53:50 +00:00
|
|
|
* A driver implementation for driver with authorization interaction.
|
2012-01-10 14:44:13 +00:00
|
|
|
*
|
|
|
|
* @author François Pluchino <francois.pluchino@opendisplay.com>
|
|
|
|
*/
|
|
|
|
abstract class VcsDriver
|
|
|
|
{
|
|
|
|
protected $url;
|
2012-01-16 13:14:15 +00:00
|
|
|
protected $io;
|
2012-01-16 19:09:32 +00:00
|
|
|
private $firstCall;
|
2012-01-17 11:52:14 +00:00
|
|
|
private $contentUrl;
|
|
|
|
private $content;
|
2012-01-10 14:44:13 +00:00
|
|
|
|
|
|
|
/**
|
2012-01-11 12:55:05 +00:00
|
|
|
* Constructor.
|
2012-01-10 14:44:13 +00:00
|
|
|
*
|
2012-01-16 13:14:15 +00:00
|
|
|
* @param string $url The URL
|
|
|
|
* @param IOInterface $io The IO instance
|
2012-01-10 14:44:13 +00:00
|
|
|
*/
|
2012-01-16 13:14:15 +00:00
|
|
|
public function __construct($url, IOInterface $io)
|
2012-01-10 14:44:13 +00:00
|
|
|
{
|
|
|
|
$this->url = $url;
|
2012-01-16 13:14:15 +00:00
|
|
|
$this->io = $io;
|
2012-01-16 19:09:32 +00:00
|
|
|
$this->firstCall = true;
|
2012-01-10 14:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-17 22:07:33 +00:00
|
|
|
* Get the https or http protocol depending on SSL support.
|
|
|
|
*
|
|
|
|
* Call this only if you know that the server supports both.
|
2012-01-10 14:44:13 +00:00
|
|
|
*
|
|
|
|
* @return string The correct type of protocol
|
|
|
|
*/
|
2012-01-11 00:11:56 +00:00
|
|
|
protected function getScheme()
|
2012-01-10 14:44:13 +00:00
|
|
|
{
|
|
|
|
if (extension_loaded('openssl')) {
|
|
|
|
return 'https';
|
|
|
|
}
|
|
|
|
return 'http';
|
|
|
|
}
|
2012-01-16 13:14:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the remote content.
|
|
|
|
*
|
|
|
|
* @param string $url The URL of content
|
|
|
|
*
|
|
|
|
* @return mixed The result
|
|
|
|
*/
|
|
|
|
protected function getContents($url)
|
|
|
|
{
|
2012-01-17 11:52:14 +00:00
|
|
|
$this->contentUrl = $url;
|
2012-01-17 13:53:50 +00:00
|
|
|
$auth = $this->io->getAuthorization($this->url);
|
2012-01-17 11:52:14 +00:00
|
|
|
$params = array();
|
2012-01-16 13:14:15 +00:00
|
|
|
|
|
|
|
// add authorization to curl options
|
2012-01-17 13:53:50 +00:00
|
|
|
if ($this->io->hasAuthorization($this->url)) {
|
2012-01-17 11:52:14 +00:00
|
|
|
$authStr = base64_encode($auth['username'] . ':' . $auth['password']);
|
|
|
|
$params['http'] = array('header' => "Authorization: Basic $authStr\r\n");
|
2012-01-16 13:14:15 +00:00
|
|
|
} else if (null !== $this->io->getLastUsername()) {
|
2012-01-17 11:52:14 +00:00
|
|
|
$authStr = base64_encode($this->io->getLastUsername() . ':' . $this->io->getLastPassword());
|
|
|
|
$params['http'] = array('header' => "Authorization: Basic $authStr\r\n");
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 11:52:14 +00:00
|
|
|
$ctx = stream_context_create($params);
|
|
|
|
stream_context_set_params($ctx, array("notification" => array($this, 'callbackGet')));
|
2012-01-16 13:14:15 +00:00
|
|
|
|
2012-01-17 11:52:14 +00:00
|
|
|
$content = @file_get_contents($url, false, $ctx);
|
2012-01-16 13:14:15 +00:00
|
|
|
|
2012-01-17 13:53:50 +00:00
|
|
|
// content get after authorization
|
2012-01-17 11:52:14 +00:00
|
|
|
if (false === $content) {
|
|
|
|
$content = $this->content;
|
|
|
|
$this->content = null;
|
|
|
|
$this->contentUrl = null;
|
2012-01-16 19:09:32 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 11:52:14 +00:00
|
|
|
return $content;
|
|
|
|
}
|
2012-01-16 13:14:15 +00:00
|
|
|
|
2012-01-17 22:07:33 +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
|
2012-01-17 11:52:14 +00:00
|
|
|
*/
|
|
|
|
protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
|
|
|
|
{
|
2012-01-17 22:07:33 +00:00
|
|
|
switch ($notificationCode) {
|
2012-01-17 11:52:14 +00:00
|
|
|
case STREAM_NOTIFY_AUTH_REQUIRED:
|
|
|
|
case STREAM_NOTIFY_FAILURE:
|
2012-01-17 13:53:50 +00:00
|
|
|
// for private repository returning 404 error when the authorization is incorrect
|
2012-01-17 22:07:33 +00:00
|
|
|
$auth = $this->io->getAuthorization($this->url);
|
2012-01-17 11:52:14 +00:00
|
|
|
$ps = $this->firstCall && 404 === $messageCode
|
|
|
|
&& null === $this->io->getLastUsername()
|
|
|
|
&& null === $auth['username'];
|
|
|
|
|
|
|
|
if (404 === $messageCode && !$this->firstCall) {
|
2012-01-17 13:53:50 +00:00
|
|
|
throw new \RuntimeException("The '" . $this->contentUrl . "' URL not found");
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 22:07:33 +00:00
|
|
|
$this->firstCall = false;
|
|
|
|
|
2012-01-17 13:53:50 +00:00
|
|
|
// get authorization informations
|
2012-01-17 22:07:33 +00:00
|
|
|
if (401 === $messageCode || $ps) {
|
|
|
|
if (!$this->io->isInteractive()) {
|
|
|
|
$mess = "The '" . $this->contentUrl . "' URL not found";
|
|
|
|
|
|
|
|
if (401 === $code || $ps) {
|
|
|
|
$mess = "The '" . $this->contentUrl . "' URL required the authorization.\nYou must be used the interactive console";
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \RuntimeException($mess);
|
2012-01-17 11:52:14 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 22:07:33 +00:00
|
|
|
$this->io->write("Authorization for <info>" . $this->contentUrl . "</info>:");
|
|
|
|
$username = $this->io->ask(' Username: ');
|
|
|
|
$password = $this->io->askAndHideAnswer(' Password: ');
|
2012-01-17 13:53:50 +00:00
|
|
|
$this->io->setAuthorization($this->url, $username, $password);
|
2012-01-17 11:52:14 +00:00
|
|
|
|
|
|
|
$this->content = $this->getContents($this->contentUrl);
|
2012-01-17 22:07:33 +00:00
|
|
|
}
|
2012-01-17 11:52:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2012-01-17 09:29:54 +00:00
|
|
|
}
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
2012-01-10 14:44:13 +00:00
|
|
|
}
|