Svn.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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\IO\IOInterface;
  13. /**
  14. * @author Till Klampaeckel <till@php.net>
  15. */
  16. class Svn
  17. {
  18. /**
  19. * @var mixed
  20. */
  21. protected $credentials;
  22. /**
  23. * @var bool
  24. */
  25. protected $hasAuth;
  26. /**
  27. * @var \Composer\IO\IOInterface
  28. */
  29. protected $io;
  30. /**
  31. * @var string
  32. */
  33. protected $url;
  34. /**
  35. * Cache credentials.
  36. * @var bool
  37. */
  38. protected $useCache = false;
  39. /**
  40. * @param string $url
  41. * @param \Composer\IO\IOInterface $io
  42. *
  43. * @return \Composer\Util\Svn
  44. */
  45. public function __construct($url, IOInterface $io)
  46. {
  47. $this->url = $url;
  48. $this->io = $io;
  49. }
  50. /**
  51. * Repositories requests credentials, let's put them in.
  52. *
  53. * @return \Composer\Util\Svn
  54. */
  55. public function doAuthDance()
  56. {
  57. $this->io->write("The Subversion server ({$this->url}) requested credentials:");
  58. $this->hasAuth = true;
  59. $this->credentials = new \stdClass();
  60. $this->credentials->username = $this->io->ask("Username: ");
  61. $this->credentials->password = $this->io->askAndHideAnswer("Password: ");
  62. $pleaseCache = $this->io->askConfirmation("Should Subversion cache these credentials? (yes/no) ", false);
  63. if ($pleaseCache) {
  64. $this->useCache = true;
  65. }
  66. return $this;
  67. }
  68. /**
  69. * Return the no-auth-cache switch.
  70. *
  71. * @return string
  72. */
  73. public function getAuthCache()
  74. {
  75. if (!$this->useCache) {
  76. return '--no-auth-cache ';
  77. }
  78. return '';
  79. }
  80. /**
  81. * A method to create the svn commands run.
  82. *
  83. * @param string $cmd Usually 'svn ls' or something like that.
  84. * @param string $url Repo URL.
  85. * @param string $path The path to run this against (e.g. a 'co' into)
  86. * @param mixed $pipe Optional pipe for the output.
  87. *
  88. * @return string
  89. */
  90. public function getCommand($cmd, $url, $path = '', $pipe = null)
  91. {
  92. $cmd = sprintf('%s %s%s %s',
  93. $cmd,
  94. '--non-interactive ',
  95. $this->getCredentialString(),
  96. escapeshellarg($url)
  97. );
  98. if (!empty($path)) {
  99. $cmd .= ' ' . escapeshellarg($path);
  100. }
  101. if ($pipe !== null) {
  102. $cmd .= ' ' . $pipe;
  103. }
  104. return $cmd;
  105. }
  106. /**
  107. * Return the credential string for the svn command.
  108. *
  109. * Adds --no-auth-cache when credentials are present.
  110. *
  111. * @return string
  112. * @uses self::$useAuth
  113. */
  114. public function getCredentialString()
  115. {
  116. if ($this->hasAuth === null) {
  117. $this->hasAuth();
  118. }
  119. if (!$this->hasAuth) {
  120. return '';
  121. }
  122. return sprintf(
  123. ' %s--username %s --password %s ',
  124. $this->getAuthCache(),
  125. escapeshellarg($this->getUsername()),
  126. escapeshellarg($this->getPassword())
  127. );
  128. }
  129. /**
  130. * Get the password for the svn command. Can be empty.
  131. *
  132. * @return string
  133. * @throws \LogicException
  134. */
  135. public function getPassword()
  136. {
  137. if ($this->credentials === null) {
  138. throw new \LogicException("No auth detected.");
  139. }
  140. if (isset($this->credentials->password)) {
  141. return $this->credentials->password;
  142. }
  143. return ''; // could be empty
  144. }
  145. /**
  146. * Get the username for the svn command.
  147. *
  148. * @return string
  149. * @throws \LogicException
  150. */
  151. public function getUsername()
  152. {
  153. if ($this->credentials === null) {
  154. throw new \LogicException("No auth detected.");
  155. }
  156. return $this->credentials->username;
  157. }
  158. /**
  159. * Detect Svn Auth.
  160. *
  161. * @param string $url
  162. *
  163. * @return \stdClass
  164. */
  165. public function hasAuth()
  166. {
  167. if ($this->hasAuth !== null) {
  168. return $this->hasAuth;
  169. }
  170. $uri = parse_url($this->url);
  171. if (empty($uri['user'])) {
  172. $this->hasAuth = false;
  173. return $this->hasAuth;
  174. }
  175. $this->hasAuth = true;
  176. $this->credentials = new \stdClass();
  177. $this->credentials->username = $uri['user'];
  178. if (!empty($uri['pass'])) {
  179. $this->credentials->password = $uri['pass'];
  180. }
  181. return $this->hasAuth;
  182. }
  183. }