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\IO\IOInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Till Klampaeckel <till@php.net>
|
2012-03-24 22:05:13 +00:00
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
2012-03-22 16:18:24 +00:00
|
|
|
*/
|
|
|
|
class Svn
|
|
|
|
{
|
|
|
|
/**
|
2012-03-24 22:05:13 +00:00
|
|
|
* @var array
|
2012-03-22 16:18:24 +00:00
|
|
|
*/
|
|
|
|
protected $credentials;
|
|
|
|
|
|
|
|
/**
|
2012-03-23 15:03:52 +00:00
|
|
|
* @var bool
|
2012-03-22 16:18:24 +00:00
|
|
|
*/
|
|
|
|
protected $hasAuth;
|
|
|
|
|
|
|
|
/**
|
2012-03-23 15:03:52 +00:00
|
|
|
* @var \Composer\IO\IOInterface
|
2012-03-22 16:18:24 +00:00
|
|
|
*/
|
|
|
|
protected $io;
|
|
|
|
|
|
|
|
/**
|
2012-03-23 15:03:52 +00:00
|
|
|
* @var string
|
2012-03-22 16:18:24 +00:00
|
|
|
*/
|
|
|
|
protected $url;
|
|
|
|
|
2012-03-22 17:40:18 +00:00
|
|
|
/**
|
2012-03-23 15:03:52 +00:00
|
|
|
* @var bool
|
2012-03-22 17:40:18 +00:00
|
|
|
*/
|
2012-03-24 22:08:43 +00:00
|
|
|
protected $cacheCredentials = true;
|
2012-03-22 17:40:18 +00:00
|
|
|
|
2012-03-24 23:29:14 +00:00
|
|
|
/**
|
|
|
|
* @var ProcessExecutor
|
|
|
|
*/
|
|
|
|
protected $process;
|
|
|
|
|
2012-03-22 16:18:24 +00:00
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
* @param \Composer\IO\IOInterface $io
|
2012-03-24 23:29:14 +00:00
|
|
|
* @param ProcessExecutor $process
|
2012-03-22 16:18:24 +00:00
|
|
|
*/
|
2012-03-24 23:29:14 +00:00
|
|
|
public function __construct($url, IOInterface $io, ProcessExecutor $process = null)
|
2012-03-22 16:18:24 +00:00
|
|
|
{
|
|
|
|
$this->url = $url;
|
|
|
|
$this->io = $io;
|
2012-03-24 23:29:14 +00:00
|
|
|
$this->process = $process ?: new ProcessExecutor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-04-04 07:36:04 +00:00
|
|
|
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);
|
2012-04-04 07:36:04 +00:00
|
|
|
$output = null;
|
|
|
|
$io = $this->io;
|
|
|
|
$handler = function ($type, $buffer) use (&$output, $io, $verbose) {
|
|
|
|
if ($type !== 'out') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$output .= $buffer;
|
|
|
|
if ($verbose) {
|
|
|
|
$io->write($buffer, false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$status = $this->process->execute($svnCommand, $handler, $cwd);
|
2012-03-24 23:29:14 +00:00
|
|
|
if (0 === $status) {
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($output)) {
|
|
|
|
$output = $this->process->getErrorOutput();
|
|
|
|
}
|
|
|
|
|
|
|
|
// the error is not auth-related
|
2013-06-10 18:00:58 +00:00
|
|
|
if (false === stripos($output, 'Could not authenticate to server:')
|
|
|
|
&& false === stripos($output, 'svn: E170001:')) {
|
2012-03-24 23:39:28 +00:00
|
|
|
throw new \RuntimeException($output);
|
2012-03-24 23:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// no auth supported for non interactive calls
|
|
|
|
if (!$this->io->isInteractive()) {
|
|
|
|
throw new \RuntimeException(
|
2012-03-24 23:39:28 +00:00
|
|
|
'can not ask for authentication in non interactive mode ('.$output.')'
|
2012-03-24 23:29:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-03-24 23:30:59 +00:00
|
|
|
// TODO keep a count of user auth attempts and ask 5 times before
|
|
|
|
// failing hard (currently it fails hard directly if the URL has credentials)
|
|
|
|
|
2012-03-24 23:29:14 +00:00
|
|
|
// try to authenticate
|
|
|
|
if (!$this->hasAuth()) {
|
|
|
|
$this->doAuthDance();
|
|
|
|
|
|
|
|
// restart the process
|
2012-04-04 07:36:04 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-03-22 17:40:18 +00:00
|
|
|
/**
|
|
|
|
* Repositories requests credentials, let's put them in.
|
|
|
|
*
|
|
|
|
* @return \Composer\Util\Svn
|
|
|
|
*/
|
2012-03-24 23:29:14 +00:00
|
|
|
protected function doAuthDance()
|
2012-03-22 17:40:18 +00:00
|
|
|
{
|
|
|
|
$this->io->write("The Subversion server ({$this->url}) requested credentials:");
|
|
|
|
|
2012-03-24 22:05:13 +00:00
|
|
|
$this->hasAuth = true;
|
|
|
|
$this->credentials['username'] = $this->io->ask("Username: ");
|
|
|
|
$this->credentials['password'] = $this->io->askAndHideAnswer("Password: ");
|
2012-03-22 17:40:18 +00:00
|
|
|
|
2012-03-24 22:08:43 +00:00
|
|
|
$this->cacheCredentials = $this->io->askConfirmation("Should Subversion cache these credentials? (yes/no) ", true);
|
2012-03-22 17:40:18 +00:00
|
|
|
|
|
|
|
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(),
|
|
|
|
escapeshellarg($url)
|
|
|
|
);
|
2012-03-24 22:05:13 +00:00
|
|
|
|
|
|
|
if ($path) {
|
2012-03-22 16:18:24 +00:00
|
|
|
$cmd .= ' ' . escapeshellarg($path);
|
|
|
|
}
|
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
|
|
|
{
|
2012-03-24 22:05:13 +00:00
|
|
|
if (!$this->hasAuth()) {
|
2012-03-22 16:18:24 +00:00
|
|
|
return '';
|
|
|
|
}
|
2012-03-24 22:05:13 +00:00
|
|
|
|
2012-03-22 16:18:24 +00:00
|
|
|
return sprintf(
|
|
|
|
' %s--username %s --password %s ',
|
|
|
|
$this->getAuthCache(),
|
|
|
|
escapeshellarg($this->getUsername()),
|
|
|
|
escapeshellarg($this->getPassword())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2012-03-24 22:05:13 +00:00
|
|
|
throw new \LogicException("No svn auth detected.");
|
2012-03-22 16:18:24 +00:00
|
|
|
}
|
2012-03-24 22:05:13 +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) {
|
2012-03-24 22:05:13 +00:00
|
|
|
throw new \LogicException("No svn auth detected.");
|
2012-03-22 16:18:24 +00:00
|
|
|
}
|
2012-03-24 22:05:13 +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
|
|
|
{
|
2012-03-24 22:05:13 +00:00
|
|
|
if (null !== $this->hasAuth) {
|
2012-03-22 16:18:24 +00:00
|
|
|
return $this->hasAuth;
|
|
|
|
}
|
|
|
|
|
|
|
|
$uri = parse_url($this->url);
|
|
|
|
if (empty($uri['user'])) {
|
2012-03-24 22:05:13 +00:00
|
|
|
return $this->hasAuth = false;
|
2012-03-22 16:18:24 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 22:05:13 +00:00
|
|
|
$this->credentials['username'] = $uri['user'];
|
2012-03-22 16:18:24 +00:00
|
|
|
if (!empty($uri['pass'])) {
|
2012-03-24 22:05:13 +00:00
|
|
|
$this->credentials['password'] = $uri['pass'];
|
2012-03-22 16:18:24 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 22:05:13 +00:00
|
|
|
return $this->hasAuth = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
2012-05-22 10:07:08 +00:00
|
|
|
}
|