From d09953da0750347f965dcdebd7a769fed98945ee Mon Sep 17 00:00:00 2001 From: Benjamin Southall Date: Fri, 23 Sep 2016 22:44:48 +0900 Subject: [PATCH] ReportNotifier Slack notification support --- inc/config.php | 6 ++++++ post.php | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/inc/config.php b/inc/config.php index 69b4bd3a..86ddd5aa 100644 --- a/inc/config.php +++ b/inc/config.php @@ -1669,3 +1669,9 @@ ''. ''. ''; + + // Slack Report Notification + $config['slack'] = true; + $config['slack_channel'] = "reports"; + $config['slack_incoming_webhook_endpoint'] = "https://hooks.slack.com/services/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"; + diff --git a/post.php b/post.php index 99ef4850..f2ed509f 100644 --- a/post.php +++ b/post.php @@ -141,15 +141,15 @@ if (isset($_POST['delete'])) { markup($reason); foreach ($report as &$id) { - $query = prepare(sprintf("SELECT `thread` FROM ``posts_%s`` WHERE `id` = :id", $board['uri'])); + $query = prepare(sprintf("SELECT `thread` , `body_nomarkup` FROM ``posts_%s`` WHERE `id` = :id", $board['uri'])); $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); - $thread = $query->fetchColumn(); + $thread = $query->fetch(PDO::FETCH_ASSOC); if ($config['syslog']) _syslog(LOG_INFO, 'Reported post: ' . - '/' . $board['dir'] . $config['dir']['res'] . link_for($post) . ($thread ? '#' . $id : '') . + '/' . $board['dir'] . $config['dir']['res'] . link_for($post) . ($thread['thread'] ? '#' . $id : '') . ' for "' . $reason . '"' ); $query = prepare("INSERT INTO ``reports`` VALUES (NULL, :time, :ip, :board, :post, :reason)"); @@ -159,6 +159,38 @@ if (isset($_POST['delete'])) { $query->bindValue(':post', $id, PDO::PARAM_INT); $query->bindValue(':reason', $reason, PDO::PARAM_STR); $query->execute() or error(db_error($query)); + if ($config['slack']) + { + function slack($message, $room = "reports", $icon = ":no_entry_sign:") + { + $room = ($room) ? $room : "reports"; + $data = "payload=" . json_encode(array( + "channel" => "#{$room}", + "text" => urlencode($message), + "icon_emoji" => $icon + )); + + // You can get your webhook endpoint from your Slack settings + // For some reason using the configuration key doesn't work + //$ch = curl_init($config['slack_incoming_webhook_endpoint']); + $ch = curl_init("https://hooks.slack.com/services/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"); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $result = curl_exec($ch); + curl_close($ch); + + return $result; + } + + $postcontent = mb_substr($thread['body_nomarkup'], 0, 120) . '... _*(POST TRIMMED)*_'; + $slackmessage = 'Post: <' .$config['domain'] . "/mod.php?/" . $board['dir'] . $config['dir']['res'] . ( $thread['thread'] ? $thread['thread'] : $id ) . ".html" . ($thread['thread'] ? '#' . $id : '') . '> \n Post Content: ' . $postcontent . '\n Reason: ' . $reason . '\n'; + + $slackresult = slack($slackmessage, $config['slack_channel']); + } + + } $is_mod = isset($_POST['mod']) && $_POST['mod'];