http.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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])) {
  88. return null;
  89. }
  90. $value = $array[$key];
  91. switch($validation) {
  92. case "int": {
  93. if(!is_array($value) && $value === '') return null;
  94. return is_array($value) ? array_map("intval", $value) : (int) $value;
  95. }
  96. case "double":
  97. case "float": {
  98. if($value === '') return null;
  99. return (float) $value;
  100. }
  101. // Return database escaped string
  102. case "db": {
  103. return is_array($value) ? array_map("db::escape", $value) : db::escape($value);
  104. }
  105. case "html": {
  106. return is_array($value) ? array_map("htmlspecialchars", $value) : htmlspecialchars($value);
  107. }
  108. case "string":
  109. case "trim": {
  110. return is_array($value) ? array_map("trim", $value) : trim($value);
  111. }
  112. case "":
  113. case "raw":
  114. case "none": {
  115. return $value;
  116. }
  117. case "email": {
  118. return is_array($value) ? filter_var_array($value, FILTER_VALIDATE_EMAIL) : filter_var($value, FILTER_VALIDATE_EMAIL);
  119. }
  120. default: {
  121. throw new RuntimeException("Unknown validation type: $validation");
  122. }
  123. }
  124. }
  125. public static function redirect($url) {
  126. header("HTTP/1.1 303 See Other");
  127. header("Location: " . $url);
  128. exit;
  129. }
  130. public static function isSSL() {
  131. if(isset($_SERVER["SERVER_PORT"])) {
  132. return $_SERVER["SERVER_PORT"] == 443;
  133. }
  134. return false;
  135. }
  136. public static function getProtocol() {
  137. return (HTTP::isSSL()) ? "https" : "http";
  138. }
  139. public static function getUploadErrorString($error, $name = "file") {
  140. switch($error) {
  141. case UPLOAD_ERR_OK: return sprintf(__("The %s uploaded successfully."), $name);
  142. case UPLOAD_ERR_FORM_SIZE:
  143. case UPLOAD_ERR_INI_SIZE: return sprintf(__("The %s uploaded was too large."), $name);
  144. case UPLOAD_ERR_PARTIAL: return sprintf(__("The %s uploaded was only partially uploaded."), $name);
  145. case UPLOAD_ERR_NO_FILE: return sprintf(__("No %s was uploaded."), $name);
  146. case UPLOAD_ERR_NO_TMP_DIR: return sprintf(__("The %s cannot be uploaded as the temporary folder is missing."), $name);
  147. case UPLOAD_ERR_CANT_WRITE: return sprintf(__("The %s cannot be written to disk."), $name);
  148. case UPLOAD_ERR_EXTENSION: return sprintf(__("The %s uploaded was blocked by an extension."), $name);
  149. }
  150. return __("Unknown error.");
  151. }
  152. public static function setStatusCode($code) {
  153. switch ($code) {
  154. case 403:
  155. $string = "403 Forbidden";
  156. break;
  157. case 404:
  158. $string = "404 Not Found";
  159. break;
  160. default:
  161. $string = "200 OK";
  162. break;
  163. }
  164. header("HTTP/1.1 $string");
  165. }
  166. /**
  167. * @param array $file
  168. * @return bool
  169. * @throws Exception
  170. */
  171. public static function validateUploadedFile(array $file) {
  172. $file = (object) $file;
  173. if($file->error > 0) {
  174. switch($file->error) {
  175. case UPLOAD_ERR_NO_FILE: throw new Exception(__("No file was submitted.")); break;
  176. case UPLOAD_ERR_INI_SIZE: throw new Exception(__("The uploaded file was too large.")); break;
  177. }
  178. throw new Exception(__("The uploaded file could not be stored. Please try again later."));
  179. }
  180. return true;
  181. }
  182. }