| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- class HTTP {
- public static $curl_last_error = "";
- public static function isAJAX() {
- return (!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest");
- }
- public static function post($url, $fields_or_data, $curl_options = []) {
- $curl_handle = curl_init($url);
- curl_setopt($curl_handle, CURLOPT_POST, 1);
- curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $fields_or_data);
- curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
- if(!empty($curl_options)) {
- foreach($curl_options as $option => $value) {
- curl_setopt($curl_handle, $option, $value);
- }
- }
- $result = curl_exec($curl_handle);
- if($result === false) {
- http::$curl_last_error = curl_error($curl_handle);
- throw new Exception("cURL Error: " . curl_error($curl_handle));
- }
- return $result;
- }
- public static function getDateFromFields($prefix = "") {
- $date = false;
- if(empty($prefix)) {
- $year_field = "year";
- $month_field = "month";
- $day_field = "day";
- }
- else {
- $year_field = $prefix . "_year";
- $month_field = $prefix . "_month";
- $day_field = $prefix . "_day";
- }
- $year = http::value($year_field, "int");
- $month = http::value($month_field, "int");
- $day = http::value($day_field, "int");
- if($year && $month && $day) {
- $month = str_pad($month, 2, "0", STR_PAD_LEFT);
- $day = str_pad($day, 2, "0", STR_PAD_LEFT);
- $unix = strtotime("$year-$month-$day");
- $date = date("Y-m-d", $unix);
- }
- return $date;
- }
- public static function value($key, $validation = "none", $request_type = "request") {
- $request_type = strtolower($request_type);
- switch($request_type) {
- case "request":
- $array = $_REQUEST;
- break;
- case "get":
- $array = $_GET;
- break;
- case "post":
- $array = $_POST;
- break;
- default:
- trigger_error("http::value called with \"$request_type\" request_type", E_USER_ERROR);
- break;
- }
- if(!isset($array[$key])) return false;
- $value = $array[$key];
- switch($validation) {
- case "int": {
- if(!is_array($value) && strlen($value)===0) return false;
- return is_array($value) ? array_map("intval", $value) : intval($value);
- }
- case "double":
- case "float": {
- if(strlen($value)===0) return false;
- return floatval($value);
- }
- // Return database escaped string
- case "db": {
- return is_array($value) ? array_map("db::escape", $value) : db::escape($value);
- }
- case "html": {
- return is_array($value) ? array_map("htmlspecialchars", $value) : htmlspecialchars($value);
- }
- case "trim": {
- return is_array($value) ? array_map("trim", $value) : trim($value);
- }
- case "":
- case "none": {
- return $value;
- }
- case "email": {
- return filter_var($value, FILTER_VALIDATE_EMAIL);
- }
- default: {
- throw new Exception("Unknown validation type: $validation");
- }
- }
- }
- public static function redirect($url) {
- header("HTTP/1.1 303 See Other");
- header("Location: " . $url);
- exit;
- }
- public static function isSSL() {
- if(isset($_SERVER["SERVER_PORT"])) {
- return $_SERVER["SERVER_PORT"] == 443;
- }
- return false;
- }
- public static function getProtocol() {
- return (HTTP::isSSL()) ? "https" : "http";
- }
- public static function getUploadErrorString($error, $name = "file") {
- switch($error) {
- case UPLOAD_ERR_OK: return "The $name uploaded successfully.";
- case UPLOAD_ERR_INI_SIZE: return "The $name uploaded was too large.";
- case UPLOAD_ERR_FORM_SIZE: return "The $name uploaded was too large.";
- case UPLOAD_ERR_PARTIAL: return "The $name uploaded was only partially uploaded.";
- case UPLOAD_ERR_NO_FILE: return "No $name was uploaded.";
- case UPLOAD_ERR_NO_TMP_DIR: return "The $name cannot be uploaded as the temporary folder is missing.";
- case UPLOAD_ERR_CANT_WRITE: return "The $name cannot be written to disk.";
- case UPLOAD_ERR_EXTENSION: return "The $name uploaded was blocked by an extension.";
- }
- return "Unknown error.";
- }
- public static function setStatusCode($code) {
- switch ($code) {
- case 403:
- $string = "403 Forbidden";
- break;
- case 404:
- $string = "404 Not Found";
- break;
- default:
- $string = "200 OK";
- break;
- }
- header("HTTP/1.1 $string");
- }
- public static function validateUploadedFile($file) {
- $file = (object) $file;
- if($file->error > 0) {
- switch($file->error) {
- case UPLOAD_ERR_NO_FILE: throw new Exception("No file was submitted."); break;
- case UPLOAD_ERR_INI_SIZE: throw new Exception("The uploaded file was too large."); break;
- }
- throw new Exception("The uploaded file could not be stored. Please try again later.");
- }
- return true;
- }
- public static function sendFile($path) {
- $modified_time = filemtime($path);
- $mime_type = Picture::getFileMIMEType($path);
- $max_age = 7 * 86400;
- header("Cache-Control: public, max-age=$max_age");
- header("Expires: " . date("r", time() + $max_age));
- header("Pragma: cache");
- header("Last-Modified: " . date("r", $modified_time));
- header("X-Sendfile: " . $path);
- header("Content-Type: " . $mime_type);
- }
- }
- ?>
|