http.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. class HTTP {
  3. public static $curl_last_error = "";
  4. public static function isAJAX() {
  5. return (!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest");
  6. }
  7. public static function post($url, $fields_or_data, $curl_options = []) {
  8. $curl_handle = curl_init($url);
  9. curl_setopt($curl_handle, CURLOPT_POST, 1);
  10. curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $fields_or_data);
  11. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
  12. if(!empty($curl_options)) {
  13. foreach($curl_options as $option => $value) {
  14. curl_setopt($curl_handle, $option, $value);
  15. }
  16. }
  17. $result = curl_exec($curl_handle);
  18. if($result === false) {
  19. http::$curl_last_error = curl_error($curl_handle);
  20. throw new Exception("cURL Error: " . curl_error($curl_handle));
  21. }
  22. return $result;
  23. }
  24. public static function setDownloadHeaders($filename) {
  25. header("Content-Disposition: attachment; filename=\"$filename\"");
  26. header("Cache-Control: private, max-age=0, must-revalidate");
  27. header("Pragma: public");
  28. }
  29. public static function getDateFromFields($prefix = "") {
  30. $date = false;
  31. if(empty($prefix)) {
  32. $year_field = "year";
  33. $month_field = "month";
  34. $day_field = "day";
  35. }
  36. else {
  37. $year_field = $prefix . "_year";
  38. $month_field = $prefix . "_month";
  39. $day_field = $prefix . "_day";
  40. }
  41. $year = http::value($year_field, "int");
  42. $month = http::value($month_field, "int");
  43. $day = http::value($day_field, "int");
  44. if($year && $month && $day) {
  45. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  46. $day = str_pad($day, 2, "0", STR_PAD_LEFT);
  47. $unix = strtotime("$year-$month-$day");
  48. $date = date("Y-m-d", $unix);
  49. }
  50. return $date;
  51. }
  52. public static function getDateTimeFromFields($prefix = "") {
  53. $date = http::getDateFromFields($prefix);
  54. if($date === false) return false;
  55. if(empty($prefix)) {
  56. $hour_field = "hour";
  57. $minute_field = "minute";
  58. $second_field = "second";
  59. }
  60. else {
  61. $hour_field = $prefix . "_hour";
  62. $minute_field = $prefix . "_minute";
  63. $second_field = $prefix . "_second";
  64. }
  65. $hour = http::value($hour_field, "int");
  66. $minute = http::value($minute_field, "int");
  67. $second = http::value($second_field, "int");
  68. if(strlen($hour) == 0) return false;
  69. if(strlen($minute) == 0) return false;
  70. if(strlen($second) == 0) return false;
  71. list($hour, $minute, $second) = array_map(function($part) {
  72. return str_pad($part, 2, "0", STR_PAD_LEFT);
  73. }, [$hour, $minute, $second]);
  74. return "$date $hour:$minute:$second";
  75. }
  76. public static function value($key, $validation = "none", $request_type = "request") {
  77. $request_type = strtolower($request_type);
  78. switch($request_type) {
  79. case "request":
  80. $array = $_REQUEST;
  81. break;
  82. case "get":
  83. $array = $_GET;
  84. break;
  85. case "post":
  86. $array = $_POST;
  87. break;
  88. default:
  89. trigger_error("http::value called with \"$request_type\" request_type", E_USER_ERROR);
  90. break;
  91. }
  92. if(!isset($array[$key])) return false;
  93. $value = $array[$key];
  94. switch($validation) {
  95. case "int": {
  96. if(!is_array($value) && strlen($value)===0) return false;
  97. return is_array($value) ? array_map("intval", $value) : intval($value);
  98. }
  99. case "double":
  100. case "float": {
  101. if(strlen($value)===0) return false;
  102. return floatval($value);
  103. }
  104. // Return database escaped string
  105. case "db": {
  106. return is_array($value) ? array_map("db::escape", $value) : db::escape($value);
  107. }
  108. case "html": {
  109. return is_array($value) ? array_map("htmlspecialchars", $value) : htmlspecialchars($value);
  110. }
  111. case "trim": {
  112. return is_array($value) ? array_map("trim", $value) : trim($value);
  113. }
  114. case "":
  115. case "none": {
  116. return $value;
  117. }
  118. case "email": {
  119. return filter_var($value, FILTER_VALIDATE_EMAIL);
  120. }
  121. default: {
  122. throw new Exception("Unknown validation type: $validation");
  123. }
  124. }
  125. }
  126. public static function redirect($url) {
  127. header("HTTP/1.1 303 See Other");
  128. header("Location: " . $url);
  129. exit;
  130. }
  131. public static function isSSL() {
  132. if(isset($_SERVER["SERVER_PORT"])) {
  133. return $_SERVER["SERVER_PORT"] == 443;
  134. }
  135. return false;
  136. }
  137. public static function getProtocol() {
  138. return (HTTP::isSSL()) ? "https" : "http";
  139. }
  140. public static function getUploadErrorString($error, $name = "file") {
  141. switch($error) {
  142. case UPLOAD_ERR_OK: return "The $name uploaded successfully.";
  143. case UPLOAD_ERR_INI_SIZE: return "The $name uploaded was too large.";
  144. case UPLOAD_ERR_FORM_SIZE: return "The $name uploaded was too large.";
  145. case UPLOAD_ERR_PARTIAL: return "The $name uploaded was only partially uploaded.";
  146. case UPLOAD_ERR_NO_FILE: return "No $name was uploaded.";
  147. case UPLOAD_ERR_NO_TMP_DIR: return "The $name cannot be uploaded as the temporary folder is missing.";
  148. case UPLOAD_ERR_CANT_WRITE: return "The $name cannot be written to disk.";
  149. case UPLOAD_ERR_EXTENSION: return "The $name uploaded was blocked by an extension.";
  150. }
  151. return "Unknown error.";
  152. }
  153. public static function setStatusCode($code) {
  154. switch ($code) {
  155. case 403:
  156. $string = "403 Forbidden";
  157. break;
  158. case 404:
  159. $string = "404 Not Found";
  160. break;
  161. default:
  162. $string = "200 OK";
  163. break;
  164. }
  165. header("HTTP/1.1 $string");
  166. }
  167. public static function validateUploadedFile($file) {
  168. $file = (object) $file;
  169. if($file->error > 0) {
  170. switch($file->error) {
  171. case UPLOAD_ERR_NO_FILE: throw new Exception("No file was submitted."); break;
  172. case UPLOAD_ERR_INI_SIZE: throw new Exception("The uploaded file was too large."); break;
  173. }
  174. throw new Exception("The uploaded file could not be stored. Please try again later.");
  175. }
  176. return true;
  177. }
  178. public static function sendFile($path) {
  179. $modified_time = filemtime($path);
  180. $mime_type = Picture::getFileMIMEType($path);
  181. $max_age = 7 * 86400;
  182. header("Cache-Control: public, max-age=$max_age");
  183. header("Expires: " . date("r", time() + $max_age));
  184. header("Pragma: cache");
  185. header("Last-Modified: " . date("r", $modified_time));
  186. header("X-Sendfile: " . $path);
  187. header("Content-Type: " . $mime_type);
  188. }
  189. }
  190. ?>