From 20a30f2661fdf8d44b577fd94ebf7f849481c59a Mon Sep 17 00:00:00 2001 From: Zankaria Date: Wed, 30 Oct 2024 12:36:44 +0100 Subject: [PATCH] post.php: split into a function matrix reporting --- post.php | 69 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/post.php b/post.php index b16b7847..a360fac4 100644 --- a/post.php +++ b/post.php @@ -229,6 +229,45 @@ function check_captcha(array $captcha_config, string $form_id, string $board_uri return $ret; } +function send_matrix_report( + string $matrix_host, + string $room_id, + string $access_token, + int $max_msg_len, + string $report_reason, + string $domain, + string $board_dir, + string $board_res_dir, + array $post, + int $id, +) { + $post_id = $post['thread'] ? $post['thread'] : $id; + + $reported_post_url = "$domain/mod.php?/{$board_dir}{$board_res_dir}{$post_id}.html"; + $post_url = "$matrix_host/_matrix/client/r0/rooms/$room_id/send/m.room.message?access_token=$access_token"; + + $end = strlen($post['body_nomarkup']) > $max_msg_len ? ' [...]' : ''; + $post_content = mb_substr($post['body_nomarkup'], 0, $max_msg_len) . $end; + $matrix_message = $reported_post_url . ($post['thread'] ? "#$post_id" : '') . " \nReason:\n" . $report_reason . " \nPost:\n" . $post_content . " \n"; + $post_data = json_encode([ + 'msgtype' => 'm.text', + 'body' => $matrix_message + ]); + + $ch = curl_init($post_url); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $c_ret = curl_exec($ch); + if ($c_ret === false) { + $err_no = curl_errno($ch); + $err_str = curl_error($ch); + + error_log("Failed to send report to matrix. Curl returned: $err_no ($err_str)"); + return false; + } + curl_close($ch); +} + /** * Deletes the (single) captcha associated with the ip and code. * @@ -742,25 +781,19 @@ function handle_report() } - if (isset($config['matrix'])) { - $reported_post_url = $config['domain'] . "/mod.php?/" . $board['dir'] . $config['dir']['res'] . ($post['thread'] ? $post['thread'] : $id) . ".html"; - $post_url = $config['matrix']['host'] . "/_matrix/client/r0/rooms/" . $config['matrix']['room_id'] . "/send/m.room.message?access_token=" . $config['matrix']['access_token']; - - $trimmed_post = strlen($post['body_nomarkup']) > $config['matrix']['max_message_length'] ? ' [...]' : ''; - $postcontent = mb_substr($post['body_nomarkup'], 0, $config['matrix']['max_message_length']) . $trimmed_post; - $matrix_message = $reported_post_url . ($post['thread'] ? '#' . $id : '') . " \nReason:\n" . $reason . " \nPost:\n" . $postcontent . " \n"; - $post_data = json_encode( - array( - "msgtype" => "m.text", - "body" => $matrix_message - ) + if ($config['matrix']['enabled']) { + send_matrix_report( + $config['matrix']['host'], + $config['matrix']['room_id'], + $config['matrix']['access_token'], + $config['matrix']['max_message_length'], + $reason, + $config['domain'], + $board['dir'], + $config['dir']['res'], + $post, + $id ); - - $ch = curl_init($post_url); - curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $postResult = curl_exec($ch); - curl_close($ch); } }