123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- /*
- * This file is part of Composer.
- *
- * (c) Nils Adermann <naderman@naderman.de>
- * Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Composer\Command;
- use Composer\Composer;
- use Composer\Factory;
- use Composer\Downloader\TransportException;
- use Composer\Util\ConfigValidator;
- use Composer\Util\RemoteFilesystem;
- use Composer\Util\StreamContextFactory;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- /**
- * @author Jordi Boggiano <j.boggiano@seld.be>
- */
- class DiagCommand extends Command
- {
- protected $rfs;
- protected $failures = 0;
- protected function configure()
- {
- $this
- ->setName('diag')
- ->setDescription('Diagnoses the system to identify common errors.')
- ->setHelp(<<<EOT
- The <info>diag</info> command checks common errors to help debugging problems.
- EOT
- )
- ;
- }
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $this->rfs = new RemoteFilesystem($this->getIO());
- $output->write('Checking platform settings: ');
- $this->outputResult($output, $this->checkPlatform());
- $output->write('Checking http connectivity: ');
- $this->outputResult($output, $this->checkHttp());
- $opts = stream_context_get_options(StreamContextFactory::getContext());
- if (!empty($opts['http']['proxy'])) {
- $output->write('Checking HTTP proxy: ');
- $this->outputResult($output, $this->checkHttpProxy());
- }
- $composer = $this->getComposer(false);
- if ($composer) {
- $output->write('Checking composer.json: ');
- $this->outputResult($output, $this->checkComposerSchema());
- }
- if ($composer) {
- $config = $composer->getConfig();
- } else {
- $config = Factory::createConfig();
- }
- if ($oauth = $config->get('github-oauth')) {
- foreach ($oauth as $domain => $token) {
- $output->write('Checking '.$domain.' oauth access: ');
- $this->outputResult($output, $this->checkGithubOauth($domain, $token));
- }
- }
- $output->write('Checking composer version: ');
- $this->outputResult($output, $this->checkVersion());
- return $this->failures;
- }
- private function checkComposerSchema()
- {
- $validator = new ConfigValidator($this->getIO());
- list($errors, $publishErrors, $warnings) = $validator->validate(Factory::getComposerFile());
- if ($errors || $publishErrors || $warnings) {
- $messages = array(
- 'error' => array_merge($errors, $publishErrors),
- 'warning' => $warnings,
- );
- $output = '';
- foreach ($messages as $style => $msgs) {
- foreach ($msgs as $msg) {
- $output .= '<' . $style . '>' . $msg . '</' . $style . '>';
- }
- }
- return $output;
- }
- return true;
- }
- private function checkHttp()
- {
- $protocol = extension_loaded('openssl') ? 'https' : 'http';
- try {
- $json = $this->rfs->getContents('packagist.org', $protocol . '://packagist.org/packages.json', false);
- } catch (\Exception $e) {
- return $e;
- }
- return true;
- }
- private function checkHttpProxy()
- {
- $protocol = extension_loaded('openssl') ? 'https' : 'http';
- try {
- $json = json_decode($this->rfs->getContents('packagist.org', $protocol . '://packagist.org/packages.json', false), true);
- $hash = reset($json['provider-includes']);
- $hash = $hash['sha256'];
- $path = str_replace('%hash%', $hash, key($json['provider-includes']));
- $provider = $this->rfs->getContents('packagist.org', $protocol . '://packagist.org/'.$path, false);
- if (hash('sha256', $provider) !== $hash) {
- return 'It seems that your proxy is modifying http traffic on the fly';
- }
- } catch (\Exception $e) {
- return $e;
- }
- return true;
- }
- private function checkGithubOauth($domain, $token)
- {
- $this->getIO()->setAuthentication($domain, $token, 'x-oauth-basic');
- try {
- $url = $domain === 'github.com' ? 'https://api.'.$domain.'/user/repos' : 'https://'.$domain.'/api/v3/user/repos';
- return $this->rfs->getContents($domain, $url, false) ? true : 'Unexpected error';
- } catch (\Exception $e) {
- if ($e instanceof TransportException && $e->getCode() === 401) {
- return '<warning>The oauth token for '.$domain.' seems invalid, run "composer config --global --unset github-oauth.'.$domain.'" to remove it</warning>';
- }
- return $e;
- }
- }
- private function checkVersion()
- {
- $protocol = extension_loaded('openssl') ? 'https' : 'http';
- $latest = trim($this->rfs->getContents('getcomposer.org', $protocol . '://getcomposer.org/version', false));
- if (Composer::VERSION !== $latest && Composer::VERSION !== '@package_version@') {
- return '<warning>Your are not running the latest version</warning>';
- } else {
- return true;
- }
- }
- private function outputResult(OutputInterface $output, $result)
- {
- if (true === $result) {
- $output->writeln('<info>OK</info>');
- } else {
- $this->failures++;
- $output->writeln('<error>FAIL</error>');
- if ($result instanceof \Exception) {
- $output->writeln('['.get_class($result).'] '.$result->getMessage());
- } elseif ($result) {
- $output->writeln($result);
- }
- }
- }
- private function checkPlatform()
- {
- $output = '';
- $out = function ($msg, $style) use (&$output) {
- $output .= '<'.$style.'>'.$msg.'</'.$style.'>';
- };
- // code below taken from getcomposer.org/installer, any changes should be made there and replicated here
- $errors = array();
- $warnings = array();
- $iniPath = php_ini_loaded_file();
- $displayIniMessage = false;
- if ($iniPath) {
- $iniMessage = PHP_EOL.PHP_EOL.'The php.ini used by your command-line PHP is: ' . $iniPath;
- } else {
- $iniMessage = PHP_EOL.PHP_EOL.'A php.ini file does not exist. You will have to create one.';
- }
- $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.';
- if (!ini_get('allow_url_fopen')) {
- $errors['allow_url_fopen'] = true;
- }
- if (version_compare(PHP_VERSION, '5.3.2', '<')) {
- $errors['php'] = PHP_VERSION;
- }
- if (version_compare(PHP_VERSION, '5.3.4', '<')) {
- $warnings['php'] = PHP_VERSION;
- }
- if (!extension_loaded('openssl')) {
- $warnings['openssl'] = true;
- }
- if (ini_get('apc.enable_cli')) {
- $warnings['apc_cli'] = true;
- }
- ob_start();
- phpinfo(INFO_GENERAL);
- $phpinfo = ob_get_clean();
- if (preg_match('{Configure Command(?: *</td><td class="v">| *=> *)(.*?)(?:</td>|$)}m', $phpinfo, $match)) {
- $configure = $match[1];
- if (false !== strpos($configure, '--enable-sigchild')) {
- $warnings['sigchild'] = true;
- }
- if (false !== strpos($configure, '--with-curlwrappers')) {
- $warnings['curlwrappers'] = true;
- }
- }
- if (!empty($errors)) {
- foreach ($errors as $error => $current) {
- switch ($error) {
- case 'php':
- $text = PHP_EOL."Your PHP ({$current}) is too old, you must upgrade to PHP 5.3.2 or higher.";
- break;
- case 'allow_url_fopen':
- $text = PHP_EOL."The allow_url_fopen setting is incorrect.".PHP_EOL;
- $text .= "Add the following to the end of your `php.ini`:".PHP_EOL;
- $text .= " allow_url_fopen = On";
- $displayIniMessage = true;
- break;
- }
- if ($displayIniMessage) {
- $text .= $iniMessage;
- }
- $out($text, 'error');
- }
- $out('');
- }
- if (!empty($warnings)) {
- foreach ($warnings as $warning => $current) {
- switch ($warning) {
- case 'apc_cli':
- $text = PHP_EOL."The apc.enable_cli setting is incorrect.".PHP_EOL;
- $text .= "Add the following to the end of your `php.ini`:".PHP_EOL;
- $text .= " apc.enable_cli = Off";
- $displayIniMessage = true;
- break;
- case 'sigchild':
- $text = PHP_EOL."PHP was compiled with --enable-sigchild which can cause issues on some platforms.".PHP_EOL;
- $text .= "Recompile it without this flag if possible, see also:".PHP_EOL;
- $text .= " https://bugs.php.net/bug.php?id=22999";
- break;
- case 'curlwrappers':
- $text = PHP_EOL."PHP was compiled with --with-curlwrappers which will cause issues with HTTP authentication and GitHub.".PHP_EOL;
- $text .= "Recompile it without this flag if possible";
- break;
- case 'openssl':
- $text = PHP_EOL."The openssl extension is missing, which will reduce the security and stability of Composer.".PHP_EOL;
- $text .= "If possible you should enable it or recompile php with --with-openssl";
- break;
- case 'php':
- $text = PHP_EOL."Your PHP ({$current}) is quite old, upgrading to PHP 5.3.4 or higher is recommended.".PHP_EOL;
- $text .= "Composer works with 5.3.2+ for most people, but there might be edge case issues.";
- break;
- }
- if ($displayIniMessage) {
- $text .= $iniMessage;
- }
- $out($text, 'warning');
- }
- }
- return !$warnings && !$errors ? true : $output;
- }
- }
|