1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Victor Dubiniuk <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
7
|
|
|
* @license AGPL-3.0 |
8
|
|
|
* |
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
11
|
|
|
* as published by the Free Software Foundation. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Owncloud\Updater\Controller; |
24
|
|
|
|
25
|
|
|
use League\Plates\Extension\URI; |
26
|
|
|
use Owncloud\Updater\Utils\ConfigReader; |
27
|
|
|
use Symfony\Component\Console\Input\StringInput; |
28
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
29
|
|
|
use Owncloud\Updater\Formatter\HtmlOutputFormatter; |
30
|
|
|
use Owncloud\Updater\Http\Request; |
31
|
|
|
use League\Plates\Engine; |
32
|
|
|
use League\Plates\Extension\Asset; |
33
|
|
|
|
34
|
|
|
class IndexController { |
35
|
|
|
|
36
|
|
|
/** @var \Pimple\Container */ |
37
|
|
|
protected $container; |
38
|
|
|
|
39
|
|
|
/** @var Request */ |
40
|
|
|
protected $request; |
41
|
|
|
|
42
|
|
|
/** @var string $command */ |
43
|
|
|
protected $command; |
44
|
|
|
|
45
|
|
|
public function __construct(\Pimple\Container $container, $request = null){ |
46
|
|
|
$this->container = $container; |
47
|
|
|
if (is_null($request)){ |
48
|
|
|
$this->request = new Request(['post' => $_POST, 'headers' => $_SERVER]); |
49
|
|
|
} else { |
50
|
|
|
$this->request = $request; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->command = $this->request->postParameter('command'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function dispatch() { |
57
|
|
|
// strip index.php and query string (if any) to get a real base url |
58
|
|
|
$baseUrl = preg_replace('/(index\.php.*|\?.*)$/', '', $_SERVER['REQUEST_URI']); |
59
|
|
|
$templates = new Engine(CURRENT_DIR . '/src/Resources/views/'); |
60
|
|
|
$templates->loadExtension(new Asset(CURRENT_DIR . '/pub/', false)); |
61
|
|
|
$templates->loadExtension(new URI($baseUrl)); |
62
|
|
|
|
63
|
|
|
// Check if the user is logged-in |
64
|
|
|
if(!$this->isLoggedIn()) { |
|
|
|
|
65
|
|
|
return $this->showLogin($templates); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (is_null($this->command)){ |
69
|
|
|
$checkpoints = $this->container['utils.checkpoint']->getAll(); |
70
|
|
|
$content = $templates->render( |
71
|
|
|
'partials/inner', |
72
|
|
|
[ |
73
|
|
|
'title' => 'Updater', |
74
|
|
|
'version' => $this->container['application']->getVersion(), |
75
|
|
|
'checkpoints' => $checkpoints |
76
|
|
|
] |
77
|
|
|
); |
78
|
|
|
} else { |
79
|
|
|
header('Content-Type: application/json'); |
80
|
|
|
$content = json_encode($this->ajaxAction(), JSON_UNESCAPED_SLASHES); |
81
|
|
|
} |
82
|
|
|
return $content; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function isLoggedIn() { |
86
|
|
|
/** @var ConfigReader $configReader */ |
87
|
|
|
$configReader = $this->container['utils.configReader']; |
88
|
|
|
$configReader->init(); |
89
|
|
|
$storedSecret = isset($configReader->get(['system'])['updater.secret']) ? $configReader->get(['system'])['updater.secret'] : null; |
90
|
|
|
if(is_null($storedSecret)) { |
91
|
|
|
die('updater.secret is undefined in config/config.php. Please define a secret.'); |
92
|
|
|
} |
93
|
|
|
$sentAuthHeader = ($this->request->header('Authorization') !== null) ? $this->request->header('Authorization') : ''; |
94
|
|
|
|
95
|
|
|
if(hash_equals($storedSecret, $sentAuthHeader)) { |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function showLogin(Engine $templates) { |
103
|
|
|
// If it is a request with invalid token just return "false" so that we can catch this |
104
|
|
|
$token = ($this->request->header('Authorization') !== null) ? $this->request->header('Authorization') : ''; |
105
|
|
|
if($token !== '') { |
106
|
|
|
return 'false'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$content = $templates->render( |
110
|
|
|
'partials/login', |
111
|
|
|
[ |
112
|
|
|
'title' => 'Login Required', |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
return $content; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function ajaxAction() { |
119
|
|
|
$application = $this->container['application']; |
120
|
|
|
|
121
|
|
|
$input = new StringInput($this->command); |
122
|
|
|
$input->setInteractive(false); |
123
|
|
|
|
124
|
|
|
$output = new BufferedOutput(); |
125
|
|
|
$formatter = $output->getFormatter(); |
126
|
|
|
$formatter->setDecorated(true); |
127
|
|
|
$output->setFormatter(new HtmlOutputFormatter($formatter)); |
128
|
|
|
|
129
|
|
|
$application->setAutoExit(false); |
130
|
|
|
// Some commands dump things out instead of returning a value |
131
|
|
|
ob_start(); |
132
|
|
|
$errorCode = $application->run($input, $output); |
133
|
|
|
if (!$result = $output->fetch()){ |
134
|
|
|
$result = ob_get_contents(); // If empty, replace it by the catched output |
135
|
|
|
} |
136
|
|
|
ob_end_clean(); |
137
|
|
|
$result = nl2br($result); |
138
|
|
|
$result = preg_replace('|<br />\r.*<br />(\r.*?)<br />|', '$1<br />', $result); |
139
|
|
|
|
140
|
|
|
return [ |
141
|
|
|
'input' => $this->command, |
142
|
|
|
'output' => $result, |
143
|
|
|
'environment' => '', |
144
|
|
|
'error_code' => $errorCode |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.