CliPhp.php
1 year ago
Output.php
2 years ago
OutputInterface.php
2 years ago
Process.php
1 year ago
ProcessSymfony.php
1 year ago
RequestCommand.php
1 year ago
RequestParser.php
1 year ago
StaticOutput.php
1 year ago
RequestCommand.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 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 | /** |
| 21 | * RequestCommand |
| 22 | */ |
| 23 | class RequestCommand extends ConsoleCommand |
| 24 | { |
| 25 | /** |
| 26 | * @var Environment |
| 27 | */ |
| 28 | private $environment; |
| 29 | protected function configure() |
| 30 | { |
| 31 | $this->setName('climulti:request'); |
| 32 | $this->setDescription('Parses and executes the given query. See Piwik\\CliMulti. Intended only for system usage.'); |
| 33 | $this->addRequiredArgument('url-query', 'Matomo URL query string, for instance: "module=API&method=API.getPiwikVersion&token_auth=123456789"'); |
| 34 | $this->addNoValueOption('superuser', null, 'If supplied, runs the code as superuser.'); |
| 35 | } |
| 36 | protected function doExecute() : int |
| 37 | { |
| 38 | $this->recreateContainerWithWebEnvironment(); |
| 39 | $this->initHostAndQueryString(); |
| 40 | if ($this->isTestModeEnabled()) { |
| 41 | $indexFile = '/tests/PHPUnit/proxy/'; |
| 42 | $this->resetDatabase(); |
| 43 | } else { |
| 44 | $indexFile = '/'; |
| 45 | } |
| 46 | $indexFile .= 'index.php'; |
| 47 | if (!empty($_GET['pid'])) { |
| 48 | $process = new \Piwik\CliMulti\Process($_GET['pid']); |
| 49 | if ($process->hasFinished()) { |
| 50 | return self::SUCCESS; |
| 51 | } |
| 52 | $process->startProcess(); |
| 53 | } |
| 54 | if ($this->getInput()->getOption('superuser')) { |
| 55 | StaticContainer::addDefinitions(array('observers.global' => \Piwik\DI::add(array(array('Environment.bootstrapped', \Piwik\DI::value(function () { |
| 56 | Access::getInstance()->setSuperUserAccess(\true); |
| 57 | })))))); |
| 58 | } |
| 59 | require_once PIWIK_INCLUDE_PATH . $indexFile; |
| 60 | while (ob_get_level()) { |
| 61 | echo ob_get_clean(); |
| 62 | } |
| 63 | if (!empty($process)) { |
| 64 | $process->finishProcess(); |
| 65 | } |
| 66 | return self::SUCCESS; |
| 67 | } |
| 68 | private function isTestModeEnabled() |
| 69 | { |
| 70 | return !empty($_GET['testmode']); |
| 71 | } |
| 72 | protected function initHostAndQueryString() |
| 73 | { |
| 74 | $_GET = array(); |
| 75 | $hostname = $this->getInput()->getOption('matomo-domain'); |
| 76 | Url::setHost($hostname); |
| 77 | $query = $this->getInput()->getArgument('url-query'); |
| 78 | $_SERVER['QUERY_STRING'] = $query; |
| 79 | $query = UrlHelper::getArrayFromQueryString($query); |
| 80 | // NOTE: this method can create the StaticContainer now |
| 81 | foreach ($query as $name => $value) { |
| 82 | $_GET[$name] = urldecode($value); |
| 83 | } |
| 84 | } |
| 85 | /** |
| 86 | * We will be simulating an HTTP request here (by including index.php). |
| 87 | * |
| 88 | * To avoid weird side-effects (e.g. the logging output messing up the HTTP response on the CLI output) |
| 89 | * we need to recreate the container with the default environment instead of the CLI environment. |
| 90 | */ |
| 91 | private function recreateContainerWithWebEnvironment() |
| 92 | { |
| 93 | StaticContainer::clearContainer(); |
| 94 | Log::unsetInstance(); |
| 95 | $this->environment = new Environment(null); |
| 96 | $this->environment->init(); |
| 97 | } |
| 98 | private function resetDatabase() |
| 99 | { |
| 100 | Option::clearCache(); |
| 101 | Db::destroyDatabaseObject(); |
| 102 | } |
| 103 | } |
| 104 |