| 1: | <?php |
| 2: | |
| 3: | namespace ngatngay\http; |
| 4: | |
| 5: | use ngatngay\json; |
| 6: | use RuntimeException; |
| 7: | |
| 8: | class request |
| 9: | { |
| 10: | |
| 11: | |
| 12: | |
| 13: | public static $file; |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | public static $header; |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | public static $server; |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | public static $payload; |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | public static function init() |
| 34: | { |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | } |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | public static function is_cli() |
| 58: | { |
| 59: | return \php_sapi_name() === 'cli'; |
| 60: | } |
| 61: | |
| 62: | |
| 63: | |
| 64: | public static function is_cli_server() |
| 65: | { |
| 66: | return \php_sapi_name() === 'cli-server'; |
| 67: | } |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | public static function script_name() |
| 73: | { |
| 74: | return self::server('script_name'); |
| 75: | } |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | public static function method() |
| 81: | { |
| 82: | return strtolower((string) self::server('REQUEST_METHOD', 'get')); |
| 83: | } |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | public static function is_method($value) |
| 90: | { |
| 91: | return strtolower($value) === self::method(); |
| 92: | } |
| 93: | |
| 94: | |
| 95: | |
| 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: | |
| 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: | |
| 126: | |
| 127: | public static function user_agent() |
| 128: | { |
| 129: | return (string) self::header('user_agent'); |
| 130: | } |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | public static function referer() |
| 136: | { |
| 137: | return (string) self::header('referer'); |
| 138: | } |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | public static function host() |
| 144: | { |
| 145: | return (string) self::header('host'); |
| 146: | } |
| 147: | |
| 148: | |
| 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: | |
| 159: | |
| 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: | |
| 177: | |
| 178: | public static function query_string() |
| 179: | { |
| 180: | return (string) self::server('query_string'); |
| 181: | } |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 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: | |
| 206: | |
| 207: | |
| 208: | public static function has_header($key) |
| 209: | { |
| 210: | return isset($_SERVER['HTTP_' . strtoupper($key)]); |
| 211: | } |
| 212: | |
| 213: | |
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 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: | |
| 230: | |
| 231: | |
| 232: | public static function has_get($key) |
| 233: | { |
| 234: | return isset($_GET[$key]); |
| 235: | } |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: | |
| 241: | public static function set_get($key, $value) |
| 242: | { |
| 243: | $_GET[$key] = $value; |
| 244: | } |
| 245: | |
| 246: | |
| 247: | |
| 248: | |
| 249: | |
| 250: | |
| 251: | |
| 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: | |
| 263: | |
| 264: | |
| 265: | public static function has_post($key) |
| 266: | { |
| 267: | return isset($_POST[$key]); |
| 268: | } |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | public static function set_post($key, $value) |
| 275: | { |
| 276: | $_POST[$key] = $value; |
| 277: | } |
| 278: | |
| 279: | |
| 280: | |
| 281: | |
| 282: | |
| 283: | |
| 284: | |
| 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: | |
| 296: | |
| 297: | |
| 298: | public static function has_cookie($key) |
| 299: | { |
| 300: | return isset($_COOKIE[$key]); |
| 301: | } |
| 302: | |
| 303: | |
| 304: | |
| 305: | |
| 306: | |
| 307: | public static function set_cookie($key, $value) |
| 308: | { |
| 309: | $_COOKIE[$key] = $value; |
| 310: | } |
| 311: | |
| 312: | |
| 313: | |
| 314: | |
| 315: | |
| 316: | |
| 317: | |
| 318: | |
| 319: | public static function session_start($prefix = 'sess_', $ttl = 86400) |
| 320: | { |
| 321: | |
| 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: | |
| 333: | |
| 334: | |
| 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: | |
| 346: | |
| 347: | |
| 348: | public static function has_session($key) |
| 349: | { |
| 350: | return isset($_SESSION[$key]); |
| 351: | } |
| 352: | |
| 353: | |
| 354: | |
| 355: | |
| 356: | |
| 357: | public static function set_session($key, $value) |
| 358: | { |
| 359: | $_SESSION[$key] = $value; |
| 360: | } |
| 361: | |
| 362: | |
| 363: | |
| 364: | |
| 365: | public static function unset_session($key) |
| 366: | { |
| 367: | unset($_SESSION[$key]); |
| 368: | } |
| 369: | |
| 370: | |
| 371: | |
| 372: | |
| 373: | |
| 374: | |
| 375: | |
| 376: | |
| 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: | |
| 388: | |
| 389: | |
| 390: | public static function has_server($key) |
| 391: | { |
| 392: | return isset($_SERVER[$key]) ? true : isset($_SERVER[strtoupper($key)]); |
| 393: | } |
| 394: | |
| 395: | |
| 396: | |
| 397: | |
| 398: | |
| 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: | |
| 429: | |
| 430: | |
| 431: | |
| 432: | |
| 433: | |
| 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: | |
| 445: | |
| 446: | |
| 447: | public static function has_request($key) |
| 448: | { |
| 449: | return isset($_REQUEST[$key]); |
| 450: | } |
| 451: | |
| 452: | |
| 453: | |
| 454: | |
| 455: | |
| 456: | |
| 457: | public static function init_payload() |
| 458: | { |
| 459: | self::$payload = json::decode(file_get_contents('php://input') ?: '[]', true); |
| 460: | } |
| 461: | |
| 462: | |
| 463: | |
| 464: | |
| 465: | public static function has_payload($key) |
| 466: | { |
| 467: | return isset(self::$payload[$key]); |
| 468: | } |
| 469: | |
| 470: | |
| 471: | |
| 472: | |
| 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: | |