Completed
Pull Request — master (#217)
by Victor
02:43
created

IndexController::ajaxAction()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 36
ccs 0
cts 30
cp 0
rs 8.5806
cc 4
eloc 25
nc 3
nop 0
crap 20
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 Symfony\Component\Console\Input\StringInput;
27
use Symfony\Component\Console\Output\BufferedOutput;
28
use Owncloud\Updater\Formatter\HtmlOutputFormatter;
29
use Owncloud\Updater\Http\Request;
30
use League\Plates\Engine;
31
use League\Plates\Extension\Asset;
32
33
class IndexController {
34
35
	/** @var \Pimple\Container */
36
	protected $container;
37
38
	/** @var Request */
39
	protected $request;
40
41
	/** @var string $command */
42
	protected $command;
43
44
	public function __construct(\Pimple\Container $container, $request = null){
45
		$this->container = $container;
46
		if (is_null($request)){
47
			$this->request = new Request(['post' => $_POST]);
48
		} else {
49
			$this->request = $request;
50
		}
51
52
		$this->command = $this->request->postParameter('command');
53
	}
54
55
	public function dispatch(){
56
		if (is_null($this->command)){
57
			if (!isset($_SESSION['updater_ajax_token'])){
58
				$_SESSION['updater_ajax_token'] = $this->gettoken();
59
			}
60
61
			// strip index.php and query string (if any) to get a real base url
62
			$baseUrl = preg_replace('/(index\.php.*|\?.*)$/', '', $_SERVER['REQUEST_URI']);
63
64
			$templates = new Engine(CURRENT_DIR . '/src/Resources/views/');
65
			$templates->loadExtension(new Asset(CURRENT_DIR . '/pub/', false));
66
			$templates->loadExtension(new URI($baseUrl));
67
68
			// TODO: Check for user permissions
69
			//$content = $templates->render('partials/login', ['title' => 'Login Required']);
70
			$content = $templates->render(
71
					'partials/inner',
72
					[
73
						'title' => 'Updater',
74
						'token' => $_SESSION['updater_ajax_token'],
75
						'version' => $this->container['application']->getVersion()
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
	public function ajaxAction(){
86
		if (is_null($this->request->postParameter('token'))
87
				|| $this->request->postParameter('token') !== $_SESSION['updater_ajax_token']
88
		){
89
			header( 'HTTP/1.0 401 Unauthorized' );
90
			exit();
91
		}
92
93
		$application = $this->container['application'];
94
95
		$input = new StringInput($this->command);
96
		$input->setInteractive(false);
97
98
		$output = new BufferedOutput();
99
		$formatter = $output->getFormatter();
100
		$formatter->setDecorated(true);
101
		$output->setFormatter(new HtmlOutputFormatter($formatter));
102
103
		$application->setAutoExit(false);
104
		// Some commands  dump things out instead of returning a value
105
		ob_start();
106
		$errorCode = $application->run($input, $output);
107
		if (!$result = $output->fetch()){
108
			$result = ob_get_contents(); // If empty, replace it by the catched output
109
		}
110
		ob_end_clean();
111
		$result = nl2br($result);
112
		$result = preg_replace('|<br />\r.*<br />(\r.*?)<br />|', '$1<br />', $result);
113
114
		return [
115
			'input' => $this->command,
116
			'output' => $result,
117
			'environment' => '',
118
			'error_code' => $errorCode
119
		];
120
	}
121
122
	protected function getToken(){
123
		return base64_encode(
124
				join(
125
						'', array_map(
126
								function($x){
0 ignored issues
show
Unused Code introduced by
The parameter $x is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
							return chr(mt_rand(1, 255));
128
						}, range(1, 15)
129
						)
130
				)
131
		);
132
	}
133
134
}
135