|
3 | 3 | namespace UnityWebPortal\lib;
|
4 | 4 |
|
5 | 5 | use phpseclib3\Crypt\PublicKeyLoader;
|
| 6 | +use UnityWebPortal\lib\exceptions\PhpUnitNoDieException; |
6 | 7 |
|
7 | 8 | class UnitySite
|
8 | 9 | {
|
| 10 | + public static function die($x = null) |
| 11 | + { |
| 12 | + if (@$GLOBALS["PHPUNIT_NO_DIE_PLEASE"] == true) { |
| 13 | + if (is_null($x)) { |
| 14 | + throw new PhpUnitNoDieException(); |
| 15 | + } else { |
| 16 | + throw new PhpUnitNoDieException($x); |
| 17 | + } |
| 18 | + } else { |
| 19 | + if (is_null($x)) { |
| 20 | + die(); |
| 21 | + } else { |
| 22 | + die($x); |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + |
9 | 27 | public static function redirect($destination)
|
10 | 28 | {
|
11 | 29 | if ($_SERVER["PHP_SELF"] != $destination) {
|
12 | 30 | header("Location: $destination");
|
13 |
| - die("Redirect failed, click <a href='$destination'>here</a> to continue."); |
| 31 | + self::die("Redirect failed, click <a href='$destination'>here</a> to continue."); |
14 | 32 | }
|
15 | 33 | }
|
16 | 34 |
|
| 35 | + private static function headerResponseCode(int $code, string $reason) |
| 36 | + { |
| 37 | + $protocol = @$_SERVER["SERVER_PROTOCOL"] ?? "HTTP/1.1"; |
| 38 | + $msg = $protocol . " " . strval($code) . " " . $reason; |
| 39 | + header($msg, true, $code); |
| 40 | + } |
| 41 | + |
| 42 | + public static function errorLog(string $title, string $message) |
| 43 | + { |
| 44 | + error_log( |
| 45 | + "$title: " . json_encode( |
| 46 | + [ |
| 47 | + "message" => $message, |
| 48 | + "REMOTE_USER" => @$_SERVER["REMOTE_USER"], |
| 49 | + "REMOTE_ADDR" => @$_SERVER["REMOTE_ADDR"], |
| 50 | + "trace" => (new \Exception())->getTraceAsString() |
| 51 | + ] |
| 52 | + ) |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + public static function badRequest($message) |
| 57 | + { |
| 58 | + self::headerResponseCode(400, "bad request"); |
| 59 | + self::errorLog("bad request", $message); |
| 60 | + self::die(); |
| 61 | + } |
| 62 | + |
| 63 | + public static function forbidden($message) |
| 64 | + { |
| 65 | + self::headerResponseCode(403, "forbidden"); |
| 66 | + self::errorLog("forbidden", $message); |
| 67 | + self::die(); |
| 68 | + } |
| 69 | + |
17 | 70 | public static function removeTrailingWhitespace($arr)
|
18 | 71 | {
|
19 | 72 | $out = array();
|
|
0 commit comments