From d39e983740d261ba4ef53e4978a6fa3e88917a73 Mon Sep 17 00:00:00 2001 From: Iris System Date: Thu, 2 Sep 2021 14:57:50 +1200 Subject: [PATCH] Do basic Markdown escaping on Discord webhook output --- lib/ApplicationAlerters.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/ApplicationAlerters.php b/lib/ApplicationAlerters.php index fc7cdbe..a67aaed 100644 --- a/lib/ApplicationAlerters.php +++ b/lib/ApplicationAlerters.php @@ -18,7 +18,7 @@ final class ApplicationAlerters { $application = new Pushover\Application($_ENV[IX_ENVBASE . '_PUSHOVER_API_TOKEN']); $pushovermessage = new PushoverMessage( - "from {$user}@{$tilde}\n> {$message}", + "from {$user} @ {$tilde}\n---\n{$message}", 'new neotel application', ); @@ -40,8 +40,15 @@ final class ApplicationAlerters { public static function discord(array $key, array $params): array { list($user, $tilde, $message) = $params; + $repl_from = ['\\', '-', '#', '*', '+', '`', '.', '[', ']', '(', ')', '!', '&', '<', '>', '_', '{', '}']; + $repl_to = ['\\\\', '\-', '\#', '\*', '\+', '\`', '\.', '\[', '\]', '\(', '\)', '\!', '\&', '\<', '\>', '\_', '\{', '\}']; + + $x_user = preg_replace('/\R/', ' ', str_replace($repl_from, $repl_to, $user)); + $x_tilde = preg_replace('/\R/', ' ', str_replace($repl_from, $repl_to, $tilde)); + $x_message = preg_replace('/\R/', ' ', str_replace($repl_from, $repl_to, $message)); + $data = json_encode([ - "content" => "From `{$user}@{$tilde}` \n> {$message}", + "content" => "**User:**\n> {$x_user}\n**Tilde/Pubnix:**\n> {$x_tilde}\n**Message:**\n> {$x_message}", ]); $curl_opts = [