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

IndexController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 1
cbo 8
dl 0
loc 105
ccs 0
cts 75
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
B dispatch() 0 32 3
B ajaxAction() 0 36 4
A getToken() 0 11 1
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
			$checkpoints = $this->container['utils.checkpoint']->getAll();
62
63
			// strip index.php and query string (if any) to get a real base url
64
			$baseUrl = preg_replace('/(index\.php.*|\?.*)$/', '', $_SERVER['REQUEST_URI']);
65
66
			$templates = new Engine(CURRENT_DIR . '/src/Resources/views/');
67
			$templates->loadExtension(new Asset(CURRENT_DIR . '/pub/', false));
68
			$templates->loadExtension(new URI($baseUrl));
69
70
			// TODO: Check for user permissions
71
			//$content = $templates->render('partials/login', ['title' => 'Login Required']);
72
			$content = $templates->render(
73
					'partials/inner',
74
					[
75
						'title' => 'Updater',
76
						'token' => $_SESSION['updater_ajax_token'],
77
						'version' => $this->container['application']->getVersion(),
78
						'checkpoints' => $checkpoints
79
					]
80
			);
81
		} else {
82
			header('Content-Type: application/json');
83
			$content = json_encode($this->ajaxAction(), JSON_UNESCAPED_SLASHES);
84
		}
85
		return $content;
86
	}
87
88
	public function ajaxAction(){
89
		if (is_null($this->request->postParameter('token'))
90
				|| $this->request->postParameter('token') !== $_SESSION['updater_ajax_token']
91
		){
92
			header( 'HTTP/1.0 401 Unauthorized' );
93
			exit();
94
		}
95
96
		$application = $this->container['application'];
97
98
		$input = new StringInput($this->command);
99
		$input->setInteractive(false);
100
101
		$output = new BufferedOutput();
102
		$formatter = $output->getFormatter();
103
		$formatter->setDecorated(true);
104
		$output->setFormatter(new HtmlOutputFormatter($formatter));
105
106
		$application->setAutoExit(false);
107
		// Some commands  dump things out instead of returning a value
108
		ob_start();
109
		$errorCode = $application->run($input, $output);
110
		if (!$result = $output->fetch()){
111
			$result = ob_get_contents(); // If empty, replace it by the catched output
112
		}
113
		ob_end_clean();
114
		$result = nl2br($result);
115
		$result = preg_replace('|<br />\r.*<br />(\r.*?)<br />|', '$1<br />', $result);
116
117
		return [
118
			'input' => $this->command,
119
			'output' => $result,
120
			'environment' => '',
121
			'error_code' => $errorCode
122
		];
123
	}
124
125
	protected function getToken(){
126
		return base64_encode(
127
				join(
128
						'', array_map(
129
								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...
130
							return chr(mt_rand(1, 255));
131
						}, range(1, 15)
132
						)
133
				)
134
		);
135
	}
136
137
}
138