RemoteFilesystem.php 7.2 KB

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