1: <?php
2:
3: namespace ngatngay\http;
4:
5: use RuntimeException;
6:
7: class request
8: {
9: public static array $file;
10: public static array $header;
11: public static array $server;
12: public static array $payload;
13:
14: public static function init(): void
15: {
16: /*
17: {
18: $request_uri = @parse_url($_SERVER['REQUEST_URI'] ?? '');
19: $add_get = [];
20:
21: if (isset($request_uri['query'])) {
22: parse_str($request_uri['query'], $add_get);
23: }
24:
25: $_GET = array_merge($_GET, $add_get);
26: $_REQUEST = array_merge($_REQUEST, $add_get);
27: }
28: */
29: //self::init_payload();
30: //self::init_file();
31: }
32:
33: // common
34:
35: public static function is_cli(): bool
36: {
37: return \php_sapi_name() === 'cli';
38: }
39: public static function is_cli_server(): bool
40: {
41: return \php_sapi_name() === 'cli-server';
42: }
43:
44: public static function script_name(): string
45: {
46: return self::server('script_name');
47: }
48:
49: public static function method(): string
50: {
51: return strtolower((string) self::server('REQUEST_METHOD', 'get'));
52: }
53:
54: public static function is_method(string $value): bool
55: {
56: return strtolower($value) === self::method();
57: }
58:
59: public static function ip(): string
60: {
61: $keys = [
62: 'HTTP_CLIENT_IP',
63: 'HTTP_X_FORWARDED_FOR',
64: 'HTTP_X_FORWARDED',
65: 'HTTP_FORWARDED_FOR',
66: 'HTTP_FORWARDED',
67: 'REMOTE_ADDR'
68: ];
69: foreach ($keys as $key) {
70: if (isset($_SERVER[$key])) {
71: return $_SERVER[$key];
72: }
73: }
74:
75: return '127.0.0.1';
76: }
77:
78: public static function user_agent(): string
79: {
80: return (string) self::header('user_agent');
81: }
82:
83: public static function referer(): string
84: {
85: return (string) self::header('referer');
86: }
87:
88: public static function host(): string
89: {
90: return (string) self::header('host');
91: }
92: public static function base_url(): string
93: {
94: return self::server('request_scheme', 'http')
95: . '://'
96: . self::server('server_name', 'localhost');
97: }
98:
99: public static function uri(string $mode = 'full'): string
100: {
101: $uri = self::server('request_uri');
102:
103: switch ($mode) {
104: case 'request':
105: return $uri;
106: case 'no_query':
107: return strtok($uri, '?');
108: default:
109: return self::base_url() . $uri;
110: }
111: }
112:
113: public static function query_string(): string
114: {
115: return (string) self::server('query_string');
116: }
117:
118: // HEADER
119: public static function header(string $key, mixed $default = null): mixed
120: {
121: if ($key === '') {
122: $headers = [];
123: foreach ($_SERVER as $key => $value) {
124: if (str_starts_with($key, 'HTTP_')) {
125: $headers[strtolower(substr($key, 5))] = $value;
126: }
127: }
128:
129: return $headers;
130: }
131:
132: return $_SERVER['HTTP_' . strtoupper($key)] ?? $default;
133: }
134: public static function has_header(string $key): bool
135: {
136: return isset($_SERVER['HTTP_' . strtoupper($key)]);
137: }
138:
139: // GET
140:
141: public static function get(string $key, mixed $default = null): mixed
142: {
143: if ($key === '') {
144: return $_GET;
145: }
146:
147: return $_GET[$key] ?? $default;
148: }
149: public static function has_get(string $key): bool
150: {
151: return isset($_GET[$key]);
152: }
153: public static function set_get(string $key, mixed $value): void
154: {
155: $_GET[$key] = $value;
156: }
157:
158: // POST
159:
160: public static function post(string $key, mixed $default = null): mixed
161: {
162: if ($key === '') {
163: return $_POST;
164: }
165:
166: return $_POST[$key] ?? $default;
167: }
168: public static function has_post(string $key): bool
169: {
170: return isset($_POST[$key]);
171: }
172: public static function set_post(string $key, mixed $value): void
173: {
174: $_POST[$key] = $value;
175: }
176:
177: // COOKIE
178:
179: public static function cookie(string $key, mixed $default = null): mixed
180: {
181: if ($key === '') {
182: return $_COOKIE;
183: }
184:
185: return $_COOKIE[$key] ?? $default;
186: }
187: public static function has_cookie(string $key): bool
188: {
189: return isset($_COOKIE[$key]);
190: }
191: public static function set_cookie(string $key, string $value): void
192: {
193: $_COOKIE[$key] = $value;
194: }
195:
196: // SESSION
197:
198: public static function session_start(string $prefix = 'sess_', int $ttl = 86400): void
199: {
200: //session_set_save_handler(new \ngatngay\session\storage\apcu($prefix, $ttl));
201:
202: if (PHP_SESSION_ACTIVE === session_status()) {
203: throw new RuntimeException('Failed to start the session: already started by PHP.');
204: }
205:
206: if (!\session_start()) {
207: throw new RuntimeException('Failed to start the session.');
208: }
209: }
210: public static function session(string $key, mixed $default = null): mixed
211: {
212: if ($key === '') {
213: return $_SESSION;
214: }
215:
216: return $_SESSION[$key] ?? $default;
217: }
218: public static function has_session(string $key): bool
219: {
220: return isset($_SESSION[$key]);
221: }
222: public static function set_session(string $key, mixed $value): void
223: {
224: $_SESSION[$key] = $value;
225: }
226: public static function unset_session(string $key): void
227: {
228: unset($_SESSION[$key]);
229: }
230:
231:
232: // SERVER
233:
234: public static function server(string $key, mixed $default = null): mixed
235: {
236: if ($key === '') {
237: return $_SERVER;
238: }
239:
240: return (string) (isset($_SERVER[$key]) ? $_SERVER[$key] : (isset($_SERVER[strtoupper($key)]) ? $_SERVER[strtoupper($key)] : $default));
241: }
242: public static function has_server(string $key): bool
243: {
244: return isset($_SERVER[$key]) ? true : isset($_SERVER[strtoupper($key)]);
245: }
246:
247: // FILES
248: public static function file(string $key): ?array
249: {
250: if ($key === '') {
251: return $_FILES;
252: }
253:
254: if (!isset($_FILES[$key])) {
255: return null;
256: }
257:
258: if (!is_array($_FILES[$key]['name'])) {
259: return [$_FILES[$key]];
260: }
261:
262: $tmp = [];
263: foreach ($_FILES[$key] as $k => $v) {
264: $fCount = count($_FILES[$key]['name']);
265: $fKeys = array_keys($_FILES[$key]);
266:
267: for ($i = 0; $i < $fCount; $i++) {
268: foreach ($fKeys as $fKey) {
269: $tmp[$key][$i][$fKey] = $_FILES[$key][$fKey][$i];
270: }
271: }
272: }
273: return $tmp;
274: }
275:
276: // REQUEST
277:
278: public static function request(string $key, mixed $default = null): mixed
279: {
280: if ($key === '') {
281: return $_REQUEST;
282: }
283:
284: return $_REQUEST[$key] ?? $default;
285: }
286: public static function has_request(string $key): bool
287: {
288: return isset($_REQUEST[$key]);
289: }
290:
291: // PAYLOAD
292:
293: private static function init_payload(): void
294: {
295: $data = @json_decode(file_get_contents('php://input'), true);
296: if (json_last_error() === \JSON_ERROR_NONE) {
297: self::$payload = $data;
298: }
299: }
300: public static function has_payload(string $key): bool
301: {
302: return isset(self::$payload[$key]);
303: }
304: public static function payload(string $key, mixed $default = null): mixed
305: {
306: if ($key === '') {
307: return self::$payload;
308: }
309:
310: return self::$payload[$key] ?? $default;
311: }
312: }
313: