|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\http; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\controllers\Startup; |
|
6
|
|
|
use Ubiquity\utils\base\UString; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Http Request utilities |
|
10
|
|
|
* @author jc |
|
11
|
|
|
* @version 1.0.1 |
|
12
|
|
|
*/ |
|
13
|
|
|
class URequest { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Affects member to member the values of the associative array $values to the members of the object $object |
|
17
|
|
|
* Used for example to retrieve the variables posted and assign them to the members of an object |
|
18
|
|
|
* @param object $object |
|
19
|
|
|
* @param associative array $values |
|
20
|
|
|
*/ |
|
21
|
1 |
|
public static function setValuesToObject($object, $values=null) { |
|
22
|
1 |
|
if (!isset($values)) |
|
23
|
|
|
$values=$_POST; |
|
24
|
1 |
|
foreach ( $values as $key => $value ) { |
|
25
|
1 |
|
$accessor="set" . ucfirst($key); |
|
26
|
1 |
|
if (method_exists($object, $accessor)) { |
|
27
|
1 |
|
$object->$accessor($value); |
|
28
|
1 |
|
$object->_rest[$key]=$value; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Affects member to member the values of $_GET to the members of the object $object |
|
35
|
|
|
* $object must have accessors to each members |
|
36
|
|
|
* @param object $object |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function setGetValuesToObject($object) { |
|
39
|
|
|
self::setValuesToObject($object,$_GET); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Affects member to member the values of $_POST to the members of the object $object |
|
44
|
|
|
* $object must have accessors to each members |
|
45
|
|
|
* @param object $object |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function setPostValuesToObject($object) { |
|
48
|
|
|
self::setValuesToObject($object,$_POST); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Call a cleaning function on the post |
|
53
|
|
|
* @param string $function the cleaning function, default htmlentities |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getPost($function="htmlentities") { |
|
57
|
|
|
return array_map($function, $_POST); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns the query data, for PUT, DELETE PATCH methods |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function getInput() { |
|
64
|
|
|
$put=array (); |
|
65
|
|
|
\parse_str(\file_get_contents('php://input'), $put); |
|
66
|
|
|
return $put; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns the query data, regardless of the method |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function getDatas() { |
|
74
|
|
|
$method=\strtolower($_SERVER['REQUEST_METHOD']); |
|
75
|
|
|
switch($method) { |
|
76
|
|
|
case 'post': |
|
77
|
|
|
return $_POST; |
|
78
|
|
|
case 'get': |
|
79
|
|
|
return $_GET; |
|
80
|
|
|
default: |
|
81
|
|
|
return self::getInput(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns the request content-type header |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function getContentType() { |
|
90
|
|
|
$headers=getallheaders(); |
|
91
|
|
|
if (isset($headers["content-type"])) { |
|
92
|
|
|
return $headers["content-type"]; |
|
93
|
|
|
} |
|
94
|
|
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Copyright © 2008 Darrin Yeager |
|
99
|
|
|
* https://www.dyeager.org/ |
|
100
|
|
|
* Licensed under BSD license. |
|
101
|
|
|
* https://www.dyeager.org/downloads/license-bsd.txt |
|
102
|
|
|
**/ |
|
103
|
1 |
|
public static function getDefaultLanguage() { |
|
104
|
1 |
|
if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) |
|
105
|
1 |
|
return self::parseDefaultLanguage($_SERVER["HTTP_ACCEPT_LANGUAGE"]); |
|
106
|
|
|
else |
|
107
|
|
|
return self::parseDefaultLanguage(NULL); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
private static function parseDefaultLanguage($http_accept, $deflang = "en") { |
|
111
|
1 |
|
if(isset($http_accept) && strlen($http_accept) > 1) { |
|
112
|
1 |
|
$x = explode(",",$http_accept); |
|
113
|
1 |
|
$lang=[]; |
|
114
|
1 |
|
foreach ($x as $val) { |
|
115
|
1 |
|
if(preg_match("/(.*);q=([0-1]{0,1}.\d{0,4})/i",$val,$matches)) |
|
116
|
1 |
|
$lang[$matches[1]] = (float)$matches[2]; |
|
117
|
|
|
else |
|
118
|
1 |
|
$lang[$val] = 1.0; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
$qval = 0.0; |
|
122
|
1 |
|
foreach ($lang as $key => $value) { |
|
123
|
1 |
|
if ($value > $qval) { |
|
124
|
1 |
|
$qval = (float)$value; |
|
125
|
1 |
|
$deflang = $key; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
1 |
|
return $deflang; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public static function setLocale(string $locale){ |
|
133
|
|
|
try { |
|
134
|
|
|
if (class_exists('Locale', false)) { |
|
135
|
|
|
\Locale::setDefault($locale); |
|
136
|
|
|
} |
|
137
|
|
|
} catch (\Exception $e) { |
|
138
|
|
|
//Nothing to do |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns true if the request is an Ajax request |
|
144
|
|
|
* @return boolean |
|
145
|
|
|
*/ |
|
146
|
12 |
|
public static function isAjax() { |
|
147
|
12 |
|
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Returns true if the request is sent by the POST method |
|
152
|
|
|
* @return boolean |
|
153
|
|
|
*/ |
|
154
|
4 |
|
public static function isPost() { |
|
155
|
4 |
|
return $_SERVER['REQUEST_METHOD'] === 'POST'; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns true if the request is cross site |
|
160
|
|
|
* @return boolean |
|
161
|
|
|
*/ |
|
162
|
|
|
public static function isCrossSite() { |
|
163
|
|
|
return stripos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) === FALSE; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Returns true if request contentType is set to json |
|
168
|
|
|
* @return boolean |
|
169
|
|
|
*/ |
|
170
|
|
|
public static function isJSON() { |
|
171
|
|
|
$contentType=self::getContentType(); |
|
172
|
|
|
return \stripos($contentType, "json") !== false; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Returns the value of the $key variable passed by the get method or $default if the $key variable does not exist |
|
177
|
|
|
* @param string $key |
|
178
|
|
|
* @param string $default return value by default |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
public static function get($key, $default=NULL) { |
|
182
|
|
|
return isset($_GET[$key]) ? $_GET[$key] : $default; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Returns a boolean at the key position in request |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $key |
|
189
|
|
|
* the key to add or set |
|
190
|
|
|
* @return boolean |
|
191
|
|
|
*/ |
|
192
|
|
|
public static function getBoolean($key) { |
|
193
|
|
|
$ret = false; |
|
194
|
|
|
if (isset ( $_REQUEST[$key] )) { |
|
195
|
|
|
$ret = UString::isBooleanTrue ( $_REQUEST[$key] ); |
|
196
|
|
|
} |
|
197
|
|
|
return $ret; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Returns the value of the $key variable passed by the post method or $default if the $key variable does not exist |
|
202
|
|
|
* @param string $key |
|
203
|
|
|
* @param string $default return value by default |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
2 |
|
public static function post($key, $default=NULL) { |
|
207
|
2 |
|
return isset($_POST[$key]) ? $_POST[$key] : $default; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
12 |
|
public static function getUrl($url) { |
|
211
|
12 |
|
$config=Startup::getConfig(); |
|
212
|
12 |
|
$siteUrl=\rtrim($config["siteUrl"], '/'); |
|
213
|
12 |
|
if (UString::startswith($url, "/") === false) { |
|
214
|
12 |
|
$url="/" . $url; |
|
215
|
|
|
} |
|
216
|
12 |
|
return $siteUrl . $url; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public static function getUrlParts() { |
|
220
|
|
|
return \explode("/", $_GET["c"]); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Returns the http method |
|
225
|
|
|
* @return string |
|
226
|
|
|
*/ |
|
227
|
1 |
|
public static function getMethod() { |
|
228
|
1 |
|
return \strtolower($_SERVER['REQUEST_METHOD']); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public static function cleanUrl($url){ |
|
232
|
|
|
$url=\str_replace("\\", "/", $url); |
|
233
|
|
|
return \str_replace("//", "/", $url); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|