PageRenderTime 62ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/composer-setup.php

https://gitlab.com/vasilyb/cornerstone
PHP | 1511 lines | 1111 code | 135 blank | 265 comment | 133 complexity | 2817c72a56695f409c9969421fc2e921 MD5 | raw file
  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. setupEnvironment();
  12. process(is_array($argv) ? $argv : array());
  13. /**
  14. * Ensures the environment is sane
  15. */
  16. function setupEnvironment()
  17. {
  18. ini_set('display_errors', 1);
  19. }
  20. /**
  21. * processes the installer
  22. */
  23. function process($argv)
  24. {
  25. // Determine ANSI output from --ansi and --no-ansi flags
  26. setUseAnsi($argv);
  27. if (in_array('--help', $argv)) {
  28. displayHelp();
  29. exit(0);
  30. }
  31. $check = in_array('--check', $argv);
  32. $help = in_array('--help', $argv);
  33. $force = in_array('--force', $argv);
  34. $quiet = in_array('--quiet', $argv);
  35. $channel = in_array('--snapshot', $argv) ? 'snapshot' : (in_array('--preview', $argv) ? 'preview' : 'stable');
  36. $disableTls = in_array('--disable-tls', $argv);
  37. $installDir = getOptValue('--install-dir', $argv, false);
  38. $version = getOptValue('--version', $argv, false);
  39. $filename = getOptValue('--filename', $argv, 'composer.phar');
  40. $cafile = getOptValue('--cafile', $argv, false);
  41. if (!checkParams($installDir, $version, $cafile)) {
  42. exit(1);
  43. }
  44. $ok = checkPlatform($warnings, $quiet, $disableTls, true);
  45. if ($check) {
  46. // Only show warnings if we haven't output any errors
  47. if ($ok) {
  48. showWarnings($warnings);
  49. showSecurityWarning($disableTls);
  50. }
  51. exit($ok ? 0 : 1);
  52. }
  53. if ($ok || $force) {
  54. $installer = new Installer($quiet, $disableTls, $cafile);
  55. if ($installer->run($version, $installDir, $filename, $channel)) {
  56. showWarnings($warnings);
  57. showSecurityWarning($disableTls);
  58. exit(0);
  59. }
  60. }
  61. exit(1);
  62. }
  63. /**
  64. * displays the help
  65. */
  66. function displayHelp()
  67. {
  68. echo <<<EOF
  69. Composer Installer
  70. ------------------
  71. Options
  72. --help this help
  73. --check for checking environment only
  74. --force forces the installation
  75. --ansi force ANSI color output
  76. --no-ansi disable ANSI color output
  77. --quiet do not output unimportant messages
  78. --install-dir="..." accepts a target installation directory
  79. --preview install the latest version from the preview (alpha/beta/rc) channel instead of stable
  80. --snapshot install the latest version from the snapshot (dev builds) channel instead of stable
  81. --version="..." accepts a specific version to install instead of the latest
  82. --filename="..." accepts a target filename (default: composer.phar)
  83. --disable-tls disable SSL/TLS security for file downloads
  84. --cafile="..." accepts a path to a Certificate Authority (CA) certificate file for SSL/TLS verification
  85. EOF;
  86. }
  87. /**
  88. * Sets the USE_ANSI define for colorizing output
  89. *
  90. * @param array $argv Command-line arguments
  91. */
  92. function setUseAnsi($argv)
  93. {
  94. // --no-ansi wins over --ansi
  95. if (in_array('--no-ansi', $argv)) {
  96. define('USE_ANSI', false);
  97. } elseif (in_array('--ansi', $argv)) {
  98. define('USE_ANSI', true);
  99. } else {
  100. // On Windows, default to no ANSI, except in ANSICON and ConEmu.
  101. // Everywhere else, default to ANSI if stdout is a terminal.
  102. define(
  103. 'USE_ANSI',
  104. (DIRECTORY_SEPARATOR == '\\')
  105. ? (false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI'))
  106. : (function_exists('posix_isatty') && posix_isatty(1))
  107. );
  108. }
  109. }
  110. /**
  111. * Returns the value of a command-line option
  112. *
  113. * @param string $opt The command-line option to check
  114. * @param array $argv Command-line arguments
  115. * @param mixed $default Default value to be returned
  116. *
  117. * @return mixed The command-line value or the default
  118. */
  119. function getOptValue($opt, $argv, $default)
  120. {
  121. $optLength = strlen($opt);
  122. foreach ($argv as $key => $value) {
  123. $next = $key + 1;
  124. if (0 === strpos($value, $opt)) {
  125. if ($optLength === strlen($value) && isset($argv[$next])) {
  126. return trim($argv[$next]);
  127. } else {
  128. return trim(substr($value, $optLength + 1));
  129. }
  130. }
  131. }
  132. return $default;
  133. }
  134. /**
  135. * Checks that user-supplied params are valid
  136. *
  137. * @param mixed $installDir The required istallation directory
  138. * @param mixed $version The required composer version to install
  139. * @param mixed $cafile Certificate Authority file
  140. *
  141. * @return bool True if the supplied params are okay
  142. */
  143. function checkParams($installDir, $version, $cafile)
  144. {
  145. $result = true;
  146. if (false !== $installDir && !is_dir($installDir)) {
  147. out("The defined install dir ({$installDir}) does not exist.", 'info');
  148. $result = false;
  149. }
  150. if (false !== $version && 1 !== preg_match('/^\d+\.\d+\.\d+(\-(alpha|beta|RC)\d*)*$/', $version)) {
  151. out("The defined install version ({$version}) does not match release pattern.", 'info');
  152. $result = false;
  153. }
  154. if (false !== $cafile && (!file_exists($cafile) || !is_readable($cafile))) {
  155. out("The defined Certificate Authority (CA) cert file ({$cafile}) does not exist or is not readable.", 'info');
  156. $result = false;
  157. }
  158. return $result;
  159. }
  160. /**
  161. * Checks the platform for possible issues running Composer
  162. *
  163. * Errors are written to the output, warnings are saved for later display.
  164. *
  165. * @param array $warnings Populated by method, to be shown later
  166. * @param bool $quiet Quiet mode
  167. * @param bool $disableTls Bypass tls
  168. * @param bool $install If we are installing, rather than diagnosing
  169. *
  170. * @return bool True if there are no errors
  171. */
  172. function checkPlatform(&$warnings, $quiet, $disableTls, $install)
  173. {
  174. getPlatformIssues($errors, $warnings, $install);
  175. // Make openssl warning an error if tls has not been specifically disabled
  176. if (isset($warnings['openssl']) && !$disableTls) {
  177. $errors['openssl'] = $warnings['openssl'];
  178. unset($warnings['openssl']);
  179. }
  180. if (!empty($errors)) {
  181. out('Some settings on your machine make Composer unable to work properly.', 'error');
  182. out('Make sure that you fix the issues listed below and run this script again:', 'error');
  183. outputIssues($errors);
  184. return false;
  185. }
  186. if (empty($warnings) && !$quiet) {
  187. out('All settings correct for using Composer', 'success');
  188. }
  189. return true;
  190. }
  191. /**
  192. * Checks platform configuration for common incompatibility issues
  193. *
  194. * @param array $errors Populated by method
  195. * @param array $warnings Populated by method
  196. * @param bool $install If we are installing, rather than diagnosing
  197. *
  198. * @return bool If any errors or warnings have been found
  199. */
  200. function getPlatformIssues(&$errors, &$warnings, $install)
  201. {
  202. $errors = array();
  203. $warnings = array();
  204. if ($iniPath = php_ini_loaded_file()) {
  205. $iniMessage = PHP_EOL.'The php.ini used by your command-line PHP is: ' . $iniPath;
  206. } else {
  207. $iniMessage = PHP_EOL.'A php.ini file does not exist. You will have to create one.';
  208. }
  209. $iniMessage .= PHP_EOL.'If you can not modify the ini file, you can also run `php -d option=value` to modify ini values on the fly. You can use -d multiple times.';
  210. if (ini_get('detect_unicode')) {
  211. $errors['unicode'] = array(
  212. 'The detect_unicode setting must be disabled.',
  213. 'Add the following to the end of your `php.ini`:',
  214. ' detect_unicode = Off',
  215. $iniMessage
  216. );
  217. }
  218. if (extension_loaded('suhosin')) {
  219. $suhosin = ini_get('suhosin.executor.include.whitelist');
  220. $suhosinBlacklist = ini_get('suhosin.executor.include.blacklist');
  221. if (false === stripos($suhosin, 'phar') && (!$suhosinBlacklist || false !== stripos($suhosinBlacklist, 'phar'))) {
  222. $errors['suhosin'] = array(
  223. 'The suhosin.executor.include.whitelist setting is incorrect.',
  224. 'Add the following to the end of your `php.ini` or suhosin.ini (Example path [for Debian]: /etc/php5/cli/conf.d/suhosin.ini):',
  225. ' suhosin.executor.include.whitelist = phar '.$suhosin,
  226. $iniMessage
  227. );
  228. }
  229. }
  230. if (!function_exists('json_decode')) {
  231. $errors['json'] = array(
  232. 'The json extension is missing.',
  233. 'Install it or recompile php without --disable-json'
  234. );
  235. }
  236. if (!extension_loaded('Phar')) {
  237. $errors['phar'] = array(
  238. 'The phar extension is missing.',
  239. 'Install it or recompile php without --disable-phar'
  240. );
  241. }
  242. if (!extension_loaded('filter')) {
  243. $errors['filter'] = array(
  244. 'The filter extension is missing.',
  245. 'Install it or recompile php without --disable-filter'
  246. );
  247. }
  248. if (!extension_loaded('hash')) {
  249. $errors['hash'] = array(
  250. 'The hash extension is missing.',
  251. 'Install it or recompile php without --disable-hash'
  252. );
  253. }
  254. if (!extension_loaded('iconv') && !extension_loaded('mbstring')) {
  255. $errors['iconv_mbstring'] = array(
  256. 'The iconv OR mbstring extension is required and both are missing.',
  257. 'Install either of them or recompile php without --disable-iconv'
  258. );
  259. }
  260. if (!ini_get('allow_url_fopen')) {
  261. $errors['allow_url_fopen'] = array(
  262. 'The allow_url_fopen setting is incorrect.',
  263. 'Add the following to the end of your `php.ini`:',
  264. ' allow_url_fopen = On',
  265. $iniMessage
  266. );
  267. }
  268. if (extension_loaded('ionCube Loader') && ioncube_loader_iversion() < 40009) {
  269. $ioncube = ioncube_loader_version();
  270. $errors['ioncube'] = array(
  271. 'Your ionCube Loader extension ('.$ioncube.') is incompatible with Phar files.',
  272. 'Upgrade to ionCube 4.0.9 or higher or remove this line (path may be different) from your `php.ini` to disable it:',
  273. ' zend_extension = /usr/lib/php5/20090626+lfs/ioncube_loader_lin_5.3.so',
  274. $iniMessage
  275. );
  276. }
  277. if (version_compare(PHP_VERSION, '5.3.2', '<')) {
  278. $errors['php'] = array(
  279. 'Your PHP ('.PHP_VERSION.') is too old, you must upgrade to PHP 5.3.2 or higher.'
  280. );
  281. }
  282. if (version_compare(PHP_VERSION, '5.3.4', '<')) {
  283. $warnings['php'] = array(
  284. 'Your PHP ('.PHP_VERSION.') is quite old, upgrading to PHP 5.3.4 or higher is recommended.',
  285. 'Composer works with 5.3.2+ for most people, but there might be edge case issues.'
  286. );
  287. }
  288. if (!extension_loaded('openssl')) {
  289. $warnings['openssl'] = array(
  290. 'The openssl extension is missing, which means that secure HTTPS transfers are impossible.',
  291. 'If possible you should enable it or recompile php with --with-openssl'
  292. );
  293. }
  294. if (extension_loaded('openssl') && OPENSSL_VERSION_NUMBER < 0x1000100f) {
  295. // Attempt to parse version number out, fallback to whole string value.
  296. $opensslVersion = trim(strstr(OPENSSL_VERSION_TEXT, ' '));
  297. $opensslVersion = substr($opensslVersion, 0, strpos($opensslVersion, ' '));
  298. $opensslVersion = $opensslVersion ? $opensslVersion : OPENSSL_VERSION_TEXT;
  299. $warnings['openssl_version'] = array(
  300. 'The OpenSSL library ('.$opensslVersion.') used by PHP does not support TLSv1.2 or TLSv1.1.',
  301. 'If possible you should upgrade OpenSSL to version 1.0.1 or above.'
  302. );
  303. }
  304. if (!defined('HHVM_VERSION') && !extension_loaded('apcu') && ini_get('apc.enable_cli')) {
  305. $warnings['apc_cli'] = array(
  306. 'The apc.enable_cli setting is incorrect.',
  307. 'Add the following to the end of your `php.ini`:',
  308. ' apc.enable_cli = Off',
  309. $iniMessage
  310. );
  311. }
  312. if (!$install && extension_loaded('xdebug')) {
  313. $warnings['xdebug_loaded'] = array(
  314. 'The xdebug extension is loaded, this can slow down Composer a little.',
  315. 'Disabling it when using Composer is recommended.'
  316. );
  317. if (ini_get('xdebug.profiler_enabled')) {
  318. $warnings['xdebug_profile'] = array(
  319. 'The xdebug.profiler_enabled setting is enabled, this can slow down Composer a lot.',
  320. 'Add the following to the end of your `php.ini` to disable it:',
  321. ' xdebug.profiler_enabled = 0',
  322. $iniMessage
  323. );
  324. }
  325. }
  326. if (!extension_loaded('zlib')) {
  327. $warnings['zlib'] = array(
  328. 'The zlib extension is not loaded, this can slow down Composer a lot.',
  329. 'If possible, install it or recompile php with --with-zlib',
  330. $iniMessage
  331. );
  332. }
  333. ob_start();
  334. phpinfo(INFO_GENERAL);
  335. $phpinfo = ob_get_clean();
  336. if (preg_match('{Configure Command(?: *</td><td class="v">| *=> *)(.*?)(?:</td>|$)}m', $phpinfo, $match)) {
  337. $configure = $match[1];
  338. if (false !== strpos($configure, '--enable-sigchild')) {
  339. $warnings['sigchild'] = array(
  340. 'PHP was compiled with --enable-sigchild which can cause issues on some platforms.',
  341. 'Recompile it without this flag if possible, see also:',
  342. ' https://bugs.php.net/bug.php?id=22999'
  343. );
  344. }
  345. if (false !== strpos($configure, '--with-curlwrappers')) {
  346. $warnings['curlwrappers'] = array(
  347. 'PHP was compiled with --with-curlwrappers which will cause issues with HTTP authentication and GitHub.',
  348. 'Recompile it without this flag if possible'
  349. );
  350. }
  351. }
  352. // Stringify the message arrays
  353. foreach ($errors as $key => $value) {
  354. $errors[$key] = PHP_EOL.implode(PHP_EOL, $value);
  355. }
  356. foreach ($warnings as $key => $value) {
  357. $warnings[$key] = PHP_EOL.implode(PHP_EOL, $value);
  358. }
  359. return !empty($errors) || !empty($warnings);
  360. }
  361. /**
  362. * Outputs an array of issues
  363. *
  364. * @param array $issues
  365. */
  366. function outputIssues($issues)
  367. {
  368. foreach ($issues as $issue) {
  369. out($issue, 'info');
  370. }
  371. out('');
  372. }
  373. /**
  374. * Outputs any warnings found
  375. *
  376. * @param array $warnings
  377. */
  378. function showWarnings($warnings)
  379. {
  380. if (!empty($warnings)) {
  381. out('Some settings on your machine may cause stability issues with Composer.', 'error');
  382. out('If you encounter issues, try to change the following:', 'error');
  383. outputIssues($warnings);
  384. }
  385. }
  386. /**
  387. * Outputs an end of process warning if tls has been bypassed
  388. *
  389. * @param bool $disableTls Bypass tls
  390. */
  391. function showSecurityWarning($disableTls)
  392. {
  393. if ($disableTls) {
  394. out('You have instructed the Installer not to enforce SSL/TLS security on remote HTTPS requests.', 'info');
  395. out('This will leave all downloads during installation vulnerable to Man-In-The-Middle (MITM) attacks', 'info');
  396. }
  397. }
  398. /**
  399. * colorize output
  400. */
  401. function out($text, $color = null, $newLine = true)
  402. {
  403. $styles = array(
  404. 'success' => "\033[0;32m%s\033[0m",
  405. 'error' => "\033[31;31m%s\033[0m",
  406. 'info' => "\033[33;33m%s\033[0m"
  407. );
  408. $format = '%s';
  409. if (isset($styles[$color]) && USE_ANSI) {
  410. $format = $styles[$color];
  411. }
  412. if ($newLine) {
  413. $format .= PHP_EOL;
  414. }
  415. printf($format, $text);
  416. }
  417. /**
  418. * Returns the system-dependent Composer home location, which may not exist
  419. *
  420. * @return string
  421. */
  422. function getHomeDir()
  423. {
  424. $home = getenv('COMPOSER_HOME');
  425. if (!$home) {
  426. $userDir = getUserDir();
  427. if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
  428. $home = $userDir.'/Composer';
  429. } else {
  430. $home = $userDir.'/.composer';
  431. if (!is_dir($home) && useXdg()) {
  432. // XDG Base Directory Specifications
  433. if (!($xdgConfig = getenv('XDG_CONFIG_HOME'))) {
  434. $xdgConfig = $userDir.'/.config';
  435. }
  436. $home = $xdgConfig.'/composer';
  437. }
  438. }
  439. }
  440. return $home;
  441. }
  442. /**
  443. * Returns the location of the user directory from the environment
  444. * @throws RuntimeException If the environment value does not exists
  445. *
  446. * @return string
  447. */
  448. function getUserDir()
  449. {
  450. $userEnv = defined('PHP_WINDOWS_VERSION_MAJOR') ? 'APPDATA' : 'HOME';
  451. $userDir = getenv($userEnv);
  452. if (!$userDir) {
  453. throw new RuntimeException('The '.$userEnv.' or COMPOSER_HOME environment variable must be set for composer to run correctly');
  454. }
  455. return rtrim(strtr($userDir, '\\', '/'), '/');
  456. }
  457. /**
  458. * @return bool
  459. */
  460. function useXdg()
  461. {
  462. foreach (array_keys($_SERVER) as $key) {
  463. if (substr($key, 0, 4) === 'XDG_') {
  464. return true;
  465. }
  466. }
  467. return false;
  468. }
  469. function validateCaFile($contents)
  470. {
  471. // assume the CA is valid if php is vulnerable to
  472. // https://www.sektioneins.de/advisories/advisory-012013-php-openssl_x509_parse-memory-corruption-vulnerability.html
  473. if (
  474. PHP_VERSION_ID <= 50327
  475. || (PHP_VERSION_ID >= 50400 && PHP_VERSION_ID < 50422)
  476. || (PHP_VERSION_ID >= 50500 && PHP_VERSION_ID < 50506)
  477. ) {
  478. return !empty($contents);
  479. }
  480. return (bool) openssl_x509_parse($contents);
  481. }
  482. class Installer
  483. {
  484. private $quiet;
  485. private $disableTls;
  486. private $cafile;
  487. private $installPath;
  488. private $target;
  489. private $tmpFile;
  490. private $baseUrl;
  491. private $algo;
  492. private $errHandler;
  493. private $httpClient;
  494. private $pubKeys = array();
  495. private $installs = array();
  496. /**
  497. * Constructor - must not do anything that throws an exception
  498. *
  499. * @param bool $quiet Quiet mode
  500. * @param bool $disableTls Bypass tls
  501. * @param mixed $cafile Path to CA bundle, or false
  502. */
  503. public function __construct($quiet, $disableTls, $caFile)
  504. {
  505. if (($this->quiet = $quiet)) {
  506. ob_start();
  507. }
  508. $this->disableTls = $disableTls;
  509. $this->cafile = $caFile;
  510. $this->errHandler = new ErrorHandler();
  511. }
  512. /**
  513. * Runs the installer
  514. *
  515. * @param mixed $version Specific version to install, or false
  516. * @param mixed $installDir Specific installation directory, or false
  517. * @param string $filename Specific filename to save to, or composer.phar
  518. * @param string $channel Specific version channel to use
  519. * @throws Exception If anything other than a RuntimeException is caught
  520. *
  521. * @return bool If the installation succeeded
  522. */
  523. public function run($version, $installDir, $filename, $channel)
  524. {
  525. try {
  526. $this->initTargets($installDir, $filename);
  527. $this->initTls();
  528. $this->httpClient = new HttpClient($this->disableTls, $this->cafile);
  529. $result = $this->install($version, $channel);
  530. if ($result && $channel !== 'stable' && !$version && defined('PHP_BINARY')) {
  531. $null = (defined('PHP_WINDOWS_VERSION_MAJOR') ? 'NUL' : '/dev/null');
  532. @exec(escapeshellarg(PHP_BINARY) .' '.escapeshellarg($this->target).' self-update --'.$channel.' --set-channel-only -q > '.$null.' 2> '.$null, $output);
  533. }
  534. } catch (Exception $e) {
  535. $result = false;
  536. }
  537. // Always clean up
  538. $this->cleanUp($result);
  539. if (isset($e)) {
  540. // Rethrow anything that is not a RuntimeException
  541. if (!$e instanceof RuntimeException) {
  542. throw $e;
  543. }
  544. out($e->getMessage(), 'error');
  545. }
  546. return $result;
  547. }
  548. /**
  549. * Initialization methods to set the required filenames and composer url
  550. *
  551. * @param mixed $installDir Specific installation directory, or false
  552. * @param string $filename Specific filename to save to, or composer.phar
  553. * @throws RuntimeException If the installation directory is not writable
  554. */
  555. protected function initTargets($installDir, $filename)
  556. {
  557. $this->installPath = (is_dir($installDir) ? rtrim($installDir, '/').'/' : '').$filename;
  558. $installDir = realpath($installDir) ? realpath($installDir) : getcwd();
  559. if (!is_writeable($installDir)) {
  560. throw new RuntimeException('The installation directory "'.$installDir.'" is not writable');
  561. }
  562. $this->target = $installDir.DIRECTORY_SEPARATOR.$filename;
  563. $this->tmpFile = $installDir.DIRECTORY_SEPARATOR.basename($this->target, '.phar').'-temp.phar';
  564. $uriScheme = $this->disableTls ? 'http' : 'https';
  565. $this->baseUrl = $uriScheme.'://getcomposer.org';
  566. }
  567. /**
  568. * A wrapper around methods to check tls and write public keys
  569. * @throws RuntimeException If SHA384 is not supported
  570. */
  571. protected function initTls()
  572. {
  573. if ($this->disableTls) {
  574. return;
  575. }
  576. if (!in_array('sha384', array_map('strtolower', openssl_get_md_methods()))) {
  577. throw new RuntimeException('SHA384 is not supported by your openssl extension');
  578. }
  579. $this->algo = defined('OPENSSL_ALGO_SHA384') ? OPENSSL_ALGO_SHA384 : 'SHA384';
  580. $home = $this->getComposerHome();
  581. $this->pubKeys = array(
  582. 'dev' => $this->installKey(self::getPKDev(), $home, 'keys.dev.pub'),
  583. 'tags' => $this->installKey(self::getPKTags(), $home, 'keys.tags.pub')
  584. );
  585. if (empty($this->cafile) && !HttpClient::getSystemCaRootBundlePath()) {
  586. $this->cafile = $this->installKey(HttpClient::getPackagedCaFile(), $home, 'cacert.pem');
  587. }
  588. }
  589. /**
  590. * Returns the Composer home directory, creating it if required
  591. * @throws RuntimeException If the directory cannot be created
  592. *
  593. * @return string
  594. */
  595. protected function getComposerHome()
  596. {
  597. $home = getHomeDir();
  598. if (!is_dir($home)) {
  599. $this->errHandler->start();
  600. if (!mkdir($home, 0777, true)) {
  601. throw new RuntimeException(sprintf(
  602. 'Unable to create Composer home directory "%s": %s',
  603. $home,
  604. $this->errHandler->message
  605. ));
  606. }
  607. $this->installs[] = $home;
  608. $this->errHandler->stop();
  609. }
  610. return $home;
  611. }
  612. /**
  613. * Writes public key data to disc
  614. *
  615. * @param string $data The public key(s) in pem format
  616. * @param string $path The directory to write to
  617. * @param string $filename The name of the file
  618. * @throws RuntimeException If the file cannot be written
  619. *
  620. * @return string The path to the saved data
  621. */
  622. protected function installKey($data, $path, $filename)
  623. {
  624. $this->errHandler->start();
  625. $target = $path.DIRECTORY_SEPARATOR.$filename;
  626. $installed = file_exists($target);
  627. $write = file_put_contents($target, $data, LOCK_EX);
  628. @chmod($target, 0644);
  629. $this->errHandler->stop();
  630. if (!$write) {
  631. throw new RuntimeException(sprintf('Unable to write %s to: %s', $filename, $path));
  632. }
  633. if (!$installed) {
  634. $this->installs[] = $target;
  635. }
  636. return $target;
  637. }
  638. /**
  639. * The main install function
  640. *
  641. * @param mixed $version Specific version to install, or false
  642. * @param string $channel Version channel to use
  643. *
  644. * @return bool If the installation succeeded
  645. */
  646. protected function install($version, $channel)
  647. {
  648. $retries = 3;
  649. $result = false;
  650. $infoMsg = 'Downloading...';
  651. $infoType = 'info';
  652. while ($retries--) {
  653. if (!$this->quiet) {
  654. out($infoMsg, $infoType);
  655. $infoMsg = 'Retrying...';
  656. $infoType = 'error';
  657. }
  658. if (!$this->getVersion($channel, $version, $url, $error)) {
  659. out($error, 'error');
  660. continue;
  661. }
  662. if (!$this->downloadToTmp($url, $signature, $error)) {
  663. out($error, 'error');
  664. continue;
  665. }
  666. if (!$this->verifyAndSave($version, $signature, $error)) {
  667. out($error, 'error');
  668. continue;
  669. }
  670. $result = true;
  671. break;
  672. }
  673. if (!$this->quiet) {
  674. if ($result) {
  675. out(PHP_EOL."Composer (version {$version}) successfully installed to: {$this->target}", 'success');
  676. out("Use it: php {$this->installPath}", 'info');
  677. out('');
  678. } else {
  679. out('The download failed repeatedly, aborting.', 'error');
  680. }
  681. }
  682. return $result;
  683. }
  684. /**
  685. * Sets the version url, downloading version data if required
  686. *
  687. * @param string $channel Version channel to use
  688. * @param false|string $version Version to install, or set by method
  689. * @param null|string $url The versioned url, set by method
  690. * @param null|string $error Set by method on failure
  691. *
  692. * @return bool If the operation succeeded
  693. */
  694. protected function getVersion($channel, &$version, &$url, &$error)
  695. {
  696. $error = '';
  697. if ($version) {
  698. if (empty($url)) {
  699. $url = $this->baseUrl."/download/{$version}/composer.phar";
  700. }
  701. return true;
  702. }
  703. $this->errHandler->start();
  704. if ($this->downloadVersionData($data, $error)) {
  705. $this->parseVersionData($data, $channel, $version, $url);
  706. }
  707. $this->errHandler->stop();
  708. return empty($error);
  709. }
  710. /**
  711. * Downloads and json-decodes version data
  712. *
  713. * @param null|array $data Downloaded version data, set by method
  714. * @param null|string $error Set by method on failure
  715. *
  716. * @return bool If the operation succeeded
  717. */
  718. protected function downloadVersionData(&$data, &$error)
  719. {
  720. $url = $this->baseUrl.'/versions';
  721. $errFmt = 'The "%s" file could not be %s: %s';
  722. if (!$json = $this->httpClient->get($url)) {
  723. $error = sprintf($errFmt, $url, 'downloaded', $this->errHandler->message);
  724. return false;
  725. }
  726. if (!$data = json_decode($json, true)) {
  727. $error = sprintf($errFmt, $url, 'json-decoded', $this->getJsonError());
  728. return false;
  729. }
  730. return true;
  731. }
  732. /**
  733. * A wrapper around the methods needed to download and save the phar
  734. *
  735. * @param string $url The versioned download url
  736. * @param null|string $signature Set by method on successful download
  737. * @param null|string $error Set by method on failure
  738. *
  739. * @return bool If the operation succeeded
  740. */
  741. protected function downloadToTmp($url, &$signature, &$error)
  742. {
  743. $error = '';
  744. $errFmt = 'The "%s" file could not be downloaded: %s';
  745. $sigUrl = $url.'.sig';
  746. $this->errHandler->start();
  747. if (!$fh = fopen($this->tmpFile, 'w')) {
  748. $error = sprintf('Could not create file "%s": %s', $this->tmpFile, $this->errHandler->message);
  749. } elseif (!$this->getSignature($sigUrl, $signature)) {
  750. $error = sprintf($errFmt, $sigUrl, $this->errHandler->message);
  751. } elseif (!fwrite($fh, $this->httpClient->get($url))) {
  752. $error = sprintf($errFmt, $url, $this->errHandler->message);
  753. }
  754. if (is_resource($fh)) {
  755. fclose($fh);
  756. }
  757. $this->errHandler->stop();
  758. return empty($error);
  759. }
  760. /**
  761. * Verifies the downloaded file and saves it to the target location
  762. *
  763. * @param string $version The composer version downloaded
  764. * @param string $signature The digital signature to check
  765. * @param null|string $error Set by method on failure
  766. *
  767. * @return bool If the operation succeeded
  768. */
  769. protected function verifyAndSave($version, $signature, &$error)
  770. {
  771. $error = '';
  772. if (!$this->validatePhar($this->tmpFile, $pharError)) {
  773. $error = 'The download is corrupt: '.$pharError;
  774. } elseif (!$this->verifySignature($version, $signature, $this->tmpFile)) {
  775. $error = 'Signature mismatch, could not verify the phar file integrity';
  776. } else {
  777. $this->errHandler->start();
  778. if (!rename($this->tmpFile, $this->target)) {
  779. $error = sprintf('Could not write to file "%s": %s', $this->target, $this->errHandler->message);
  780. }
  781. chmod($this->target, 0755);
  782. $this->errHandler->stop();
  783. }
  784. return empty($error);
  785. }
  786. /**
  787. * Parses an array of version data to match the required channel
  788. *
  789. * @param array $data Downloaded version data
  790. * @param mixed $channel Version channel to use
  791. * @param false|string $version Set by method
  792. * @param mixed $url The versioned url, set by method
  793. */
  794. protected function parseVersionData(array $data, $channel, &$version, &$url)
  795. {
  796. foreach ($data[$channel] as $candidate) {
  797. if ($candidate['min-php'] <= PHP_VERSION_ID) {
  798. $version = $candidate['version'];
  799. $url = $this->baseUrl.$candidate['path'];
  800. break;
  801. }
  802. }
  803. if (!$version) {
  804. $error = sprintf(
  805. 'None of the %d %s version(s) of Composer matches your PHP version (%s / ID: %d)',
  806. count($data[$channel]),
  807. $channel,
  808. PHP_VERSION,
  809. PHP_VERSION_ID
  810. );
  811. throw new RuntimeException($error);
  812. }
  813. }
  814. /**
  815. * Downloads the digital signature of required phar file
  816. *
  817. * @param string $url The signature url
  818. * @param null|string $signature Set by method on success
  819. *
  820. * @return bool If the download succeeded
  821. */
  822. protected function getSignature($url, &$signature)
  823. {
  824. if (!$result = $this->disableTls) {
  825. $signature = $this->httpClient->get($url);
  826. if ($signature) {
  827. $signature = json_decode($signature, true);
  828. $signature = base64_decode($signature['sha384']);
  829. $result = true;
  830. }
  831. }
  832. return $result;
  833. }
  834. /**
  835. * Verifies the signature of the downloaded phar
  836. *
  837. * @param string $version The composer versione
  838. * @param string $signature The downloaded digital signature
  839. * @param string $file The temp phar file
  840. *
  841. * @return bool If the operation succeeded
  842. */
  843. protected function verifySignature($version, $signature, $file)
  844. {
  845. if (!$result = $this->disableTls) {
  846. $path = preg_match('{^[0-9a-f]{40}$}', $version) ? $this->pubKeys['dev'] : $this->pubKeys['tags'];
  847. $pubkeyid = openssl_pkey_get_public('file://'.$path);
  848. $result = 1 === openssl_verify(
  849. file_get_contents($file),
  850. $signature,
  851. $pubkeyid,
  852. $this->algo
  853. );
  854. openssl_free_key($pubkeyid);
  855. }
  856. return $result;
  857. }
  858. /**
  859. * Validates the downloaded phar file
  860. *
  861. * @param string $pharFile The temp phar file
  862. * @param null|string $error Set by method on failure
  863. *
  864. * @return bool If the operation succeeded
  865. */
  866. protected function validatePhar($pharFile, &$error)
  867. {
  868. if (ini_get('phar.readonly')) {
  869. return true;
  870. }
  871. try {
  872. // Test the phar validity
  873. $phar = new Phar($pharFile);
  874. // Free the variable to unlock the file
  875. unset($phar);
  876. $result = true;
  877. } catch (Exception $e) {
  878. if (!$e instanceof UnexpectedValueException && !$e instanceof PharException) {
  879. throw $e;
  880. }
  881. $error = $e->getMessage();
  882. $result = false;
  883. }
  884. return $result;
  885. }
  886. /**
  887. * Returns a string representation of the last json error
  888. *
  889. * @return string The error string or code
  890. */
  891. protected function getJsonError()
  892. {
  893. if (function_exists('json_last_error_msg')) {
  894. return json_last_error_msg();
  895. } else {
  896. return 'json_last_error = '.json_last_error();
  897. }
  898. }
  899. /**
  900. * Cleans up resources at the end of the installation
  901. *
  902. * @param bool $result If the installation succeeded
  903. */
  904. protected function cleanUp($result)
  905. {
  906. if (!$result) {
  907. // Output buffered errors
  908. if ($this->quiet) {
  909. $this->outputErrors();
  910. }
  911. // Clean up stuff we created
  912. $this->uninstall();
  913. }
  914. }
  915. /**
  916. * Outputs unique errors when in quiet mode
  917. *
  918. */
  919. protected function outputErrors()
  920. {
  921. $errors = explode(PHP_EOL, ob_get_clean());
  922. $shown = array();
  923. foreach ($errors as $error) {
  924. if ($error && !in_array($error, $shown)) {
  925. out($error, 'error');
  926. $shown[] = $error;
  927. }
  928. }
  929. }
  930. /**
  931. * Uninstalls newly-created files and directories on failure
  932. *
  933. */
  934. protected function uninstall()
  935. {
  936. foreach (array_reverse($this->installs) as $target) {
  937. if (is_file($target)) {
  938. @unlink($target);
  939. } elseif (is_dir($target)) {
  940. @rmdir($target);
  941. }
  942. }
  943. if (file_exists($this->tmpFile)) {
  944. @unlink($this->tmpFile);
  945. }
  946. }
  947. public static function getPKDev()
  948. {
  949. return <<<PKDEV
  950. -----BEGIN PUBLIC KEY-----
  951. MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAnBDHjZS6e0ZMoK3xTD7f
  952. FNCzlXjX/Aie2dit8QXA03pSrOTbaMnxON3hUL47Lz3g1SC6YJEMVHr0zYq4elWi
  953. i3ecFEgzLcj+pZM5X6qWu2Ozz4vWx3JYo1/a/HYdOuW9e3lwS8VtS0AVJA+U8X0A
  954. hZnBmGpltHhO8hPKHgkJtkTUxCheTcbqn4wGHl8Z2SediDcPTLwqezWKUfrYzu1f
  955. o/j3WFwFs6GtK4wdYtiXr+yspBZHO3y1udf8eFFGcb2V3EaLOrtfur6XQVizjOuk
  956. 8lw5zzse1Qp/klHqbDRsjSzJ6iL6F4aynBc6Euqt/8ccNAIz0rLjLhOraeyj4eNn
  957. 8iokwMKiXpcrQLTKH+RH1JCuOVxQ436bJwbSsp1VwiqftPQieN+tzqy+EiHJJmGf
  958. TBAbWcncicCk9q2md+AmhNbvHO4PWbbz9TzC7HJb460jyWeuMEvw3gNIpEo2jYa9
  959. pMV6cVqnSa+wOc0D7pC9a6bne0bvLcm3S+w6I5iDB3lZsb3A9UtRiSP7aGSo7D72
  960. 8tC8+cIgZcI7k9vjvOqH+d7sdOU2yPCnRY6wFh62/g8bDnUpr56nZN1G89GwM4d4
  961. r/TU7BQQIzsZgAiqOGXvVklIgAMiV0iucgf3rNBLjjeNEwNSTTG9F0CtQ+7JLwaE
  962. wSEuAuRm+pRqi8BRnQ/GKUcCAwEAAQ==
  963. -----END PUBLIC KEY-----
  964. PKDEV;
  965. }
  966. public static function getPKTags()
  967. {
  968. return <<<PKTAGS
  969. -----BEGIN PUBLIC KEY-----
  970. MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA0Vi/2K6apCVj76nCnCl2
  971. MQUPdK+A9eqkYBacXo2wQBYmyVlXm2/n/ZsX6pCLYPQTHyr5jXbkQzBw8SKqPdlh
  972. vA7NpbMeNCz7wP/AobvUXM8xQuXKbMDTY2uZ4O7sM+PfGbptKPBGLe8Z8d2sUnTO
  973. bXtX6Lrj13wkRto7st/w/Yp33RHe9SlqkiiS4MsH1jBkcIkEHsRaveZzedUaxY0M
  974. mba0uPhGUInpPzEHwrYqBBEtWvP97t2vtfx8I5qv28kh0Y6t+jnjL1Urid2iuQZf
  975. noCMFIOu4vksK5HxJxxrN0GOmGmwVQjOOtxkwikNiotZGPR4KsVj8NnBrLX7oGuM
  976. nQvGciiu+KoC2r3HDBrpDeBVdOWxDzT5R4iI0KoLzFh2pKqwbY+obNPS2bj+2dgJ
  977. rV3V5Jjry42QOCBN3c88wU1PKftOLj2ECpewY6vnE478IipiEu7EAdK8Zwj2LmTr
  978. RKQUSa9k7ggBkYZWAeO/2Ag0ey3g2bg7eqk+sHEq5ynIXd5lhv6tC5PBdHlWipDK
  979. tl2IxiEnejnOmAzGVivE1YGduYBjN+mjxDVy8KGBrjnz1JPgAvgdwJ2dYw4Rsc/e
  980. TzCFWGk/HM6a4f0IzBWbJ5ot0PIi4amk07IotBXDWwqDiQTwyuGCym5EqWQ2BD95
  981. RGv89BPD+2DLnJysngsvVaUCAwEAAQ==
  982. -----END PUBLIC KEY-----
  983. PKTAGS;
  984. }
  985. }
  986. class ErrorHandler
  987. {
  988. public $message;
  989. protected $active;
  990. /**
  991. * Handle php errors
  992. *
  993. * @param mixed $code The error code
  994. * @param mixed $msg The error message
  995. */
  996. public function handleError($code, $msg)
  997. {
  998. if ($this->message) {
  999. $this->message .= PHP_EOL;
  1000. }
  1001. $this->message .= preg_replace('{^file_get_contents\(.*?\): }', '', $msg);
  1002. }
  1003. /**
  1004. * Starts error-handling if not already active
  1005. *
  1006. * Any message is cleared
  1007. */
  1008. public function start()
  1009. {
  1010. if (!$this->active) {
  1011. set_error_handler(array($this, 'handleError'));
  1012. $this->active = true;
  1013. }
  1014. $this->message = '';
  1015. }
  1016. /**
  1017. * Stops error-handling if active
  1018. *
  1019. * Any message is preserved until the next call to start()
  1020. */
  1021. public function stop()
  1022. {
  1023. if ($this->active) {
  1024. restore_error_handler();
  1025. $this->active = false;
  1026. }
  1027. }
  1028. }
  1029. class HttpClient {
  1030. private $options = array('http' => array());
  1031. private $disableTls = false;
  1032. public function __construct($disableTls = false, $cafile = false)
  1033. {
  1034. $this->disableTls = $disableTls;
  1035. if ($this->disableTls === false) {
  1036. if (!empty($cafile) && !is_dir($cafile)) {
  1037. if (!is_readable($cafile) || !validateCaFile(file_get_contents($cafile))) {
  1038. throw new RuntimeException('The configured cafile (' .$cafile. ') was not valid or could not be read.');
  1039. }
  1040. }
  1041. $options = $this->getTlsStreamContextDefaults($cafile);
  1042. $this->options = array_replace_recursive($this->options, $options);
  1043. }
  1044. }
  1045. public function get($url)
  1046. {
  1047. $context = $this->getStreamContext($url);
  1048. $result = file_get_contents($url, false, $context);
  1049. if ($result && extension_loaded('zlib')) {
  1050. $decode = false;
  1051. foreach ($http_response_header as $header) {
  1052. if (preg_match('{^content-encoding: *gzip *$}i', $header)) {
  1053. $decode = true;
  1054. continue;
  1055. } elseif (preg_match('{^HTTP/}i', $header)) {
  1056. $decode = false;
  1057. }
  1058. }
  1059. if ($decode) {
  1060. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  1061. $result = zlib_decode($result);
  1062. } else {
  1063. // work around issue with gzuncompress & co that do not work with all gzip checksums
  1064. $result = file_get_contents('compress.zlib://data:application/octet-stream;base64,'.base64_encode($result));
  1065. }
  1066. if (!$result) {
  1067. throw new RuntimeException('Failed to decode zlib stream');
  1068. }
  1069. }
  1070. }
  1071. return $result;
  1072. }
  1073. protected function getStreamContext($url)
  1074. {
  1075. if ($this->disableTls === false) {
  1076. $host = parse_url($url, PHP_URL_HOST);
  1077. if (PHP_VERSION_ID < 50600) {
  1078. $this->options['ssl']['CN_match'] = $host;
  1079. $this->options['ssl']['SNI_server_name'] = $host;
  1080. }
  1081. }
  1082. // Keeping the above mostly isolated from the code copied from Composer.
  1083. return $this->getMergedStreamContext($url);
  1084. }
  1085. protected function getTlsStreamContextDefaults($cafile)
  1086. {
  1087. $ciphers = implode(':', array(
  1088. 'ECDHE-RSA-AES128-GCM-SHA256',
  1089. 'ECDHE-ECDSA-AES128-GCM-SHA256',
  1090. 'ECDHE-RSA-AES256-GCM-SHA384',
  1091. 'ECDHE-ECDSA-AES256-GCM-SHA384',
  1092. 'DHE-RSA-AES128-GCM-SHA256',
  1093. 'DHE-DSS-AES128-GCM-SHA256',
  1094. 'kEDH+AESGCM',
  1095. 'ECDHE-RSA-AES128-SHA256',
  1096. 'ECDHE-ECDSA-AES128-SHA256',
  1097. 'ECDHE-RSA-AES128-SHA',
  1098. 'ECDHE-ECDSA-AES128-SHA',
  1099. 'ECDHE-RSA-AES256-SHA384',
  1100. 'ECDHE-ECDSA-AES256-SHA384',
  1101. 'ECDHE-RSA-AES256-SHA',
  1102. 'ECDHE-ECDSA-AES256-SHA',
  1103. 'DHE-RSA-AES128-SHA256',
  1104. 'DHE-RSA-AES128-SHA',
  1105. 'DHE-DSS-AES128-SHA256',
  1106. 'DHE-RSA-AES256-SHA256',
  1107. 'DHE-DSS-AES256-SHA',
  1108. 'DHE-RSA-AES256-SHA',
  1109. 'AES128-GCM-SHA256',
  1110. 'AES256-GCM-SHA384',
  1111. 'AES128-SHA256',
  1112. 'AES256-SHA256',
  1113. 'AES128-SHA',
  1114. 'AES256-SHA',
  1115. 'AES',
  1116. 'CAMELLIA',
  1117. 'DES-CBC3-SHA',
  1118. '!aNULL',
  1119. '!eNULL',
  1120. '!EXPORT',
  1121. '!DES',
  1122. '!RC4',
  1123. '!MD5',
  1124. '!PSK',
  1125. '!aECDH',
  1126. '!EDH-DSS-DES-CBC3-SHA',
  1127. '!EDH-RSA-DES-CBC3-SHA',
  1128. '!KRB5-DES-CBC3-SHA',
  1129. ));
  1130. /**
  1131. * CN_match and SNI_server_name are only known once a URL is passed.
  1132. * They will be set in the getOptionsForUrl() method which receives a URL.
  1133. *
  1134. * cafile or capath can be overridden by passing in those options to constructor.
  1135. */
  1136. $options = array(
  1137. 'ssl' => array(
  1138. 'ciphers' => $ciphers,
  1139. 'verify_peer' => true,
  1140. 'verify_depth' => 7,
  1141. 'SNI_enabled' => true,
  1142. )
  1143. );
  1144. /**
  1145. * Attempt to find a local cafile or throw an exception.
  1146. * The user may go download one if this occurs.
  1147. */
  1148. if (!$cafile) {
  1149. $cafile = self::getSystemCaRootBundlePath();
  1150. }
  1151. if (is_dir($cafile)) {
  1152. $options['ssl']['capath'] = $cafile;
  1153. } elseif ($cafile) {
  1154. $options['ssl']['cafile'] = $cafile;
  1155. } else {
  1156. throw new RuntimeException('A valid cafile could not be located automatically.');
  1157. }
  1158. /**
  1159. * Disable TLS compression to prevent CRIME attacks where supported.
  1160. */
  1161. if (version_compare(PHP_VERSION, '5.4.13') >= 0) {
  1162. $options['ssl']['disable_compression'] = true;
  1163. }
  1164. return $options;
  1165. }
  1166. /**
  1167. * function copied from Composer\Util\StreamContextFactory::getContext
  1168. *
  1169. * Any changes should be applied there as well, or backported here.
  1170. *
  1171. * @param string $url URL the context is to be used for
  1172. * @return resource Default context
  1173. * @throws \RuntimeException if https proxy required and OpenSSL uninstalled
  1174. */
  1175. protected function getMergedStreamContext($url)
  1176. {
  1177. $options = $this->options;
  1178. // Handle system proxy
  1179. if (!empty($_SERVER['HTTP_PROXY']) || !empty($_SERVER['http_proxy'])) {
  1180. // Some systems seem to rely on a lowercased version instead...
  1181. $proxy = parse_url(!empty($_SERVER['http_proxy']) ? $_SERVER['http_proxy'] : $_SERVER['HTTP_PROXY']);
  1182. }
  1183. if (!empty($proxy)) {
  1184. $proxyURL = isset($proxy['scheme']) ? $proxy['scheme'] . '://' : '';
  1185. $proxyURL .= isset($proxy['host']) ? $proxy['host'] : '';
  1186. if (isset($proxy['port'])) {
  1187. $proxyURL .= ":" . $proxy['port'];
  1188. } elseif ('http://' == substr($proxyURL, 0, 7)) {
  1189. $proxyURL .= ":80";
  1190. } elseif ('https://' == substr($proxyURL, 0, 8)) {
  1191. $proxyURL .= ":443";
  1192. }
  1193. // http(s):// is not supported in proxy
  1194. $proxyURL = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxyURL);
  1195. if (0 === strpos($proxyURL, 'ssl:') && !extension_loaded('openssl')) {
  1196. throw new RuntimeException('You must enable the openssl extension to use a proxy over https');
  1197. }
  1198. $options['http'] = array(
  1199. 'proxy' => $proxyURL,
  1200. );
  1201. // enabled request_fulluri unless it is explicitly disabled
  1202. switch (parse_url($url, PHP_URL_SCHEME)) {
  1203. case 'http': // default request_fulluri to true
  1204. $reqFullUriEnv = getenv('HTTP_PROXY_REQUEST_FULLURI');
  1205. if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) {
  1206. $options['http']['request_fulluri'] = true;
  1207. }
  1208. break;
  1209. case 'https': // default request_fulluri to true
  1210. $reqFullUriEnv = getenv('HTTPS_PROXY_REQUEST_FULLURI');
  1211. if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) {
  1212. $options['http']['request_fulluri'] = true;
  1213. }
  1214. break;
  1215. }
  1216. // handle proxy auth if present
  1217. if (isset($proxy['user'])) {
  1218. $auth = rawurldecode($proxy['user']);
  1219. if (isset($proxy['pass'])) {
  1220. $auth .= ':' . rawurldecode($proxy['pass']);
  1221. }
  1222. $auth = base64_encode($auth);
  1223. $options['http']['header'] = "Proxy-Authorization: Basic {$auth}\r\n";
  1224. }
  1225. }
  1226. if (isset($options['http']['header'])) {
  1227. $options['http']['header'] .= "Connection: close\r\n";
  1228. } else {
  1229. $options['http']['header'] = "Connection: close\r\n";
  1230. }
  1231. if (extension_loaded('zlib')) {
  1232. $options['http']['header'] .= "Accept-Encoding: gzip\r\n";
  1233. }
  1234. $options['http']['header'] .= "User-Agent: Composer Installer\r\n";
  1235. $options['http']['protocol_version'] = 1.1;
  1236. $options['http']['timeout'] = 600;
  1237. return stream_context_create($options);
  1238. }
  1239. /**
  1240. * This method was adapted from Sslurp.
  1241. * https://github.com/EvanDotPro/Sslurp
  1242. *
  1243. * (c) Evan Coury <me@evancoury.com>
  1244. *
  1245. * For the full copyright and license information, please see below:
  1246. *
  1247. * Copyright (c) 2013, Evan Coury
  1248. * All rights reserved.
  1249. *
  1250. * Redistribution and use in source and binary forms, with or without modification,
  1251. * are permitted provided that the following conditions are met:
  1252. *
  1253. * * Redistributions of source code must retain the above copyright notice,
  1254. * this list of conditions and the following disclaimer.
  1255. *
  1256. * * Redistributions in binary form must reproduce the above copyright notice,
  1257. * this list of conditions and the following disclaimer in the documentation
  1258. * and/or other materials provided with the distribution.
  1259. *
  1260. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  1261. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  1262. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  1263. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  1264. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  1265. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  1266. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  1267. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1268. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1269. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1270. */
  1271. public static function getSystemCaRootBundlePath()
  1272. {
  1273. static $caPath = null;
  1274. if ($caPath !== null) {
  1275. return $caPath;
  1276. }
  1277. // If SSL_CERT_FILE env variable points to a valid certificate/bundle, use that.
  1278. // This mimics how OpenSSL uses the SSL_CERT_FILE env variable.
  1279. $envCertFile = getenv('SSL_CERT_FILE');
  1280. if ($envCertFile && is_readable($envCertFile) && validateCaFile(file_get_contents($envCertFile))) {
  1281. return $caPath = $envCertFile;
  1282. }
  1283. // If SSL_CERT_DIR env variable points to a valid certificate/bundle, use that.
  1284. // This mimics how OpenSSL uses the SSL_CERT_FILE env variable.
  1285. $envCertDir = getenv('SSL_CERT_DIR');
  1286. if ($envCertDir && is_dir($envCertDir) && is_readable($envCertDir)) {
  1287. return $caPath = $envCertDir;
  1288. }
  1289. $configured = ini_get('openssl.cafile');
  1290. if ($configured && strlen($configured) > 0 && is_readable($configured) && validateCaFile(file_get_contents($configured))) {
  1291. return $caPath = $configured;
  1292. }
  1293. $configured = ini_get('openssl.capath');
  1294. if ($configured && is_dir($configured) && is_readable($configured)) {
  1295. return $caPath = $configured;
  1296. }
  1297. $caBundlePaths = array(
  1298. '/etc/pki/tls/certs/ca-bundle.crt', // Fedora, RHEL, CentOS (ca-certificates package)
  1299. '/etc/ssl/certs/ca-certificates.crt', // Debian, Ubuntu, Gentoo, Arch Linux (ca-certificates package)
  1300. '/etc/ssl/ca-bundle.pem', // SUSE, openSUSE (ca-certificates package)
  1301. '/usr/local/share/certs/ca-root-nss.crt', // FreeBSD (ca_root_nss_package)
  1302. '/usr/ssl/certs/ca-bundle.crt', // Cygwin
  1303. '/opt/local/share/curl/curl-ca-bundle.crt', // OS X macports, curl-ca-bundle package
  1304. '/usr/local/share/curl/curl-ca-bundle.crt', // Default cURL CA bunde path (without --with-ca-bundle option)
  1305. '/usr/share/ssl/certs/ca-bundle.crt', // Really old RedHat?
  1306. '/etc/ssl/cert.pem', // OpenBSD
  1307. '/usr/local/etc/ssl/cert.pem', // FreeBSD 10.x
  1308. );
  1309. foreach ($caBundlePaths as $caBundle) {
  1310. if (@is_readable($caBundle) && validateCaFile(file_get_contents($caBundle))) {
  1311. return $caPath = $caBundle;
  1312. }
  1313. }
  1314. foreach ($caBundlePaths as $caBundle) {
  1315. $caBundle = dirname($caBundle);
  1316. if (is_dir($caBundle) && glob($caBundle.'/*')) {
  1317. return $caPath = $caBundle;
  1318. }
  1319. }
  1320. return $caPath = false;
  1321. }
  1322. public static function getPackagedCaFile()
  1323. {
  1324. return <<<CACERT
  1325. ##
  1326. ## Bundle of CA Root Certificates
  1327. ##
  1328. ## Certificate data from Mozilla as of: Wed Jun 20 03:12:06 2018 GMT
  1329. ##
  1330. ## This is a bundle of X.509 certificates of public Certificat