From a0fc3956fedc1bee26e247d4ac581d933f65ff03 Mon Sep 17 00:00:00 2001 From: towards-a-new-leftypol Date: Mon, 14 Aug 2023 22:35:55 -0400 Subject: [PATCH] Sends POST request to spamnoticer /add_post_to_known_spam --- inc/mod/pages.php | 12 +++++- inc/spamnoticer.php | 94 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 4 deletions(-) diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 1ceb8fad..d9887aa3 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -1778,7 +1778,7 @@ function parse_spamnoticer_content_fields($postData, $post): BanFormFieldsForSp return new BanFormFieldsForSpamnoticer($ban, $delete, $ban_content, $text_is_spam, $files_info); } -function mod_ban_post($board, $delete, $post_num, $token = false) { +function mod_ban_post(string $board, $delete, $post_num, $token = false) { global $config, $mod; if (!openBoard($board)) @@ -1809,7 +1809,15 @@ function mod_ban_post($board, $delete, $post_num, $token = false) { if (isset($_POST['spamnoticer'])) { $spamnoticer_info = parse_spamnoticer_content_fields($_POST, $po); - echo json_encode($spamnoticer_info); + $spamnoticer_result = addToSpamNoticer($config, $po, $board, $spamnoticer_info); + echo $spamnoticer_result; + echo json_encode( + array( + "spamnoticer_info" => $spamnoticer_info, + "board" => $board, + "po" => $po + ) + ); die(); return; } diff --git a/inc/spamnoticer.php b/inc/spamnoticer.php index 51d68595..102ae2c9 100644 --- a/inc/spamnoticer.php +++ b/inc/spamnoticer.php @@ -47,12 +47,103 @@ class BanFormFieldsForSpamnoticer { $this->ban = $ban; $this->delete = $delete; $this->ban_content = $ban_content; - $this->files_info = $files_info; $this->text_is_spam = $text_is_spam; + $this->files_info = array(); + + foreach ($files_info as $info) { + $this->files_info[$info->filename] = $info; + } } } +function getUsername() { + global $config; + + if (isset($_COOKIE[$config['cookies']['mod']])) { + // Should be username:hash:salt + $cookie = explode(':', $_COOKIE[$config['cookies']['mod']]); + return $cookie[0]; + } else { + return '__BOARD_MOD_USERNAME__'; + } +} + +function addToSpamNoticer($config, $post, $boardname, BanFormFieldsForSpamnoticer $form_info) { + global $board; + + $client = _createClient($config); + + $attachments = array(); + + foreach ($post->files as $file) { + $key = $file->file_id . '.' . $file->extension; + $file_info = $form_info->files_info[$key]; + $thumb_uri + = $config['spam_noticer']['imageboard_root'] + . $board['uri'] + . '/' . $config['dir']['thumb'] + . $file->file_id + . '.png'; + + $a = array( + 'filename' => $file->filename, + 'mimetype' => $file->type ? $file->type : mime_content_type($file->tmp_name), + 'md5_hash' => $file->hash, + 'thumbnail_url' => $thumb_uri, + 'is_spam' => $file_info->is_spam + ); + + if ($file_info->is_illegal) { + $a['is_illegal'] = true; + } + + $attachments[] = $a; + } + + $json_payload = [ + 'attachments' => $attachments, + 'body' => $post->body_nomarkup, + 'body_is_spam' => $form_info->text_is_spam, + 'time_stamp' => $post->time, + 'website_name' => $config['spam_noticer']['website_name'], + 'board_name' => $boardname, + 'thread_id' => isset($post->thread) ? $post->thread : NULL, + 'reporter_name' => getUsername() + ]; + + + try { + $multipart = array(); + $multipart[] = + array( + 'name' => 'json', + 'contents' => json_encode($json_payload) + ); + + foreach ($post->files as $file) { + $filename = $board['dir'] . $config['dir']['img'] . $file->file; + + $multipart[] = array( + 'name' => 'attachments', + 'contents' => GuzzleHttp\Psr7\Utils::tryFopen($filename, 'r') + ); + } + + + $response = $client->request('POST', '/add_post_to_known_spam', [ + 'multipart' => $multipart + ]); + + $status_code = $response->getStatusCode(); + $result_body = (string) $response->getBody(); + return "$status_code $result_body"; + + } catch (GuzzleHttp\Exception\ConnectException $e) { + return 'Spamnoticer Connection ERROR: ' . $e->getMessage(); + } +} + function checkWithSpamNoticer($config, $post, $boardname) { $client = _createClient($config); @@ -63,7 +154,6 @@ function checkWithSpamNoticer($config, $post, $boardname) { 'filename' => $file['filename'], 'mimetype' => $file['type'] ? $file['type'] : mime_content_type($file['tmp_name']), 'md5_hash' => $file['hash'], - 'thumbnail_url' => $config['spam_noticer']['imageboard_root'] . $file['thumb'] ); }