RemoteFilesystem.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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\Config;
  13. use Composer\IO\IOInterface;
  14. use Composer\Downloader\TransportException;
  15. use Composer\CaBundle\CaBundle;
  16. /**
  17. * @author François Pluchino <francois.pluchino@opendisplay.com>
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. * @author Nils Adermann <naderman@naderman.de>
  20. */
  21. class RemoteFilesystem
  22. {
  23. private $io;
  24. private $config;
  25. private $scheme;
  26. private $bytesMax;
  27. private $originUrl;
  28. private $fileUrl;
  29. private $fileName;
  30. private $retry;
  31. private $progress;
  32. private $lastProgress;
  33. private $options = array();
  34. private $peerCertificateMap = array();
  35. private $disableTls = false;
  36. private $retryAuthFailure;
  37. private $lastHeaders;
  38. private $storeAuth;
  39. private $authHelper;
  40. private $degradedMode = false;
  41. private $redirects;
  42. private $maxRedirects = 20;
  43. /**
  44. * Constructor.
  45. *
  46. * @param IOInterface $io The IO instance
  47. * @param Config $config The config
  48. * @param array $options The options
  49. * @param bool $disableTls
  50. */
  51. public function __construct(IOInterface $io, Config $config, array $options = array(), $disableTls = false)
  52. {
  53. $this->io = $io;
  54. // Setup TLS options
  55. // The cafile option can be set via config.json
  56. if ($disableTls === false) {
  57. $this->options = StreamContextFactory::getTlsDefaults($options, $io);
  58. } else {
  59. $this->disableTls = true;
  60. }
  61. // handle the other externally set options normally.
  62. $this->options = array_replace_recursive($this->options, $options);
  63. $this->config = $config;
  64. $this->authHelper = new AuthHelper($io, $config);
  65. }
  66. /**
  67. * Copy the remote file in local.
  68. *
  69. * @param string $originUrl The origin URL
  70. * @param string $fileUrl The file URL
  71. * @param string $fileName the local filename
  72. * @param bool $progress Display the progression
  73. * @param array $options Additional context options
  74. *
  75. * @return bool true
  76. */
  77. public function copy($originUrl, $fileUrl, $fileName, $progress = true, $options = array())
  78. {
  79. return $this->get($originUrl, $fileUrl, $options, $fileName, $progress);
  80. }
  81. /**
  82. * Get the content.
  83. *
  84. * @param string $originUrl The origin URL
  85. * @param string $fileUrl The file URL
  86. * @param bool $progress Display the progression
  87. * @param array $options Additional context options
  88. *
  89. * @return bool|string The content
  90. */
  91. public function getContents($originUrl, $fileUrl, $progress = true, $options = array())
  92. {
  93. return $this->get($originUrl, $fileUrl, $options, null, $progress);
  94. }
  95. /**
  96. * Retrieve the options set in the constructor
  97. *
  98. * @return array Options
  99. */
  100. public function getOptions()
  101. {
  102. return $this->options;
  103. }
  104. /**
  105. * Merges new options
  106. *
  107. * @param array $options
  108. */
  109. public function setOptions(array $options)
  110. {
  111. $this->options = array_replace_recursive($this->options, $options);
  112. }
  113. /**
  114. * Check is disable TLS.
  115. *
  116. * @return bool
  117. */
  118. public function isTlsDisabled()
  119. {
  120. return $this->disableTls === true;
  121. }
  122. /**
  123. * Returns the headers of the last request
  124. *
  125. * @return array
  126. */
  127. public function getLastHeaders()
  128. {
  129. return $this->lastHeaders;
  130. }
  131. /**
  132. * @param array $headers array of returned headers like from getLastHeaders()
  133. * @param string $name header name (case insensitive)
  134. * @return string|null
  135. */
  136. public static function findHeaderValue(array $headers, $name)
  137. {
  138. $value = null;
  139. foreach ($headers as $header) {
  140. if (preg_match('{^'.$name.':\s*(.+?)\s*$}i', $header, $match)) {
  141. $value = $match[1];
  142. } elseif (preg_match('{^HTTP/}i', $header)) {
  143. // In case of redirects, http_response_headers contains the headers of all responses
  144. // so we reset the flag when a new response is being parsed as we are only interested in the last response
  145. $value = null;
  146. }
  147. }
  148. return $value;
  149. }
  150. /**
  151. * @param array $headers array of returned headers like from getLastHeaders()
  152. * @return int|null
  153. */
  154. public static function findStatusCode(array $headers)
  155. {
  156. $value = null;
  157. foreach ($headers as $header) {
  158. if (preg_match('{^HTTP/\S+ (\d+)}i', $header, $match)) {
  159. // In case of redirects, http_response_headers contains the headers of all responses
  160. // so we can not return directly and need to keep iterating
  161. $value = (int) $match[1];
  162. }
  163. }
  164. return $value;
  165. }
  166. /**
  167. * @param array $headers array of returned headers like from getLastHeaders()
  168. * @return string|null
  169. */
  170. public function findStatusMessage(array $headers)
  171. {
  172. $value = null;
  173. foreach ($headers as $header) {
  174. if (preg_match('{^HTTP/\S+ \d+}i', $header)) {
  175. // In case of redirects, http_response_headers contains the headers of all responses
  176. // so we can not return directly and need to keep iterating
  177. $value = $header;
  178. }
  179. }
  180. return $value;
  181. }
  182. /**
  183. * Get file content or copy action.
  184. *
  185. * @param string $originUrl The origin URL
  186. * @param string $fileUrl The file URL
  187. * @param array $additionalOptions context options
  188. * @param string $fileName the local filename
  189. * @param bool $progress Display the progression
  190. *
  191. * @throws TransportException|\Exception
  192. * @throws TransportException When the file could not be downloaded
  193. *
  194. * @return bool|string
  195. */
  196. protected function get($originUrl, $fileUrl, $additionalOptions = array(), $fileName = null, $progress = true)
  197. {
  198. $this->scheme = parse_url($fileUrl, PHP_URL_SCHEME);
  199. $this->bytesMax = 0;
  200. $this->originUrl = $originUrl;
  201. $this->fileUrl = $fileUrl;
  202. $this->fileName = $fileName;
  203. $this->progress = $progress;
  204. $this->lastProgress = null;
  205. $this->retryAuthFailure = true;
  206. $this->lastHeaders = array();
  207. $this->redirects = 1; // The first request counts.
  208. $tempAdditionalOptions = $additionalOptions;
  209. if (isset($tempAdditionalOptions['retry-auth-failure'])) {
  210. $this->retryAuthFailure = (bool) $tempAdditionalOptions['retry-auth-failure'];
  211. unset($tempAdditionalOptions['retry-auth-failure']);
  212. }
  213. $isRedirect = false;
  214. if (isset($tempAdditionalOptions['redirects'])) {
  215. $this->redirects = $tempAdditionalOptions['redirects'];
  216. $isRedirect = true;
  217. unset($tempAdditionalOptions['redirects']);
  218. }
  219. $options = $this->getOptionsForUrl($originUrl, $tempAdditionalOptions);
  220. unset($tempAdditionalOptions);
  221. $origFileUrl = $fileUrl;
  222. if (isset($options['gitlab-token'])) {
  223. $fileUrl .= (false === strpos($fileUrl, '?') ? '?' : '&') . 'access_token='.$options['gitlab-token'];
  224. unset($options['gitlab-token']);
  225. }
  226. if (isset($options['http'])) {
  227. $options['http']['ignore_errors'] = true;
  228. }
  229. if ($this->degradedMode && substr($fileUrl, 0, 26) === 'http://repo.packagist.org/') {
  230. // access packagist using the resolved IPv4 instead of the hostname to force IPv4 protocol
  231. $fileUrl = 'http://' . gethostbyname('repo.packagist.org') . substr($fileUrl, 20);
  232. $degradedPackagist = true;
  233. }
  234. $ctx = StreamContextFactory::getContext($fileUrl, $options, array('notification' => array($this, 'callbackGet')));
  235. $actualContextOptions = stream_context_get_options($ctx);
  236. $usingProxy = !empty($actualContextOptions['http']['proxy']) ? ' using proxy ' . $actualContextOptions['http']['proxy'] : '';
  237. $this->io->writeError((substr($origFileUrl, 0, 4) === 'http' ? 'Downloading ' : 'Reading ') . $origFileUrl . $usingProxy, true, IOInterface::DEBUG);
  238. unset($origFileUrl, $actualContextOptions);
  239. // Check for secure HTTP, but allow insecure Packagist calls to $hashed providers as file integrity is verified with sha256
  240. if ((!preg_match('{^http://(repo\.)?packagist\.org/p/}', $fileUrl) || (false === strpos($fileUrl, '$') && false === strpos($fileUrl, '%24'))) && empty($degradedPackagist) && $this->config) {
  241. $this->config->prohibitUrlByConfig($fileUrl, $this->io);
  242. }
  243. if ($this->progress && !$isRedirect) {
  244. $this->io->writeError("Downloading (<comment>connecting...</comment>)", false);
  245. }
  246. $errorMessage = '';
  247. $errorCode = 0;
  248. $result = false;
  249. set_error_handler(function ($code, $msg) use (&$errorMessage) {
  250. if ($errorMessage) {
  251. $errorMessage .= "\n";
  252. }
  253. $errorMessage .= preg_replace('{^file_get_contents\(.*?\): }', '', $msg);
  254. });
  255. try {
  256. $result = $this->getRemoteContents($originUrl, $fileUrl, $ctx, $http_response_header);
  257. if (!empty($http_response_header[0])) {
  258. $statusCode = $this->findStatusCode($http_response_header);
  259. if (in_array($statusCode, array(401, 403)) && $this->retryAuthFailure) {
  260. $warning = null;
  261. if ($this->findHeaderValue($http_response_header, 'content-type') === 'application/json') {
  262. $data = json_decode($result, true);
  263. if (!empty($data['warning'])) {
  264. $warning = $data['warning'];
  265. }
  266. }
  267. $this->promptAuthAndRetry($statusCode, $this->findStatusMessage($http_response_header), $warning, $http_response_header);
  268. }
  269. }
  270. $contentLength = !empty($http_response_header[0]) ? $this->findHeaderValue($http_response_header, 'content-length') : null;
  271. if ($contentLength && Platform::strlen($result) < $contentLength) {
  272. // alas, this is not possible via the stream callback because STREAM_NOTIFY_COMPLETED is documented, but not implemented anywhere in PHP
  273. $e = new TransportException('Content-Length mismatch, received '.Platform::strlen($result).' bytes out of the expected '.$contentLength);
  274. $e->setHeaders($http_response_header);
  275. $e->setStatusCode($this->findStatusCode($http_response_header));
  276. $e->setResponse($result);
  277. $this->io->writeError('Content-Length mismatch, received '.Platform::strlen($result).' out of '.$contentLength.' bytes: (' . base64_encode($result).')', true, IOInterface::DEBUG);
  278. throw $e;
  279. }
  280. if (PHP_VERSION_ID < 50600 && !empty($options['ssl']['peer_fingerprint'])) {
  281. // Emulate fingerprint validation on PHP < 5.6
  282. $params = stream_context_get_params($ctx);
  283. $expectedPeerFingerprint = $options['ssl']['peer_fingerprint'];
  284. $peerFingerprint = TlsHelper::getCertificateFingerprint($params['options']['ssl']['peer_certificate']);
  285. // Constant time compare??!
  286. if ($expectedPeerFingerprint !== $peerFingerprint) {
  287. throw new TransportException('Peer fingerprint did not match');
  288. }
  289. }
  290. } catch (\Exception $e) {
  291. if ($e instanceof TransportException && !empty($http_response_header[0])) {
  292. $e->setHeaders($http_response_header);
  293. $e->setStatusCode($this->findStatusCode($http_response_header));
  294. }
  295. if ($e instanceof TransportException && $result !== false) {
  296. $e->setResponse($result);
  297. }
  298. $result = false;
  299. }
  300. if ($errorMessage && !filter_var(ini_get('allow_url_fopen'), FILTER_VALIDATE_BOOLEAN)) {
  301. $errorMessage = 'allow_url_fopen must be enabled in php.ini ('.$errorMessage.')';
  302. }
  303. restore_error_handler();
  304. if (isset($e) && !$this->retry) {
  305. if (!$this->degradedMode && false !== strpos($e->getMessage(), 'Operation timed out')) {
  306. $this->degradedMode = true;
  307. $this->io->writeError('');
  308. $this->io->writeError(array(
  309. '<error>'.$e->getMessage().'</error>',
  310. '<error>Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info</error>',
  311. ));
  312. return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress);
  313. }
  314. throw $e;
  315. }
  316. $statusCode = null;
  317. $contentType = null;
  318. $locationHeader = null;
  319. if (!empty($http_response_header[0])) {
  320. $statusCode = $this->findStatusCode($http_response_header);
  321. $contentType = $this->findHeaderValue($http_response_header, 'content-type');
  322. $locationHeader = $this->findHeaderValue($http_response_header, 'location');
  323. }
  324. // check for bitbucket login page asking to authenticate
  325. if ($originUrl === 'bitbucket.org'
  326. && !$this->authHelper->isPublicBitBucketDownload($fileUrl)
  327. && substr($fileUrl, -4) === '.zip'
  328. && (!$locationHeader || substr($locationHeader, -4) !== '.zip')
  329. && $contentType && preg_match('{^text/html\b}i', $contentType)
  330. ) {
  331. $result = false;
  332. if ($this->retryAuthFailure) {
  333. $this->promptAuthAndRetry(401);
  334. }
  335. }
  336. // check for gitlab 404 when downloading archives
  337. if ($statusCode === 404
  338. && $this->config && in_array($originUrl, $this->config->get('gitlab-domains'), true)
  339. && false !== strpos($fileUrl, 'archive.zip')
  340. ) {
  341. $result = false;
  342. if ($this->retryAuthFailure) {
  343. $this->promptAuthAndRetry(401);
  344. }
  345. }
  346. // handle 3xx redirects, 304 Not Modified is excluded
  347. $hasFollowedRedirect = false;
  348. if ($statusCode >= 300 && $statusCode <= 399 && $statusCode !== 304 && $this->redirects < $this->maxRedirects) {
  349. $hasFollowedRedirect = true;
  350. $result = $this->handleRedirect($http_response_header, $additionalOptions, $result);
  351. }
  352. // fail 4xx and 5xx responses and capture the response
  353. if ($statusCode && $statusCode >= 400 && $statusCode <= 599) {
  354. if (!$this->retry) {
  355. if ($this->progress && !$this->retry && !$isRedirect) {
  356. $this->io->overwriteError("Downloading (<error>failed</error>)", false);
  357. }
  358. $e = new TransportException('The "'.$this->fileUrl.'" file could not be downloaded ('.$http_response_header[0].')', $statusCode);
  359. $e->setHeaders($http_response_header);
  360. $e->setResponse($result);
  361. $e->setStatusCode($statusCode);
  362. throw $e;
  363. }
  364. $result = false;
  365. }
  366. if ($this->progress && !$this->retry && !$isRedirect) {
  367. $this->io->overwriteError("Downloading (".($result === false ? '<error>failed</error>' : '<comment>100%</comment>').")", false);
  368. }
  369. // decode gzip
  370. if ($result && extension_loaded('zlib') && substr($fileUrl, 0, 4) === 'http' && !$hasFollowedRedirect) {
  371. $contentEncoding = $this->findHeaderValue($http_response_header, 'content-encoding');
  372. $decode = $contentEncoding && 'gzip' === strtolower($contentEncoding);
  373. if ($decode) {
  374. try {
  375. if (PHP_VERSION_ID >= 50400) {
  376. $result = zlib_decode($result);
  377. } else {
  378. // work around issue with gzuncompress & co that do not work with all gzip checksums
  379. $result = file_get_contents('compress.zlib://data:application/octet-stream;base64,'.base64_encode($result));
  380. }
  381. if (!$result) {
  382. throw new TransportException('Failed to decode zlib stream');
  383. }
  384. } catch (\Exception $e) {
  385. if ($this->degradedMode) {
  386. throw $e;
  387. }
  388. $this->degradedMode = true;
  389. $this->io->writeError(array(
  390. '',
  391. '<error>Failed to decode response: '.$e->getMessage().'</error>',
  392. '<error>Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info</error>',
  393. ));
  394. return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress);
  395. }
  396. }
  397. }
  398. // handle copy command if download was successful
  399. if (false !== $result && null !== $fileName && !$isRedirect) {
  400. if ('' === $result) {
  401. throw new TransportException('"'.$this->fileUrl.'" appears broken, and returned an empty 200 response');
  402. }
  403. $errorMessage = '';
  404. set_error_handler(function ($code, $msg) use (&$errorMessage) {
  405. if ($errorMessage) {
  406. $errorMessage .= "\n";
  407. }
  408. $errorMessage .= preg_replace('{^file_put_contents\(.*?\): }', '', $msg);
  409. });
  410. $result = (bool) file_put_contents($fileName, $result);
  411. restore_error_handler();
  412. if (false === $result) {
  413. throw new TransportException('The "'.$this->fileUrl.'" file could not be written to '.$fileName.': '.$errorMessage);
  414. }
  415. }
  416. // Handle SSL cert match issues
  417. if (false === $result && false !== strpos($errorMessage, 'Peer certificate') && PHP_VERSION_ID < 50600) {
  418. // Certificate name error, PHP doesn't support subjectAltName on PHP < 5.6
  419. // The procedure to handle sAN for older PHP's is:
  420. //
  421. // 1. Open socket to remote server and fetch certificate (disabling peer
  422. // validation because PHP errors without giving up the certificate.)
  423. //
  424. // 2. Verifying the domain in the URL against the names in the sAN field.
  425. // If there is a match record the authority [host/port], certificate
  426. // common name, and certificate fingerprint.
  427. //
  428. // 3. Retry the original request but changing the CN_match parameter to
  429. // the common name extracted from the certificate in step 2.
  430. //
  431. // 4. To prevent any attempt at being hoodwinked by switching the
  432. // certificate between steps 2 and 3 the fingerprint of the certificate
  433. // presented in step 3 is compared against the one recorded in step 2.
  434. if (CaBundle::isOpensslParseSafe()) {
  435. $certDetails = $this->getCertificateCnAndFp($this->fileUrl, $options);
  436. if ($certDetails) {
  437. $this->peerCertificateMap[$this->getUrlAuthority($this->fileUrl)] = $certDetails;
  438. $this->retry = true;
  439. }
  440. } else {
  441. $this->io->writeError('');
  442. $this->io->writeError(sprintf(
  443. '<error>Your version of PHP, %s, is affected by CVE-2013-6420 and cannot safely perform certificate validation, we strongly suggest you upgrade.</error>',
  444. PHP_VERSION
  445. ));
  446. }
  447. }
  448. if ($this->retry) {
  449. $this->retry = false;
  450. $result = $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress);
  451. if ($this->storeAuth && $this->config) {
  452. $this->authHelper->storeAuth($this->originUrl, $this->storeAuth);
  453. $this->storeAuth = false;
  454. }
  455. return $result;
  456. }
  457. if (false === $result) {
  458. $e = new TransportException('The "'.$this->fileUrl.'" file could not be downloaded: '.$errorMessage, $errorCode);
  459. if (!empty($http_response_header[0])) {
  460. $e->setHeaders($http_response_header);
  461. }
  462. if (!$this->degradedMode && false !== strpos($e->getMessage(), 'Operation timed out')) {
  463. $this->degradedMode = true;
  464. $this->io->writeError('');
  465. $this->io->writeError(array(
  466. '<error>'.$e->getMessage().'</error>',
  467. '<error>Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info</error>',
  468. ));
  469. return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress);
  470. }
  471. throw $e;
  472. }
  473. if (!empty($http_response_header[0])) {
  474. $this->lastHeaders = $http_response_header;
  475. }
  476. return $result;
  477. }
  478. /**
  479. * Get contents of remote URL.
  480. *
  481. * @param string $originUrl The origin URL
  482. * @param string $fileUrl The file URL
  483. * @param resource $context The stream context
  484. *
  485. * @return string|false The response contents or false on failure
  486. */
  487. protected function getRemoteContents($originUrl, $fileUrl, $context, array &$responseHeaders = null)
  488. {
  489. try {
  490. $e = null;
  491. $result = file_get_contents($fileUrl, false, $context);
  492. } catch (\Throwable $e) {
  493. } catch (\Exception $e) {
  494. }
  495. $responseHeaders = isset($http_response_header) ? $http_response_header : array();
  496. if (null !== $e) {
  497. throw $e;
  498. }
  499. return $result;
  500. }
  501. /**
  502. * Get notification action.
  503. *
  504. * @param int $notificationCode The notification code
  505. * @param int $severity The severity level
  506. * @param string $message The message
  507. * @param int $messageCode The message code
  508. * @param int $bytesTransferred The loaded size
  509. * @param int $bytesMax The total size
  510. * @throws TransportException
  511. */
  512. protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  513. {
  514. switch ($notificationCode) {
  515. case STREAM_NOTIFY_FAILURE:
  516. if (400 === $messageCode) {
  517. // This might happen if your host is secured by ssl client certificate authentication
  518. // but you do not send an appropriate certificate
  519. throw new TransportException("The '" . $this->fileUrl . "' URL could not be accessed: " . $message, $messageCode);
  520. }
  521. break;
  522. case STREAM_NOTIFY_FILE_SIZE_IS:
  523. $this->bytesMax = $bytesMax;
  524. break;
  525. case STREAM_NOTIFY_PROGRESS:
  526. if ($this->bytesMax > 0 && $this->progress) {
  527. $progression = min(100, round($bytesTransferred / $this->bytesMax * 100));
  528. if ((0 === $progression % 5) && 100 !== $progression && $progression !== $this->lastProgress) {
  529. $this->lastProgress = $progression;
  530. $this->io->overwriteError("Downloading (<comment>$progression%</comment>)", false);
  531. }
  532. }
  533. break;
  534. default:
  535. break;
  536. }
  537. }
  538. protected function promptAuthAndRetry($httpStatus, $reason = null, $warning = null, $headers = array())
  539. {
  540. $result = $this->authHelper->promptAuthIfNeeded($this->fileUrl, $this->originUrl, $httpStatus, $reason, $warning, $headers);
  541. $this->storeAuth = $result['storeAuth'];
  542. $this->retry = $result['retry'];
  543. if ($this->retry) {
  544. throw new TransportException('RETRY');
  545. }
  546. }
  547. protected function getOptionsForUrl($originUrl, $additionalOptions)
  548. {
  549. $tlsOptions = array();
  550. // Setup remaining TLS options - the matching may need monitoring, esp. www vs none in CN
  551. if ($this->disableTls === false && PHP_VERSION_ID < 50600 && !stream_is_local($this->fileUrl)) {
  552. $host = parse_url($this->fileUrl, PHP_URL_HOST);
  553. if (PHP_VERSION_ID < 50304) {
  554. // PHP < 5.3.4 does not support follow_location, for those people
  555. // do some really nasty hard coded transformations. These will
  556. // still breakdown if the site redirects to a domain we don't
  557. // expect.
  558. if ($host === 'github.com' || $host === 'api.github.com') {
  559. $host = '*.github.com';
  560. }
  561. }
  562. $tlsOptions['ssl']['CN_match'] = $host;
  563. $tlsOptions['ssl']['SNI_server_name'] = $host;
  564. $urlAuthority = $this->getUrlAuthority($this->fileUrl);
  565. if (isset($this->peerCertificateMap[$urlAuthority])) {
  566. // Handle subjectAltName on lesser PHP's.
  567. $certMap = $this->peerCertificateMap[$urlAuthority];
  568. $this->io->writeError('', true, IOInterface::DEBUG);
  569. $this->io->writeError(sprintf(
  570. 'Using <info>%s</info> as CN for subjectAltName enabled host <info>%s</info>',
  571. $certMap['cn'],
  572. $urlAuthority
  573. ), true, IOInterface::DEBUG);
  574. $tlsOptions['ssl']['CN_match'] = $certMap['cn'];
  575. $tlsOptions['ssl']['peer_fingerprint'] = $certMap['fp'];
  576. } elseif (!CaBundle::isOpensslParseSafe() && $host === 'repo.packagist.org') {
  577. // handle subjectAltName for packagist.org's repo domain on very old PHPs
  578. $tlsOptions['ssl']['CN_match'] = 'packagist.org';
  579. }
  580. }
  581. $headers = array();
  582. if (extension_loaded('zlib')) {
  583. $headers[] = 'Accept-Encoding: gzip';
  584. }
  585. $options = array_replace_recursive($this->options, $tlsOptions, $additionalOptions);
  586. if (!$this->degradedMode) {
  587. // degraded mode disables HTTP/1.1 which causes issues with some bad
  588. // proxies/software due to the use of chunked encoding
  589. $options['http']['protocol_version'] = 1.1;
  590. $headers[] = 'Connection: close';
  591. }
  592. $headers = $this->authHelper->addAuthenticationHeader($headers, $originUrl, $this->fileUrl);
  593. $options['http']['follow_location'] = 0;
  594. if (isset($options['http']['header']) && !is_array($options['http']['header'])) {
  595. $options['http']['header'] = explode("\r\n", trim($options['http']['header'], "\r\n"));
  596. }
  597. foreach ($headers as $header) {
  598. $options['http']['header'][] = $header;
  599. }
  600. return $options;
  601. }
  602. private function handleRedirect(array $http_response_header, array $additionalOptions, $result)
  603. {
  604. if ($locationHeader = $this->findHeaderValue($http_response_header, 'location')) {
  605. if (parse_url($locationHeader, PHP_URL_SCHEME)) {
  606. // Absolute URL; e.g. https://example.com/composer
  607. $targetUrl = $locationHeader;
  608. } elseif (parse_url($locationHeader, PHP_URL_HOST)) {
  609. // Scheme relative; e.g. //example.com/foo
  610. $targetUrl = $this->scheme.':'.$locationHeader;
  611. } elseif ('/' === $locationHeader[0]) {
  612. // Absolute path; e.g. /foo
  613. $urlHost = parse_url($this->fileUrl, PHP_URL_HOST);
  614. // Replace path using hostname as an anchor.
  615. $targetUrl = preg_replace('{^(.+(?://|@)'.preg_quote($urlHost).'(?::\d+)?)(?:[/\?].*)?$}', '\1'.$locationHeader, $this->fileUrl);
  616. } else {
  617. // Relative path; e.g. foo
  618. // This actually differs from PHP which seems to add duplicate slashes.
  619. $targetUrl = preg_replace('{^(.+/)[^/?]*(?:\?.*)?$}', '\1'.$locationHeader, $this->fileUrl);
  620. }
  621. }
  622. if (!empty($targetUrl)) {
  623. $this->redirects++;
  624. $this->io->writeError('', true, IOInterface::DEBUG);
  625. $this->io->writeError(sprintf('Following redirect (%u) %s', $this->redirects, $targetUrl), true, IOInterface::DEBUG);
  626. $additionalOptions['redirects'] = $this->redirects;
  627. return $this->get(parse_url($targetUrl, PHP_URL_HOST), $targetUrl, $additionalOptions, $this->fileName, $this->progress);
  628. }
  629. if (!$this->retry) {
  630. $e = new TransportException('The "'.$this->fileUrl.'" file could not be downloaded, got redirect without Location ('.$http_response_header[0].')');
  631. $e->setHeaders($http_response_header);
  632. $e->setResponse($result);
  633. throw $e;
  634. }
  635. return false;
  636. }
  637. /**
  638. * Fetch certificate common name and fingerprint for validation of SAN.
  639. *
  640. * @todo Remove when PHP 5.6 is minimum supported version.
  641. */
  642. private function getCertificateCnAndFp($url, $options)
  643. {
  644. if (PHP_VERSION_ID >= 50600) {
  645. throw new \BadMethodCallException(sprintf(
  646. '%s must not be used on PHP >= 5.6',
  647. __METHOD__
  648. ));
  649. }
  650. $context = StreamContextFactory::getContext($url, $options, array('options' => array(
  651. 'ssl' => array(
  652. 'capture_peer_cert' => true,
  653. 'verify_peer' => false, // Yes this is fucking insane! But PHP is lame.
  654. ), ),
  655. ));
  656. // Ideally this would just use stream_socket_client() to avoid sending a
  657. // HTTP request but that does not capture the certificate.
  658. if (false === $handle = @fopen($url, 'rb', false, $context)) {
  659. return;
  660. }
  661. // Close non authenticated connection without reading any content.
  662. fclose($handle);
  663. $handle = null;
  664. $params = stream_context_get_params($context);
  665. if (!empty($params['options']['ssl']['peer_certificate'])) {
  666. $peerCertificate = $params['options']['ssl']['peer_certificate'];
  667. if (TlsHelper::checkCertificateHost($peerCertificate, parse_url($url, PHP_URL_HOST), $commonName)) {
  668. return array(
  669. 'cn' => $commonName,
  670. 'fp' => TlsHelper::getCertificateFingerprint($peerCertificate),
  671. );
  672. }
  673. }
  674. }
  675. private function getUrlAuthority($url)
  676. {
  677. $defaultPorts = array(
  678. 'ftp' => 21,
  679. 'http' => 80,
  680. 'https' => 443,
  681. 'ssh2.sftp' => 22,
  682. 'ssh2.scp' => 22,
  683. );
  684. $scheme = parse_url($url, PHP_URL_SCHEME);
  685. if (!isset($defaultPorts[$scheme])) {
  686. throw new \InvalidArgumentException(sprintf(
  687. 'Could not get default port for unknown scheme: %s',
  688. $scheme
  689. ));
  690. }
  691. $defaultPort = $defaultPorts[$scheme];
  692. $port = parse_url($url, PHP_URL_PORT) ?: $defaultPort;
  693. return parse_url($url, PHP_URL_HOST).':'.$port;
  694. }
  695. }