|
4 | 4 |
|
5 | 5 | use UnityWebPortal\lib\exceptions\NoDieException; |
6 | 6 | use UnityWebPortal\lib\exceptions\ArrayKeyException; |
| 7 | +use UnityWebPortal\lib\exceptions\UnityHTTPDMessageNotFoundException; |
7 | 8 | use RuntimeException; |
8 | 9 |
|
9 | 10 | enum UnityHTTPDMessageLevel: string |
@@ -226,13 +227,22 @@ public static function errorHandler(int $severity, string $message, string $file |
226 | 227 |
|
227 | 228 | public static function getPostData(string $key): mixed |
228 | 229 | { |
229 | | - try { |
230 | | - return $_POST[$key]; |
231 | | - } catch (ArrayKeyException $e) { |
232 | | - self::badRequest('failed to get $_POST data', $e, [ |
233 | | - '$_POST' => $_POST, |
234 | | - ]); |
| 230 | + if (!isset($_SERVER)) { |
| 231 | + throw new RuntimeException('$_SERVER is unset'); |
| 232 | + } |
| 233 | + if (!array_key_exists("REQUEST_METHOD", $_SERVER)) { |
| 234 | + throw new RuntimeException('$_SERVER has no array key "REQUEST_METHOD"'); |
| 235 | + } |
| 236 | + if ($_SERVER["REQUEST_METHOD"] !== "POST") { |
| 237 | + self::badRequest('$_SERVER["REQUEST_METHOD"] != "POST"'); |
235 | 238 | } |
| 239 | + if (!isset($_POST)) { |
| 240 | + self::badRequest('$_POST is unset'); |
| 241 | + } |
| 242 | + if (!array_key_exists($key, $_POST)) { |
| 243 | + self::badRequest("\$_POST has no array key '$key'"); |
| 244 | + } |
| 245 | + return $_POST[$key]; |
236 | 246 | } |
237 | 247 |
|
238 | 248 | public static function getUploadedFileContents( |
@@ -325,6 +335,43 @@ public static function getMessages() |
325 | 335 |
|
326 | 336 | public static function clearMessages() |
327 | 337 | { |
| 338 | + self::ensureSessionMessagesSanity(); |
328 | 339 | $_SESSION["messages"] = []; |
329 | 340 | } |
| 341 | + |
| 342 | + private static function getMessageIndex( |
| 343 | + UnityHTTPDMessageLevel $level, |
| 344 | + string $title, |
| 345 | + string $body, |
| 346 | + ) { |
| 347 | + $messages = self::getMessages(); |
| 348 | + $error_msg = sprintf( |
| 349 | + "message(level='%s' title='%s' body='%s'), not found. found messages: %s", |
| 350 | + $level->value, |
| 351 | + $title, |
| 352 | + $body, |
| 353 | + jsonEncode($messages), |
| 354 | + ); |
| 355 | + foreach ($messages as $i => $message) { |
| 356 | + if ($title == $message[0] && $body == $message[1] && $level == $message[2]) { |
| 357 | + return $i; |
| 358 | + } |
| 359 | + } |
| 360 | + throw new UnityHTTPDMessageNotFoundException($error_msg); |
| 361 | + } |
| 362 | + |
| 363 | + /* returns the 1st message that matches or throws UnityHTTPDMessageNotFoundException */ |
| 364 | + public static function getMessage(UnityHTTPDMessageLevel $level, string $title, string $body) |
| 365 | + { |
| 366 | + $index = self::getMessageIndex($level, $title, $body); |
| 367 | + return $_SESSION["messages"][$index]; |
| 368 | + } |
| 369 | + |
| 370 | + /* deletes the 1st message that matches or throws UnityHTTPDMessageNotFoundException */ |
| 371 | + public static function deleteMessage(UnityHTTPDMessageLevel $level, string $title, string $body) |
| 372 | + { |
| 373 | + $index = self::getMessageIndex($level, $title, $body); |
| 374 | + unset($_SESSION["messages"][$index]); |
| 375 | + $_SESSION["messages"] = array_values($_SESSION["messages"]); |
| 376 | + } |
330 | 377 | } |
0 commit comments