RemoteFilesystem.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 François Pluchino <francois.pluchino@opendisplay.com>
  15. */
  16. class RemoteFilesystem
  17. {
  18. private $io;
  19. private $firstCall;
  20. private $bytesMax;
  21. private $originUrl;
  22. private $fileUrl;
  23. private $fileName;
  24. private $content;
  25. private $progess;
  26. private $lastProgress;
  27. /**
  28. * Constructor.
  29. *
  30. * @param IOInterface $io The IO instance
  31. */
  32. public function __construct(IOInterface $io)
  33. {
  34. $this->io = $io;
  35. }
  36. /**
  37. * Copy the remote file in local.
  38. *
  39. * @param string $originUrl The orgin URL
  40. * @param string $fileUrl The file URL
  41. * @param string $fileName the local filename
  42. * @param boolean $progess Display the progression
  43. */
  44. public function copy($originUrl, $fileUrl, $fileName, $progess = true)
  45. {
  46. $this->get($originUrl, $fileUrl, $fileName, $progess);
  47. }
  48. /**
  49. * Get the content.
  50. *
  51. * @param string $originUrl The orgin URL
  52. * @param string $fileUrl The file URL
  53. * @param boolean $progess Display the progression
  54. *
  55. * @return string The content
  56. */
  57. public function getContents($originUrl, $fileUrl, $progess = true)
  58. {
  59. $this->get($originUrl, $fileUrl, null, $progess);
  60. return $this->content;
  61. }
  62. /**
  63. * Get file content or copy action.
  64. *
  65. * @param string $originUrl The orgin URL
  66. * @param string $fileUrl The file URL
  67. * @param string $fileName the local filename
  68. * @param boolean $progess Display the progression
  69. * @param boolean $firstCall Whether this is the first attempt at fetching this resource
  70. *
  71. * @throws \RuntimeException When the file could not be downloaded
  72. */
  73. protected function get($originUrl, $fileUrl, $fileName = null, $progess = true, $firstCall = true)
  74. {
  75. $this->firstCall = $firstCall;
  76. $this->bytesMax = 0;
  77. $this->content = null;
  78. $this->originUrl = $originUrl;
  79. $this->fileUrl = $fileUrl;
  80. $this->fileName = $fileName;
  81. $this->progress = $progess;
  82. $this->lastProgress = null;
  83. // add authorization in context
  84. $options = array();
  85. if ($this->io->hasAuthorization($originUrl)) {
  86. $auth = $this->io->getAuthorization($originUrl);
  87. $authStr = base64_encode($auth['username'] . ':' . $auth['password']);
  88. $options['http']['header'] = "Authorization: Basic $authStr\r\n";
  89. } elseif (null !== $this->io->getLastUsername()) {
  90. $authStr = base64_encode($this->io->getLastUsername() . ':' . $this->io->getLastPassword());
  91. $options['http'] = array('header' => "Authorization: Basic $authStr\r\n");
  92. $this->io->setAuthorization($originUrl, $this->io->getLastUsername(), $this->io->getLastPassword());
  93. }
  94. $ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet')));
  95. if ($this->progress) {
  96. $this->io->overwrite(" Downloading: <comment>connection...</comment>", false);
  97. }
  98. if (null !== $fileName) {
  99. $result = @copy($fileUrl, $fileName, $ctx);
  100. } else {
  101. $result = @file_get_contents($fileUrl, false, $ctx);
  102. $this->content = $result;
  103. }
  104. if ($this->progress) {
  105. $this->io->overwrite(" Downloading", false);
  106. }
  107. if (false === $result) {
  108. throw new \RuntimeException("The '$fileUrl' file could not be downloaded");
  109. }
  110. }
  111. /**
  112. * Get notification action.
  113. *
  114. * @param integer $notificationCode The notification code
  115. * @param integer $severity The severity level
  116. * @param string $message The message
  117. * @param integer $messageCode The message code
  118. * @param integer $bytesTransferred The loaded size
  119. * @param integer $bytesMax The total size
  120. */
  121. protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  122. {
  123. switch ($notificationCode) {
  124. case STREAM_NOTIFY_AUTH_REQUIRED:
  125. case STREAM_NOTIFY_FAILURE:
  126. // for private repository returning 404 error when the authorization is incorrect
  127. $auth = $this->io->getAuthorization($this->originUrl);
  128. $attemptAuthentication = $this->firstCall && 404 === $messageCode && null === $auth['username'];
  129. if (404 === $messageCode && !$this->firstCall) {
  130. throw new \RuntimeException("The '" . $this->fileUrl . "' URL not found");
  131. }
  132. $this->firstCall = false;
  133. // get authorization informations
  134. if (401 === $messageCode || $attemptAuthentication) {
  135. if (!$this->io->isInteractive()) {
  136. $mess = "The '" . $this->fileUrl . "' URL was not found";
  137. if (401 === $code || $attemptAuthentication) {
  138. $mess = "The '" . $this->fileUrl . "' URL required authentication.\nYou must be using the interactive console";
  139. }
  140. throw new \RuntimeException($mess);
  141. }
  142. $this->io->overwrite(' Authentication required (<info>'.parse_url($this->fileUrl, PHP_URL_HOST).'</info>):');
  143. $username = $this->io->ask(' Username: ');
  144. $password = $this->io->askAndHideAnswer(' Password: ');
  145. $this->io->setAuthorization($this->originUrl, $username, $password);
  146. $this->content = $this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progress, false);
  147. }
  148. break;
  149. case STREAM_NOTIFY_FILE_SIZE_IS:
  150. if ($this->bytesMax < $bytesMax) {
  151. $this->bytesMax = $bytesMax;
  152. }
  153. break;
  154. case STREAM_NOTIFY_PROGRESS:
  155. if ($this->bytesMax > 0 && $this->progress) {
  156. $progression = 0;
  157. if ($this->bytesMax > 0) {
  158. $progression = round($bytesTransferred / $this->bytesMax * 100);
  159. }
  160. if ((0 === $progression % 5) && $progression !== $this->lastProgress) {
  161. $this->lastProgress = $progression;
  162. $this->io->overwrite(" Downloading: <comment>$progression%</comment>", false);
  163. }
  164. }
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. }