Svn.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Util;
  12. use Composer\Config;
  13. use Composer\IO\IOInterface;
  14. /**
  15. * @author Till Klampaeckel <till@php.net>
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. class Svn
  19. {
  20. const MAX_QTY_AUTH_TRIES = 5;
  21. /**
  22. * @var array
  23. */
  24. protected $credentials;
  25. /**
  26. * @var bool
  27. */
  28. protected $hasAuth;
  29. /**
  30. * @var \Composer\IO\IOInterface
  31. */
  32. protected $io;
  33. /**
  34. * @var string
  35. */
  36. protected $url;
  37. /**
  38. * @var bool
  39. */
  40. protected $cacheCredentials = true;
  41. /**
  42. * @var ProcessExecutor
  43. */
  44. protected $process;
  45. /**
  46. * @var integer
  47. */
  48. protected $qtyAuthTries = 0;
  49. /**
  50. * @var \Composer\Config
  51. */
  52. protected $config;
  53. /**
  54. * @param string $url
  55. * @param \Composer\IO\IOInterface $io
  56. * @param Config $config
  57. * @param ProcessExecutor $process
  58. */
  59. public function __construct($url, IOInterface $io, Config $config, ProcessExecutor $process = null)
  60. {
  61. $this->url = $url;
  62. $this->io = $io;
  63. $this->config = $config;
  64. $this->process = $process ?: new ProcessExecutor;
  65. }
  66. public static function cleanEnv()
  67. {
  68. // clean up env for OSX, see https://github.com/composer/composer/issues/2146#issuecomment-35478940
  69. putenv("DYLD_LIBRARY_PATH");
  70. }
  71. /**
  72. * Execute an SVN command and try to fix up the process with credentials
  73. * if necessary.
  74. *
  75. * @param string $command SVN command to run
  76. * @param string $url SVN url
  77. * @param string $cwd Working directory
  78. * @param string $path Target for a checkout
  79. * @param bool $verbose Output all output to the user
  80. *
  81. * @return string
  82. *
  83. * @throws \RuntimeException
  84. */
  85. public function execute($command, $url, $cwd = null, $path = null, $verbose = false)
  86. {
  87. $svnCommand = $this->getCommand($command, $url, $path);
  88. $output = null;
  89. $io = $this->io;
  90. $handler = function ($type, $buffer) use (&$output, $io, $verbose) {
  91. if ($type !== 'out') {
  92. return;
  93. }
  94. if ('Redirecting to URL ' === substr($buffer, 0, 19)) {
  95. return;
  96. }
  97. $output .= $buffer;
  98. if ($verbose) {
  99. $io->write($buffer, false);
  100. }
  101. };
  102. $status = $this->process->execute($svnCommand, $handler, $cwd);
  103. if (0 === $status) {
  104. return $output;
  105. }
  106. if (empty($output)) {
  107. $output = $this->process->getErrorOutput();
  108. }
  109. // the error is not auth-related
  110. if (false === stripos($output, 'Could not authenticate to server:')
  111. && false === stripos($output, 'authorization failed')
  112. && false === stripos($output, 'svn: E170001:')
  113. && false === stripos($output, 'svn: E215004:')) {
  114. throw new \RuntimeException($output);
  115. }
  116. if (!$this->hasAuth()) {
  117. $this->doAuthDance();
  118. }
  119. // try to authenticate if maximum quantity of tries not reached
  120. if ($this->qtyAuthTries++ < self::MAX_QTY_AUTH_TRIES) {
  121. // restart the process
  122. return $this->execute($command, $url, $cwd, $path, $verbose);
  123. }
  124. throw new \RuntimeException(
  125. 'wrong credentials provided ('.$output.')'
  126. );
  127. }
  128. /**
  129. * Repositories requests credentials, let's put them in.
  130. *
  131. * @return \Composer\Util\Svn
  132. * @throws \RuntimeException
  133. */
  134. protected function doAuthDance()
  135. {
  136. // cannot ask for credentials in non interactive mode
  137. if (!$this->io->isInteractive()) {
  138. throw new \RuntimeException(
  139. 'can not ask for authentication in non interactive mode'
  140. );
  141. }
  142. $this->io->write("The Subversion server ({$this->url}) requested credentials:");
  143. $this->hasAuth = true;
  144. $this->credentials['username'] = $this->io->ask("Username: ");
  145. $this->credentials['password'] = $this->io->askAndHideAnswer("Password: ");
  146. $this->cacheCredentials = $this->io->askConfirmation("Should Subversion cache these credentials? (yes/no) ", true);
  147. return $this;
  148. }
  149. /**
  150. * A method to create the svn commands run.
  151. *
  152. * @param string $cmd Usually 'svn ls' or something like that.
  153. * @param string $url Repo URL.
  154. * @param string $path Target for a checkout
  155. *
  156. * @return string
  157. */
  158. protected function getCommand($cmd, $url, $path = null)
  159. {
  160. $cmd = sprintf('%s %s%s %s',
  161. $cmd,
  162. '--non-interactive ',
  163. $this->getCredentialString(),
  164. ProcessUtil::escapeArgument($url)
  165. );
  166. if ($path) {
  167. $cmd .= ' ' . ProcessUtil::escapeArgument($path);
  168. }
  169. return $cmd;
  170. }
  171. /**
  172. * Return the credential string for the svn command.
  173. *
  174. * Adds --no-auth-cache when credentials are present.
  175. *
  176. * @return string
  177. */
  178. protected function getCredentialString()
  179. {
  180. if (!$this->hasAuth()) {
  181. return '';
  182. }
  183. return sprintf(
  184. ' %s--username %s --password %s ',
  185. $this->getAuthCache(),
  186. ProcessUtil::escapeArgument($this->getUsername()),
  187. ProcessUtil::escapeArgument($this->getPassword())
  188. );
  189. }
  190. /**
  191. * Get the password for the svn command. Can be empty.
  192. *
  193. * @return string
  194. * @throws \LogicException
  195. */
  196. protected function getPassword()
  197. {
  198. if ($this->credentials === null) {
  199. throw new \LogicException("No svn auth detected.");
  200. }
  201. return isset($this->credentials['password']) ? $this->credentials['password'] : '';
  202. }
  203. /**
  204. * Get the username for the svn command.
  205. *
  206. * @return string
  207. * @throws \LogicException
  208. */
  209. protected function getUsername()
  210. {
  211. if ($this->credentials === null) {
  212. throw new \LogicException("No svn auth detected.");
  213. }
  214. return $this->credentials['username'];
  215. }
  216. /**
  217. * Detect Svn Auth.
  218. *
  219. * @return bool
  220. */
  221. protected function hasAuth()
  222. {
  223. if (null !== $this->hasAuth) {
  224. return $this->hasAuth;
  225. }
  226. if (false === $this->createAuthFromConfig()) {
  227. $this->createAuthFromUrl();
  228. }
  229. return $this->hasAuth;
  230. }
  231. /**
  232. * Return the no-auth-cache switch.
  233. *
  234. * @return string
  235. */
  236. protected function getAuthCache()
  237. {
  238. return $this->cacheCredentials ? '' : '--no-auth-cache ';
  239. }
  240. /**
  241. * Create the auth params from the configuration file.
  242. *
  243. * @return bool
  244. */
  245. private function createAuthFromConfig()
  246. {
  247. if (!$this->config->has('http-basic')) {
  248. return $this->hasAuth = false;
  249. }
  250. $authConfig = $this->config->get('http-basic');
  251. $host = parse_url($this->url, PHP_URL_HOST);
  252. if (isset($authConfig[$host])) {
  253. $this->credentials['username'] = $authConfig[$host]['username'];
  254. $this->credentials['password'] = $authConfig[$host]['password'];
  255. return $this->hasAuth = true;
  256. }
  257. return $this->hasAuth = false;
  258. }
  259. /**
  260. * Create the auth params from the url
  261. *
  262. * @return bool
  263. */
  264. private function createAuthFromUrl()
  265. {
  266. $uri = parse_url($this->url);
  267. if (empty($uri['user'])) {
  268. return $this->hasAuth = false;
  269. }
  270. $this->credentials['username'] = $uri['user'];
  271. if (!empty($uri['pass'])) {
  272. $this->credentials['password'] = $uri['pass'];
  273. }
  274. return $this->hasAuth = true;
  275. }
  276. }