DiagnoseCommand.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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\Command;
  12. use Composer\Composer;
  13. use Composer\Factory;
  14. use Composer\Downloader\TransportException;
  15. use Composer\Plugin\CommandEvent;
  16. use Composer\Plugin\PluginEvents;
  17. use Composer\Util\ConfigValidator;
  18. use Composer\Util\RemoteFilesystem;
  19. use Composer\Util\StreamContextFactory;
  20. use Symfony\Component\Console\Input\InputInterface;
  21. use Symfony\Component\Console\Output\OutputInterface;
  22. /**
  23. * @author Jordi Boggiano <j.boggiano@seld.be>
  24. */
  25. class DiagnoseCommand extends Command
  26. {
  27. protected $rfs;
  28. protected $failures = 0;
  29. protected function configure()
  30. {
  31. $this
  32. ->setName('diagnose')
  33. ->setDescription('Diagnoses the system to identify common errors.')
  34. ->setHelp(<<<EOT
  35. The <info>diagnose</info> command checks common errors to help debugging problems.
  36. EOT
  37. )
  38. ;
  39. }
  40. protected function execute(InputInterface $input, OutputInterface $output)
  41. {
  42. $this->rfs = new RemoteFilesystem($this->getIO());
  43. $output->write('Checking platform settings: ');
  44. $this->outputResult($output, $this->checkPlatform());
  45. $output->write('Checking http connectivity: ');
  46. $this->outputResult($output, $this->checkHttp());
  47. $opts = stream_context_get_options(StreamContextFactory::getContext('http://example.org'));
  48. if (!empty($opts['http']['proxy'])) {
  49. $output->write('Checking HTTP proxy: ');
  50. $this->outputResult($output, $this->checkHttpProxy());
  51. $output->write('Checking HTTP proxy support for request_fulluri: ');
  52. $this->outputResult($output, $this->checkHttpProxyFullUriRequestParam());
  53. $output->write('Checking HTTPS proxy support for request_fulluri: ');
  54. $this->outputResult($output, $this->checkHttpsProxyFullUriRequestParam());
  55. }
  56. $composer = $this->getComposer(false);
  57. if ($composer) {
  58. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'diagnose', $input, $output);
  59. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  60. $output->write('Checking composer.json: ');
  61. $this->outputResult($output, $this->checkComposerSchema());
  62. }
  63. if ($composer) {
  64. $config = $composer->getConfig();
  65. } else {
  66. $config = Factory::createConfig();
  67. }
  68. if ($oauth = $config->get('github-oauth')) {
  69. foreach ($oauth as $domain => $token) {
  70. $output->write('Checking '.$domain.' oauth access: ');
  71. $this->outputResult($output, $this->checkGithubOauth($domain, $token));
  72. }
  73. }
  74. $output->write('Checking disk free space: ');
  75. $this->outputResult($output, $this->checkDiskSpace($config));
  76. $output->write('Checking composer version: ');
  77. $this->outputResult($output, $this->checkVersion());
  78. return $this->failures;
  79. }
  80. private function checkComposerSchema()
  81. {
  82. $validator = new ConfigValidator($this->getIO());
  83. list($errors, $publishErrors, $warnings) = $validator->validate(Factory::getComposerFile());
  84. if ($errors || $publishErrors || $warnings) {
  85. $messages = array(
  86. 'error' => array_merge($errors, $publishErrors),
  87. 'warning' => $warnings,
  88. );
  89. $output = '';
  90. foreach ($messages as $style => $msgs) {
  91. foreach ($msgs as $msg) {
  92. $output .= '<' . $style . '>' . $msg . '</' . $style . '>' . PHP_EOL;
  93. }
  94. }
  95. return rtrim($output);
  96. }
  97. return true;
  98. }
  99. private function checkHttp()
  100. {
  101. $protocol = extension_loaded('openssl') ? 'https' : 'http';
  102. try {
  103. $json = $this->rfs->getContents('packagist.org', $protocol . '://packagist.org/packages.json', false);
  104. } catch (\Exception $e) {
  105. return $e;
  106. }
  107. return true;
  108. }
  109. private function checkHttpProxy()
  110. {
  111. $protocol = extension_loaded('openssl') ? 'https' : 'http';
  112. try {
  113. $json = json_decode($this->rfs->getContents('packagist.org', $protocol . '://packagist.org/packages.json', false), true);
  114. $hash = reset($json['provider-includes']);
  115. $hash = $hash['sha256'];
  116. $path = str_replace('%hash%', $hash, key($json['provider-includes']));
  117. $provider = $this->rfs->getContents('packagist.org', $protocol . '://packagist.org/'.$path, false);
  118. if (hash('sha256', $provider) !== $hash) {
  119. return 'It seems that your proxy is modifying http traffic on the fly';
  120. }
  121. } catch (\Exception $e) {
  122. return $e;
  123. }
  124. return true;
  125. }
  126. /**
  127. * Due to various proxy servers configurations, some servers can't handle non-standard HTTP "http_proxy_request_fulluri" parameter,
  128. * and will return error 500/501 (as not implemented), see discussion @ https://github.com/composer/composer/pull/1825.
  129. * This method will test, if you need to disable this parameter via setting extra environment variable in your system.
  130. *
  131. * @return bool|string
  132. */
  133. private function checkHttpProxyFullUriRequestParam()
  134. {
  135. $url = 'http://packagist.org/packages.json';
  136. try {
  137. $this->rfs->getContents('packagist.org', $url, false);
  138. } catch (TransportException $e) {
  139. try {
  140. $this->rfs->getContents('packagist.org', $url, false, array('http' => array('request_fulluri' => false)));
  141. } catch (TransportException $e) {
  142. return 'Unable to assert the situation, maybe packagist.org is down ('.$e->getMessage().')';
  143. }
  144. return 'It seems there is a problem with your proxy server, try setting the "HTTP_PROXY_REQUEST_FULLURI" and "HTTPS_PROXY_REQUEST_FULLURI" environment variables to "false"';
  145. }
  146. return true;
  147. }
  148. /**
  149. * Due to various proxy servers configurations, some servers can't handle non-standard HTTP "http_proxy_request_fulluri" parameter,
  150. * and will return error 500/501 (as not implemented), see discussion @ https://github.com/composer/composer/pull/1825.
  151. * This method will test, if you need to disable this parameter via setting extra environment variable in your system.
  152. *
  153. * @return bool|string
  154. */
  155. private function checkHttpsProxyFullUriRequestParam()
  156. {
  157. if (!extension_loaded('openssl')) {
  158. return 'You need the openssl extension installed for this check';
  159. }
  160. $url = 'https://api.github.com/repos/Seldaek/jsonlint/zipball/1.0.0';
  161. try {
  162. $rfcResult = $this->rfs->getContents('api.github.com', $url, false);
  163. } catch (TransportException $e) {
  164. try {
  165. $this->rfs->getContents('api.github.com', $url, false, array('http' => array('request_fulluri' => false)));
  166. } catch (TransportException $e) {
  167. return 'Unable to assert the situation, maybe github is down ('.$e->getMessage().')';
  168. }
  169. return 'It seems there is a problem with your proxy server, try setting the "HTTPS_PROXY_REQUEST_FULLURI" environment variable to "false"';
  170. }
  171. return true;
  172. }
  173. private function checkGithubOauth($domain, $token)
  174. {
  175. $this->getIO()->setAuthentication($domain, $token, 'x-oauth-basic');
  176. try {
  177. $url = $domain === 'github.com' ? 'https://api.'.$domain.'/user/repos' : 'https://'.$domain.'/api/v3/user/repos';
  178. return $this->rfs->getContents($domain, $url, false) ? true : 'Unexpected error';
  179. } catch (\Exception $e) {
  180. if ($e instanceof TransportException && $e->getCode() === 401) {
  181. return '<warning>The oauth token for '.$domain.' seems invalid, run "composer config --global --unset github-oauth.'.$domain.'" to remove it</warning>';
  182. }
  183. return $e;
  184. }
  185. }
  186. private function checkDiskSpace($config)
  187. {
  188. $minSpaceFree = 1024*1024;
  189. if ((($df = @disk_free_space($dir = $config->get('home'))) !== false && $df < $minSpaceFree)
  190. || (($df = @disk_free_space($dir = $config->get('vendor-dir'))) !== false && $df < $minSpaceFree)
  191. ) {
  192. return '<error>The disk hosting '.$dir.' is full</error>';
  193. }
  194. return true;
  195. }
  196. private function checkVersion()
  197. {
  198. $protocol = extension_loaded('openssl') ? 'https' : 'http';
  199. $latest = trim($this->rfs->getContents('getcomposer.org', $protocol . '://getcomposer.org/version', false));
  200. if (Composer::VERSION !== $latest && Composer::VERSION !== '@package_version@') {
  201. return '<warning>Your are not running the latest version</warning>';
  202. }
  203. return true;
  204. }
  205. private function outputResult(OutputInterface $output, $result)
  206. {
  207. if (true === $result) {
  208. $output->writeln('<info>OK</info>');
  209. } else {
  210. $this->failures++;
  211. $output->writeln('<error>FAIL</error>');
  212. if ($result instanceof \Exception) {
  213. $output->writeln('['.get_class($result).'] '.$result->getMessage());
  214. } elseif ($result) {
  215. $output->writeln($result);
  216. }
  217. }
  218. }
  219. private function checkPlatform()
  220. {
  221. $output = '';
  222. $out = function ($msg, $style) use (&$output) {
  223. $output .= '<'.$style.'>'.$msg.'</'.$style.'>';
  224. };
  225. // code below taken from getcomposer.org/installer, any changes should be made there and replicated here
  226. $errors = array();
  227. $warnings = array();
  228. $iniPath = php_ini_loaded_file();
  229. $displayIniMessage = false;
  230. if ($iniPath) {
  231. $iniMessage = PHP_EOL.PHP_EOL.'The php.ini used by your command-line PHP is: ' . $iniPath;
  232. } else {
  233. $iniMessage = PHP_EOL.PHP_EOL.'A php.ini file does not exist. You will have to create one.';
  234. }
  235. $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.';
  236. if (!ini_get('allow_url_fopen')) {
  237. $errors['allow_url_fopen'] = true;
  238. }
  239. if (version_compare(PHP_VERSION, '5.3.2', '<')) {
  240. $errors['php'] = PHP_VERSION;
  241. }
  242. if (!isset($errors['php']) && version_compare(PHP_VERSION, '5.3.4', '<')) {
  243. $warnings['php'] = PHP_VERSION;
  244. }
  245. if (!extension_loaded('openssl')) {
  246. $warnings['openssl'] = true;
  247. }
  248. if (ini_get('apc.enable_cli')) {
  249. $warnings['apc_cli'] = true;
  250. }
  251. ob_start();
  252. phpinfo(INFO_GENERAL);
  253. $phpinfo = ob_get_clean();
  254. if (preg_match('{Configure Command(?: *</td><td class="v">| *=> *)(.*?)(?:</td>|$)}m', $phpinfo, $match)) {
  255. $configure = $match[1];
  256. if (false !== strpos($configure, '--enable-sigchild')) {
  257. $warnings['sigchild'] = true;
  258. }
  259. if (false !== strpos($configure, '--with-curlwrappers')) {
  260. $warnings['curlwrappers'] = true;
  261. }
  262. }
  263. if (!empty($errors)) {
  264. foreach ($errors as $error => $current) {
  265. switch ($error) {
  266. case 'php':
  267. $text = PHP_EOL."Your PHP ({$current}) is too old, you must upgrade to PHP 5.3.2 or higher.";
  268. break;
  269. case 'allow_url_fopen':
  270. $text = PHP_EOL."The allow_url_fopen setting is incorrect.".PHP_EOL;
  271. $text .= "Add the following to the end of your `php.ini`:".PHP_EOL;
  272. $text .= " allow_url_fopen = On";
  273. $displayIniMessage = true;
  274. break;
  275. }
  276. $out($text, 'error');
  277. }
  278. $output .= PHP_EOL;
  279. }
  280. if (!empty($warnings)) {
  281. foreach ($warnings as $warning => $current) {
  282. switch ($warning) {
  283. case 'apc_cli':
  284. $text = PHP_EOL."The apc.enable_cli setting is incorrect.".PHP_EOL;
  285. $text .= "Add the following to the end of your `php.ini`:".PHP_EOL;
  286. $text .= " apc.enable_cli = Off";
  287. $displayIniMessage = true;
  288. break;
  289. case 'sigchild':
  290. $text = PHP_EOL."PHP was compiled with --enable-sigchild which can cause issues on some platforms.".PHP_EOL;
  291. $text .= "Recompile it without this flag if possible, see also:".PHP_EOL;
  292. $text .= " https://bugs.php.net/bug.php?id=22999";
  293. break;
  294. case 'curlwrappers':
  295. $text = PHP_EOL."PHP was compiled with --with-curlwrappers which will cause issues with HTTP authentication and GitHub.".PHP_EOL;
  296. $text .= "Recompile it without this flag if possible";
  297. break;
  298. case 'openssl':
  299. $text = PHP_EOL."The openssl extension is missing, which will reduce the security and stability of Composer.".PHP_EOL;
  300. $text .= "If possible you should enable it or recompile php with --with-openssl";
  301. break;
  302. case 'php':
  303. $text = PHP_EOL."Your PHP ({$current}) is quite old, upgrading to PHP 5.3.4 or higher is recommended.".PHP_EOL;
  304. $text .= "Composer works with 5.3.2+ for most people, but there might be edge case issues.";
  305. break;
  306. }
  307. $out($text, 'warning');
  308. }
  309. }
  310. if ($displayIniMessage) {
  311. $out($iniMessage, 'warning');
  312. }
  313. return !$warnings && !$errors ? true : $output;
  314. }
  315. }