$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"); } /** * @param string $prefix * @return string * @throws Exception */ public static function getDateFromFields(string $prefix = ""): string { $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; } /** * @param string $prefix * @return bool|string * @throws Exception */ public static function getDateTimeFromFields(string $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"; } /** * @param string $key * @param string $validation * @param string $request_type * @return mixed * @throws Exception */ public static function value(string $key, string $validation = "none", string $request_type = "request") { $request_type = strtolower($request_type); $array = []; 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 sprintf(__("The %s uploaded successfully."), $name); case UPLOAD_ERR_FORM_SIZE: case UPLOAD_ERR_INI_SIZE: return sprintf(__("The %s uploaded was too large."), $name); case UPLOAD_ERR_PARTIAL: return sprintf(__("The %s uploaded was only partially uploaded."), $name); case UPLOAD_ERR_NO_FILE: return sprintf(__("No %s was uploaded."), $name); case UPLOAD_ERR_NO_TMP_DIR: return sprintf(__("The %s cannot be uploaded as the temporary folder is missing."), $name); case UPLOAD_ERR_CANT_WRITE: return sprintf(__("The %s cannot be written to disk."), $name); case UPLOAD_ERR_EXTENSION: return sprintf(__("The %s uploaded was blocked by an extension."), $name); } 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"); } /** * @param array $file * @return bool * @throws Exception */ public static function validateUploadedFile(array $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; } }