diff --git a/src/Composer/Command/SelfUpdateCommand.php b/src/Composer/Command/SelfUpdateCommand.php index 50ad96c6d..febc78295 100644 --- a/src/Composer/Command/SelfUpdateCommand.php +++ b/src/Composer/Command/SelfUpdateCommand.php @@ -13,6 +13,7 @@ namespace Composer\Command; use Composer\Composer; +use Composer\Util\StreamContextFactory; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -39,7 +40,9 @@ EOT protected function execute(InputInterface $input, OutputInterface $output) { - $latest = trim(file_get_contents('http://getcomposer.org/version')); + $ctx = StreamContextFactory::getContext(); + + $latest = trim(file_get_contents('http://getcomposer.org/version'), false, $ctx); if (Composer::VERSION !== $latest) { $output->writeln(sprintf("Updating to version %s.", $latest)); @@ -47,7 +50,7 @@ EOT $remoteFilename = 'http://getcomposer.org/composer.phar'; $localFilename = $_SERVER['argv'][0]; - file_put_contents($localFilename, file_get_contents($remoteFilename)); + copy($remoteFilename, $localFilename, $ctx); } else { $output->writeln("You are using the latest composer version."); } diff --git a/src/Composer/Downloader/FileDownloader.php b/src/Composer/Downloader/FileDownloader.php index ae2b199b2..168705b5a 100644 --- a/src/Composer/Downloader/FileDownloader.php +++ b/src/Composer/Downloader/FileDownloader.php @@ -14,6 +14,7 @@ namespace Composer\Downloader; use Composer\IO\IOInterface; use Composer\Package\PackageInterface; use Composer\Util\Filesystem; +use Composer\Util\StreamContextFactory; /** * Base downloader for file packages @@ -78,31 +79,14 @@ abstract class FileDownloader implements DownloaderInterface } } - // Handle system proxy - $params = array('http' => array()); - - if (isset($_SERVER['HTTP_PROXY'])) { - // http(s):// is not supported in proxy - $proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $_SERVER['HTTP_PROXY']); - - if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) { - throw new \RuntimeException('You must enable the openssl extension to use a proxy over https'); - } - - $params['http'] = array( - 'proxy' => $proxy, - 'request_fulluri' => true, - ); - } - + $options = array(); if ($this->io->hasAuthorization($package->getSourceUrl())) { $auth = $this->io->getAuthorization($package->getSourceUrl()); $authStr = base64_encode($auth['username'] . ':' . $auth['password']); - $params['http'] = array_merge($params['http'], array('header' => "Authorization: Basic $authStr\r\n")); + $options['http']['header'] = "Authorization: Basic $authStr\r\n"; } - $ctx = stream_context_create($params); - stream_context_set_params($ctx, array("notification" => array($this, 'callbackGet'))); + $ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet'))); $this->io->overwrite(" Downloading: connection...", false); @copy($url, $fileName, $ctx); diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 67a60c1ea..7d4ab8cb6 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -14,6 +14,7 @@ namespace Composer\Json; use Composer\Repository\RepositoryManager; use Composer\Composer; +use Composer\Util\StreamContextFactory; /** * Reads/writes json files. @@ -59,11 +60,12 @@ class JsonFile */ public function read() { - $context = stream_context_create(array( - 'http' => array('header' => 'User-Agent: Composer/'.Composer::VERSION."\r\n") - )); + $ctx = StreamContextFactory::getContext(array( + 'http' => array( + 'header' => 'User-Agent: Composer/'.Composer::VERSION."\r\n" + ))); - $json = file_get_contents($this->path, false, $context); + $json = file_get_contents($this->path, false, $ctx); if (!$json) { throw new \RuntimeException('Could not read '.$this->path.', you are probably offline'); } diff --git a/src/Composer/Repository/PearRepository.php b/src/Composer/Repository/PearRepository.php index ff975d99a..edf5b9e3b 100644 --- a/src/Composer/Repository/PearRepository.php +++ b/src/Composer/Repository/PearRepository.php @@ -13,6 +13,7 @@ namespace Composer\Repository; use Composer\Package\Loader\ArrayLoader; +use Composer\Util\StreamContextFactory; /** * @author Benjamin Eberlei @@ -20,7 +21,8 @@ use Composer\Package\Loader\ArrayLoader; */ class PearRepository extends ArrayRepository { - protected $url; + private $url; + private $streamContext; public function __construct(array $config) { @@ -41,6 +43,7 @@ class PearRepository extends ArrayRepository set_error_handler(function($severity, $message, $file, $line) { throw new \ErrorException($message, $severity, $severity, $file, $line); }); + $this->streamContext = StreamContextFactory::getContext(); $this->fetchFromServer(); restore_error_handler(); } @@ -106,7 +109,7 @@ class PearRepository extends ArrayRepository ); try { - $deps = file_get_contents($releaseLink . "/deps.".$pearVersion.".txt"); + $deps = file_get_contents($releaseLink . "/deps.".$pearVersion.".txt", false, $this->streamContext); } catch (\ErrorException $e) { if (strpos($e->getMessage(), '404')) { continue; @@ -274,7 +277,7 @@ class PearRepository extends ArrayRepository */ private function requestXml($url) { - $content = file_get_contents($url); + $content = file_get_contents($url, false, $this->streamContext); if (!$content) { throw new \UnexpectedValueException('The PEAR channel at '.$url.' did not respond.'); } @@ -283,4 +286,4 @@ class PearRepository extends ArrayRepository return $dom; } -} \ No newline at end of file +} diff --git a/src/Composer/Util/StreamContextFactory.php b/src/Composer/Util/StreamContextFactory.php new file mode 100644 index 000000000..4ea31da4d --- /dev/null +++ b/src/Composer/Util/StreamContextFactory.php @@ -0,0 +1,56 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Util; + +/** + * Allows the creation of a basic context supporting http proxy + * + * @author Jordan Alliot + */ +final class StreamContextFactory +{ + /** + * Creates a context supporting HTTP proxies + * + * @param array $defaultOptions Options to merge with the default + * @param array $defaultParams Parameters to specify on the context + * @return resource Default context + * @throws \RuntimeException if https proxy required and OpenSSL uninstalled + */ + static public function getContext(array $defaultOptions = array(), array $defaultParams = array()) + { + $options = array('http' => array()); + + // Handle system proxy + if (isset($_SERVER['HTTP_PROXY']) || isset($_SERVER['http_proxy'])) { + // Some systems seem to rely on a lowercased version instead... + $proxy = isset($_SERVER['HTTP_PROXY']) ? $_SERVER['HTTP_PROXY'] : $_SERVER['http_proxy']; + + // http(s):// is not supported in proxy + $proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxy); + + if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) { + throw new \RuntimeException('You must enable the openssl extension to use a proxy over https'); + } + + $options['http'] = array( + 'proxy' => $proxy, + 'request_fulluri' => true, + ); + } + + $options = array_merge_recursive($options, $defaultOptions); + + return stream_context_create($options, $defaultParams); + } +}