/usr/share/php/Composer/Util/Svn.php is in composer 1.0.0~beta2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | <?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\Config;
use Composer\Downloader\TransportException;
use Composer\IO\IOInterface;
/**
* @author Till Klampaeckel <till@php.net>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class Svn
{
const MAX_QTY_AUTH_TRIES = 5;
/**
* @var array
*/
protected $credentials;
/**
* @var bool
*/
protected $hasAuth;
/**
* @var \Composer\IO\IOInterface
*/
protected $io;
/**
* @var string
*/
protected $url;
/**
* @var bool
*/
protected $cacheCredentials = true;
/**
* @var ProcessExecutor
*/
protected $process;
/**
* @var int
*/
protected $qtyAuthTries = 0;
/**
* @var \Composer\Config
*/
protected $config;
/**
* @param string $url
* @param \Composer\IO\IOInterface $io
* @param Config $config
* @param ProcessExecutor $process
*/
public function __construct($url, IOInterface $io, Config $config, ProcessExecutor $process = null)
{
$this->url = $url;
$this->io = $io;
$this->config = $config;
$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']);
}
/**
* Execute an SVN command and try to fix up the process with credentials
* if necessary.
*
* @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
*
* @throws \RuntimeException
* @return string
*/
public function execute($command, $url, $cwd = null, $path = null, $verbose = false)
{
if (preg_match('{^(http|svn):}i', $url) && $this->config->get('secure-http')) {
throw new TransportException("Your configuration does not allow connection to $url. See https://getcomposer.org/doc/06-config.md#secure-http for details.");
}
$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);
if (0 === $status) {
return $output;
}
$errorOutput = $this->process->getErrorOutput();
$fullOutput = implode("\n", array($output, $errorOutput));
// 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);
}
if (!$this->hasAuth()) {
$this->doAuthDance();
}
// try to authenticate if maximum quantity of tries not reached
if ($this->qtyAuthTries++ < self::MAX_QTY_AUTH_TRIES) {
// restart the process
return $this->execute($command, $url, $cwd, $path, $verbose);
}
throw new \RuntimeException(
'wrong credentials provided ('.$fullOutput.')'
);
}
/**
* @param bool $cacheCredentials
*/
public function setCacheCredentials($cacheCredentials)
{
$this->cacheCredentials = $cacheCredentials;
}
/**
* Repositories requests credentials, let's put them in.
*
* @throws \RuntimeException
* @return \Composer\Util\Svn
*/
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;
}
/**
* A method to create the svn commands run.
*
* @param string $cmd Usually 'svn ls' or something like that.
* @param string $url Repo URL.
* @param string $path Target for a checkout
*
* @return string
*/
protected function getCommand($cmd, $url, $path = null)
{
$cmd = sprintf('%s %s%s %s',
$cmd,
'--non-interactive ',
$this->getCredentialString(),
ProcessExecutor::escape($url)
);
if ($path) {
$cmd .= ' ' . ProcessExecutor::escape($path);
}
return $cmd;
}
/**
* Return the credential string for the svn command.
*
* Adds --no-auth-cache when credentials are present.
*
* @return string
*/
protected function getCredentialString()
{
if (!$this->hasAuth()) {
return '';
}
return sprintf(
' %s--username %s --password %s ',
$this->getAuthCache(),
ProcessExecutor::escape($this->getUsername()),
ProcessExecutor::escape($this->getPassword())
);
}
/**
* Get the password for the svn command. Can be empty.
*
* @throws \LogicException
* @return string
*/
protected function getPassword()
{
if ($this->credentials === null) {
throw new \LogicException("No svn auth detected.");
}
return isset($this->credentials['password']) ? $this->credentials['password'] : '';
}
/**
* Get the username for the svn command.
*
* @throws \LogicException
* @return string
*/
protected function getUsername()
{
if ($this->credentials === null) {
throw new \LogicException("No svn auth detected.");
}
return $this->credentials['username'];
}
/**
* Detect Svn Auth.
*
* @return bool
*/
protected function hasAuth()
{
if (null !== $this->hasAuth) {
return $this->hasAuth;
}
if (false === $this->createAuthFromConfig()) {
$this->createAuthFromUrl();
}
return $this->hasAuth;
}
/**
* Return the no-auth-cache switch.
*
* @return string
*/
protected function getAuthCache()
{
return $this->cacheCredentials ? '' : '--no-auth-cache ';
}
/**
* Create the auth params from the configuration file.
*
* @return bool
*/
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
*/
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;
}
}
|