$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 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(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); } } ?>