http.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /**
  8. * @param string $url
  9. * @param $fields_or_data
  10. * @param array $curl_options
  11. * @return bool|string
  12. * @throws Exception
  13. */
  14. public static function post(string $url, $fields_or_data, $curl_options = []) {
  15. $curl_handle = curl_init($url);
  16. curl_setopt($curl_handle, CURLOPT_POST, 1);
  17. curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $fields_or_data);
  18. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
  19. if(!empty($curl_options)) {
  20. foreach($curl_options as $option => $value) {
  21. curl_setopt($curl_handle, $option, $value);
  22. }
  23. }
  24. $result = curl_exec($curl_handle);
  25. if($result === false) {
  26. http::$curl_last_error = curl_error($curl_handle);
  27. throw new Exception("cURL Error: " . curl_error($curl_handle));
  28. }
  29. return $result;
  30. }
  31. public static function setDownloadHeaders($filename) {
  32. header("Content-Disposition: attachment; filename=\"$filename\"");
  33. header("Cache-Control: private, max-age=0, must-revalidate");
  34. header("Pragma: public");
  35. }
  36. /**
  37. * @param string $prefix
  38. * @return string
  39. * @throws Exception
  40. */
  41. public static function getDateFromFields(string $prefix = ""): string {
  42. $date = false;
  43. if(empty($prefix)) {
  44. $year_field = "year";
  45. $month_field = "month";
  46. $day_field = "day";
  47. }
  48. else {
  49. $year_field = $prefix . "_year";
  50. $month_field = $prefix . "_month";
  51. $day_field = $prefix . "_day";
  52. }
  53. $year = http::value($year_field, "int");
  54. $month = http::value($month_field, "int");
  55. $day = http::value($day_field, "int");
  56. if($year && $month && $day) {
  57. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  58. $day = str_pad($day, 2, "0", STR_PAD_LEFT);
  59. $unix = strtotime("$year-$month-$day");
  60. $date = date("Y-m-d", $unix);
  61. }
  62. return $date;
  63. }
  64. /**
  65. * @param string $key
  66. * @param string $validation
  67. * @param string $request_type
  68. * @return mixed
  69. * @throws Exception
  70. */
  71. public static function value(string $key, string $validation = "none", string $request_type = "request") {
  72. $request_type = strtolower($request_type);
  73. $array = [];
  74. switch($request_type) {
  75. case "request":
  76. $array = $_REQUEST;
  77. break;
  78. case "get":
  79. $array = $_GET;
  80. break;
  81. case "post":
  82. $array = $_POST;
  83. break;
  84. default:
  85. trigger_error("http::value called with \"$request_type\" request_type", E_USER_ERROR);
  86. }
  87. if(!isset($array[$key])) return null;
  88. $value = $array[$key];
  89. switch($validation) {
  90. case "int": {
  91. if(!is_array($value) && $value === '') return null;
  92. return is_array($value) ? array_map("intval", $value) : (int) $value;
  93. }
  94. case "double":
  95. case "float": {
  96. if($value === '') return null;
  97. return (float) $value;
  98. }
  99. // Return database escaped string
  100. case "db": {
  101. return is_array($value) ? array_map("db::escape", $value) : db::escape($value);
  102. }
  103. case "html": {
  104. return is_array($value) ? array_map("htmlspecialchars", $value) : htmlspecialchars($value);
  105. }
  106. case "trim": {
  107. return is_array($value) ? array_map("trim", $value) : trim($value);
  108. }
  109. case "":
  110. case "none": {
  111. return $value;
  112. }
  113. case "email": {
  114. return filter_var($value, FILTER_VALIDATE_EMAIL);
  115. }
  116. default: {
  117. throw new Exception("Unknown validation type: $validation");
  118. }
  119. }
  120. }
  121. public static function redirect($url) {
  122. header("HTTP/1.1 303 See Other");
  123. header("Location: " . $url);
  124. exit;
  125. }
  126. public static function isSSL() {
  127. if(isset($_SERVER["SERVER_PORT"])) {
  128. return $_SERVER["SERVER_PORT"] == 443;
  129. }
  130. return false;
  131. }
  132. public static function getProtocol() {
  133. return (HTTP::isSSL()) ? "https" : "http";
  134. }
  135. public static function getUploadErrorString($error, $name = "file") {
  136. switch($error) {
  137. case UPLOAD_ERR_OK: return sprintf(__("The %s uploaded successfully."), $name);
  138. case UPLOAD_ERR_FORM_SIZE:
  139. case UPLOAD_ERR_INI_SIZE: return sprintf(__("The %s uploaded was too large."), $name);
  140. case UPLOAD_ERR_PARTIAL: return sprintf(__("The %s uploaded was only partially uploaded."), $name);
  141. case UPLOAD_ERR_NO_FILE: return sprintf(__("No %s was uploaded."), $name);
  142. case UPLOAD_ERR_NO_TMP_DIR: return sprintf(__("The %s cannot be uploaded as the temporary folder is missing."), $name);
  143. case UPLOAD_ERR_CANT_WRITE: return sprintf(__("The %s cannot be written to disk."), $name);
  144. case UPLOAD_ERR_EXTENSION: return sprintf(__("The %s uploaded was blocked by an extension."), $name);
  145. }
  146. return __("Unknown error.");
  147. }
  148. public static function setStatusCode($code) {
  149. switch ($code) {
  150. case 403:
  151. $string = "403 Forbidden";
  152. break;
  153. case 404:
  154. $string = "404 Not Found";
  155. break;
  156. default:
  157. $string = "200 OK";
  158. break;
  159. }
  160. header("HTTP/1.1 $string");
  161. }
  162. /**
  163. * @param array $file
  164. * @return bool
  165. * @throws Exception
  166. */
  167. public static function validateUploadedFile(array $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. }