RemoteFilesystem.php 11 KB

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