CliPhp.php
6 years ago
Output.php
6 years ago
Process.php
6 years ago
RequestCommand.php
6 years ago
RequestCommand.php
134 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | |
| 9 | namespace Piwik\CliMulti; |
| 10 | |
| 11 | use Piwik\Application\Environment; |
| 12 | use Piwik\Access; |
| 13 | use Piwik\Container\StaticContainer; |
| 14 | use Piwik\Db; |
| 15 | use Piwik\Log; |
| 16 | use Piwik\Option; |
| 17 | use Piwik\Plugin\ConsoleCommand; |
| 18 | use Piwik\Url; |
| 19 | use Piwik\UrlHelper; |
| 20 | use Symfony\Component\Console\Input\InputArgument; |
| 21 | use Symfony\Component\Console\Input\InputInterface; |
| 22 | use Symfony\Component\Console\Input\InputOption; |
| 23 | use Symfony\Component\Console\Output\OutputInterface; |
| 24 | |
| 25 | /** |
| 26 | * RequestCommand |
| 27 | */ |
| 28 | class RequestCommand extends ConsoleCommand |
| 29 | { |
| 30 | /** |
| 31 | * @var Environment |
| 32 | */ |
| 33 | private $environment; |
| 34 | |
| 35 | protected function configure() |
| 36 | { |
| 37 | $this->setName('climulti:request'); |
| 38 | $this->setDescription('Parses and executes the given query. See Piwik\CliMulti. Intended only for system usage.'); |
| 39 | $this->addArgument('url-query', InputArgument::REQUIRED, 'Matomo URL query string, for instance: "module=API&method=API.getPiwikVersion&token_auth=123456789"'); |
| 40 | $this->addOption('superuser', null, InputOption::VALUE_NONE, 'If supplied, runs the code as superuser.'); |
| 41 | } |
| 42 | |
| 43 | protected function execute(InputInterface $input, OutputInterface $output) |
| 44 | { |
| 45 | $this->recreateContainerWithWebEnvironment(); |
| 46 | |
| 47 | $this->initHostAndQueryString($input); |
| 48 | |
| 49 | if ($this->isTestModeEnabled()) { |
| 50 | $indexFile = '/tests/PHPUnit/proxy/'; |
| 51 | |
| 52 | $this->resetDatabase(); |
| 53 | } else { |
| 54 | $indexFile = '/'; |
| 55 | } |
| 56 | |
| 57 | $indexFile .= 'index.php'; |
| 58 | |
| 59 | if (!empty($_GET['pid'])) { |
| 60 | $process = new Process($_GET['pid']); |
| 61 | |
| 62 | if ($process->hasFinished()) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | $process->startProcess(); |
| 67 | } |
| 68 | |
| 69 | if ($input->getOption('superuser')) { |
| 70 | StaticContainer::addDefinitions(array( |
| 71 | 'observers.global' => \DI\add(array( |
| 72 | array('Environment.bootstrapped', function () { |
| 73 | Access::getInstance()->setSuperUserAccess(true); |
| 74 | }) |
| 75 | )), |
| 76 | )); |
| 77 | } |
| 78 | |
| 79 | require_once PIWIK_INCLUDE_PATH . $indexFile; |
| 80 | |
| 81 | if (!empty($process)) { |
| 82 | $process->finishProcess(); |
| 83 | } |
| 84 | |
| 85 | while (ob_get_level()) { |
| 86 | echo ob_get_clean(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private function isTestModeEnabled() |
| 91 | { |
| 92 | return !empty($_GET['testmode']); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param InputInterface $input |
| 97 | */ |
| 98 | protected function initHostAndQueryString(InputInterface $input) |
| 99 | { |
| 100 | $_GET = array(); |
| 101 | |
| 102 | // @todo remove piwik-domain fallback in Matomo 4 |
| 103 | $hostname = $input->getOption('matomo-domain') ?: $input->getOption('piwik-domain'); |
| 104 | Url::setHost($hostname); |
| 105 | |
| 106 | $query = $input->getArgument('url-query'); |
| 107 | $query = UrlHelper::getArrayFromQueryString($query); // NOTE: this method can create the StaticContainer now |
| 108 | foreach ($query as $name => $value) { |
| 109 | $_GET[$name] = $value; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * We will be simulating an HTTP request here (by including index.php). |
| 115 | * |
| 116 | * To avoid weird side-effects (e.g. the logging output messing up the HTTP response on the CLI output) |
| 117 | * we need to recreate the container with the default environment instead of the CLI environment. |
| 118 | */ |
| 119 | private function recreateContainerWithWebEnvironment() |
| 120 | { |
| 121 | StaticContainer::clearContainer(); |
| 122 | Log::unsetInstance(); |
| 123 | |
| 124 | $this->environment = new Environment(null); |
| 125 | $this->environment->init(); |
| 126 | } |
| 127 | |
| 128 | private function resetDatabase() |
| 129 | { |
| 130 | Option::clearCache(); |
| 131 | Db::destroyDatabaseObject(); |
| 132 | } |
| 133 | } |
| 134 |