Completed
Pull Request — master (#355)
by Victor
02:52
created

OccRunner::runAsRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

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 18
ccs 0
cts 13
cp 0
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace Owncloud\Updater\Utils;
23
24
use GuzzleHttp\Client;
25
use Symfony\Component\Process\Process;
26
use Symfony\Component\Process\ProcessUtils;
27
use Symfony\Component\Process\Exception\ProcessFailedException;
28
use Owncloud\Updater\Utils\Locator;
29
30
class OccRunner {
31
	/**
32
	 * @var Locator $locator
33
	 */
34
	protected $locator;
35
36
	/**
37
	 * @var bool
38
	 */
39
	protected $canUseProcess;
40
41
	/**
42
	 *
43
	 * @param Locator $locator
44
	 */
45
	public function __construct(Locator $locator){
46
		$this->locator = $locator;
47
48
		$disabled = explode(',', ini_get('disable_functions'));
49
		$this->canUseProcess = function_exists('proc_open') && !in_array('proc_open', $disabled);
50
	}
51
52
	public function run($command, $args = [], $extra = ''){
53
		if ($this->canUseProcess){
54
			$cmdLine = trim($command . ' ' . $extra);
55
			foreach ($args as $optionTitle => $optionValue){
56
				if (strpos($optionTitle, '--') === 0){
57
					$line = trim("$optionTitle $optionValue");
58
				} else {
59
					$line = $optionValue;
60
				}
61
				$escapedLine = ProcessUtils::escapeArgument($line);
62
				$cmdLine .= " $escapedLine";
63
			}
64
			return $this->runAsProcess($cmdLine);
65
		} else {
66
			return $this->runAsRequest($command, $args);
67
		}
68
	}
69
70 3
	public function runJson($command, $args = []){
71 3
		$plain = $this->run($command, $args, '--output=json');
72 3
		$decoded = json_decode($plain, true);
73 3
		if (!is_array($decoded)){
74
			throw new \UnexpectedValueException('Could not parse a response for ' . $command . '. Please check if the current shell user can run occ command. Raw output: ' . PHP_EOL . $plain);
75
		}
76 3
		return $decoded;
77
	}
78
79
	private function runAsRequest($command, $args){
80
		$client = new Client();
81
		$request = $client->createRequest(
82
			'POST',
83
			// TODO Get endpoint somehow :-/
84
			'http://localhost/~deo/oc-tmp/index.php/occ/' . $command,
85
			[
86
				'timeout' => 0
87
			]
88
		);
89
90
		$query = $request->getQuery();
91
		foreach ($args as $optionTitle => $optionValue){
92
			$query[$optionTitle] = $optionValue;
93
		}
94
		$response = $client->send($request);
95
		return $response->getBody()->getContents();
96
	}
97
98
	private function runAsProcess($cmdLine){
99
		$occPath = $this->locator->getPathToOccFile();
100
		$cmd = "php $occPath --no-warnings $cmdLine";
101
		$process = new Process($cmd);
102
		$process->setTimeout(null);
103
		$process->run();
104
105
		if (!$process->isSuccessful()){
106
			throw new ProcessFailedException($process);
107
		}
108
		return $process->getOutput();
109
	}
110
}
111