44
55use UnityWebPortal \lib \exceptions \NoDieException ;
66use UnityWebPortal \lib \exceptions \ArrayKeyException ;
7+ use RuntimeException ;
8+
9+ enum UnityHTTPDMessageLevel: string
10+ {
11+ case DEBUG = "debug " ;
12+ case INFO = "info " ;
13+ case SUCCESS = "success " ;
14+ case WARNING = "warning " ;
15+ case ERROR = "error " ;
16+ }
717
818class UnityHTTPD
919{
@@ -24,8 +34,10 @@ public static function die(mixed $x = null, bool $show_user = false): never
2434 }
2535 }
2636
27- public static function redirect ($ dest ): never
37+ public static function redirect (? string $ dest = null ): never
2838 {
39+ $ dest ??= pathJoin (CONFIG ["site " ]["prefix " ], $ _SERVER ["REQUEST_URI " ]);
40+ $ dest = htmlspecialchars ($ dest );
2941 header ("Location: $ dest " );
3042 self ::errorToUser ("Redirect failed, click <a href=' $ dest'>here</a> to continue. " , 302 );
3143 self ::die ();
@@ -196,4 +208,66 @@ public static function alert(string $message): void
196208 // jsonEncode escapes quotes
197209 echo "<script type='text/javascript'>alert( " . \jsonEncode ($ message ) . ");</script> " ;
198210 }
211+
212+ private static function ensureSessionMessagesSanity ()
213+ {
214+ if (!isset ($ _SESSION )) {
215+ throw new RuntimeException ('$_SESSION is unset ' );
216+ }
217+ if (!array_key_exists ("messages " , $ _SESSION )) {
218+ self ::errorLog (
219+ "invalid session messages " ,
220+ 'array key "messages" does not exist for $_SESSION ' ,
221+ data: ['$_SESSION ' => $ _SESSION ],
222+ );
223+ $ _SESSION ["messages " ] = [];
224+ }
225+ if (!is_array ($ _SESSION ["messages " ])) {
226+ $ type = gettype ($ _SESSION ["messages " ]);
227+ self ::errorLog (
228+ "invalid session messages " ,
229+ "\$_SESSION['messages'] is type ' $ type', not an array " ,
230+ data: ['$_SESSION ' => $ _SESSION ],
231+ );
232+ $ _SESSION ["messages " ] = [];
233+ }
234+ }
235+
236+ public static function message (string $ title , string $ body , UnityHTTPDMessageLevel $ level )
237+ {
238+ self ::ensureSessionMessagesSanity ();
239+ array_push ($ _SESSION ["messages " ], [$ title , $ body , $ level ]);
240+ }
241+
242+ public static function messageDebug (string $ title , string $ body )
243+ {
244+ return self ::message ($ title , $ body , UnityHTTPDMessageLevel::DEBUG );
245+ }
246+ public static function messageInfo (string $ title , string $ body )
247+ {
248+ return self ::message ($ title , $ body , UnityHTTPDMessageLevel::INFO );
249+ }
250+ public static function messageSuccess (string $ title , string $ body )
251+ {
252+ return self ::message ($ title , $ body , UnityHTTPDMessageLevel::SUCCESS );
253+ }
254+ public static function messageWarning (string $ title , string $ body )
255+ {
256+ return self ::message ($ title , $ body , UnityHTTPDMessageLevel::WARNING );
257+ }
258+ public static function messageError (string $ title , string $ body )
259+ {
260+ return self ::message ($ title , $ body , UnityHTTPDMessageLevel::ERROR );
261+ }
262+
263+ public static function getMessages ()
264+ {
265+ self ::ensureSessionMessagesSanity ();
266+ return $ _SESSION ["messages " ];
267+ }
268+
269+ public static function clearMessages ()
270+ {
271+ $ _SESSION ["messages " ] = [];
272+ }
199273}
0 commit comments