-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.php
282 lines (226 loc) · 7.17 KB
/
Main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* @package CLI API
* @author Iurii Makukh <gplcart.software@gmail.com>
* @copyright Copyright (c) 2018, Iurii Makukh <gplcart.software@gmail.com>
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
*/
namespace gplcart\modules\cli_api;
use Exception;
use gplcart\core\Config;
use gplcart\core\Container;
use RuntimeException;
use UnexpectedValueException;
/**
* Main class for CLI API module
*/
class Main
{
/**
* Config class instance
* @var \gplcart\core\Config $config
*/
protected $config;
/**
* @param Config $config
*/
public function __construct(Config $config)
{
$this->config = $config;
}
/**
* Implements hook "module.install.before"
* @param null|string $result
*/
public function hookModuleInstallBefore(&$result)
{
$exec_enabled = function_exists('exec')
&& !in_array('exec', array_map('trim', explode(',', ini_get('disable_functions'))))
&& exec('echo EXEC') === 'EXEC';
if (!$exec_enabled) {
$result = gplcart_text('exec() function is disabled');
}
}
/**
* Implements hook "module.uninstall.before"
*/
public function hookModuleUninstallAfter()
{
$this->config->reset('module_cli_api_phpexe_file');
}
/**
* Implements hook "module.api.process"
* @param array $params
* @param array $user
* @param mixed $response
*/
public function hookModuleApiProcess(array $params, array $user, &$response)
{
if (!isset($response)) {
$result = $this->exec($params, $user);
$response = json_decode($result, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$response = $result;
}
}
}
/**
* Executes a command using an array of API request parameters
* @param array $params
* @param array $user
* @param bool $return_string
* @return string|array
*/
public function exec(array $params, array $user, $return_string = true)
{
try {
$php = $this->getPhpExe();
} catch (Exception $ex) {
return 'Error defining PHP executable: ' . $ex->getMessage();
}
try {
$cmd = $this->getCommand($params, $user);
} catch (Exception $ex) {
return 'Error constructing CLI command: ' . $ex->getMessage();
}
$exe = GC_FILE_CLI;
$output = $return = null;
exec(escapeshellcmd("$php $exe $cmd"), $output, $return);
if (!empty($return) && empty($output)) {
$output = $return;
}
return $return_string ? trim(implode('', $output)) : $output;
}
/**
* Returns a saved path to PHP executable file
* @return string
*/
protected function getPhpExe()
{
$file = (string) $this->config->get('module_cli_api_phpexe_file', '');
if (empty($file)) {
$file = $this->findPhpExe();
$this->setPhpExe($file);
}
return $file;
}
/**
* Sets the path to PHP executable file
* @param string $file
* @return bool
*/
public function setPhpExe($file)
{
return $this->config->set('module_cli_api_phpexe_file', $file);
}
/**
* Returns the path to PHP executable file
* @return string
* @throws RuntimeException
*/
public function findPhpExe()
{
$php = getenv('PHP_BINARY');
if (!empty($php)) {
if (is_executable($php)) {
return $php;
}
throw new RuntimeException('PHP_BINARY is not executable');
}
$php = getenv('PHP_PATH');
if (!empty($php)) {
if (is_executable($php)) {
return $php;
}
throw new RuntimeException('PHP_PATH is not executable');
}
$php = getenv('PHP_PEAR_PHP_BIN');
if (!empty($php) && is_executable($php)) {
return $php;
}
$php = PHP_BINDIR . (DIRECTORY_SEPARATOR === '\\' ? '\\php.exe' : '/php');
if (is_executable($php)) {
return $php;
}
if (ini_get('open_basedir')) {
$dirs = array();
foreach (explode(PATH_SEPARATOR, ini_get('open_basedir')) as $path) {
// Silencing against https://bugs.php.net/69240
if (@is_dir($path)) {
$dirs[] = $path;
continue;
}
if (basename($path) === 'php' && @is_executable($path)) {
return $path;
}
}
} else {
$dirs = array(PHP_BINDIR);
if (DIRECTORY_SEPARATOR === '\\') {
$dirs[] = 'C:\xampp\php\\';
}
$dirs = array_merge(explode(PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')), $dirs);
}
$suffixes = array('');
if (DIRECTORY_SEPARATOR === '\\') {
$path_ext = getenv('PATHEXT');
if (!empty($path_ext)) {
$suffixes = array_merge($suffixes, explode(PATH_SEPARATOR, $path_ext));
}
}
foreach ($suffixes as $suffix) {
foreach ($dirs as $dir) {
$file = $dir . DIRECTORY_SEPARATOR . "php$suffix";
if (@is_file($file) && (DIRECTORY_SEPARATOR === '\\' || is_executable($file))) {
return $file;
}
}
}
throw new RuntimeException('Cannot find PHP executable file');
}
/**
* Convert an array of params into a CLI command
* @param array $params
* @param array $user
* @throws RuntimeException
* @throws UnexpectedValueException
* @return string
*/
public function getCommand(array $params, array $user)
{
$params += array(
'get' => array(),
'post' => array(),
'arguments' => array()
);
if (count($params['arguments']) != 1) {
throw new UnexpectedValueException('"arguments" key must contain exactly one array element');
}
if (empty($user['user_id'])) {
throw new UnexpectedValueException('Second argument must contain a valid user ID under "user_id" key');
}
$command = array(reset($params['arguments']));
$route = $this->getCliRouteInstance()->get($command[0]);
if (empty($route['access'])) {
throw new RuntimeException('Undefined user access');
}
foreach (array_merge($params['get'], $params['post']) as $key => $value) {
if (is_string($value) && strlen($key) > 1) {
$command[] = rtrim("--$key=" . escapeshellarg($value), '=');
}
}
$command[] = " -u=" . escapeshellarg($user['user_id']);
$command[] = " -f=json";
return implode(' ', $command);
}
/**
* Returns CLI router instance
* @return \gplcart\core\CliRoute
*/
protected function getCliRouteInstance()
{
/** @var \gplcart\core\CliRoute $instance */
$instance = Container::get('gplcart\\core\\CliRoute');
return $instance;
}
}