http.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 getDateFromFields($prefix = "") {
  25. $date = false;
  26. if(empty($prefix)) {
  27. $year_field = "year";
  28. $month_field = "month";
  29. $day_field = "day";
  30. }
  31. else {
  32. $year_field = $prefix . "_year";
  33. $month_field = $prefix . "_month";
  34. $day_field = $prefix . "_day";
  35. }
  36. $year = http::value($year_field, "int");
  37. $month = http::value($month_field, "int");
  38. $day = http::value($day_field, "int");
  39. if($year && $month && $day) {
  40. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  41. $day = str_pad($day, 2, "0", STR_PAD_LEFT);
  42. $unix = strtotime("$year-$month-$day");
  43. $date = date("Y-m-d", $unix);
  44. }
  45. return $date;
  46. }
  47. public static function value($key, $validation = "none", $request_type = "request") {
  48. $request_type = strtolower($request_type);
  49. switch($request_type) {
  50. case "request":
  51. $array = $_REQUEST;
  52. break;
  53. case "get":
  54. $array = $_GET;
  55. break;
  56. case "post":
  57. $array = $_POST;
  58. break;
  59. default:
  60. trigger_error("http::value called with \"$request_type\" request_type", E_USER_ERROR);
  61. break;
  62. }
  63. if(!isset($array[$key])) return false;
  64. $value = $array[$key];
  65. switch($validation) {
  66. case "int": {
  67. if(!is_array($value) && strlen($value)===0) return false;
  68. return is_array($value) ? array_map("intval", $value) : intval($value);
  69. }
  70. case "double":
  71. case "float": {
  72. if(strlen($value)===0) return false;
  73. return floatval($value);
  74. }
  75. // Return database escaped string
  76. case "db": {
  77. return is_array($value) ? array_map("db::escape", $value) : db::escape($value);
  78. }
  79. case "html": {
  80. return is_array($value) ? array_map("htmlspecialchars", $value) : htmlspecialchars($value);
  81. }
  82. case "trim": {
  83. return is_array($value) ? array_map("trim", $value) : trim($value);
  84. }
  85. case "":
  86. case "none": {
  87. return $value;
  88. }
  89. case "email": {
  90. return filter_var($value, FILTER_VALIDATE_EMAIL);
  91. }
  92. default: {
  93. throw new Exception("Unknown validation type: $validation");
  94. }
  95. }
  96. }
  97. public static function redirect($url) {
  98. header("HTTP/1.1 303 See Other");
  99. header("Location: " . $url);
  100. exit;
  101. }
  102. public static function isSSL() {
  103. if(isset($_SERVER["SERVER_PORT"])) {
  104. return $_SERVER["SERVER_PORT"] == 443;
  105. }
  106. return false;
  107. }
  108. public static function getProtocol() {
  109. return (HTTP::isSSL()) ? "https" : "http";
  110. }
  111. public static function getUploadErrorString($error, $name = "file") {
  112. switch($error) {
  113. case UPLOAD_ERR_OK: return "The $name uploaded successfully.";
  114. case UPLOAD_ERR_INI_SIZE: return "The $name uploaded was too large.";
  115. case UPLOAD_ERR_FORM_SIZE: return "The $name uploaded was too large.";
  116. case UPLOAD_ERR_PARTIAL: return "The $name uploaded was only partially uploaded.";
  117. case UPLOAD_ERR_NO_FILE: return "No $name was uploaded.";
  118. case UPLOAD_ERR_NO_TMP_DIR: return "The $name cannot be uploaded as the temporary folder is missing.";
  119. case UPLOAD_ERR_CANT_WRITE: return "The $name cannot be written to disk.";
  120. case UPLOAD_ERR_EXTENSION: return "The $name uploaded was blocked by an extension.";
  121. }
  122. return "Unknown error.";
  123. }
  124. public static function setStatusCode($code) {
  125. switch ($code) {
  126. case 403:
  127. $string = "403 Forbidden";
  128. break;
  129. case 404:
  130. $string = "404 Not Found";
  131. break;
  132. default:
  133. $string = "200 OK";
  134. break;
  135. }
  136. header("HTTP/1.1 $string");
  137. }
  138. public static function validateUploadedFile($file) {
  139. $file = (object) $file;
  140. if($file->error > 0) {
  141. switch($file->error) {
  142. case UPLOAD_ERR_NO_FILE: throw new Exception("No file was submitted."); break;
  143. case UPLOAD_ERR_INI_SIZE: throw new Exception("The uploaded file was too large."); break;
  144. }
  145. throw new Exception("The uploaded file could not be stored. Please try again later.");
  146. }
  147. return true;
  148. }
  149. public static function sendFile($path) {
  150. $modified_time = filemtime($path);
  151. $mime_type = Picture::getFileMIMEType($path);
  152. $max_age = 7 * 86400;
  153. header("Cache-Control: public, max-age=$max_age");
  154. header("Expires: " . date("r", time() + $max_age));
  155. header("Pragma: cache");
  156. header("Last-Modified: " . date("r", $modified_time));
  157. header("X-Sendfile: " . $path);
  158. header("Content-Type: " . $mime_type);
  159. }
  160. }
  161. ?>