diff --git a/src/Joomlatools/Console/Application.php b/src/Joomlatools/Console/Application.php index fcfdcaec..8a065734 100644 --- a/src/Joomlatools/Console/Application.php +++ b/src/Joomlatools/Console/Application.php @@ -135,6 +135,7 @@ protected function getDefaultCommands() new Command\Site\Delete(), new Command\Site\Download(), new Command\Site\Install(), + new Command\Site\Listing(), new Command\Site\Token(), new Command\Vhost\Create(), diff --git a/src/Joomlatools/Console/Command/Site/Listing.php b/src/Joomlatools/Console/Command/Site/Listing.php new file mode 100644 index 00000000..bd38aad7 --- /dev/null +++ b/src/Joomlatools/Console/Command/Site/Listing.php @@ -0,0 +1,144 @@ +setName('site:list') + ->setDescription('List Joomla sites') + ->addOption( + 'format', + null, + InputOption::VALUE_OPTIONAL, + 'The output format (txt or json)', + 'txt' + ) + ->addOption( + 'www', + null, + InputOption::VALUE_REQUIRED, + "Web server root", + '/var/www' + ) + ->setHelp('List Joomla sites running on this box'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + define('_JEXEC', true); + define('JPATH_BASE', true); + define('JPATH_PLATFORM', true); + + $docroot = $input->getOption('www'); + + if (!file_exists($docroot)) { + throw new \RuntimeException(sprintf('Web server root \'%s\' does not exist.', $docroot)); + } + + $dir = new \DirectoryIterator($docroot); + $sites = array(); + + $canonical = function($version) { + if (isset($version->RELEASE)) { + return 'v' . $version->RELEASE . '.' . $version->DEV_LEVEL; + } + + // Joomla 3.5 and up uses constants instead of properties in JVersion + $className = get_class($version); + if (defined("$className::RELEASE")) { + return 'v'. $version::RELEASE . '.' . $version::DEV_LEVEL; + } + + return 'unknown'; + }; + + foreach ($dir as $fileinfo) + { + $code = $application = null; + + if ($fileinfo->isDir() && !$fileinfo->isDot()) + { + $files = array( + 'joomla-cms' => $fileinfo->getPathname() . '/libraries/cms/version/version.php', + 'joomlatools-platform' => $fileinfo->getPathname() . '/lib/libraries/cms/version/version.php', + 'joomla-1.5' => $fileinfo->getPathname() . '/libraries/joomla/version.php' + ); + + foreach ($files as $type => $file) + { + if (file_exists($file)) + { + $code = $file; + $application = $type; + + break; + } + } + + if (!is_null($code) && file_exists($code)) + { + $identifier = uniqid(); + + $source = file_get_contents($code); + $source = preg_replace('/<\?php/', '', $source, 1); + $source = preg_replace('/class JVersion/i', 'class JVersion' . $identifier, $source); + + eval($source); + + $class = 'JVersion'.$identifier; + $version = new $class(); + + $sites[] = (object) array( + 'name' => $fileinfo->getFilename(), + 'docroot' => $docroot . '/' . $fileinfo->getFilename() . '/' . ($application == 'joomlatools-platform' ? 'web' : ''), + 'type' => $application, + 'version' => $canonical($version) + ); + } + } + } + + if (!in_array($input->getOption('format'), array('txt', 'json'))) { + throw new \InvalidArgumentException(sprintf('Unsupported format "%s".', $input->getOption('format'))); + } + + + + switch ($input->getOption('format')) + { + case 'json': + $result = new \stdClass(); + $result->command = $input->getArgument('command'); + $result->data = $sites; + + $options = (version_compare(phpversion(),'5.4.0') >= 0 ? JSON_PRETTY_PRINT : 0); + $string = json_encode($result, $options); + break; + case 'txt': + default: + $lines = array(); + foreach ($sites as $i => $site) { + $lines[] = sprintf("%s. %s (%s %s)", ($i+1), $site->name, $site->type, $site->version); + } + + $string = implode("\n", $lines); + break; + } + + $output->writeln($string); + } +}