DiagCommand.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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\Util\ConfigValidator;
  16. use Composer\Util\RemoteFilesystem;
  17. use Composer\Util\StreamContextFactory;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. /**
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. class DiagCommand extends Command
  24. {
  25. protected $rfs;
  26. protected $failures = 0;
  27. protected function configure()
  28. {
  29. $this
  30. ->setName('diag')
  31. ->setDescription('Diagnoses the system to identify common errors.')
  32. ->setHelp(<<<EOT
  33. The <info>diag</info> command checks common errors to help debugging problems.
  34. EOT
  35. )
  36. ;
  37. }
  38. protected function execute(InputInterface $input, OutputInterface $output)
  39. {
  40. $this->rfs = new RemoteFilesystem($this->getIO());
  41. $output->write('Checking platform settings: ');
  42. $this->outputResult($output, $this->checkPlatform());
  43. $output->write('Checking http connectivity: ');
  44. $this->outputResult($output, $this->checkHttp());
  45. $opts = stream_context_get_options(StreamContextFactory::getContext());
  46. if (!empty($opts['http']['proxy'])) {
  47. $output->write('Checking HTTP proxy: ');
  48. $this->outputResult($output, $this->checkHttpProxy());
  49. }
  50. $composer = $this->getComposer(false);
  51. if ($composer) {
  52. $output->write('Checking composer.json: ');
  53. $this->outputResult($output, $this->checkComposerSchema());
  54. }
  55. if ($composer) {
  56. $config = $composer->getConfig();
  57. } else {
  58. $config = Factory::createConfig();
  59. }
  60. if ($oauth = $config->get('github-oauth')) {
  61. foreach ($oauth as $domain => $token) {
  62. $output->write('Checking '.$domain.' oauth access: ');
  63. $this->outputResult($output, $this->checkGithubOauth($domain, $token));
  64. }
  65. }
  66. $output->write('Checking composer version: ');
  67. $this->outputResult($output, $this->checkVersion());
  68. return $this->failures;
  69. }
  70. private function checkComposerSchema()
  71. {
  72. $validator = new ConfigValidator($this->getIO());
  73. list($errors, $publishErrors, $warnings) = $validator->validate(Factory::getComposerFile());
  74. if ($errors || $publishErrors || $warnings) {
  75. $messages = array(
  76. 'error' => array_merge($errors, $publishErrors),
  77. 'warning' => $warnings,
  78. );
  79. $output = '';
  80. foreach ($messages as $style => $msgs) {
  81. foreach ($msgs as $msg) {
  82. $output .= '<' . $style . '>' . $msg . '</' . $style . '>';
  83. }
  84. }
  85. return $output;
  86. }
  87. return true;
  88. }
  89. private function checkHttp()
  90. {
  91. $protocol = extension_loaded('openssl') ? 'https' : 'http';
  92. try {
  93. $json = $this->rfs->getContents('packagist.org', $protocol . '://packagist.org/packages.json', false);
  94. } catch (\Exception $e) {
  95. return $e;
  96. }
  97. return true;
  98. }
  99. private function checkHttpProxy()
  100. {
  101. $protocol = extension_loaded('openssl') ? 'https' : 'http';
  102. try {
  103. $json = json_decode($this->rfs->getContents('packagist.org', $protocol . '://packagist.org/packages.json', false), true);
  104. $hash = reset($json['provider-includes']);
  105. $hash = $hash['sha256'];
  106. $path = str_replace('%hash%', $hash, key($json['provider-includes']));
  107. $provider = $this->rfs->getContents('packagist.org', $protocol . '://packagist.org/'.$path, false);
  108. if (hash('sha256', $provider) !== $hash) {
  109. return 'It seems that your proxy is modifying http traffic on the fly';
  110. }
  111. } catch (\Exception $e) {
  112. return $e;
  113. }
  114. return true;
  115. }
  116. private function checkGithubOauth($domain, $token)
  117. {
  118. $this->getIO()->setAuthentication($domain, $token, 'x-oauth-basic');
  119. try {
  120. $url = $domain === 'github.com' ? 'https://api.'.$domain.'/user/repos' : 'https://'.$domain.'/api/v3/user/repos';
  121. return $this->rfs->getContents($domain, $url, false) ? true : 'Unexpected error';
  122. } catch (\Exception $e) {
  123. if ($e instanceof TransportException && $e->getCode() === 401) {
  124. return '<warning>The oauth token for '.$domain.' seems invalid, run "composer config --global --unset github-oauth.'.$domain.'" to remove it</warning>';
  125. }
  126. return $e;
  127. }
  128. }
  129. private function checkVersion()
  130. {
  131. $protocol = extension_loaded('openssl') ? 'https' : 'http';
  132. $latest = trim($this->rfs->getContents('getcomposer.org', $protocol . '://getcomposer.org/version', false));
  133. if (Composer::VERSION !== $latest && Composer::VERSION !== '@package_version@') {
  134. return '<warning>Your are not running the latest version</warning>';
  135. } else {
  136. return true;
  137. }
  138. }
  139. private function outputResult(OutputInterface $output, $result)
  140. {
  141. if (true === $result) {
  142. $output->writeln('<info>OK</info>');
  143. } else {
  144. $this->failures++;
  145. $output->writeln('<error>FAIL</error>');
  146. if ($result instanceof \Exception) {
  147. $output->writeln('['.get_class($result).'] '.$result->getMessage());
  148. } elseif ($result) {
  149. $output->writeln($result);
  150. }
  151. }
  152. }
  153. private function checkPlatform()
  154. {
  155. $output = '';
  156. $out = function ($msg, $style) use (&$output) {
  157. $output .= '<'.$style.'>'.$msg.'</'.$style.'>';
  158. };
  159. // code below taken from getcomposer.org/installer, any changes should be made there and replicated here
  160. $errors = array();
  161. $warnings = array();
  162. $iniPath = php_ini_loaded_file();
  163. $displayIniMessage = false;
  164. if ($iniPath) {
  165. $iniMessage = PHP_EOL.PHP_EOL.'The php.ini used by your command-line PHP is: ' . $iniPath;
  166. } else {
  167. $iniMessage = PHP_EOL.PHP_EOL.'A php.ini file does not exist. You will have to create one.';
  168. }
  169. $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.';
  170. if (!ini_get('allow_url_fopen')) {
  171. $errors['allow_url_fopen'] = true;
  172. }
  173. if (version_compare(PHP_VERSION, '5.3.2', '<')) {
  174. $errors['php'] = PHP_VERSION;
  175. }
  176. if (version_compare(PHP_VERSION, '5.3.4', '<')) {
  177. $warnings['php'] = PHP_VERSION;
  178. }
  179. if (!extension_loaded('openssl')) {
  180. $warnings['openssl'] = true;
  181. }
  182. if (ini_get('apc.enable_cli')) {
  183. $warnings['apc_cli'] = true;
  184. }
  185. ob_start();
  186. phpinfo(INFO_GENERAL);
  187. $phpinfo = ob_get_clean();
  188. if (preg_match('{Configure Command(?: *</td><td class="v">| *=> *)(.*?)(?:</td>|$)}m', $phpinfo, $match)) {
  189. $configure = $match[1];
  190. if (false !== strpos($configure, '--enable-sigchild')) {
  191. $warnings['sigchild'] = true;
  192. }
  193. if (false !== strpos($configure, '--with-curlwrappers')) {
  194. $warnings['curlwrappers'] = true;
  195. }
  196. }
  197. if (!empty($errors)) {
  198. foreach ($errors as $error => $current) {
  199. switch ($error) {
  200. case 'php':
  201. $text = PHP_EOL."Your PHP ({$current}) is too old, you must upgrade to PHP 5.3.2 or higher.";
  202. break;
  203. case 'allow_url_fopen':
  204. $text = PHP_EOL."The allow_url_fopen setting is incorrect.".PHP_EOL;
  205. $text .= "Add the following to the end of your `php.ini`:".PHP_EOL;
  206. $text .= " allow_url_fopen = On";
  207. $displayIniMessage = true;
  208. break;
  209. }
  210. if ($displayIniMessage) {
  211. $text .= $iniMessage;
  212. }
  213. $out($text, 'error');
  214. }
  215. $out('');
  216. }
  217. if (!empty($warnings)) {
  218. foreach ($warnings as $warning => $current) {
  219. switch ($warning) {
  220. case 'apc_cli':
  221. $text = PHP_EOL."The apc.enable_cli setting is incorrect.".PHP_EOL;
  222. $text .= "Add the following to the end of your `php.ini`:".PHP_EOL;
  223. $text .= " apc.enable_cli = Off";
  224. $displayIniMessage = true;
  225. break;
  226. case 'sigchild':
  227. $text = PHP_EOL."PHP was compiled with --enable-sigchild which can cause issues on some platforms.".PHP_EOL;
  228. $text .= "Recompile it without this flag if possible, see also:".PHP_EOL;
  229. $text .= " https://bugs.php.net/bug.php?id=22999";
  230. break;
  231. case 'curlwrappers':
  232. $text = PHP_EOL."PHP was compiled with --with-curlwrappers which will cause issues with HTTP authentication and GitHub.".PHP_EOL;
  233. $text .= "Recompile it without this flag if possible";
  234. break;
  235. case 'openssl':
  236. $text = PHP_EOL."The openssl extension is missing, which will reduce the security and stability of Composer.".PHP_EOL;
  237. $text .= "If possible you should enable it or recompile php with --with-openssl";
  238. break;
  239. case 'php':
  240. $text = PHP_EOL."Your PHP ({$current}) is quite old, upgrading to PHP 5.3.4 or higher is recommended.".PHP_EOL;
  241. $text .= "Composer works with 5.3.2+ for most people, but there might be edge case issues.";
  242. break;
  243. }
  244. if ($displayIniMessage) {
  245. $text .= $iniMessage;
  246. }
  247. $out($text, 'warning');
  248. }
  249. }
  250. return !$warnings && !$errors ? true : $output;
  251. }
  252. }