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

334 lines
8.3 KiB
PHP
Raw Normal View History

2012-03-22 16:18:24 +00:00
<?php
2012-03-24 21:39:34 +00:00
2012-03-22 16:18:24 +00:00
/*
* 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\Config;
2012-03-22 16:18:24 +00:00
use Composer\IO\IOInterface;
/**
* @author Till Klampaeckel <till@php.net>
* @author Jordi Boggiano <j.boggiano@seld.be>
2012-03-22 16:18:24 +00:00
*/
class Svn
{
const MAX_QTY_AUTH_TRIES = 5;
2012-03-22 16:18:24 +00:00
/**
* @var array
2012-03-22 16:18:24 +00:00
*/
protected $credentials;
/**
* @var bool
2012-03-22 16:18:24 +00:00
*/
protected $hasAuth;
/**
* @var \Composer\IO\IOInterface
2012-03-22 16:18:24 +00:00
*/
protected $io;
/**
* @var string
2012-03-22 16:18:24 +00:00
*/
protected $url;
/**
* @var bool
*/
protected $cacheCredentials = true;
2012-03-24 23:29:14 +00:00
/**
* @var ProcessExecutor
*/
protected $process;
/**
* @var integer
*/
protected $qtyAuthTries = 0;
/**
* @var \Composer\Config
*/
protected $config;
2012-03-22 16:18:24 +00:00
/**
* @param string $url
* @param \Composer\IO\IOInterface $io
2014-07-16 13:17:38 +00:00
* @param Config $config
2012-03-24 23:29:14 +00:00
* @param ProcessExecutor $process
2012-03-22 16:18:24 +00:00
*/
public function __construct($url, IOInterface $io, Config $config, ProcessExecutor $process = null)
2012-03-22 16:18:24 +00:00
{
$this->url = $url;
$this->io = $io;
$this->config = $config;
2012-03-24 23:29:14 +00:00
$this->process = $process ?: new ProcessExecutor;
}
public static function cleanEnv()
{
// clean up env for OSX, see https://github.com/composer/composer/issues/2146#issuecomment-35478940
putenv("DYLD_LIBRARY_PATH");
unset($_SERVER['DYLD_LIBRARY_PATH']);
}
2012-03-24 23:29:14 +00:00
/**
* Execute an SVN command and try to fix up the process with credentials
* if necessary.
*
2012-06-23 09:58:18 +00:00
* @param string $command SVN command to run
* @param string $url SVN url
* @param string $cwd Working directory
* @param string $path Target for a checkout
* @param bool $verbose Output all output to the user
2012-03-24 23:29:14 +00:00
*
* @return string
*
* @throws \RuntimeException
*/
public function execute($command, $url, $cwd = null, $path = null, $verbose = false)
2012-03-24 23:29:14 +00:00
{
$svnCommand = $this->getCommand($command, $url, $path);
$output = null;
$io = $this->io;
$handler = function ($type, $buffer) use (&$output, $io, $verbose) {
if ($type !== 'out') {
return;
}
if ('Redirecting to URL ' === substr($buffer, 0, 19)) {
return;
}
$output .= $buffer;
if ($verbose) {
$io->writeError($buffer, false);
}
};
$status = $this->process->execute($svnCommand, $handler, $cwd);
2012-03-24 23:29:14 +00:00
if (0 === $status) {
return $output;
}
$errorOutput = $this->process->getErrorOutput();
2012-03-24 23:29:14 +00:00
if (empty($output)) {
$output = $errorOutput;
2012-03-24 23:29:14 +00:00
}
$fullOutput = "$output\n$errorOutput";
2012-03-24 23:29:14 +00:00
// the error is not auth-related
if (false === stripos($fullOutput, 'Could not authenticate to server:')
&& false === stripos($fullOutput, 'authorization failed')
&& false === stripos($fullOutput, 'svn: E170001:')
&& false === stripos($fullOutput, 'svn: E215004:')) {
throw new \RuntimeException($fullOutput);
2012-03-24 23:29:14 +00:00
}
if (!$this->hasAuth()) {
$this->doAuthDance();
2012-03-24 23:29:14 +00:00
}
// try to authenticate if maximum quantity of tries not reached
if ($this->qtyAuthTries++ < self::MAX_QTY_AUTH_TRIES) {
2012-03-24 23:29:14 +00:00
// restart the process
return $this->execute($command, $url, $cwd, $path, $verbose);
2012-03-24 23:29:14 +00:00
}
throw new \RuntimeException(
2012-03-24 23:39:28 +00:00
'wrong credentials provided ('.$output.')'
2012-03-24 23:29:14 +00:00
);
2012-03-22 16:18:24 +00:00
}
/**
* @param boolean $cacheCredentials
*/
public function setCacheCredentials($cacheCredentials)
{
$this->cacheCredentials = $cacheCredentials;
}
/**
* Repositories requests credentials, let's put them in.
*
* @return \Composer\Util\Svn
* @throws \RuntimeException
*/
2012-03-24 23:29:14 +00:00
protected function doAuthDance()
{
// cannot ask for credentials in non interactive mode
if (!$this->io->isInteractive()) {
throw new \RuntimeException(
'can not ask for authentication in non interactive mode'
);
}
$this->io->writeError("The Subversion server ({$this->url}) requested credentials:");
$this->hasAuth = true;
$this->credentials['username'] = $this->io->ask("Username: ");
$this->credentials['password'] = $this->io->askAndHideAnswer("Password: ");
$this->cacheCredentials = $this->io->askConfirmation("Should Subversion cache these credentials? (yes/no) ", true);
return $this;
}
2012-03-22 16:18:24 +00:00
/**
* A method to create the svn commands run.
*
* @param string $cmd Usually 'svn ls' or something like that.
* @param string $url Repo URL.
2012-03-24 23:29:14 +00:00
* @param string $path Target for a checkout
2012-03-22 16:18:24 +00:00
*
* @return string
*/
2012-03-24 23:29:14 +00:00
protected function getCommand($cmd, $url, $path = null)
2012-03-22 16:18:24 +00:00
{
$cmd = sprintf('%s %s%s %s',
$cmd,
'--non-interactive ',
$this->getCredentialString(),
2014-09-24 16:30:12 +00:00
ProcessExecutor::escape($url)
2012-03-22 16:18:24 +00:00
);
if ($path) {
2014-09-24 16:30:12 +00:00
$cmd .= ' ' . ProcessExecutor::escape($path);
2012-03-22 16:18:24 +00:00
}
2012-03-24 21:47:16 +00:00
2012-03-22 16:18:24 +00:00
return $cmd;
}
/**
* Return the credential string for the svn command.
*
* Adds --no-auth-cache when credentials are present.
*
* @return string
*/
2012-03-24 23:29:14 +00:00
protected function getCredentialString()
2012-03-22 16:18:24 +00:00
{
if (!$this->hasAuth()) {
2012-03-22 16:18:24 +00:00
return '';
}
2012-03-22 16:18:24 +00:00
return sprintf(
' %s--username %s --password %s ',
$this->getAuthCache(),
2014-09-24 16:30:12 +00:00
ProcessExecutor::escape($this->getUsername()),
ProcessExecutor::escape($this->getPassword())
2012-03-22 16:18:24 +00:00
);
}
/**
* Get the password for the svn command. Can be empty.
*
* @return string
* @throws \LogicException
*/
2012-03-24 23:29:14 +00:00
protected function getPassword()
2012-03-22 16:18:24 +00:00
{
if ($this->credentials === null) {
throw new \LogicException("No svn auth detected.");
2012-03-22 16:18:24 +00:00
}
return isset($this->credentials['password']) ? $this->credentials['password'] : '';
2012-03-22 16:18:24 +00:00
}
/**
* Get the username for the svn command.
*
* @return string
* @throws \LogicException
*/
2012-03-24 23:29:14 +00:00
protected function getUsername()
2012-03-22 16:18:24 +00:00
{
if ($this->credentials === null) {
throw new \LogicException("No svn auth detected.");
2012-03-22 16:18:24 +00:00
}
return $this->credentials['username'];
2012-03-22 16:18:24 +00:00
}
/**
* Detect Svn Auth.
*
2012-06-23 09:58:18 +00:00
* @return bool
2012-03-22 16:18:24 +00:00
*/
2012-03-24 23:29:14 +00:00
protected function hasAuth()
2012-03-22 16:18:24 +00:00
{
if (null !== $this->hasAuth) {
2012-03-22 16:18:24 +00:00
return $this->hasAuth;
}
if (false === $this->createAuthFromConfig()) {
$this->createAuthFromUrl();
2012-03-22 16:18:24 +00:00
}
return $this->hasAuth;
}
/**
* Return the no-auth-cache switch.
*
* @return string
*/
protected function getAuthCache()
{
return $this->cacheCredentials ? '' : '--no-auth-cache ';
2012-03-22 16:18:24 +00:00
}
/**
* Create the auth params from the configuration file.
*
* @return bool
*/
2014-06-03 11:34:58 +00:00
private function createAuthFromConfig()
{
if (!$this->config->has('http-basic')) {
return $this->hasAuth = false;
}
$authConfig = $this->config->get('http-basic');
$host = parse_url($this->url, PHP_URL_HOST);
if (isset($authConfig[$host])) {
$this->credentials['username'] = $authConfig[$host]['username'];
$this->credentials['password'] = $authConfig[$host]['password'];
return $this->hasAuth = true;
}
return $this->hasAuth = false;
}
/**
* Create the auth params from the url
*
* @return bool
*/
2014-06-03 11:34:58 +00:00
private function createAuthFromUrl()
{
$uri = parse_url($this->url);
if (empty($uri['user'])) {
return $this->hasAuth = false;
}
$this->credentials['username'] = $uri['user'];
if (!empty($uri['pass'])) {
$this->credentials['password'] = $uri['pass'];
}
return $this->hasAuth = true;
}
2012-05-22 10:07:08 +00:00
}