Completed
Pull Request — master (#217)
by Thomas
02:42
created

IndexController::ajaxAction()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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