http.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. $array = [];
  103. switch($request_type) {
  104. case "request":
  105. $array = $_REQUEST;
  106. break;
  107. case "get":
  108. $array = $_GET;
  109. break;
  110. case "post":
  111. $array = $_POST;
  112. break;
  113. default:
  114. trigger_error("http::value called with \"$request_type\" request_type", E_USER_ERROR);
  115. break;
  116. }
  117. if(!isset($array[$key])) return null;
  118. $value = $array[$key];
  119. switch($validation) {
  120. case "int": {
  121. if(!is_array($value) && strlen($value)===0) return null;
  122. return is_array($value) ? array_map("intval", $value) : intval($value);
  123. }
  124. case "double":
  125. case "float": {
  126. if(strlen($value)===0) return null;
  127. return floatval($value);
  128. }
  129. // Return database escaped string
  130. case "db": {
  131. return is_array($value) ? array_map("db::escape", $value) : db::escape($value);
  132. }
  133. case "html": {
  134. return is_array($value) ? array_map("htmlspecialchars", $value) : htmlspecialchars($value);
  135. }
  136. case "trim": {
  137. return is_array($value) ? array_map("trim", $value) : trim($value);
  138. }
  139. case "":
  140. case "none": {
  141. return $value;
  142. }
  143. case "email": {
  144. return filter_var($value, FILTER_VALIDATE_EMAIL);
  145. }
  146. default: {
  147. throw new Exception("Unknown validation type: $validation");
  148. }
  149. }
  150. }
  151. public static function redirect($url) {
  152. header("HTTP/1.1 303 See Other");
  153. header("Location: " . $url);
  154. exit;
  155. }
  156. public static function isSSL() {
  157. if(isset($_SERVER["SERVER_PORT"])) {
  158. return $_SERVER["SERVER_PORT"] == 443;
  159. }
  160. return false;
  161. }
  162. public static function getProtocol() {
  163. return (HTTP::isSSL()) ? "https" : "http";
  164. }
  165. public static function getUploadErrorString($error, $name = "file") {
  166. switch($error) {
  167. case UPLOAD_ERR_OK: return sprintf(__("The %s uploaded successfully."), $name);
  168. case UPLOAD_ERR_FORM_SIZE:
  169. case UPLOAD_ERR_INI_SIZE: return sprintf(__("The %s uploaded was too large."), $name);
  170. case UPLOAD_ERR_PARTIAL: return sprintf(__("The %s uploaded was only partially uploaded."), $name);
  171. case UPLOAD_ERR_NO_FILE: return sprintf(__("No %s was uploaded."), $name);
  172. case UPLOAD_ERR_NO_TMP_DIR: return sprintf(__("The %s cannot be uploaded as the temporary folder is missing."), $name);
  173. case UPLOAD_ERR_CANT_WRITE: return sprintf(__("The %s cannot be written to disk."), $name);
  174. case UPLOAD_ERR_EXTENSION: return sprintf(__("The %s uploaded was blocked by an extension."), $name);
  175. }
  176. return __("Unknown error.");
  177. }
  178. public static function setStatusCode($code) {
  179. switch ($code) {
  180. case 403:
  181. $string = "403 Forbidden";
  182. break;
  183. case 404:
  184. $string = "404 Not Found";
  185. break;
  186. default:
  187. $string = "200 OK";
  188. break;
  189. }
  190. header("HTTP/1.1 $string");
  191. }
  192. /**
  193. * @param array $file
  194. * @return bool
  195. * @throws Exception
  196. */
  197. public static function validateUploadedFile(array $file) {
  198. $file = (object) $file;
  199. if($file->error > 0) {
  200. switch($file->error) {
  201. case UPLOAD_ERR_NO_FILE: throw new Exception(__("No file was submitted.")); break;
  202. case UPLOAD_ERR_INI_SIZE: throw new Exception(__("The uploaded file was too large.")); break;
  203. }
  204. throw new Exception(__("The uploaded file could not be stored. Please try again later."));
  205. }
  206. return true;
  207. }
  208. }