@@ -35,13 +35,18 @@ public static function die(mixed $x = null, bool $show_user = false): never
3535 }
3636 }
3737
38+ /*
39+ send HTTP heaer, set HTTP response code,
40+ print a message just in case the browser fails to redirect if PHP is not being run from the CLI,
41+ and then die
42+ */
3843 public static function redirect (?string $ dest = null ): never
3944 {
4045 $ dest ??= pathJoin (CONFIG ["site " ]["prefix " ], $ _SERVER ["REQUEST_URI " ]);
4146 $ dest = htmlspecialchars ($ dest );
4247 header ("Location: $ dest " );
4348 http_response_code (302 );
44- if (CONFIG [ " site " ][ " enable_error_to_user " ] ) {
49+ if (php_sapi_name () !== " cli " ) {
4550 echo "If you're reading this message, then your browser has failed to redirect you " .
4651 "to the proper destination. click <a href=' $ dest'>here</a> to continue. " ;
4752 }
@@ -62,10 +67,13 @@ public static function errorID(?\Throwable $e = null): string
6267
6368 /*
6469 generates a unique error ID, writes to error log, and then:
65- if the user is doing an HTTP POST:
70+ if PHP is being run in the CLI:
71+ prints a message to stdout and dies
72+ else, if the user is doing an HTTP POST:
6673 registers a message in the user's session and issues a redirect to display that message
6774 else:
68- prints a message to stdout, sets an HTTP response code, and dies
75+ prints an HTML message to stdout, sets an HTTP response code, and dies
76+ we don't want HTML formatted output in the CLI
6977 we can't always do a redirect or else we could risk an infinite loop.
7078 */
7179 public static function gracefulDie (
@@ -89,7 +97,9 @@ public static function gracefulDie(
8997 $ user_message_body .= " $ suffix " ;
9098 }
9199 self ::errorLog ($ log_title , $ log_message , data: $ data , error: $ error , errorid: $ errorid );
92- if ($ _SERVER ["REQUEST_METHOD " ] ?? "" == "POST " ) {
100+ if (php_sapi_name () == "cli " ) {
101+ self ::die ("$ user_message_title -- $ user_message_body " );
102+ } elseif ($ _SERVER ["REQUEST_METHOD " ] ?? "" == "POST " ) {
93103 self ::messageError ($ user_message_title , $ user_message_body );
94104 self ::redirect ();
95105 } else {
0 commit comments