RemoteFilesystem.php 6.8 KB

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