post.php: split into a function matrix reporting

This commit is contained in:
Zankaria 2024-10-30 12:36:44 +01:00
parent 703637a948
commit 20a30f2661

View file

@ -229,6 +229,45 @@ function check_captcha(array $captcha_config, string $form_id, string $board_uri
return $ret; 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. * Deletes the (single) captcha associated with the ip and code.
* *
@ -742,25 +781,19 @@ function handle_report()
} }
if (isset($config['matrix'])) { if ($config['matrix']['enabled']) {
$reported_post_url = $config['domain'] . "/mod.php?/" . $board['dir'] . $config['dir']['res'] . ($post['thread'] ? $post['thread'] : $id) . ".html"; send_matrix_report(
$post_url = $config['matrix']['host'] . "/_matrix/client/r0/rooms/" . $config['matrix']['room_id'] . "/send/m.room.message?access_token=" . $config['matrix']['access_token']; $config['matrix']['host'],
$config['matrix']['room_id'],
$trimmed_post = strlen($post['body_nomarkup']) > $config['matrix']['max_message_length'] ? ' [...]' : ''; $config['matrix']['access_token'],
$postcontent = mb_substr($post['body_nomarkup'], 0, $config['matrix']['max_message_length']) . $trimmed_post; $config['matrix']['max_message_length'],
$matrix_message = $reported_post_url . ($post['thread'] ? '#' . $id : '') . " \nReason:\n" . $reason . " \nPost:\n" . $postcontent . " \n"; $reason,
$post_data = json_encode( $config['domain'],
array( $board['dir'],
"msgtype" => "m.text", $config['dir']['res'],
"body" => $matrix_message $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);
} }
} }