From f20c62b54cc35b78f56196ef35b031033255f644 Mon Sep 17 00:00:00 2001 From: Pietro Carrara Date: Sat, 3 Jul 2021 00:27:25 -0300 Subject: [PATCH 1/6] List boards --- status.php | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/status.php b/status.php index a42530a8..dcda0af3 100644 --- a/status.php +++ b/status.php @@ -2,8 +2,55 @@ require_once 'inc/functions.php'; +function endsWith( $haystack, $needle ) { + $length = strlen( $needle ); + if( !$length ) { + return true; + } + return substr( $haystack, -$length ) === $needle; +} + +// Boards that are nsfw +$nsfw_boards = ['b', 'overboard']; +// Boards that use spoiler_alunya.png as their spoiler +$alunya_spoiler = ['leftypol', 'anime']; +// Boards where posts are not allowed to be created +$readonly_boards = ['overboard', 'sfw', 'alt']; + +$board_list = listBoards(); + +// Add objects that are not boards but are treated as such +$board_list[] = ['uri' => 'overboard', 'title' => 'Overboard']; +$board_list[] = ['uri' => 'sfw', 'title' => 'SFW Overboard']; +$board_list[] = ['uri' => 'alt', 'title' => 'Alternate Overboard']; + +/** + * Allowed fields for the board object: + * - code: The board code ('b', 'tech', ...) + * - name: The board user-readable name ('Siberia', ...) + * - description: The board description ('Leftist Politically Incorrect', ...) + * - sfw: Is this board sfw? + * - alternate_spoilers: Does this board use the alunya spoiler? + */ +$boards = []; +foreach ($board_list as $board) { + // Skip archives + if (endsWith($board['uri'], '_archive')) { + continue; + } + + $boards[] = [ + 'code' => $board['uri'], + 'name' => $board['title'], + 'sfw' => !in_array($board['uri'], $nsfw_boards), + 'alternate_spoilers' => in_array($board['uri'], $alunya_spoiler), + 'posting_enabled' => !in_array($board['uri'], $readonly_boards), + ]; +} + header('Content-Type: application/json'); echo json_encode([ 'captcha' => $config['securimage'], - 'flags' => $config['user_flags'] + 'flags' => $config['user_flags'], + 'boards' => $boards, ]); \ No newline at end of file From 2252634892bfe19af30adda7d564a77a11783b5a Mon Sep 17 00:00:00 2001 From: Pietro Carrara Date: Sun, 4 Jul 2021 11:03:41 -0300 Subject: [PATCH 2/6] Modify api image fields --- inc/api.php | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/inc/api.php b/inc/api.php index 52419445..1c6552f6 100644 --- a/inc/api.php +++ b/inc/api.php @@ -45,8 +45,10 @@ class Api { ); $this->fileFields = array( - 'thumbheight' => 'tn_h', - 'thumbwidth' => 'tn_w', + 'file_id' => 'id', + 'file_path' => 'file_path', + 'type' => 'mime', + 'extension' => 'ext', 'height' => 'h', 'width' => 'w', 'size' => 'fsize', @@ -90,13 +92,12 @@ class Api { } private function translateFile($file, $post, &$apiPost) { + global $config; + $this->translateFields($this->fileFields, $file, $apiPost); $apiPost['filename'] = @substr($file->name, 0, strrpos($file->name, '.')); - $dotPos = strrpos($file->file, '.'); - $apiPost['ext'] = substr($file->file, $dotPos); - $apiPost['tim'] = substr($file->file, 0, $dotPos); if (isset ($file->thumb) && $file->thumb) { - $apiPost['spoiler'] = $file->thumb === 'spoiler' ? 1 : 0; + $apiPost['spoiler'] = $file->thumb === 'spoiler'; } if (isset ($file->hash) && $file->hash) { $apiPost['md5'] = base64_encode(hex2bin($file->hash)); @@ -104,6 +105,19 @@ class Api { else if (isset ($post->filehash) && $post->filehash) { $apiPost['md5'] = base64_encode(hex2bin($post->filehash)); } + + // Pick the correct thumbnail + if ($file->thumb_path === 'file') { + $ext = $file->extension; + $thumbFile = $config['file_icons']['default']; + if (isset($config['file_icons'][$ext])) { + $thumbFile = $config['file_icons'][$ext]; + } + + $apiPost['thumb_path'] = sprintf($config['file_thumb'], $thumbFile); + } else { + $apiPost['thumb_path'] = $file->thumb_path; + } } private function translatePost($post, $threadsPage = false) { @@ -138,21 +152,13 @@ class Api { } // Handle files - // Note: 4chan only supports one file, so only the first file is taken into account for 4chan-compatible API. if (isset($post->files) && $post->files && !$threadsPage) { - $file = $post->files[0]; - $this->translateFile($file, $post, $apiPost); - if (sizeof($post->files) > 1) { - $extra_files = array(); - foreach ($post->files as $i => $f) { - if ($i == 0) continue; + $apiPost['files'] = []; + foreach ($post->files as $f) { + $file = array(); + $this->translateFile($f, $post, $file); - $extra_file = array(); - $this->translateFile($f, $post, $extra_file); - - $extra_files[] = $extra_file; - } - $apiPost['extra_files'] = $extra_files; + $apiPost['files'][] = $file; } } From 1da08a57f7a48aad49ce99b9a27ccb734dfcb010 Mon Sep 17 00:00:00 2001 From: Pietro Carrara Date: Sun, 4 Jul 2021 12:11:14 -0300 Subject: [PATCH 3/6] Enhance status.php --- status.php | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/status.php b/status.php index dcda0af3..ef2e28f8 100644 --- a/status.php +++ b/status.php @@ -1,14 +1,13 @@ 'overboard', 'title' => 'Overboard']; -$board_list[] = ['uri' => 'sfw', 'title' => 'SFW Overboard']; -$board_list[] = ['uri' => 'alt', 'title' => 'Alternate Overboard']; +foreach ($overboards_config as $overboard) { + $board_list[] = $overboard; +} /** * Allowed fields for the board object: * - code: The board code ('b', 'tech', ...) * - name: The board user-readable name ('Siberia', ...) - * - description: The board description ('Leftist Politically Incorrect', ...) * - sfw: Is this board sfw? * - alternate_spoilers: Does this board use the alunya spoiler? + * - posting_enabled: Can new posts be created belonging to this board? */ $boards = []; foreach ($board_list as $board) { - // Skip archives - if (endsWith($board['uri'], '_archive')) { - continue; + // Skip blacklisted boards + foreach ($blacklist as $regex) { + if (preg_match($regex, $board['uri'])) { + continue 2; // Skip to the next board + } } $boards[] = [ From 293590a426c10aa3a21c90f81cd3eab21e7957bd Mon Sep 17 00:00:00 2001 From: Pietro Carrara Date: Sun, 4 Jul 2021 13:47:16 -0300 Subject: [PATCH 4/6] Use whitelist on boards --- status.php | 67 ++++++++++-------------------------------------------- 1 file changed, 12 insertions(+), 55 deletions(-) diff --git a/status.php b/status.php index 7b326de9..b02a650b 100644 --- a/status.php +++ b/status.php @@ -3,11 +3,16 @@ require_once 'inc/functions.php'; require_once 'templates/themes/overboards/overboards.php'; -// List of regexes that blacklist boards -$blacklist = [ - '/^.*_archive$/', // b_archive, leftypol_archive, ... - '/^b_.*$/', // b_anime, b_leftypol, ... -]; +// Allowed boards +$whitelist = []; +foreach ($config['boards'] as $boards) { + foreach ($boards as $board) { + $whitelist[] = $board; + } +} +foreach ($overboards_config as $board) { + $whitelist[] = $board['uri']; +} // Boards that are nsfw $nsfw_boards = ['b', 'overboard']; @@ -33,56 +38,8 @@ foreach ($overboards_config as $overboard) { */ $boards = []; foreach ($board_list as $board) { - // Skip blacklisted boards - foreach ($blacklist as $regex) { - if (preg_match($regex, $board['uri'])) { - continue 2; // Skip to the next board - } - } - - $boards[] = [ - 'code' => $board['uri'], - 'name' => $board['title'], - 'sfw' => !in_array($board['uri'], $nsfw_boards), - 'alternate_spoilers' => in_array($board['uri'], $alunya_spoiler), - 'posting_enabled' => !in_array($board['uri'], $readonly_boards), - ]; -} - -function endsWith( $haystack, $needle ) { - $length = strlen( $needle ); - if( !$length ) { - return true; - } - return substr( $haystack, -$length ) === $needle; -} - -// Boards that are nsfw -$nsfw_boards = ['b', 'overboard']; -// Boards that use spoiler_alunya.png as their spoiler -$alunya_spoiler = ['leftypol', 'anime']; -// Boards where posts are not allowed to be created -$readonly_boards = ['overboard', 'sfw', 'alt']; - -$board_list = listBoards(); - -// Add objects that are not boards but are treated as such -$board_list[] = ['uri' => 'overboard', 'title' => 'Overboard']; -$board_list[] = ['uri' => 'sfw', 'title' => 'SFW Overboard']; -$board_list[] = ['uri' => 'alt', 'title' => 'Alternate Overboard']; - -/** - * Allowed fields for the board object: - * - code: The board code ('b', 'tech', ...) - * - name: The board user-readable name ('Siberia', ...) - * - description: The board description ('Leftist Politically Incorrect', ...) - * - sfw: Is this board sfw? - * - alternate_spoilers: Does this board use the alunya spoiler? - */ -$boards = []; -foreach ($board_list as $board) { - // Skip archives - if (endsWith($board['uri'], '_archive')) { + // Skip non-whitelisted boards + if (!in_array($board['uri'], $whitelist)) { continue; } From 6d9867f5b4564857ce823bfdaf471336809a7ee2 Mon Sep 17 00:00:00 2001 From: Pietro Carrara Date: Sun, 4 Jul 2021 15:05:06 -0300 Subject: [PATCH 5/6] Use thumb, not thumb_path --- inc/api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/api.php b/inc/api.php index 1c6552f6..63e292d9 100644 --- a/inc/api.php +++ b/inc/api.php @@ -107,7 +107,7 @@ class Api { } // Pick the correct thumbnail - if ($file->thumb_path === 'file') { + if ($file->thumb === 'file') { $ext = $file->extension; $thumbFile = $config['file_icons']['default']; if (isset($config['file_icons'][$ext])) { @@ -116,7 +116,7 @@ class Api { $apiPost['thumb_path'] = sprintf($config['file_thumb'], $thumbFile); } else { - $apiPost['thumb_path'] = $file->thumb_path; + $apiPost['thumb_path'] = $config['uri_thumb'] . $file->thumb; } } From 91bbfccf17f6848c5541c5008abc01ab5bcc80c7 Mon Sep 17 00:00:00 2001 From: Pietro Carrara Date: Sun, 4 Jul 2021 15:42:47 -0300 Subject: [PATCH 6/6] Check fields, use config paths --- inc/api.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/inc/api.php b/inc/api.php index 63e292d9..9175ede2 100644 --- a/inc/api.php +++ b/inc/api.php @@ -46,7 +46,6 @@ class Api { $this->fileFields = array( 'file_id' => 'id', - 'file_path' => 'file_path', 'type' => 'mime', 'extension' => 'ext', 'height' => 'h', @@ -106,15 +105,16 @@ class Api { $apiPost['md5'] = base64_encode(hex2bin($post->filehash)); } + $apiPost['file_path'] = $config['uri_img'] . $file->file; + // Pick the correct thumbnail - if ($file->thumb === 'file') { - $ext = $file->extension; + if (!isset ($file->thumb) || $file->thumb === 'file') { $thumbFile = $config['file_icons']['default']; - if (isset($config['file_icons'][$ext])) { - $thumbFile = $config['file_icons'][$ext]; + if (isset($file->extension) && isset($config['file_icons'][$file->extension])) { + $thumbFile = $config['file_icons'][$file->extension]; } - $apiPost['thumb_path'] = sprintf($config['file_thumb'], $thumbFile); + $apiPost['thumb_path'] = $config['root'] . sprintf($config['file_thumb'], $thumbFile); } else { $apiPost['thumb_path'] = $config['uri_thumb'] . $file->thumb; }