RemoteFilesystem.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. *
  74. * @throws TransportException When the file could not be downloaded
  75. */
  76. protected function get($originUrl, $fileUrl, $fileName = null, $progress = true)
  77. {
  78. $this->bytesMax = 0;
  79. $this->result = null;
  80. $this->originUrl = $originUrl;
  81. $this->fileUrl = $fileUrl;
  82. $this->fileName = $fileName;
  83. $this->progress = $progress;
  84. $this->lastProgress = null;
  85. $options = $this->getOptionsForUrl($originUrl);
  86. $ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet')));
  87. if ($this->progress) {
  88. $this->io->write(" Downloading: <comment>connection...</comment>", false);
  89. }
  90. if (null !== $fileName) {
  91. $result = @copy($fileUrl, $fileName, $ctx);
  92. } else {
  93. $result = @file_get_contents($fileUrl, false, $ctx);
  94. }
  95. // fix for 5.4.0 https://bugs.php.net/bug.php?id=61336
  96. if (!empty($http_response_header[0]) && preg_match('{^HTTP/\S+ 404}i', $http_response_header[0])) {
  97. $result = false;
  98. }
  99. // avoid overriding if content was loaded by a sub-call to get()
  100. if (null === $this->result) {
  101. $this->result = $result;
  102. }
  103. if ($this->progress) {
  104. $this->io->overwrite(" Downloading", false);
  105. }
  106. if (false === $this->result) {
  107. throw new TransportException("The '$fileUrl' file could not be downloaded");
  108. }
  109. }
  110. /**
  111. * Get notification action.
  112. *
  113. * @param integer $notificationCode The notification code
  114. * @param integer $severity The severity level
  115. * @param string $message The message
  116. * @param integer $messageCode The message code
  117. * @param integer $bytesTransferred The loaded size
  118. * @param integer $bytesMax The total size
  119. */
  120. protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  121. {
  122. switch ($notificationCode) {
  123. case STREAM_NOTIFY_FAILURE:
  124. throw new TransportException(trim($message), $messageCode);
  125. break;
  126. case STREAM_NOTIFY_AUTH_REQUIRED:
  127. if (401 === $messageCode) {
  128. if (!$this->io->isInteractive()) {
  129. $message = "The '" . $this->fileUrl . "' URL required authentication.\nYou must be using the interactive console";
  130. throw new TransportException($message, 401);
  131. }
  132. $this->io->overwrite(' Authentication required (<info>'.parse_url($this->fileUrl, PHP_URL_HOST).'</info>):');
  133. $username = $this->io->ask(' Username: ');
  134. $password = $this->io->askAndHideAnswer(' Password: ');
  135. $this->io->setAuthorization($this->originUrl, $username, $password);
  136. $this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progress);
  137. }
  138. break;
  139. case STREAM_NOTIFY_FILE_SIZE_IS:
  140. if ($this->bytesMax < $bytesMax) {
  141. $this->bytesMax = $bytesMax;
  142. }
  143. break;
  144. case STREAM_NOTIFY_PROGRESS:
  145. if ($this->bytesMax > 0 && $this->progress) {
  146. $progression = 0;
  147. if ($this->bytesMax > 0) {
  148. $progression = round($bytesTransferred / $this->bytesMax * 100);
  149. }
  150. if ((0 === $progression % 5) && $progression !== $this->lastProgress) {
  151. $this->lastProgress = $progression;
  152. $this->io->overwrite(" Downloading: <comment>$progression%</comment>", false);
  153. }
  154. }
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. protected function getOptionsForUrl($url)
  161. {
  162. $options = array();
  163. if ($this->io->hasAuthorization($url)) {
  164. $auth = $this->io->getAuthorization($url);
  165. $authStr = base64_encode($auth['username'] . ':' . $auth['password']);
  166. $options['http'] = array('header' => "Authorization: Basic $authStr\r\n");
  167. } elseif (null !== $this->io->getLastUsername()) {
  168. $authStr = base64_encode($this->io->getLastUsername() . ':' . $this->io->getLastPassword());
  169. $options['http'] = array('header' => "Authorization: Basic $authStr\r\n");
  170. $this->io->setAuthorization($url, $this->io->getLastUsername(), $this->io->getLastPassword());
  171. }
  172. return $options;
  173. }
  174. }