1: <?php
2:
3: namespace ngatngay\http;
4:
5: use ngatngay\json;
6: use RuntimeException;
7:
8: class request
9: {
10: /**
11: * @var array
12: */
13: public static $file;
14:
15: /**
16: * @var array
17: */
18: public static $header;
19:
20: /**
21: * @var array
22: */
23: public static $server;
24:
25: /**
26: * @var array
27: */
28: public static $payload;
29:
30: /**
31: * @return void
32: */
33: public static function init()
34: {
35: /*
36: {
37: $request_uri = @parse_url($_SERVER['REQUEST_URI'] ?? '');
38: $add_get = [];
39:
40: if (isset($request_uri['query'])) {
41: parse_str($request_uri['query'], $add_get);
42: }
43:
44: $_GET = array_merge($_GET, $add_get);
45: $_REQUEST = array_merge($_REQUEST, $add_get);
46: }
47: */
48: //self::init_payload();
49: //self::init_file();
50: }
51:
52: // common
53:
54: /**
55: * @return bool
56: */
57: public static function is_cli()
58: {
59: return \php_sapi_name() === 'cli';
60: }
61: /**
62: * @return bool
63: */
64: public static function is_cli_server()
65: {
66: return \php_sapi_name() === 'cli-server';
67: }
68:
69: /**
70: * @return string
71: */
72: public static function script_name()
73: {
74: return self::server('script_name');
75: }
76:
77: /**
78: * @return string
79: */
80: public static function method()
81: {
82: return strtolower((string) self::server('REQUEST_METHOD', 'get'));
83: }
84:
85: /**
86: * @param string $value
87: * @return bool
88: */
89: public static function is_method($value)
90: {
91: return strtolower($value) === self::method();
92: }
93:
94: /**
95: * @return bool
96: */
97: public static function is_ajax() {
98: return self::has_header('X_REQUESTED_WITH') &&
99: strtolower((string) self::header('X_REQUESTED_WITH')) === 'xmlhttprequest';
100: }
101:
102: /**
103: * @return string
104: */
105: public static function ip()
106: {
107: $keys = [
108: 'HTTP_CLIENT_IP',
109: 'HTTP_X_FORWARDED_FOR',
110: 'HTTP_X_FORWARDED',
111: 'HTTP_FORWARDED_FOR',
112: 'HTTP_FORWARDED',
113: 'REMOTE_ADDR'
114: ];
115: foreach ($keys as $key) {
116: if (isset($_SERVER[$key])) {
117: return $_SERVER[$key];
118: }
119: }
120:
121: return '127.0.0.1';
122: }
123:
124: /**
125: * @return string
126: */
127: public static function user_agent()
128: {
129: return (string) self::header('user_agent');
130: }
131:
132: /**
133: * @return string
134: */
135: public static function referer()
136: {
137: return (string) self::header('referer');
138: }
139:
140: /**
141: * @return string
142: */
143: public static function host()
144: {
145: return (string) self::header('host');
146: }
147: /**
148: * @return string
149: */
150: public static function base_url()
151: {
152: return self::server('request_scheme', 'http')
153: . '://'
154: . self::server('server_name', 'localhost');
155: }
156:
157: /**
158: * @param string $mode
159: * @return string
160: */
161: public static function uri($mode = 'full')
162: {
163: $uri = self::server('request_uri');
164:
165: switch ($mode) {
166: case 'request':
167: return $uri;
168: case 'no_query':
169: return strtok($uri, '?');
170: default:
171: return self::base_url() . $uri;
172: }
173: }
174:
175: /**
176: * @return string
177: */
178: public static function query_string()
179: {
180: return (string) self::server('query_string');
181: }
182:
183: // HEADER
184: /**
185: * @param string $key
186: * @param mixed $default
187: * @return mixed
188: */
189: public static function header($key = '', $default = null)
190: {
191: if ($key === '') {
192: $headers = [];
193: foreach ($_SERVER as $key => $value) {
194: if (str_starts_with($key, 'HTTP_')) {
195: $headers[str_replace('_', '-', strtolower(substr($key, 5)))] = $value;
196: }
197: }
198:
199: return $headers;
200: }
201:
202: return $_SERVER['HTTP_' . str_replace('-', '_', strtoupper($key))] ?? $default;
203: }
204: /**
205: * @param string $key
206: * @return bool
207: */
208: public static function has_header($key)
209: {
210: return isset($_SERVER['HTTP_' . strtoupper($key)]);
211: }
212:
213: // GET
214:
215: /**
216: * @param string $key
217: * @param mixed $default
218: * @return mixed
219: */
220: public static function get($key = '', $default = null)
221: {
222: if ($key === '') {
223: return $_GET;
224: }
225:
226: return $_GET[$key] ?? $default;
227: }
228: /**
229: * @param string $key
230: * @return bool
231: */
232: public static function has_get($key)
233: {
234: return isset($_GET[$key]);
235: }
236: /**
237: * @param string $key
238: * @param mixed $value
239: * @return void
240: */
241: public static function set_get($key, $value)
242: {
243: $_GET[$key] = $value;
244: }
245:
246: // POST
247:
248: /**
249: * @param string $key
250: * @param mixed $default
251: * @return mixed
252: */
253: public static function post($key, $default = null)
254: {
255: if ($key === '') {
256: return $_POST;
257: }
258:
259: return $_POST[$key] ?? $default;
260: }
261: /**
262: * @param string $key
263: * @return bool
264: */
265: public static function has_post($key)
266: {
267: return isset($_POST[$key]);
268: }
269: /**
270: * @param string $key
271: * @param mixed $value
272: * @return void
273: */
274: public static function set_post($key, $value)
275: {
276: $_POST[$key] = $value;
277: }
278:
279: // COOKIE
280:
281: /**
282: * @param string $key
283: * @param mixed $default
284: * @return mixed
285: */
286: public static function cookie($key, $default = null)
287: {
288: if ($key === '') {
289: return $_COOKIE;
290: }
291:
292: return $_COOKIE[$key] ?? $default;
293: }
294: /**
295: * @param string $key
296: * @return bool
297: */
298: public static function has_cookie($key)
299: {
300: return isset($_COOKIE[$key]);
301: }
302: /**
303: * @param string $key
304: * @param string $value
305: * @return void
306: */
307: public static function set_cookie($key, $value)
308: {
309: $_COOKIE[$key] = $value;
310: }
311:
312: // SESSION
313:
314: /**
315: * @param string $prefix
316: * @param int $ttl
317: * @return void
318: */
319: public static function session_start($prefix = 'sess_', $ttl = 86400)
320: {
321: //session_set_save_handler(new \ngatngay\session\storage\apcu($prefix, $ttl));
322:
323: if (PHP_SESSION_ACTIVE === session_status()) {
324: throw new RuntimeException('Failed to start the session: already started by PHP.');
325: }
326:
327: if (!\session_start()) {
328: throw new RuntimeException('Failed to start the session.');
329: }
330: }
331: /**
332: * @param string $key
333: * @param mixed $default
334: * @return mixed
335: */
336: public static function session($key, $default = null)
337: {
338: if ($key === '') {
339: return $_SESSION;
340: }
341:
342: return $_SESSION[$key] ?? $default;
343: }
344: /**
345: * @param string $key
346: * @return bool
347: */
348: public static function has_session($key)
349: {
350: return isset($_SESSION[$key]);
351: }
352: /**
353: * @param string $key
354: * @param mixed $value
355: * @return void
356: */
357: public static function set_session($key, $value)
358: {
359: $_SESSION[$key] = $value;
360: }
361: /**
362: * @param string $key
363: * @return void
364: */
365: public static function unset_session($key)
366: {
367: unset($_SESSION[$key]);
368: }
369:
370:
371: // SERVER
372:
373: /**
374: * @param string $key
375: * @param mixed $default
376: * @return mixed
377: */
378: public static function server($key, $default = null)
379: {
380: if ($key === '') {
381: return $_SERVER;
382: }
383:
384: return (string) (isset($_SERVER[$key]) ? $_SERVER[$key] : (isset($_SERVER[strtoupper($key)]) ? $_SERVER[strtoupper($key)] : $default));
385: }
386: /**
387: * @param string $key
388: * @return bool
389: */
390: public static function has_server($key)
391: {
392: return isset($_SERVER[$key]) ? true : isset($_SERVER[strtoupper($key)]);
393: }
394:
395: // FILES
396: /**
397: * @param string $key
398: * @return array|null
399: */
400: public static function file($key)
401: {
402: if ($key === '') {
403: return $_FILES;
404: }
405:
406: if (!isset($_FILES[$key])) {
407: return null;
408: }
409:
410: if (!is_array($_FILES[$key]['name'])) {
411: return [$_FILES[$key]];
412: }
413:
414: $tmp = [];
415: foreach ($_FILES[$key] as $k => $v) {
416: $fCount = count($_FILES[$key]['name']);
417: $fKeys = array_keys($_FILES[$key]);
418:
419: for ($i = 0; $i < $fCount; $i++) {
420: foreach ($fKeys as $fKey) {
421: $tmp[$key][$i][$fKey] = $_FILES[$key][$fKey][$i];
422: }
423: }
424: }
425: return $tmp;
426: }
427:
428: // REQUEST
429:
430: /**
431: * @param string $key
432: * @param mixed $default
433: * @return mixed
434: */
435: public static function request($key, $default = null)
436: {
437: if ($key === '') {
438: return $_REQUEST;
439: }
440:
441: return $_REQUEST[$key] ?? $default;
442: }
443: /**
444: * @param string $key
445: * @return bool
446: */
447: public static function has_request($key)
448: {
449: return isset($_REQUEST[$key]);
450: }
451:
452: // PAYLOAD
453:
454: /**
455: * @return void
456: */
457: public static function init_payload()
458: {
459: self::$payload = json::decode(file_get_contents('php://input') ?: '[]', true);
460: }
461: /**
462: * @param string $key
463: * @return bool
464: */
465: public static function has_payload($key)
466: {
467: return isset(self::$payload[$key]);
468: }
469: /**
470: * @param string $key
471: * @param mixed $default
472: * @return mixed
473: */
474: public static function payload($key = '', $default = null)
475: {
476: if ($key === '') {
477: return self::$payload;
478: }
479:
480: return self::$payload[$key] ?? $default;
481: }
482: }
483: