Completed
Pull Request — master (#217)
by Thomas
03:10
created

IndexController::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 6
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
			$templates = new Engine(CURRENT_DIR . '/src/Resources/views/');
58
			$templates->loadExtension(new Asset(CURRENT_DIR . '/pub/', false));
59
			$templates->loadExtension(new URI($_SERVER['REQUEST_URI']));
60
61
			// TODO: Check for user permissions
62
			//$content = $templates->render('partials/login', ['title' => 'Login Required']);
63
			$content = $templates->render('partials/inner', ['title' => 'Updater']);
64
		} else {
65
			header('Content-Type: application/json');
66
			$content = json_encode($this->ajaxAction(), JSON_UNESCAPED_SLASHES);
67
		}
68
		return $content;
69
	}
70
71
	public function ajaxAction(){
72
		$application = $this->container['application'];
73
74
		$input = new StringInput($this->command);
75
		$input->setInteractive(false);
76
77
		$output = new BufferedOutput();
78
		$formatter = $output->getFormatter();
79
		$formatter->setDecorated(true);
80
		$output->setFormatter(new HtmlOutputFormatter($formatter));
81
82
		$application->setAutoExit(false);
83
// Some commands  dump things out instead of returning a value
84
		ob_start();
85
		$errorCode = $application->run($input, $output);
86
		if (!$result = $output->fetch()){
87
			$result = ob_get_contents(); // If empty, replace it by the catched output
88
		}
89
		ob_end_clean();
90
91
		return [
92
			'input' => $this->command,
93
			'output' => nl2br($result),
94
			'environment' => '',
95
			'error_code' => $errorCode
96
		];
97
	}
98
99
}
100