|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Control; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Environment; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* CLI specific request building logic |
|
9
|
|
|
*/ |
|
10
|
|
|
class CLIRequestBuilder extends HTTPRequestBuilder |
|
11
|
|
|
{ |
|
12
|
|
|
public static function cleanEnvironment(array $variables) |
|
13
|
|
|
{ |
|
14
|
|
|
// Create all blank vars |
|
15
|
|
|
foreach (['_REQUEST', '_GET', '_POST', '_SESSION', '_SERVER', '_COOKIE', '_ENV', '_FILES'] as $key) { |
|
16
|
|
|
if (!isset($variables[$key])) { |
|
17
|
|
|
$variables[$key] = []; |
|
18
|
|
|
}; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
// We update the $_SERVER variable to contain data consistent with the rest of the application. |
|
22
|
|
|
$variables['_SERVER'] = array_merge(array( |
|
23
|
|
|
'SERVER_PROTOCOL' => 'HTTP/1.1', |
|
24
|
|
|
'HTTP_ACCEPT' => 'text/plain;q=0.5', |
|
25
|
|
|
'HTTP_ACCEPT_LANGUAGE' => '*;q=0.5', |
|
26
|
|
|
'HTTP_ACCEPT_ENCODING' => '', |
|
27
|
|
|
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1;q=0.5', |
|
28
|
|
|
'SERVER_SIGNATURE' => 'Command-line PHP/' . phpversion(), |
|
29
|
|
|
'SERVER_SOFTWARE' => 'PHP/' . phpversion(), |
|
30
|
|
|
'SERVER_ADDR' => '127.0.0.1', |
|
31
|
|
|
'REMOTE_ADDR' => '127.0.0.1', |
|
32
|
|
|
'REQUEST_METHOD' => 'GET', |
|
33
|
|
|
'HTTP_USER_AGENT' => 'CLI', |
|
34
|
|
|
), $variables['_SERVER']); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Process arguments and load them into the $_GET and $_REQUEST arrays |
|
38
|
|
|
* For example, |
|
39
|
|
|
* sake my/url somearg otherarg key=val --otherkey=val third=val&fourth=val |
|
40
|
|
|
* |
|
41
|
|
|
* Will result in the following get data: |
|
42
|
|
|
* args => array('somearg', 'otherarg'), |
|
43
|
|
|
* key => val |
|
44
|
|
|
* otherkey => val |
|
45
|
|
|
* third => val |
|
46
|
|
|
* fourth => val |
|
47
|
|
|
*/ |
|
48
|
|
|
if (isset($variables['_SERVER']['argv'][2])) { |
|
49
|
|
|
$args = array_slice($variables['_SERVER']['argv'], 2); |
|
50
|
|
|
foreach ($args as $arg) { |
|
51
|
|
|
if (strpos($arg, '=') == false) { |
|
|
|
|
|
|
52
|
|
|
$variables['_GET']['args'][] = $arg; |
|
53
|
|
|
} else { |
|
54
|
|
|
$newItems = array(); |
|
55
|
|
|
parse_str((substr($arg, 0, 2) == '--') ? substr($arg, 2) : $arg, $newItems); |
|
56
|
|
|
$variables['_GET'] = array_merge($variables['_GET'], $newItems); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
$_REQUEST = array_merge($_REQUEST, $variables['_GET']); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Set 'url' GET parameter |
|
63
|
|
|
if (isset($variables['_SERVER']['argv'][1])) { |
|
64
|
|
|
$variables['_GET']['url'] = $variables['_SERVER']['argv'][1]; |
|
65
|
|
|
$variables['_SERVER']['REQUEST_URI'] = $variables['_SERVER']['argv'][1]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// Parse rest of variables as standard |
|
69
|
|
|
return parent::cleanEnvironment($variables); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $variables |
|
74
|
|
|
* @param string $input |
|
75
|
|
|
* @return HTTPRequest |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function createFromVariables(array $variables, $input) |
|
78
|
|
|
{ |
|
79
|
|
|
$request = parent::createFromVariables($variables, $input); |
|
80
|
|
|
// unset scheme so that SS_BASE_URL can provide `is_https` information if required |
|
81
|
|
|
$scheme = parse_url(Environment::getEnv('SS_BASE_URL'), PHP_URL_SCHEME); |
|
82
|
|
|
$request->setScheme($scheme); |
|
83
|
|
|
|
|
84
|
|
|
return $request; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|