RemoteFilesystem.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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\Composer;
  13. use Composer\IO\IOInterface;
  14. use Composer\Downloader\TransportException;
  15. /**
  16. * @author François Pluchino <francois.pluchino@opendisplay.com>
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. class RemoteFilesystem
  20. {
  21. private $io;
  22. private $firstCall;
  23. private $bytesMax;
  24. private $originUrl;
  25. private $fileUrl;
  26. private $fileName;
  27. private $retry;
  28. private $progress;
  29. private $lastProgress;
  30. private $options;
  31. /**
  32. * Constructor.
  33. *
  34. * @param IOInterface $io The IO instance
  35. * @param array $options The options
  36. */
  37. public function __construct(IOInterface $io, $options = array())
  38. {
  39. $this->io = $io;
  40. $this->options = $options;
  41. }
  42. /**
  43. * Copy the remote file in local.
  44. *
  45. * @param string $originUrl The origin URL
  46. * @param string $fileUrl The file URL
  47. * @param string $fileName the local filename
  48. * @param boolean $progress Display the progression
  49. * @param array $options Additional context options
  50. *
  51. * @return bool true
  52. */
  53. public function copy($originUrl, $fileUrl, $fileName, $progress = true, $options = array())
  54. {
  55. return $this->get($originUrl, $fileUrl, $options, $fileName, $progress);
  56. }
  57. /**
  58. * Get the content.
  59. *
  60. * @param string $originUrl The origin URL
  61. * @param string $fileUrl The file URL
  62. * @param boolean $progress Display the progression
  63. * @param array $options Additional context options
  64. *
  65. * @return string The content
  66. */
  67. public function getContents($originUrl, $fileUrl, $progress = true, $options = array())
  68. {
  69. return $this->get($originUrl, $fileUrl, $options, null, $progress);
  70. }
  71. /**
  72. * Get file content or copy action.
  73. *
  74. * @param string $originUrl The origin URL
  75. * @param string $fileUrl The file URL
  76. * @param array $additionalOptions context options
  77. * @param string $fileName the local filename
  78. * @param boolean $progress Display the progression
  79. *
  80. * @throws TransportException|\Exception
  81. * @throws TransportException When the file could not be downloaded
  82. *
  83. * @return bool|string
  84. */
  85. protected function get($originUrl, $fileUrl, $additionalOptions = array(), $fileName = null, $progress = true)
  86. {
  87. $this->bytesMax = 0;
  88. $this->originUrl = $originUrl;
  89. $this->fileUrl = $fileUrl;
  90. $this->fileName = $fileName;
  91. $this->progress = $progress;
  92. $this->lastProgress = null;
  93. $options = $this->getOptionsForUrl($originUrl, $additionalOptions);
  94. if ($this->io->isDebug()) {
  95. $this->io->write('Downloading '.$fileUrl);
  96. }
  97. if (isset($options['github-token'])) {
  98. $fileUrl .= (false === strpos($fileUrl, '?') ? '?' : '&') . 'access_token='.$options['github-token'];
  99. unset($options['github-token']);
  100. }
  101. $ctx = StreamContextFactory::getContext($fileUrl, $options, array('notification' => array($this, 'callbackGet')));
  102. if ($this->progress) {
  103. $this->io->write(" Downloading: <comment>connection...</comment>", false);
  104. }
  105. $errorMessage = '';
  106. $errorCode = 0;
  107. $result = false;
  108. set_error_handler(function ($code, $msg) use (&$errorMessage) {
  109. if ($errorMessage) {
  110. $errorMessage .= "\n";
  111. }
  112. $errorMessage .= preg_replace('{^file_get_contents\(.*?\): }', '', $msg);
  113. });
  114. try {
  115. $result = file_get_contents($fileUrl, false, $ctx);
  116. } catch (\Exception $e) {
  117. if ($e instanceof TransportException && !empty($http_response_header[0])) {
  118. $e->setHeaders($http_response_header);
  119. }
  120. }
  121. if ($errorMessage && !ini_get('allow_url_fopen')) {
  122. $errorMessage = 'allow_url_fopen must be enabled in php.ini ('.$errorMessage.')';
  123. }
  124. restore_error_handler();
  125. if (isset($e) && !$this->retry) {
  126. throw $e;
  127. }
  128. // fix for 5.4.0 https://bugs.php.net/bug.php?id=61336
  129. if (!empty($http_response_header[0]) && preg_match('{^HTTP/\S+ ([45]\d\d)}i', $http_response_header[0], $match)) {
  130. $result = false;
  131. $errorCode = $match[1];
  132. }
  133. // decode gzip
  134. if ($result && extension_loaded('zlib') && substr($fileUrl, 0, 4) === 'http') {
  135. $decode = false;
  136. foreach ($http_response_header as $header) {
  137. if (preg_match('{^content-encoding: *gzip *$}i', $header)) {
  138. $decode = true;
  139. continue;
  140. } elseif (preg_match('{^HTTP/}i', $header)) {
  141. $decode = false;
  142. }
  143. }
  144. if ($decode) {
  145. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  146. $result = zlib_decode($result);
  147. } else {
  148. // work around issue with gzuncompress & co that do not work with all gzip checksums
  149. $result = file_get_contents('compress.zlib://data:application/octet-stream;base64,'.base64_encode($result));
  150. }
  151. }
  152. }
  153. if ($this->progress) {
  154. $this->io->overwrite(" Downloading: <comment>100%</comment>");
  155. }
  156. // handle copy command if download was successful
  157. if (false !== $result && null !== $fileName) {
  158. if ('' === $result) {
  159. throw new TransportException('"'.$this->fileUrl.'" appears broken, and returned an empty 200 response');
  160. }
  161. $errorMessage = '';
  162. set_error_handler(function ($code, $msg) use (&$errorMessage) {
  163. if ($errorMessage) {
  164. $errorMessage .= "\n";
  165. }
  166. $errorMessage .= preg_replace('{^file_put_contents\(.*?\): }', '', $msg);
  167. });
  168. $result = (bool) file_put_contents($fileName, $result);
  169. restore_error_handler();
  170. if (false === $result) {
  171. throw new TransportException('The "'.$this->fileUrl.'" file could not be written to '.$fileName.': '.$errorMessage);
  172. }
  173. }
  174. if ($this->retry) {
  175. $this->retry = false;
  176. return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress);
  177. }
  178. if (false === $result) {
  179. $e = new TransportException('The "'.$this->fileUrl.'" file could not be downloaded: '.$errorMessage, $errorCode);
  180. if (!empty($http_response_header[0])) {
  181. $e->setHeaders($http_response_header);
  182. }
  183. throw $e;
  184. }
  185. return $result;
  186. }
  187. /**
  188. * Get notification action.
  189. *
  190. * @param integer $notificationCode The notification code
  191. * @param integer $severity The severity level
  192. * @param string $message The message
  193. * @param integer $messageCode The message code
  194. * @param integer $bytesTransferred The loaded size
  195. * @param integer $bytesMax The total size
  196. * @throws TransportException
  197. */
  198. protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  199. {
  200. switch ($notificationCode) {
  201. case STREAM_NOTIFY_FAILURE:
  202. case STREAM_NOTIFY_AUTH_REQUIRED:
  203. if (401 === $messageCode) {
  204. if (!$this->io->isInteractive()) {
  205. $message = "The '" . $this->fileUrl . "' URL required authentication.\nYou must be using the interactive console";
  206. throw new TransportException($message, 401);
  207. }
  208. $this->io->overwrite(' Authentication required (<info>'.parse_url($this->fileUrl, PHP_URL_HOST).'</info>):');
  209. $username = $this->io->ask(' Username: ');
  210. $password = $this->io->askAndHideAnswer(' Password: ');
  211. $this->io->setAuthentication($this->originUrl, $username, $password);
  212. $this->retry = true;
  213. throw new TransportException('RETRY');
  214. break;
  215. }
  216. if ($notificationCode === STREAM_NOTIFY_AUTH_REQUIRED) {
  217. break;
  218. }
  219. throw new TransportException('The "'.$this->fileUrl.'" file could not be downloaded ('.trim($message).')', $messageCode);
  220. case STREAM_NOTIFY_AUTH_RESULT:
  221. if (403 === $messageCode) {
  222. $message = "The '" . $this->fileUrl . "' URL could not be accessed: " . $message;
  223. throw new TransportException($message, 403);
  224. }
  225. break;
  226. case STREAM_NOTIFY_FILE_SIZE_IS:
  227. if ($this->bytesMax < $bytesMax) {
  228. $this->bytesMax = $bytesMax;
  229. }
  230. break;
  231. case STREAM_NOTIFY_PROGRESS:
  232. if ($this->bytesMax > 0 && $this->progress) {
  233. $progression = 0;
  234. if ($this->bytesMax > 0) {
  235. $progression = round($bytesTransferred / $this->bytesMax * 100);
  236. }
  237. if ((0 === $progression % 5) && $progression !== $this->lastProgress) {
  238. $this->lastProgress = $progression;
  239. $this->io->overwrite(" Downloading: <comment>$progression%</comment>", false);
  240. }
  241. }
  242. break;
  243. default:
  244. break;
  245. }
  246. }
  247. protected function getOptionsForUrl($originUrl, $additionalOptions)
  248. {
  249. $headers = array(
  250. sprintf(
  251. 'User-Agent: Composer/%s (%s; %s; PHP %s.%s.%s)',
  252. Composer::VERSION === '@package_version@' ? 'source' : Composer::VERSION,
  253. php_uname('s'),
  254. php_uname('r'),
  255. PHP_MAJOR_VERSION,
  256. PHP_MINOR_VERSION,
  257. PHP_RELEASE_VERSION
  258. )
  259. );
  260. if (extension_loaded('zlib')) {
  261. $headers[] = 'Accept-Encoding: gzip';
  262. }
  263. $options = array_replace_recursive($this->options, $additionalOptions);
  264. if ($this->io->hasAuthentication($originUrl)) {
  265. $auth = $this->io->getAuthentication($originUrl);
  266. if ('github.com' === $originUrl && 'x-oauth-basic' === $auth['password']) {
  267. $options['github-token'] = $auth['username'];
  268. } else {
  269. $authStr = base64_encode($auth['username'] . ':' . $auth['password']);
  270. $headers[] = 'Authorization: Basic '.$authStr;
  271. }
  272. }
  273. if (isset($options['http']['header']) && !is_array($options['http']['header'])) {
  274. $options['http']['header'] = explode("\r\n", trim($options['http']['header'], "\r\n"));
  275. }
  276. foreach ($headers as $header) {
  277. $options['http']['header'][] = $header;
  278. }
  279. return $options;
  280. }
  281. }