From 4d62dcd9a231a0f8e9d7b0f23975840b7a3f3283 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Mon, 4 Nov 2024 22:43:39 +0100 Subject: [PATCH] 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'"); + } } /**