Skip to content

Commit 4552701

Browse files
committed
X button deletes specific messag with ajax
1 parent d467ad1 commit 4552701

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
@@ -170,13 +170,25 @@ public static function errorHandler(int $severity, string $message, string $file
170170

171171
public static function getPostData(string $key): mixed
172172
{
173-
try {
174-
return $_POST[$key];
175-
} catch (ArrayKeyException $e) {
176-
self::badRequest('failed to get $_POST data', $e, [
177-
'$_POST' => $_POST,
178-
]);
173+
if (!isset($_SERVER)) {
174+
throw new RuntimeException('$_SERVER is unset');
175+
}
176+
if (!array_key_exists("REQUEST_METHOD", $_SERVER)) {
177+
throw new RuntimeException('$_SERVER has no array key "REQUEST_METHOD"');
178+
}
179+
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
180+
self::badRequest('$_SERVER["REQUEST_METHOD"] != "POST"');
181+
}
182+
if (!isset($_POST)) {
183+
self::badRequest('$_POST is unset');
184+
}
185+
if ($_POST === null) {
186+
self::badRequest('$_POST is null');
187+
}
188+
if (!array_key_exists($key, $_POST)) {
189+
self::badRequest("\$_POST has no array key '$key'");
179190
}
191+
return $_POST[$key];
180192
}
181193

182194
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)