VcsDriver.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\Repository\Vcs;
  12. use Composer\IO\IOInterface;
  13. use Composer\Util\ProcessExecutor;
  14. /**
  15. * A driver implementation for driver with authorization interaction.
  16. *
  17. * @author François Pluchino <francois.pluchino@opendisplay.com>
  18. */
  19. abstract class VcsDriver
  20. {
  21. protected $url;
  22. protected $io;
  23. protected $process;
  24. private $firstCall;
  25. private $contentUrl;
  26. private $content;
  27. /**
  28. * Constructor.
  29. *
  30. * @param string $url The URL
  31. * @param IOInterface $io The IO instance
  32. * @param ProcessExecutor $process Process instance, injectable for mocking
  33. */
  34. public function __construct($url, IOInterface $io, ProcessExecutor $process = null)
  35. {
  36. $this->url = $url;
  37. $this->io = $io;
  38. $this->process = $process ?: new ProcessExecutor;
  39. $this->firstCall = true;
  40. }
  41. /**
  42. * Get the https or http protocol depending on SSL support.
  43. *
  44. * Call this only if you know that the server supports both.
  45. *
  46. * @return string The correct type of protocol
  47. */
  48. protected function getScheme()
  49. {
  50. if (extension_loaded('openssl')) {
  51. return 'https';
  52. }
  53. return 'http';
  54. }
  55. /**
  56. * Get the remote content.
  57. *
  58. * @param string $url The URL of content
  59. *
  60. * @return mixed The result
  61. */
  62. protected function getContents($url)
  63. {
  64. $this->contentUrl = $url;
  65. $auth = $this->io->getAuthorization($this->url);
  66. $params = array();
  67. // add authorization to curl options
  68. if ($this->io->hasAuthorization($this->url)) {
  69. $authStr = base64_encode($auth['username'] . ':' . $auth['password']);
  70. $params['http'] = array('header' => "Authorization: Basic $authStr\r\n");
  71. } else if (null !== $this->io->getLastUsername()) {
  72. $authStr = base64_encode($this->io->getLastUsername() . ':' . $this->io->getLastPassword());
  73. $params['http'] = array('header' => "Authorization: Basic $authStr\r\n");
  74. $this->io->setAuthorization($this->url, $this->io->getLastUsername(), $this->io->getLastPassword());
  75. }
  76. $ctx = stream_context_create($params);
  77. stream_context_set_params($ctx, array("notification" => array($this, 'callbackGet')));
  78. $content = @file_get_contents($url, false, $ctx);
  79. // content get after authorization
  80. if (false === $content) {
  81. $content = $this->content;
  82. $this->content = null;
  83. $this->contentUrl = null;
  84. }
  85. return $content;
  86. }
  87. /**
  88. * Get notification action.
  89. *
  90. * @param integer $notificationCode The notification code
  91. * @param integer $severity The severity level
  92. * @param string $message The message
  93. * @param integer $messageCode The message code
  94. * @param integer $bytesTransferred The loaded size
  95. * @param integer $bytesMax The total size
  96. */
  97. protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  98. {
  99. switch ($notificationCode) {
  100. case STREAM_NOTIFY_AUTH_REQUIRED:
  101. case STREAM_NOTIFY_FAILURE:
  102. // for private repository returning 404 error when the authorization is incorrect
  103. $auth = $this->io->getAuthorization($this->url);
  104. $ps = $this->firstCall && 404 === $messageCode
  105. && null === $this->io->getLastUsername()
  106. && null === $auth['username'];
  107. if (404 === $messageCode && !$this->firstCall) {
  108. throw new \RuntimeException("The '" . $this->contentUrl . "' URL not found");
  109. }
  110. $this->firstCall = false;
  111. // get authorization informations
  112. if (401 === $messageCode || $ps) {
  113. if (!$this->io->isInteractive()) {
  114. $mess = "The '" . $this->contentUrl . "' URL not found";
  115. if (401 === $code || $ps) {
  116. $mess = "The '" . $this->contentUrl . "' URL required the authorization.\nYou must be used the interactive console";
  117. }
  118. throw new \RuntimeException($mess);
  119. }
  120. $this->io->write("Authorization for <info>" . $this->contentUrl . "</info>:");
  121. $username = $this->io->ask(' Username: ');
  122. $password = $this->io->askAndHideAnswer(' Password: ');
  123. $this->io->setAuthorization($this->url, $username, $password);
  124. $this->content = $this->getContents($this->contentUrl);
  125. }
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. }