|
1
|
|
|
<?php |
|
2
|
|
|
namespace CloudFramework\Tool; |
|
3
|
|
|
|
|
4
|
|
|
final class RequestParser |
|
5
|
|
|
{ |
|
6
|
|
|
/** |
|
7
|
|
|
* Catch all request headers |
|
8
|
|
|
* @return array |
|
9
|
|
|
*/ |
|
10
|
|
|
public static function getRequestHeaders() |
|
11
|
|
|
{ |
|
12
|
|
|
$headers = array(); |
|
13
|
|
|
if(function_exists("getallheaders")) { |
|
14
|
|
|
foreach (getallheaders() as $key => $value) { |
|
15
|
|
|
$headers[strtolower($key)] = $value; |
|
16
|
|
|
} |
|
17
|
|
|
} |
|
18
|
|
|
return $headers; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Catch all post data |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function getPostParams() |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$data = array(); |
|
28
|
|
|
try { |
|
29
|
|
|
$rawData = file_get_contents('php://input'); |
|
30
|
|
|
$data = json_decode($rawData, JSON_UNESCAPED_UNICODE); |
|
31
|
|
|
if (null === $data) { |
|
32
|
|
|
parse_str($rawData, $data); |
|
33
|
|
|
} |
|
34
|
|
|
} catch (\Exception $t) { |
|
35
|
|
|
syslog(LOG_ERR, $t->getMessage()); |
|
36
|
|
|
} |
|
37
|
|
|
$data = array_merge($data, $_POST); |
|
38
|
|
|
return $data; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Catch query string data |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function getQueryString() |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$query = array(); |
|
48
|
|
|
try { |
|
49
|
|
|
parse_str($_SERVER['QUERY_STRING'], $query); |
|
50
|
|
|
} catch(\Exception $t) { |
|
51
|
|
|
syslog(LOG_ERR, $t->getMessage()); |
|
52
|
|
|
} |
|
53
|
|
|
return $query; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Gets post param |
|
58
|
|
|
* @param $key |
|
59
|
|
|
* @return mixed|null |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function getPostParam($key) |
|
62
|
|
|
{ |
|
63
|
|
|
$post = self::getPostParams(); |
|
64
|
|
|
return array_key_exists($key, $post) ? $post[$key] : null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get specific header |
|
69
|
|
|
* @param string $key |
|
70
|
|
|
* @return string|null |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function getHeader($key) |
|
73
|
|
|
{ |
|
74
|
|
|
$key = strtolower(trim($key)); |
|
75
|
|
|
$headers = self::getRequestHeaders(); |
|
76
|
|
|
return (array_key_exists($key, $headers)) ? $headers[$key] : null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get query param |
|
81
|
|
|
* @param $key |
|
82
|
|
|
* @return array|null |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function getQueryParam($key) |
|
85
|
|
|
{ |
|
86
|
|
|
$query = self::getQueryString(); |
|
87
|
|
|
return array_key_exists($key, $query) ? $query[$key] : null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get $_SERVER value |
|
92
|
|
|
* @param string $key |
|
93
|
|
|
* @return null|mixed |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function getServerKey($key) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
$value = null; |
|
98
|
|
|
if (array_key_exists($key, $_SERVER)) { |
|
99
|
|
|
$value = $_SERVER[$key]; |
|
100
|
|
|
} |
|
101
|
|
|
return $value; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get $_FILES value |
|
106
|
|
|
* @param string $key |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function getUploadedFile($key) |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
$file = array(); |
|
112
|
|
|
if (array_key_exists($key, $_FILES)) { |
|
113
|
|
|
$file = $_FILES[$key]; |
|
114
|
|
|
} |
|
115
|
|
|
return $file; |
|
116
|
|
|
} |
|
117
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: