Config.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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;
  12. use Composer\Config\ConfigSourceInterface;
  13. use Composer\Downloader\TransportException;
  14. use Composer\IO\IOInterface;
  15. use Composer\Util\Platform;
  16. /**
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. class Config
  20. {
  21. const RELATIVE_PATHS = 1;
  22. public static $defaultConfig = array(
  23. 'process-timeout' => 300,
  24. 'use-include-path' => false,
  25. 'preferred-install' => 'auto',
  26. 'notify-on-install' => true,
  27. 'github-protocols' => array('https', 'ssh', 'git'),
  28. 'vendor-dir' => 'vendor',
  29. 'bin-dir' => '{$vendor-dir}/bin',
  30. 'cache-dir' => '{$home}/cache',
  31. 'data-dir' => '{$home}',
  32. 'cache-files-dir' => '{$cache-dir}/files',
  33. 'cache-repo-dir' => '{$cache-dir}/repo',
  34. 'cache-vcs-dir' => '{$cache-dir}/vcs',
  35. 'cache-ttl' => 15552000, // 6 months
  36. 'cache-files-ttl' => null, // fallback to cache-ttl
  37. 'cache-files-maxsize' => '300MiB',
  38. 'bin-compat' => 'auto',
  39. 'discard-changes' => false,
  40. 'autoloader-suffix' => null,
  41. 'sort-packages' => false,
  42. 'optimize-autoloader' => false,
  43. 'classmap-authoritative' => false,
  44. 'apcu-autoloader' => false,
  45. 'prepend-autoloader' => true,
  46. 'github-domains' => array('github.com'),
  47. 'bitbucket-expose-hostname' => true,
  48. 'disable-tls' => false,
  49. 'secure-http' => true,
  50. 'cafile' => null,
  51. 'capath' => null,
  52. 'github-expose-hostname' => true,
  53. 'gitlab-domains' => array('gitlab.com'),
  54. 'store-auths' => 'prompt',
  55. 'platform' => array(),
  56. 'archive-format' => 'tar',
  57. 'archive-dir' => '.',
  58. 'htaccess-protect' => true,
  59. // valid keys without defaults (auth config stuff):
  60. // bitbucket-oauth
  61. // github-oauth
  62. // gitlab-oauth
  63. // gitlab-token
  64. // http-basic
  65. );
  66. public static $defaultRepositories = array(
  67. 'packagist.org' => array(
  68. 'type' => 'composer',
  69. 'url' => 'https?://repo.packagist.org',
  70. 'allow_ssl_downgrade' => true,
  71. ),
  72. );
  73. private $config;
  74. private $baseDir;
  75. private $repositories;
  76. /** @var ConfigSourceInterface */
  77. private $configSource;
  78. /** @var ConfigSourceInterface */
  79. private $authConfigSource;
  80. private $useEnvironment;
  81. private $warnedHosts = array();
  82. /**
  83. * @param bool $useEnvironment Use COMPOSER_ environment variables to replace config settings
  84. * @param string $baseDir Optional base directory of the config
  85. */
  86. public function __construct($useEnvironment = true, $baseDir = null)
  87. {
  88. // load defaults
  89. $this->config = static::$defaultConfig;
  90. $this->repositories = static::$defaultRepositories;
  91. $this->useEnvironment = (bool) $useEnvironment;
  92. $this->baseDir = $baseDir;
  93. }
  94. public function setConfigSource(ConfigSourceInterface $source)
  95. {
  96. $this->configSource = $source;
  97. }
  98. public function getConfigSource()
  99. {
  100. return $this->configSource;
  101. }
  102. public function setAuthConfigSource(ConfigSourceInterface $source)
  103. {
  104. $this->authConfigSource = $source;
  105. }
  106. public function getAuthConfigSource()
  107. {
  108. return $this->authConfigSource;
  109. }
  110. /**
  111. * Merges new config values with the existing ones (overriding)
  112. *
  113. * @param array $config
  114. */
  115. public function merge($config)
  116. {
  117. // override defaults with given config
  118. if (!empty($config['config']) && is_array($config['config'])) {
  119. foreach ($config['config'] as $key => $val) {
  120. if (in_array($key, array('bitbucket-oauth', 'github-oauth', 'gitlab-oauth', 'gitlab-token', 'http-basic')) && isset($this->config[$key])) {
  121. $this->config[$key] = array_merge($this->config[$key], $val);
  122. } elseif ('preferred-install' === $key && isset($this->config[$key])) {
  123. if (is_array($val) || is_array($this->config[$key])) {
  124. if (is_string($val)) {
  125. $val = array('*' => $val);
  126. }
  127. if (is_string($this->config[$key])) {
  128. $this->config[$key] = array('*' => $this->config[$key]);
  129. }
  130. $this->config[$key] = array_merge($this->config[$key], $val);
  131. // the full match pattern needs to be last
  132. if (isset($this->config[$key]['*'])) {
  133. $wildcard = $this->config[$key]['*'];
  134. unset($this->config[$key]['*']);
  135. $this->config[$key]['*'] = $wildcard;
  136. }
  137. } else {
  138. $this->config[$key] = $val;
  139. }
  140. } else {
  141. $this->config[$key] = $val;
  142. }
  143. }
  144. }
  145. if (!empty($config['repositories']) && is_array($config['repositories'])) {
  146. $this->repositories = array_reverse($this->repositories, true);
  147. $newRepos = array_reverse($config['repositories'], true);
  148. foreach ($newRepos as $name => $repository) {
  149. // disable a repository by name
  150. if (false === $repository) {
  151. $this->disableRepoByName($name);
  152. continue;
  153. }
  154. // disable a repository with an anonymous {"name": false} repo
  155. if (is_array($repository) && 1 === count($repository) && false === current($repository)) {
  156. $this->disableRepoByName(key($repository));
  157. continue;
  158. }
  159. // store repo
  160. if (is_int($name)) {
  161. $this->repositories[] = $repository;
  162. } else {
  163. if ($name === 'packagist') { // BC support for default "packagist" named repo
  164. $this->repositories[$name . '.org'] = $repository;
  165. } else {
  166. $this->repositories[$name] = $repository;
  167. }
  168. }
  169. }
  170. $this->repositories = array_reverse($this->repositories, true);
  171. }
  172. }
  173. /**
  174. * @return array
  175. */
  176. public function getRepositories()
  177. {
  178. return $this->repositories;
  179. }
  180. /**
  181. * Returns a setting
  182. *
  183. * @param string $key
  184. * @param int $flags Options (see class constants)
  185. * @throws \RuntimeException
  186. * @return mixed
  187. */
  188. public function get($key, $flags = 0)
  189. {
  190. switch ($key) {
  191. case 'vendor-dir':
  192. case 'bin-dir':
  193. case 'process-timeout':
  194. case 'data-dir':
  195. case 'cache-dir':
  196. case 'cache-files-dir':
  197. case 'cache-repo-dir':
  198. case 'cache-vcs-dir':
  199. case 'cafile':
  200. case 'capath':
  201. case 'htaccess-protect':
  202. // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
  203. $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
  204. $val = $this->getComposerEnv($env);
  205. $val = rtrim((string) $this->process(false !== $val ? $val : $this->config[$key], $flags), '/\\');
  206. $val = Platform::expandPath($val);
  207. if (substr($key, -4) !== '-dir') {
  208. return $val;
  209. }
  210. return (($flags & self::RELATIVE_PATHS) == self::RELATIVE_PATHS) ? $val : $this->realpath($val);
  211. case 'cache-ttl':
  212. return (int) $this->config[$key];
  213. case 'cache-files-maxsize':
  214. if (!preg_match('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $this->config[$key], $matches)) {
  215. throw new \RuntimeException(
  216. "Could not parse the value of 'cache-files-maxsize': {$this->config[$key]}"
  217. );
  218. }
  219. $size = $matches[1];
  220. if (isset($matches[2])) {
  221. switch (strtolower($matches[2])) {
  222. case 'g':
  223. $size *= 1024;
  224. // intentional fallthrough
  225. // no break
  226. case 'm':
  227. $size *= 1024;
  228. // intentional fallthrough
  229. // no break
  230. case 'k':
  231. $size *= 1024;
  232. break;
  233. }
  234. }
  235. return $size;
  236. case 'cache-files-ttl':
  237. if (isset($this->config[$key])) {
  238. return (int) $this->config[$key];
  239. }
  240. return (int) $this->config['cache-ttl'];
  241. case 'home':
  242. $val = preg_replace('#^(\$HOME|~)(/|$)#', rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\') . '/', $this->config[$key]);
  243. return rtrim($this->process($val, $flags), '/\\');
  244. case 'bin-compat':
  245. $value = $this->getComposerEnv('COMPOSER_BIN_COMPAT') ?: $this->config[$key];
  246. if (!in_array($value, array('auto', 'full'))) {
  247. throw new \RuntimeException(
  248. "Invalid value for 'bin-compat': {$value}. Expected auto, full"
  249. );
  250. }
  251. return $value;
  252. case 'discard-changes':
  253. if ($env = $this->getComposerEnv('COMPOSER_DISCARD_CHANGES')) {
  254. if (!in_array($env, array('stash', 'true', 'false', '1', '0'), true)) {
  255. throw new \RuntimeException(
  256. "Invalid value for COMPOSER_DISCARD_CHANGES: {$env}. Expected 1, 0, true, false or stash"
  257. );
  258. }
  259. if ('stash' === $env) {
  260. return 'stash';
  261. }
  262. // convert string value to bool
  263. return $env !== 'false' && (bool) $env;
  264. }
  265. if (!in_array($this->config[$key], array(true, false, 'stash'), true)) {
  266. throw new \RuntimeException(
  267. "Invalid value for 'discard-changes': {$this->config[$key]}. Expected true, false or stash"
  268. );
  269. }
  270. return $this->config[$key];
  271. case 'github-protocols':
  272. $protos = $this->config['github-protocols'];
  273. if ($this->config['secure-http'] && false !== ($index = array_search('git', $protos))) {
  274. unset($protos[$index]);
  275. }
  276. if (reset($protos) === 'http') {
  277. throw new \RuntimeException('The http protocol for github is not available anymore, update your config\'s github-protocols to use "https", "git" or "ssh"');
  278. }
  279. return $protos;
  280. case 'disable-tls':
  281. return $this->config[$key] !== 'false' && (bool) $this->config[$key];
  282. case 'secure-http':
  283. return $this->config[$key] !== 'false' && (bool) $this->config[$key];
  284. default:
  285. if (!isset($this->config[$key])) {
  286. return null;
  287. }
  288. return $this->process($this->config[$key], $flags);
  289. }
  290. }
  291. public function all($flags = 0)
  292. {
  293. $all = array(
  294. 'repositories' => $this->getRepositories(),
  295. );
  296. foreach (array_keys($this->config) as $key) {
  297. $all['config'][$key] = $this->get($key, $flags);
  298. }
  299. return $all;
  300. }
  301. public function raw()
  302. {
  303. return array(
  304. 'repositories' => $this->getRepositories(),
  305. 'config' => $this->config,
  306. );
  307. }
  308. /**
  309. * Checks whether a setting exists
  310. *
  311. * @param string $key
  312. * @return bool
  313. */
  314. public function has($key)
  315. {
  316. return array_key_exists($key, $this->config);
  317. }
  318. /**
  319. * Replaces {$refs} inside a config string
  320. *
  321. * @param string|int|null $value a config string that can contain {$refs-to-other-config}
  322. * @param int $flags Options (see class constants)
  323. * @return string|int|null
  324. */
  325. private function process($value, $flags)
  326. {
  327. $config = $this;
  328. if (!is_string($value)) {
  329. return $value;
  330. }
  331. return preg_replace_callback('#\{\$(.+)\}#', function ($match) use ($config, $flags) {
  332. return $config->get($match[1], $flags);
  333. }, $value);
  334. }
  335. /**
  336. * Turns relative paths in absolute paths without realpath()
  337. *
  338. * Since the dirs might not exist yet we can not call realpath or it will fail.
  339. *
  340. * @param string $path
  341. * @return string
  342. */
  343. private function realpath($path)
  344. {
  345. if (preg_match('{^(?:/|[a-z]:|[a-z0-9.]+://)}i', $path)) {
  346. return $path;
  347. }
  348. return $this->baseDir . '/' . $path;
  349. }
  350. /**
  351. * Reads the value of a Composer environment variable
  352. *
  353. * This should be used to read COMPOSER_ environment variables
  354. * that overload config values.
  355. *
  356. * @param string $var
  357. * @return string|bool
  358. */
  359. private function getComposerEnv($var)
  360. {
  361. if ($this->useEnvironment) {
  362. return getenv($var);
  363. }
  364. return false;
  365. }
  366. private function disableRepoByName($name)
  367. {
  368. if (isset($this->repositories[$name])) {
  369. unset($this->repositories[$name]);
  370. } elseif ($name === 'packagist') { // BC support for default "packagist" named repo
  371. unset($this->repositories['packagist.org']);
  372. }
  373. }
  374. /**
  375. * Validates that the passed URL is allowed to be used by current config, or throws an exception.
  376. *
  377. * @param string $url
  378. * @param IOInterface $io
  379. */
  380. public function prohibitUrlByConfig($url, IOInterface $io = null)
  381. {
  382. // Return right away if the URL is malformed or custom (see issue #5173)
  383. if (false === filter_var($url, FILTER_VALIDATE_URL)) {
  384. return;
  385. }
  386. // Extract scheme and throw exception on known insecure protocols
  387. $scheme = parse_url($url, PHP_URL_SCHEME);
  388. if (in_array($scheme, array('http', 'git', 'ftp', 'svn'))) {
  389. if ($this->get('secure-http')) {
  390. throw new TransportException("Your configuration does not allow connections to $url. See https://getcomposer.org/doc/06-config.md#secure-http for details.");
  391. } elseif ($io) {
  392. $host = parse_url($url, PHP_URL_HOST);
  393. if (!isset($this->warnedHosts[$host])) {
  394. $io->writeError("<warning>Warning: Accessing $host over $scheme which is an insecure protocol.</warning>");
  395. }
  396. $this->warnedHosts[$host] = true;
  397. }
  398. }
  399. }
  400. }