Repository navigation
Expand file tree
/
Copy pathController.php
More file actions
171 lines (141 loc) · 5.67 KB
/
Copy pathController.php
File metadata and controls
171 lines (141 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\ConsoleUI;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Log\LoggerInterface;
use Piwik\Piwik;
use Piwik\Plugins\ConsoleUI\Console\CommandNotAllowedException;
use Piwik\Plugins\ConsoleUI\Console\CommandRunner;
use Piwik\Plugins\ConsoleUI\Console\StopSignal;
use Piwik\Plugins\Login\PasswordVerifier;
use Piwik\Request;
use Piwik\Session;
class Controller extends \Piwik\Plugin\ControllerAdmin
{
private const EMPTY_CATALOG = ['commands' => [], 'globalOptions' => []];
/** @var PasswordVerifier */
private $passwordVerify;
/** @var CommandRunner */
private $runner;
public function __construct(PasswordVerifier $passwordVerify, CommandRunner $runner)
{
$this->passwordVerify = $passwordVerify;
$this->runner = $runner;
parent::__construct();
}
/**
* The Console page. Like installing a plugin, it asks the super user to confirm their password first.
*/
public function index()
{
Piwik::checkUserHasSuperUserAccess();
if (!$this->runner->getPolicy()->isEnabled()) {
return $this->renderTemplate('index', ['enabled' => false, 'catalog' => self::EMPTY_CATALOG]);
}
$redirectParams = ['module' => 'ConsoleUI', 'action' => 'index'];
if (
$this->requiresPasswordConfirmation()
&& !$this->passwordVerify->requirePasswordVerifiedRecently($redirectParams)
) {
return '';
}
try {
$catalog = $this->runner->getCatalog();
} catch (\Throwable $e) {
// The console stays usable without the library, a plugin shipping a broken command must not hide it
StaticContainer::get(LoggerInterface::class)->error('ConsoleUI: cannot list the commands: {message}', [
'message' => $e->getMessage(),
'exception' => $e,
]);
$catalog = self::EMPTY_CATALOG;
}
return $this->renderTemplate('index', ['enabled' => true, 'catalog' => $catalog]);
}
/**
* Runs a command and streams its output as Server-Sent Events.
*
* A controller action and not an API method on purpose: it is only reachable from a logged in browser session
* (token of the session, POST only, recently confirmed password), never with a token_auth alone.
*/
public function execute(): void
{
$this->checkSessionPost();
$request = Request::fromPost();
$commandLine = $request->getStringParameter('command', '');
$runId = $request->getStringParameter('runId', '');
$passwordConfirmed = !$this->requiresPasswordConfirmation() || $this->passwordVerify->hasBeenVerified();
// The command may run for a while, the other requests of the user must not wait for the session lock
Session::close();
@set_time_limit(0);
// Closing the tab must not stop a command halfway, an interrupted core:update would leave Matomo broken
ignore_user_abort(true);
$stream = new EventStream();
$stream->open();
if (!$passwordConfirmed) {
$stream->emit('error', [
'message' => Piwik::translate('ConsoleUI_PasswordConfirmationExpired'),
'reload' => true,
]);
$stream->close();
return;
}
$start = microtime(true);
$stopSignal = null;
try {
$stopSignal = new StopSignal($runId);
$stopSignal->start(Piwik::getCurrentUserLogin());
$exitCode = $this->runner->run($commandLine, [$stream, 'output'], $stopSignal);
$stream->emit('exit', [
'exitCode' => $exitCode,
'durationMs' => (int) round((microtime(true) - $start) * 1000),
'stopped' => $stopSignal->wasStopped(),
]);
} catch (\InvalidArgumentException | CommandNotAllowedException $e) {
$stream->emit('error', ['message' => $e->getMessage()]);
} catch (\Throwable $e) {
StaticContainer::get(LoggerInterface::class)->error('ConsoleUI: command failed: {message}', [
'message' => $e->getMessage(),
'exception' => $e,
]);
$stream->emit('error', ['message' => Piwik::translate('ConsoleUI_UnexpectedError')]);
} finally {
if ($stopSignal) {
$stopSignal->finish();
}
}
$stream->close();
}
/**
* Asks the command a user started to stop, see StopSignal
*/
public function stop(): string
{
$this->checkSessionPost();
Common::sendHeader('Content-Type: application/json; charset=utf-8');
$runId = Request::fromPost()->getStringParameter('runId', '');
$requested = StopSignal::isValidRunId($runId)
&& (new StopSignal($runId))->request(Piwik::getCurrentUserLogin());
return (string) json_encode(['requested' => $requested]);
}
/**
* Super user, POST and token of the session: a command never runs, nor stops, from a link or a token_auth alone
*/
private function checkSessionPost(): void
{
Piwik::checkUserHasSuperUserAccess();
if (strtoupper($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
throw new \Exception(Piwik::translate('ConsoleUI_PostRequired'));
}
$this->checkTokenInUrl();
}
private function requiresPasswordConfirmation(): bool
{
return Piwik::doesUserRequirePasswordConfirmation(Piwik::getCurrentUserLogin());
}
}