Skip to content

Commit 0561da6

Browse files
committed
X button deletes specific messag with ajax
1 parent ef4f147 commit 0561da6

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

resources/lib/UnityHTTPD.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,25 @@ public static function errorHandler(int $severity, string $message, string $file
227227

228228
public static function getPostData(string $key): mixed
229229
{
230-
try {
231-
return $_POST[$key];
232-
} catch (ArrayKeyException $e) {
233-
self::badRequest('failed to get $_POST data', $e, [
234-
'$_POST' => $_POST,
235-
]);
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"');
238+
}
239+
if (!isset($_POST)) {
240+
self::badRequest('$_POST is unset');
241+
}
242+
if ($_POST === null) {
243+
self::badRequest('$_POST is null');
244+
}
245+
if (!array_key_exists($key, $_POST)) {
246+
self::badRequest("\$_POST has no array key '$key'");
236247
}
248+
return $_POST[$key];
237249
}
238250

239251
public static function getUploadedFileContents(

resources/templates/header.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,32 @@
147147
<div class='message %s'>
148148
<h3>%s</h3>
149149
<p>%s</p>
150-
<button onclick=\"this.parentElement.style.display='none';\">×</button>
150+
<button
151+
onclick=\"
152+
this.parentElement.style.display='none';
153+
$.ajax({
154+
url: '/panel/ajax/delete_message.php',
155+
method: 'POST',
156+
data: {
157+
'level': '%s',
158+
'title_regex': '/^%s$/',
159+
'body_regex': '/^%s$/',
160+
}
161+
});
162+
\"
163+
>
164+
×
165+
</button>
151166
</div>
152167
",
153168
htmlspecialchars($level->value),
154169
htmlspecialchars($title),
155-
htmlspecialchars($body)
170+
htmlspecialchars($body),
171+
htmlspecialchars($level->value),
172+
htmlspecialchars($title),
173+
htmlspecialchars($body),
156174
);
157175
}
158-
UnityHTTPD::clearMessages();
159176
if (
160177
isset($_SESSION["is_admin"])
161178
&& $_SESSION["is_admin"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
require_once __DIR__ . "/../../../resources/autoload.php";
4+
5+
use UnityWebPortal\lib\UnityHTTPD;
6+
use UnityWebPortal\lib\UnityHTTPDMessageLevel;
7+
8+
$level_str = UnityHTTPD::getPostData("level");
9+
$level = UnityHTTPDMessageLevel::from($level_str);
10+
$title_regex = UnityHTTPD::getPostData("title_regex");
11+
$body_regex = UnityHTTPD::getPostData("body_regex");
12+
UnityHTTPD::deleteMessage($level, $title_regex, $body_regex);

0 commit comments

Comments
 (0)