From 703637a948b219dab3c2febe5eaeb2779cf13314 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Wed, 30 Oct 2024 12:36:23 +0100 Subject: [PATCH 1/3] config.php: restructure matrix configuration a bit --- inc/config.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/inc/config.php b/inc/config.php index f3e041d0..766fe912 100644 --- a/inc/config.php +++ b/inc/config.php @@ -1895,12 +1895,14 @@ */ // Matrix integration for reports - // $config['matrix'] = array( - // 'access_token' => 'ACCESS_TOKEN', - // 'room_id' => '%21askjdlkajsdlka:matrix.org', - // 'host' => 'https://matrix.org', - // 'max_message_length' => 240 - // ); + $config['matrix'] = [ + 'enabled' => false, + 'access_token' => 'ACCESS_TOKEN', + // Note: must be already url-escaped. + 'room_id' => '%21askjdlkajsdlka:matrix.org', + 'host' => 'https://matrix.org', + 'max_message_length' => 240 + ]; //Securimage captcha //Note from lainchan PR: "TODO move a bunch of things here" From 20a30f2661fdf8d44b577fd94ebf7f849481c59a Mon Sep 17 00:00:00 2001 From: Zankaria Date: Wed, 30 Oct 2024 12:36:44 +0100 Subject: [PATCH 2/3] 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); } } From 4d62dcd9a231a0f8e9d7b0f23975840b7a3f3283 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Mon, 4 Nov 2024 22:43:39 +0100 Subject: [PATCH 3/3] post.php: use modern matrix API --- post.php | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/post.php b/post.php index a360fac4..997201b0 100644 --- a/post.php +++ b/post.php @@ -244,19 +244,29 @@ function send_matrix_report( $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([ + $text_body = $reported_post_url . ($post['thread'] ? "#$post_id" : '') . " \nReason:\n" . $report_reason . " \nPost:\n" . $post_content . " \n"; + + $random_transaction_id = mt_rand(); + $json_body = json_encode([ 'msgtype' => 'm.text', - 'body' => $matrix_message + 'body' => $text_body ]); - $ch = curl_init($post_url); - curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $ch = curl_init(); + curl_setopt_array($ch, [ + CURLOPT_URL => "$matrix_host/_matrix/client/v3/rooms/$room_id/send/m.room.message/$random_transaction_id", + CURLOPT_CUSTOMREQUEST => 'PUT', + CURLOPT_HTTPHEADER => [ + 'Content-Type: application/json', + "Authorization: Bearer $access_token" + ], + CURLOPT_POSTFIELDS => $json_body, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 3, + ]); $c_ret = curl_exec($ch); if ($c_ret === false) { $err_no = curl_errno($ch); @@ -266,6 +276,15 @@ function send_matrix_report( return false; } curl_close($ch); + + $json = json_decode($c_ret, true); + if ($json === null) { + error_log("Report forwarding failed, matrix returned a non-json value"); + } elseif (!isset($json["event_id"])) { + $code = $json["errcode"] ?? ''; + $desc = $json["error"] ?? ''; + error_log("Report forwarding failed, matrix returned code '$code', with description '$desc'"); + } } /**