From 84e2b2be6f7537e0a4a9f0c323555404d2682a8a Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Thu, 29 Aug 2019 18:45:48 +0200 Subject: Add HTTP class This class was written in 2015 and originally had it's own repository. I decided to include it to the snippets repository because this piece of code does not need it's own repository. --- PHP/HTTP-Class/include/HTTP.php | 221 ++++++++++++++++++++++++++++++++++++++++ PHP/HTTP-Class/index.php | 71 +++++++++++++ 2 files changed, 292 insertions(+) create mode 100755 PHP/HTTP-Class/include/HTTP.php create mode 100755 PHP/HTTP-Class/index.php diff --git a/PHP/HTTP-Class/include/HTTP.php b/PHP/HTTP-Class/include/HTTP.php new file mode 100755 index 0000000..5d609ff --- /dev/null +++ b/PHP/HTTP-Class/include/HTTP.php @@ -0,0 +1,221 @@ + 'OK', + 301 => 'Moved Permanently', + 302 => 'Found', + 303 => 'See Other', + 307 => 'Temporary Redirect', + 400 => 'Bad Request', + 401 => 'Authorization Required', + 403 => 'Forbidden', + 404 => 'Not Found', + 500 => 'Internal Server Error', + 503 => 'Service Temporarily Unavailable', + ][$code]; + } + + #=============================================================================== + # Trim all strings in argument + #=============================================================================== + private static function trim($mixed) { + if(is_array($mixed)) { + return array_map('self::trim', $mixed); + } + + return trim($mixed); + } + + #=============================================================================== + # Check if all elements of $arguments are set as key of $data + #=============================================================================== + private static function issetData($data, $arguments) { + foreach($arguments as $key) { + if(is_array($key)) { + if(!isset($data[key($key)]) OR $data[key($key)] !== $key[key($key)]) { + return FALSE; + } + } + + else if(!isset($data[$key]) OR !is_string($data[$key])) { + return FALSE; + } + } + + return TRUE; + } + + #=============================================================================== + # Return null or the value (if set) from one of the three requests attributes + #=============================================================================== + public static function returnKey($data, Array $paths) { + $current = &$data; + + foreach($paths as $path) { + if(!isset($current[$path]) OR (!is_string($current[$path]) AND !is_array($current[$path]))) { + return NULL; + } + $current = &$current[$path]; + } + + return $current; + } + + #=============================================================================== + # Return GET value + #=============================================================================== + public static function GET() { + return self::returnKey(self::$GET, func_get_args()); + } + + #=============================================================================== + # Return POST value + #=============================================================================== + public static function POST() { + return self::returnKey(self::$POST, func_get_args()); + } + + #=============================================================================== + # Return FILE value + #=============================================================================== + public static function FILE() { + return self::returnKey(self::$FILE, func_get_args()); + } + + #=============================================================================== + # Check if all elements of func_get_args() are set key of self::$POST + #=============================================================================== + public static function issetPOST() { + return self::issetData(self::$POST, func_get_args()); + } + + #=============================================================================== + # Check if all elements of func_get_args() are set key of self::$GET + #=============================================================================== + public static function issetGET() { + return self::issetData(self::$GET, func_get_args()); + } + + #=============================================================================== + # Check if all elements of func_get_args() are set key of self::$FILE + #=============================================================================== + public static function issetFILE() { + return self::issetData(self::$FILE, func_get_args()); + } + + #=============================================================================== + # Return HTTP request method or check if request method equals with $method + #=============================================================================== + public static function requestMethod($method = NULL) { + if(!empty($method)) { + return ($_SERVER['REQUEST_METHOD'] === $method); + } + + return $_SERVER['REQUEST_METHOD']; + } + + #=============================================================================== + # Return REQUEST_URI + #=============================================================================== + public static function requestURI() { + return $_SERVER['REQUEST_URI'] ?? FALSE; + } + + #=============================================================================== + # Return HTTP_USER_AGENT + #=============================================================================== + public static function useragent() { + return trim($_SERVER['HTTP_USER_AGENT'] ?? ''); + } + + #=============================================================================== + # Return HTTP_REFERER + #=============================================================================== + public static function referer() { + return trim($_SERVER['HTTP_REFERER'] ?? ''); + } + + #=============================================================================== + # Return response status + #=============================================================================== + public static function responseStatus($code = NULL) { + if(!empty($code)) { + self::sendHeader(sprintf('HTTP/1.1 %d %s', $code, self::getStatuscode($code))); + } + return http_response_code(); + } + + #=============================================================================== + # Send a HTTP header line to the client + #=============================================================================== + public static function responseHeader($field, $value) { + self::sendHeader("{$field}: {$value}"); + } + + #=============================================================================== + # Send a HTTP redirect to the client + #=============================================================================== + public static function redirect($location, $code = 303, $exit = TRUE) { + self::sendHeader(sprintf('HTTP/1.1 %d %s', $code, self::getStatuscode($code))); + self::sendHeader("Location: {$location}"); + $exit AND exit(); + } + + #=============================================================================== + # Send a new HTTP header line to the client if headers are not already sent + #=============================================================================== + private static function sendHeader($header) { + if(!headers_sent()) { + header($header); + } + } +} +?> \ No newline at end of file diff --git a/PHP/HTTP-Class/index.php b/PHP/HTTP-Class/index.php new file mode 100755 index 0000000..cc52561 --- /dev/null +++ b/PHP/HTTP-Class/index.php @@ -0,0 +1,71 @@ + 'value_three'])) { + // do something +} + +#============================================================================== +# Check the HTTP request method +#============================================================================== +if(HTTP::requestMethod('GET') OR HTTP::requestMethod('POST') OR HTTP::requestMethod('HEAD')) { + // do something +} + +#============================================================================== +# Get the HTTP request method +#============================================================================== +$requestMethod = HTTP::requestMethod(); + +#============================================================================== +# Get the HTTP status code of the current request +#============================================================================== +$statusCode = HTTP::responseStatus(); + +#============================================================================== +# Send a HTTP status code to the client +#============================================================================== +HTTP::responseStatus(200); + +#============================================================================== +# Send a custom HTTP response header to the client +#============================================================================== +HTTP::responseHeader(HTTP::HEADER_CONTENT_TYPE, HTTP::CONTENT_TYPE_TEXT); + +#============================================================================== +# Send a HTTP redirect to the client and stop script execution +#============================================================================== +# HTTP::redirect('https://example.org/'); +# HTTP::redirect('https://example.org/', 303); +?> \ No newline at end of file -- cgit v1.2.3