http.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 $prefix
  66. * @return bool|string
  67. * @throws Exception
  68. */
  69. public static function getDateTimeFromFields(string $prefix = "") {
  70. $date = http::getDateFromFields($prefix);
  71. if($date === false) return false;
  72. if(empty($prefix)) {
  73. $hour_field = "hour";
  74. $minute_field = "minute";
  75. $second_field = "second";
  76. }
  77. else {
  78. $hour_field = $prefix . "_hour";
  79. $minute_field = $prefix . "_minute";
  80. $second_field = $prefix . "_second";
  81. }
  82. $hour = http::value($hour_field, "int");
  83. $minute = http::value($minute_field, "int");
  84. $second = http::value($second_field, "int");
  85. if(strlen($hour) == 0) return false;
  86. if(strlen($minute) == 0) return false;
  87. if(strlen($second) == 0) return false;
  88. list($hour, $minute, $second) = array_map(function($part) {
  89. return str_pad($part, 2, "0", STR_PAD_LEFT);
  90. }, [$hour, $minute, $second]);
  91. return "$date $hour:$minute:$second";
  92. }
  93. /**
  94. * @param string $key
  95. * @param string $validation
  96. * @param string $request_type
  97. * @return mixed
  98. * @throws Exception
  99. */
  100. public static function value(string $key, string $validation = "none", string $request_type = "request") {
  101. $request_type = strtolower($request_type);
  102. switch($request_type) {
  103. case "request":
  104. $array = $_REQUEST;
  105. break;
  106. case "get":
  107. $array = $_GET;
  108. break;
  109. case "post":
  110. $array = $_POST;
  111. break;
  112. default:
  113. trigger_error("http::value called with \"$request_type\" request_type", E_USER_ERROR);
  114. break;
  115. }
  116. if(!isset($array[$key])) return false;
  117. $value = $array[$key];
  118. switch($validation) {
  119. case "int": {
  120. if(!is_array($value) && strlen($value)===0) return false;
  121. return is_array($value) ? array_map("intval", $value) : intval($value);
  122. }
  123. case "double":
  124. case "float": {
  125. if(strlen($value)===0) return false;
  126. return floatval($value);
  127. }
  128. // Return database escaped string
  129. case "db": {
  130. return is_array($value) ? array_map("db::escape", $value) : db::escape($value);
  131. }
  132. case "html": {
  133. return is_array($value) ? array_map("htmlspecialchars", $value) : htmlspecialchars($value);
  134. }
  135. case "trim": {
  136. return is_array($value) ? array_map("trim", $value) : trim($value);
  137. }
  138. case "":
  139. case "none": {
  140. return $value;
  141. }
  142. case "email": {
  143. return filter_var($value, FILTER_VALIDATE_EMAIL);
  144. }
  145. default: {
  146. throw new Exception("Unknown validation type: $validation");
  147. }
  148. }
  149. }
  150. public static function redirect($url) {
  151. header("HTTP/1.1 303 See Other");
  152. header("Location: " . $url);
  153. exit;
  154. }
  155. public static function isSSL() {
  156. if(isset($_SERVER["SERVER_PORT"])) {
  157. return $_SERVER["SERVER_PORT"] == 443;
  158. }
  159. return false;
  160. }
  161. public static function getProtocol() {
  162. return (HTTP::isSSL()) ? "https" : "http";
  163. }
  164. public static function getUploadErrorString($error, $name = "file") {
  165. switch($error) {
  166. case UPLOAD_ERR_OK: return "The $name uploaded successfully.";
  167. case UPLOAD_ERR_INI_SIZE: return "The $name uploaded was too large.";
  168. case UPLOAD_ERR_FORM_SIZE: return "The $name uploaded was too large.";
  169. case UPLOAD_ERR_PARTIAL: return "The $name uploaded was only partially uploaded.";
  170. case UPLOAD_ERR_NO_FILE: return "No $name was uploaded.";
  171. case UPLOAD_ERR_NO_TMP_DIR: return "The $name cannot be uploaded as the temporary folder is missing.";
  172. case UPLOAD_ERR_CANT_WRITE: return "The $name cannot be written to disk.";
  173. case UPLOAD_ERR_EXTENSION: return "The $name uploaded was blocked by an extension.";
  174. }
  175. return "Unknown error.";
  176. }
  177. public static function setStatusCode($code) {
  178. switch ($code) {
  179. case 403:
  180. $string = "403 Forbidden";
  181. break;
  182. case 404:
  183. $string = "404 Not Found";
  184. break;
  185. default:
  186. $string = "200 OK";
  187. break;
  188. }
  189. header("HTTP/1.1 $string");
  190. }
  191. /**
  192. * @param array $file
  193. * @return bool
  194. * @throws Exception
  195. */
  196. public static function validateUploadedFile(array $file) {
  197. $file = (object) $file;
  198. if($file->error > 0) {
  199. switch($file->error) {
  200. case UPLOAD_ERR_NO_FILE: throw new Exception("No file was submitted."); break;
  201. case UPLOAD_ERR_INI_SIZE: throw new Exception("The uploaded file was too large."); break;
  202. }
  203. throw new Exception("The uploaded file could not be stored. Please try again later.");
  204. }
  205. return true;
  206. }
  207. }