Completed
Pull Request — master (#217)
by Victor
06:07
created

IndexController::dispatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
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 Symfony\Component\Console\Input\StringInput;
26
use Symfony\Component\Console\Output\BufferedOutput;
27
use Owncloud\Updater\Formatter\HtmlOutputFormatter;
28
use Owncloud\Updater\Http\Request;
29
use League\Plates\Engine;
30
use League\Plates\Extension\Asset;
31
32
class IndexController {
33
34
	/** @var \Pimple\Container */
35
	protected $container;
36
37
	/** @var Request */
38
	protected $request;
39
40
	/** @var string $command */
41
	protected $command;
42
43
	public function __construct(\Pimple\Container $container, $request = null){
44
		$this->container = $container;
45
		if (is_null($request)){
46
			$this->request = new Request(['post' => $_POST]);
47
		} else {
48
			$this->request = $request;
49
		}
50
51
		$this->command = $this->request->postParameter('command');
52
	}
53
54
	public function dispatch(){
55
		if (is_null($this->command)){
56
			$templates = new Engine(CURRENT_DIR . '/src/Resources/views/');
57
			$templates->loadExtension(new Asset(CURRENT_DIR . '/pub/', false));
58
59
			// TODO: Check for user permissions
60
			//$content = $templates->render('partials/login', ['title' => 'Login Required']);
61
			$content = $templates->render('partials/inner', ['title' => 'Updater']);
62
		} else {
63
			header('Content-Type: application/json');
64
			$content = json_encode($this->ajaxAction(), JSON_UNESCAPED_SLASHES);
65
		}
66
		return $content;
67
	}
68
69
	public function ajaxAction(){
70
		$application = $this->container['application'];
71
72
		$input = new StringInput($this->command);
73
		$input->setInteractive(false);
74
75
		$output = new BufferedOutput();
76
		$formatter = $output->getFormatter();
77
		$formatter->setDecorated(true);
78
		$output->setFormatter(new HtmlOutputFormatter($formatter));
79
80
		$application->setAutoExit(false);
81
// Some commands  dump things out instead of returning a value
82
		ob_start();
83
		$errorCode = $application->run($input, $output);
84
		if (!$result = $output->fetch()){
85
			$result = ob_get_contents(); // If empty, replace it by the catched output
86
		}
87
		ob_end_clean();
88
89
		return [
90
			'input' => $this->command,
91
			'output' => nl2br($result),
92
			'environment' => '',
93
			'error_code' => $errorCode
94
		];
95
	}
96
97
}
98