PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / CliMulti / RequestCommand.php
matomo / app / core / CliMulti Last commit date
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