1
0

http.php 4.4 KB

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