| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?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");
- }
- /**
- * @param string $url
- * @param $fields_or_data
- * @param array $curl_options
- * @return bool|string
- * @throws Exception
- */
- public static function post(string $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 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 null;
- $value = $array[$key];
- switch($validation) {
- case "int": {
- if(!is_array($value) && strlen($value)===0) return null;
- return is_array($value) ? array_map("intval", $value) : intval($value);
- }
- case "double":
- case "float": {
- if(strlen($value)===0) return null;
- 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;
- }
- }
|