RemoteFilesystem.php 7.0 KB

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