RemoteFilesystem.php 6.9 KB

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