-
-
Save Awilum/0c6085c4a617e1cd66aec47e63bf594a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @package Flextype Components | |
| * | |
| * @author Sergey Romanenko <awilum@yandex.ru> | |
| * @link http://components.flextype.org | |
| * | |
| * For the full copyright and license information, please view the LICENSE | |
| * file that was distributed with this source code. | |
| */ | |
| namespace Flextype\Component\Http; | |
| use Flextype\Component\Arr\Arr; | |
| class Http | |
| { | |
| /** | |
| * HTTP status codes and messages | |
| * | |
| * @var array | |
| */ | |
| public static $http_status_messages = [ | |
| 100 => 'Continue', | |
| 101 => 'Switching Protocols', | |
| 102 => 'Processing', // RFC2518 | |
| 200 => 'OK', | |
| 201 => 'Created', | |
| 202 => 'Accepted', | |
| 203 => 'Non-Authoritative Information', | |
| 204 => 'No Content', | |
| 205 => 'Reset Content', | |
| 206 => 'Partial Content', | |
| 207 => 'Multi-Status', // RFC4918 | |
| 208 => 'Already Reported', // RFC5842 | |
| 226 => 'IM Used', // RFC3229 | |
| 300 => 'Multiple Choices', | |
| 301 => 'Moved Permanently', | |
| 302 => 'Found', | |
| 303 => 'See Other', | |
| 304 => 'Not Modified', | |
| 305 => 'Use Proxy', | |
| 306 => 'Reserved', | |
| 307 => 'Temporary Redirect', | |
| 308 => 'Permanent Redirect', // RFC-reschke-http-status-308-07 | |
| 400 => 'Bad Request', | |
| 401 => 'Unauthorized', | |
| 402 => 'Payment Required', | |
| 403 => 'Forbidden', | |
| 404 => 'Not Found', | |
| 405 => 'Method Not Allowed', | |
| 406 => 'Not Acceptable', | |
| 407 => 'Proxy Authentication Required', | |
| 408 => 'Request Timeout', | |
| 409 => 'Conflict', | |
| 410 => 'Gone', | |
| 411 => 'Length Required', | |
| 412 => 'Precondition Failed', | |
| 413 => 'Request Entity Too Large', | |
| 414 => 'Request-URI Too Long', | |
| 415 => 'Unsupported Media Type', | |
| 416 => 'Requested Range Not Satisfiable', | |
| 417 => 'Expectation Failed', | |
| 418 => 'I\'m a teapot', // RFC2324 | |
| 422 => 'Unprocessable Entity', // RFC4918 | |
| 423 => 'Locked', // RFC4918 | |
| 424 => 'Failed Dependency', // RFC4918 | |
| 425 => 'Reserved for WebDAV advanced collections expired proposal', // RFC2817 | |
| 426 => 'Upgrade Required', // RFC2817 | |
| 428 => 'Precondition Required', // RFC6585 | |
| 429 => 'Too Many Requests', // RFC6585 | |
| 431 => 'Request Header Fields Too Large', // RFC6585 | |
| 500 => 'Internal Server Error', | |
| 501 => 'Not Implemented', | |
| 502 => 'Bad Gateway', | |
| 503 => 'Service Unavailable', | |
| 504 => 'Gateway Timeout', | |
| 505 => 'HTTP Version Not Supported', | |
| 506 => 'Variant Also Negotiates (Experimental)', // RFC2295 | |
| 507 => 'Insufficient Storage', // RFC4918 | |
| 508 => 'Loop Detected', // RFC5842 | |
| 510 => 'Not Extended', // RFC2774 | |
| 511 => 'Network Authentication Required', // RFC6585 | |
| ]; | |
| public static function request() : array | |
| { | |
| return [ | |
| 'host' => ((isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') ? 'https://' : 'http://') . rtrim($_SERVER['HTTP_HOST'], '\\/'), | |
| 'url' => str_replace('@', '%40', Http::getVar('REQUEST_URI', '/')), | |
| 'base' => str_replace(array('\\',' '), array('/','%20'), dirname(Http::getVar('SCRIPT_NAME'))), | |
| 'site_url' => ((isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') ? 'https://' : 'http://') . rtrim($_SERVER['HTTP_HOST'], '\\/') . str_replace(array('\\',' '), array('/','%20'), dirname(Http::getVar('SCRIPT_NAME'))), | |
| 'current_url' => ((isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') ? 'https://' : 'http://') . rtrim($_SERVER['HTTP_HOST'], '\\/') . str_replace('@', '%40', Http::getVar('REQUEST_URI', '/')), | |
| 'uri_string' => Http::getUriString(), | |
| 'uri_segments' => Http::getUriSegments(), | |
| 'method' => Http::getMethod(), | |
| 'referrer' => Http::getVar('HTTP_REFERER'), | |
| 'ip' => Http::getVar('REMOTE_ADDR'), | |
| 'ajax' => Http::getVar('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest', | |
| 'scheme' => Http::getVar('SERVER_PROTOCOL', 'HTTP/1.1'), | |
| 'user_agent' => Http::getVar('HTTP_USER_AGENT'), | |
| 'type' => Http::getVar('CONTENT_TYPE'), | |
| 'length' => Http::getVar('CONTENT_LENGTH', 0), | |
| 'query' => $_GET, | |
| 'data' => $_POST, | |
| 'cookies' => $_COOKIE, | |
| 'files' => $_FILES, | |
| 'secure' => Http::getVar('HTTPS', 'off') != 'off', | |
| 'accept' => Http::getVar('HTTP_ACCEPT'), | |
| 'proxy_ip' => Http::getProxyIpAddress() | |
| ]; | |
| } | |
| /** | |
| * Gets the body of the request. | |
| * | |
| * @return string Raw HTTP request body | |
| */ | |
| public static function getBody() { | |
| static $body; | |
| if (!is_null($body)) { | |
| return $body; | |
| } | |
| $method = Http::getMethod(); | |
| if ($method == 'POST' || $method == 'PUT' || $method == 'PATCH') { | |
| $body = file_get_contents('php://input'); | |
| } | |
| return $body; | |
| } | |
| /** | |
| * Gets the request method. | |
| * | |
| * @return string | |
| */ | |
| public static function getMethod() { | |
| $method = Http::getVar('REQUEST_METHOD', 'GET'); | |
| if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { | |
| $method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']; | |
| } | |
| elseif (isset($_REQUEST['_method'])) { | |
| $method = $_REQUEST['_method']; | |
| } | |
| return strtoupper($method); | |
| } | |
| /** | |
| * Gets a variable from $_SERVER using $default if not provided. | |
| * | |
| * @param string $var Variable name | |
| * @param string $default Default value to substitute | |
| * @return string Server variable value | |
| */ | |
| public static function getVar($var, $default = '') { | |
| return isset($_SERVER[$var]) ? $_SERVER[$var] : $default; | |
| } | |
| /** | |
| * Gets the real remote IP address. | |
| * | |
| * @return string IP address | |
| */ | |
| public static function getProxyIpAddress() { | |
| static $forwarded = array( | |
| 'HTTP_CLIENT_IP', | |
| 'HTTP_X_FORWARDED_FOR', | |
| 'HTTP_X_FORWARDED', | |
| 'HTTP_X_CLUSTER_CLIENT_IP', | |
| 'HTTP_FORWARDED_FOR', | |
| 'HTTP_FORWARDED' | |
| ); | |
| $flags = \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE; | |
| foreach ($forwarded as $key) { | |
| if (array_key_exists($key, $_SERVER)) { | |
| sscanf($_SERVER[$key], '%[^,]', $ip); | |
| if (filter_var($ip, \FILTER_VALIDATE_IP, $flags) !== false) { | |
| return $ip; | |
| } | |
| } | |
| } | |
| return ''; | |
| } | |
| /** | |
| * Set response header status | |
| * | |
| * Http::setResponseStatus(404); | |
| * | |
| * @param integer $status Status code | |
| * @return void | |
| */ | |
| public static function setResponseStatus(int $status) : void | |
| { | |
| if (array_key_exists($status, Http::$http_status_messages)) { | |
| header('HTTP/1.1 ' . $status . ' ' . Http::$http_status_messages[$status]); | |
| } | |
| } | |
| /** | |
| * Redirects the browser to a page specified by the $url argument. | |
| * | |
| * Http::redirect('test'); | |
| * | |
| * @param string $url The URL | |
| * @param integer $status Status | |
| * @param integer $delay Delay | |
| */ | |
| public static function redirect(string $url, int $status = 302, int $delay = null) | |
| { | |
| // Status codes | |
| $messages = []; | |
| $messages[301] = '301 Moved Permanently'; | |
| $messages[302] = '302 Found'; | |
| // Is Headers sent ? | |
| if (headers_sent()) { | |
| echo "<script>document.location.href='" . $url . "';</script>\n"; | |
| } else { | |
| // Redirect headers | |
| Http::setRequestHeaders('HTTP/1.1 ' . $status . ' ' . Arr::get($messages, $status, 302)); | |
| // Delay execution | |
| if ($delay !== null) { | |
| sleep((int) $delay); | |
| } | |
| // Redirect | |
| Http::setRequestHeaders("Location: $url"); | |
| // Shutdown request | |
| Http::requestShutdown(); | |
| } | |
| } | |
| /** | |
| * Set one or multiple headers. | |
| * | |
| * Http::setRequestHeaders('Location: http://site.com/'); | |
| * | |
| * @param mixed $headers String or array with headers to send. | |
| */ | |
| public static function setRequestHeaders($headers) | |
| { | |
| // Loop elements | |
| foreach ((array) $headers as $header) { | |
| // Set header | |
| header((string) $header); | |
| } | |
| } | |
| /** | |
| * Get Uri String | |
| * | |
| * $uri_string = Http::getUriString(); | |
| * | |
| * @access public | |
| * @return string | |
| */ | |
| public static function getUriString() : string | |
| { | |
| // Get request url and script url | |
| $url = ''; | |
| $request_url = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : ''; | |
| $script_url = (isset($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : ''; | |
| // Get our url path and trim the / of the left and the right | |
| if ($request_url != $script_url) { | |
| $url = trim(preg_replace('/'. str_replace('/', '\/', str_replace('index.php', '', $script_url)) .'/', '', $request_url, 1), '/'); | |
| } | |
| $url = preg_replace('/\?.*/', '', $url); // Strip query string | |
| return $url; | |
| } | |
| /** | |
| * Get Uri Segments | |
| * | |
| * $uri_segments = Http::getUriSegments(); | |
| * | |
| * @access public | |
| * @return array | |
| */ | |
| public static function getUriSegments() : array | |
| { | |
| return explode('/', Http::getUriString()); | |
| } | |
| /** | |
| * Get Uri Segment | |
| * | |
| * $uri_segment = Http::getUriSegment(1); | |
| * | |
| * @access public | |
| * @param int $segment segment | |
| * @return string | |
| */ | |
| public static function getUriSegment(int $segment) | |
| { | |
| $segments = Http::getUriSegments(); | |
| return isset($segments[$segment]) ? $segments[$segment] : null; | |
| } | |
| /** | |
| * Terminate request | |
| * | |
| * Http::requestShutdown(); | |
| * | |
| * @access public | |
| */ | |
| public static function requestShutdown() | |
| { | |
| exit(0); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment