33namespace UnityWebPortal \lib ;
44
55use phpseclib3 \Crypt \PublicKeyLoader ;
6- use UnityWebPortal \lib \exceptions \TestingDieException ;
6+ use UnityWebPortal \lib \exceptions \PhpUnitNoDieException ;
77
88class UnitySite
99{
1010 public static function die ($ x )
1111 {
12- if ($ GLOBALS ["PHPUNIT_NO_DIE_PLEASE " ] ?? false ) {
12+ if (@ $ GLOBALS ["PHPUNIT_NO_DIE_PLEASE " ] == true ) {
1313 throw new PhpUnitNoDieException (strval ($ x ));
1414 } else {
1515 \die ($ x );
@@ -20,10 +20,39 @@ public static function redirect($destination)
2020 {
2121 if ($ _SERVER ["PHP_SELF " ] != $ destination ) {
2222 header ("Location: $ destination " );
23- die ("Redirect failed, click <a href=' $ destination'>here</a> to continue. " );
23+ self :: die ("Redirect failed, click <a href=' $ destination'>here</a> to continue. " );
2424 }
2525 }
2626
27+ public static function headerResponseCode (int $ code )
28+ {
29+ $ responseCodeMessage = @http_response_code ($ code ) ?? "" ;
30+ $ msg = $ _SERVER ["SERVER_PROTOCOL " ] . " " . strval ($ code ) . " " . $ responseCodeMessage ;
31+ header ($ msg , true , $ ncode );
32+ }
33+
34+ public static function errorLog (string $ title , string $ message )
35+ {
36+ error_log (
37+ "$ title: " . json_encode (
38+ [
39+ "message " => $ message ,
40+ "REMOTE_USER " => @$ _SERVER ["REMOTE_USER " ], // "@": allow null default value
41+ "REMOTE_ADDR " => @$ _SERVER ["REMOTE_ADDR " ], // "@": allow null default value
42+ // getTrace() is a list but the JSON is very verbose
43+ "trace " => explode (PHP_EOL , (new \Exception ())->getTraceAsString ())
44+ ]
45+ )
46+ );
47+ }
48+
49+ public static function badRequest ($ message )
50+ {
51+ self ::headerResponseCode (400 );
52+ self ::errorLog ("bad request " , $ message );
53+ self ::die ($ message );
54+ }
55+
2756 public static function removeTrailingWhitespace ($ arr )
2857 {
2958 $ out = array ();
0 commit comments