|
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 Owncloud\Updater\Console\Application; |
|
26
|
|
|
use Symfony\Component\Process\Process; |
|
27
|
|
|
use Symfony\Component\Process\ProcessUtils; |
|
28
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
|
29
|
|
|
use Owncloud\Updater\Utils\Locator; |
|
30
|
|
|
|
|
31
|
|
|
class OccRunner { |
|
32
|
|
|
/** |
|
33
|
|
|
* @var Locator $locator |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $locator; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $canUseProcess; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* |
|
44
|
|
|
* @param Locator $locator |
|
45
|
|
|
* @param bool $canUseProcess |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function __construct(Locator $locator, $canUseProcess){ |
|
48
|
2 |
|
$this->locator = $locator; |
|
49
|
2 |
|
$this->canUseProcess = $canUseProcess; |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function run($command, $args = [], $asJson = false){ |
|
53
|
2 |
|
if ($this->canUseProcess){ |
|
54
|
1 |
|
$extra = $asJson ? '--output=json' : ''; |
|
55
|
1 |
|
$cmdLine = trim($command . ' ' . $extra); |
|
56
|
1 |
|
foreach ($args as $optionTitle => $optionValue){ |
|
57
|
|
|
if (strpos($optionTitle, '--') === 0){ |
|
58
|
|
|
$line = trim("$optionTitle $optionValue"); |
|
59
|
|
|
} else { |
|
60
|
|
|
$line = $optionValue; |
|
61
|
|
|
} |
|
62
|
|
|
$escapedLine = ProcessUtils::escapeArgument($line); |
|
63
|
|
|
$cmdLine .= " $escapedLine"; |
|
64
|
1 |
|
} |
|
65
|
1 |
|
return $this->runAsProcess($cmdLine); |
|
66
|
|
|
} else { |
|
67
|
1 |
|
if ($asJson){ |
|
68
|
1 |
|
$args['--output'] = 'json'; |
|
69
|
1 |
|
} |
|
70
|
1 |
|
$response = $this->runAsRequest($command, $args); |
|
71
|
1 |
|
$decodedResponse = json_decode($response, true); |
|
72
|
1 |
|
return $decodedResponse['response']; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
5 |
|
public function runJson($command, $args = []){ |
|
77
|
5 |
|
$plain = $this->run($command, $args, true); |
|
78
|
|
|
// trim response to always be a valid json. Capture everything between the first and the last curly brace |
|
79
|
5 |
|
preg_match_all('!(\{.*\})!ms', $plain, $matches); |
|
80
|
5 |
|
$clean = isset($matches[1][0]) ? $matches[1][0] : ''; |
|
81
|
5 |
|
$decoded = json_decode($clean, true); |
|
82
|
5 |
|
if (!is_array($decoded)){ |
|
83
|
1 |
|
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); |
|
84
|
|
|
} |
|
85
|
4 |
|
return $decoded; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function runAsRequest($command, $args){ |
|
89
|
|
|
$application = $this->getApplication(); |
|
90
|
|
|
$client = new Client(); |
|
91
|
|
|
$request = $client->createRequest( |
|
92
|
|
|
'POST', |
|
93
|
|
|
$application->getEndpoint() . $command, |
|
94
|
|
|
[ |
|
95
|
|
|
'timeout' => 0, |
|
96
|
|
|
'json' => [ |
|
97
|
|
|
'token' => $application->getAuthToken(), |
|
98
|
|
|
'params'=> $args |
|
99
|
|
|
] |
|
100
|
|
|
] |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
$response = $client->send($request); |
|
104
|
|
|
$responseBody = $response->getBody()->getContents(); |
|
105
|
|
|
return $responseBody; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function getApplication(){ |
|
109
|
|
|
$container = Application::$container; |
|
110
|
|
|
$application = $container['application']; |
|
111
|
|
|
return $application; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
protected function runAsProcess($cmdLine){ |
|
115
|
|
|
$occPath = $this->locator->getPathToOccFile(); |
|
116
|
|
|
$cmd = "php $occPath --no-warnings $cmdLine"; |
|
117
|
|
|
$process = new Process($cmd); |
|
118
|
|
|
$process->setTimeout(null); |
|
119
|
|
|
$process->run(); |
|
120
|
|
|
|
|
121
|
|
|
if (!$process->isSuccessful()){ |
|
122
|
|
|
throw new ProcessFailedException($process); |
|
123
|
|
|
} |
|
124
|
|
|
return $process->getOutput(); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|