forked from leftypol/leftypol
Merge branch '7-matrix-reports' into 'config'
Resolve "Matrix report bot" Closes #7 See merge request leftypol/leftypol!10
This commit is contained in:
commit
5fa893655d
2 changed files with 78 additions and 24 deletions
|
@ -1895,12 +1895,14 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Matrix integration for reports
|
// Matrix integration for reports
|
||||||
// $config['matrix'] = array(
|
$config['matrix'] = [
|
||||||
// 'access_token' => 'ACCESS_TOKEN',
|
'enabled' => false,
|
||||||
// 'room_id' => '%21askjdlkajsdlka:matrix.org',
|
'access_token' => 'ACCESS_TOKEN',
|
||||||
// 'host' => 'https://matrix.org',
|
// Note: must be already url-escaped.
|
||||||
// 'max_message_length' => 240
|
'room_id' => '%21askjdlkajsdlka:matrix.org',
|
||||||
// );
|
'host' => 'https://matrix.org',
|
||||||
|
'max_message_length' => 240
|
||||||
|
];
|
||||||
|
|
||||||
//Securimage captcha
|
//Securimage captcha
|
||||||
//Note from lainchan PR: "TODO move a bunch of things here"
|
//Note from lainchan PR: "TODO move a bunch of things here"
|
||||||
|
|
88
post.php
88
post.php
|
@ -229,6 +229,64 @@ 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";
|
||||||
|
|
||||||
|
$end = strlen($post['body_nomarkup']) > $max_msg_len ? ' [...]' : '';
|
||||||
|
$post_content = mb_substr($post['body_nomarkup'], 0, $max_msg_len) . $end;
|
||||||
|
$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' => $text_body
|
||||||
|
]);
|
||||||
|
|
||||||
|
$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);
|
||||||
|
$err_str = curl_error($ch);
|
||||||
|
|
||||||
|
error_log("Failed to send report to matrix. Curl returned: $err_no ($err_str)");
|
||||||
|
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'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes the (single) captcha associated with the ip and code.
|
* Deletes the (single) captcha associated with the ip and code.
|
||||||
*
|
*
|
||||||
|
@ -742,25 +800,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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue