$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 setDownloadHeaders($filename) { header("Content-Disposition: attachment; filename=\"$filename\""); header("Cache-Control: private, max-age=0, must-revalidate"); header("Pragma: public"); } 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 getDateTimeFromFields($prefix = "") { $date = http::getDateFromFields($prefix); if($date === false) return false; if(empty($prefix)) { $hour_field = "hour"; $minute_field = "minute"; $second_field = "second"; } else { $hour_field = $prefix . "_hour"; $minute_field = $prefix . "_minute"; $second_field = $prefix . "_second"; } $hour = http::value($hour_field, "int"); $minute = http::value($minute_field, "int"); $second = http::value($second_field, "int"); if(strlen($hour) == 0) return false; if(strlen($minute) == 0) return false; if(strlen($second) == 0) return false; list($hour, $minute, $second) = array_map(function($part) { return str_pad($part, 2, "0", STR_PAD_LEFT); }, [$hour, $minute, $second]); return "$date $hour:$minute:$second"; } 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); } } ?>