From 4223e82c66882fcf31f0e4133298da8d8ce6ab72 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Wed, 12 Aug 2020 09:28:41 -0700 Subject: [PATCH] Tweak JSON changes --- imgboard.php | 6 ++-- inc/database_flatfile.php | 8 ++--- inc/database_mysql.php | 4 +-- inc/database_mysqli.php | 4 +-- inc/database_pdo.php | 19 +++++----- inc/database_sqlite.php | 4 +-- inc/database_sqlite3.php | 4 +-- inc/html.php | 70 +++++++++++++++++++++++++++++++++--- inc/json.php | 76 --------------------------------------- settings.default.php | 31 ++++++++-------- 10 files changed, 106 insertions(+), 120 deletions(-) delete mode 100644 inc/json.php diff --git a/imgboard.php b/imgboard.php index 6d74b45..aa1ff2d 100644 --- a/imgboard.php +++ b/imgboard.php @@ -1,6 +1,7 @@ +TinyIB +https://gitlab.com/tslocum/tinyib MIT License @@ -79,9 +80,6 @@ foreach ($writedirs as $dir) { } $includes = array("inc/defines.php", "inc/functions.php", "inc/html.php"); -if (TINYIB_JSON) { - $includes[] = 'inc/json.php'; -} if (in_array(TINYIB_DBMODE, array('flatfile', 'mysql', 'mysqli', 'sqlite', 'sqlite3', 'pdo'))) { $includes[] = 'inc/database_' . TINYIB_DBMODE . '.php'; } else { diff --git a/inc/database_flatfile.php b/inc/database_flatfile.php index 7fd2fa1..5d6a36b 100644 --- a/inc/database_flatfile.php +++ b/inc/database_flatfile.php @@ -3,7 +3,7 @@ if (!defined('TINYIB_BOARD')) { die(''); } -# Post Structure +// Post Structure define('POSTS_FILE', '.posts'); define('POST_ID', 0); define('POST_PARENT', 1); @@ -30,7 +30,7 @@ define('POST_THUMB_HEIGHT', 21); define('POST_STICKIED', 22); define('POST_LOCKED', 23); -# Ban Structure +// Ban Structure define('BANS_FILE', '.bans'); define('BAN_ID', 0); define('BAN_IP', 1); @@ -42,7 +42,7 @@ require_once 'flatfile/flatfile.php'; $db = new Flatfile(); $db->datadir = 'inc/flatfile/'; -# Post Functions +// Post Functions function uniquePosts() { return 0; // Unsupported by this database option } @@ -242,7 +242,7 @@ function lastPostByIP() { return convertPostsToSQLStyle($rows, true); } -# Ban Functions +// Ban Functions function banByID($id) { return convertBansToSQLStyle($GLOBALS['db']->selectWhere(BANS_FILE, new SimpleWhereClause(BAN_ID, '=', $id, INTEGER_COMPARISON), 1), true); } diff --git a/inc/database_mysql.php b/inc/database_mysql.php index 86e08b4..540e4fd 100644 --- a/inc/database_mysql.php +++ b/inc/database_mysql.php @@ -35,7 +35,7 @@ if (mysql_num_rows(mysql_query("SHOW COLUMNS FROM `" . TINYIB_DBPOSTS . "` LIKE mysql_query("ALTER TABLE `" . TINYIB_DBPOSTS . "` ADD COLUMN locked TINYINT(1) NOT NULL DEFAULT '0'"); } -# Post Functions +// Post Functions function uniquePosts() { $row = mysql_fetch_row(mysql_query("SELECT COUNT(DISTINCT(`ip`)) FROM " . TINYIB_DBPOSTS)); return $row[0]; @@ -177,7 +177,7 @@ function lastPostByIP() { } } -# Ban Functions +// Ban Functions function banByID($id) { $result = mysql_query("SELECT * FROM `" . TINYIB_DBBANS . "` WHERE `id` = '" . mysql_real_escape_string($id) . "' LIMIT 1"); if ($result) { diff --git a/inc/database_mysqli.php b/inc/database_mysqli.php index c7852db..16ca01c 100644 --- a/inc/database_mysqli.php +++ b/inc/database_mysqli.php @@ -35,7 +35,7 @@ if (mysqli_num_rows(mysqli_query($link, "SHOW COLUMNS FROM `" . TINYIB_DBPOSTS . mysqli_query($link,"ALTER TABLE `" . TINYIB_DBPOSTS . "` ADD COLUMN locked TINYINT(1) NOT NULL DEFAULT '0'"); } -# Post Functions +// Post Functions function uniquePosts() { global $link; $row = mysqli_fetch_row(mysqli_query($link, "SELECT COUNT(DISTINCT(`ip`)) FROM " . TINYIB_DBPOSTS)); @@ -194,7 +194,7 @@ function lastPostByIP() { } } -# Ban Functions +// Ban Functions function banByID($id) { global $link; $result = mysqli_query($link, "SELECT * FROM `" . TINYIB_DBBANS . "` WHERE `id` = '" . mysqli_real_escape_string($link, $id) . "' LIMIT 1"); diff --git a/inc/database_pdo.php b/inc/database_pdo.php index d7f1c69..ff74672 100644 --- a/inc/database_pdo.php +++ b/inc/database_pdo.php @@ -78,7 +78,7 @@ if (!$stickied_exists) { $dbh->exec("ALTER TABLE `" . TINYIB_DBPOSTS . "` ADD COLUMN locked TINYINT(1) NOT NULL DEFAULT '0'"); } -# Utility +// Utility function pdoQuery($sql, $params = false) { global $dbh; @@ -92,7 +92,7 @@ function pdoQuery($sql, $params = false) { return $statement; } -# Post Functions +// Post Functions function uniquePosts() { $result = pdoQuery("SELECT COUNT(DISTINCT(ip)) FROM " . TINYIB_DBPOSTS); return (int)$result->fetchColumn(); @@ -218,12 +218,13 @@ function deletePostByID($id) { function trimThreads() { $limit = (int)TINYIB_MAXTHREADS; if ($limit > 0) { - $results = pdoQuery("SELECT id FROM " . TINYIB_DBPOSTS . " WHERE parent = 0 AND moderated = 1 ORDER BY stickied DESC, bumped DESC LIMIT 100 OFFSET " . $limit - ); - # old mysql, sqlite3: SELECT id FROM $table ORDER BY bumped LIMIT $limit,100 - # mysql, postgresql, sqlite3: SELECT id FROM $table ORDER BY bumped LIMIT 100 OFFSET $limit - # oracle: SELECT id FROM ( SELECT id, rownum FROM $table ORDER BY bumped) WHERE rownum >= $limit - # MSSQL: WITH ts AS (SELECT ROWNUMBER() OVER (ORDER BY bumped) AS 'rownum', * FROM $table) SELECT id FROM ts WHERE rownum >= $limit + $results = pdoQuery("SELECT id FROM " . TINYIB_DBPOSTS . " WHERE parent = 0 AND moderated = 1 ORDER BY stickied DESC, bumped DESC LIMIT 100 OFFSET " . $limit); + /* + old mysql, sqlite3: SELECT id FROM $table ORDER BY bumped LIMIT $limit,100 + mysql, postgresql, sqlite3: SELECT id FROM $table ORDER BY bumped LIMIT 100 OFFSET $limit + oracle: SELECT id FROM ( SELECT id, rownum FROM $table ORDER BY bumped) WHERE rownum >= $limit + MSSQL: WITH ts AS (SELECT ROWNUMBER() OVER (ORDER BY bumped) AS 'rownum', * FROM $table) SELECT id FROM ts WHERE rownum >= $limit + */ foreach ($results as $post) { deletePostByID($post['id']); } @@ -235,7 +236,7 @@ function lastPostByIP() { return $result->fetch(PDO::FETCH_ASSOC); } -# Ban Functions +// Ban Functions function banByID($id) { $result = pdoQuery("SELECT * FROM " . TINYIB_DBBANS . " WHERE id = ?", array($id)); return $result->fetch(PDO::FETCH_ASSOC); diff --git a/inc/database_sqlite.php b/inc/database_sqlite.php index 8bcb3a8..9728068 100644 --- a/inc/database_sqlite.php +++ b/inc/database_sqlite.php @@ -59,7 +59,7 @@ sqlite_query($db, "ALTER TABLE " . TINYIB_DBPOSTS . " ADD COLUMN stickied INTEGE // Add locked column if it isn't present sqlite_query($db, "ALTER TABLE " . TINYIB_DBPOSTS . " ADD COLUMN locked INTEGER NOT NULL DEFAULT '0'"); -# Post Functions +// Post Functions function uniquePosts() { return sqlite_fetch_single(sqlite_query($GLOBALS["db"], "SELECT COUNT(ip) FROM (SELECT DISTINCT ip FROM " . TINYIB_DBPOSTS . ")")); } @@ -182,7 +182,7 @@ function lastPostByIP() { } } -# Ban Functions +// Ban Functions function banByID($id) { $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT * FROM " . TINYIB_DBBANS . " WHERE id = '" . sqlite_escape_string($id) . "' LIMIT 1"), SQLITE_ASSOC); foreach ($result as $ban) { diff --git a/inc/database_sqlite3.php b/inc/database_sqlite3.php index f2111d8..c3714f9 100644 --- a/inc/database_sqlite3.php +++ b/inc/database_sqlite3.php @@ -60,7 +60,7 @@ if (!$result->fetchArray()) { // Add locked column if it isn't present @$db->exec("ALTER TABLE " . TINYIB_DBPOSTS . " ADD COLUMN locked INTEGER NOT NULL DEFAULT '0'"); -# Post Functions +// Post Functions function uniquePosts() { global $db; return $db->querySingle("SELECT COUNT(ip) FROM (SELECT DISTINCT ip FROM " . TINYIB_DBPOSTS . ")"); @@ -199,7 +199,7 @@ function lastPostByIP() { } } -# Ban Functions +// Ban Functions function banByID($id) { global $db; $result = $db->query("SELECT * FROM " . TINYIB_DBBANS . " WHERE id = '" . $db->escapeString($id) . "' LIMIT 1"); diff --git a/inc/html.php b/inc/html.php index 543f07d..d955a8b 100644 --- a/inc/html.php +++ b/inc/html.php @@ -623,10 +623,6 @@ function rebuildIndexes() { $i = 0; $htmlposts = ''; } - - if (TINYIB_JSON) { - writePage("res/" . $thread['id'] . '.json', buildThreadNoJSON($thread['id'])); - } } if ($page == 0 || $htmlposts != '') { @@ -639,12 +635,14 @@ function rebuildIndexes() { } if (TINYIB_JSON) { - writePage('threads.json', buildThreadsJSON()); + writePage('threads.json', buildIndexJSON()); writePage('catalog.json', buildCatalogJSON()); } } function rebuildThread($id) { + $id = intval($id); + $htmlposts = ""; $posts = postsInThreadByID($id); foreach ($posts as $post) { @@ -654,6 +652,10 @@ function rebuildThread($id) { $htmlposts .= "\n
"; writePage('res/' . $id . '.html', fixLinksInRes(buildPage($htmlposts, $id))); + + if (TINYIB_JSON) { + writePage('res/' . $id . '.json', buildSingleThreadJSON($id)); + } } function adminBar() { @@ -943,3 +945,61 @@ EOF; function manageInfo($text) { return '
' . $text . '
'; } + +function encodeJSON($array) { + if (version_compare(phpversion(), '5.4.0', '>')) { + return json_encode($array, JSON_PRETTY_PRINT); + } + return json_encode($array); +} + +function buildSinglePostJSON($post) { + $name = $post['name']; + if ($name == '') { + $name = 'Anonymous'; + } + + $output = array('id' => $post['id'], 'parent' => $post['parent'], 'timestamp' => $post['timestamp'], 'bumped' => $post['bumped'], 'name' => $name, 'tripcode' => $post['tripcode'], 'subject' => $post['subject'], 'message' => $post['message'], 'file' => $post['file'], 'file_hex' => $post['file_hex'], 'file_original' => $post['file_original'], 'file_size' => $post['file_size'], 'file_size_formated' => $post['file_size_formatted'], 'image_width' => $post['image_width'], 'image_height' => $post['image_height'], 'thumb' => $post['thumb'], 'thumb_width' => $post['thumb_width'], 'thumb_height' => $post['thumb_height']); + + if($post['parent'] == TINYIB_NEWTHREAD) { + $replies = count(postsInThreadByID($post['id'])) - 1; + $images = imagesInThreadByID($post['id']); + + $output = array_merge($output, array('stickied' => $post['stickied'], 'locked' => $post['locked'], 'replies' => $replies, 'images' => $images)); + } + + return $output; +} + +function buildIndexJSON() { + $output = array('threads' => array()); + + $threads = allThreads(); + foreach ($threads as $thread) { + array_push($output['threads'], array('id' => $thread['id'], 'subject' => $thread['subject'], 'bumped' => $thread['bumped'])); + } + + return encodeJSON($output); +} + +function buildCatalogJSON() { + $output = array('threads' => array()); + + $threads = allThreads(); + foreach ($threads as $post) { + array_push($output['threads'], buildSinglePostJSON($post)); + } + + return encodeJSON($output); +} + +function buildSingleThreadJSON($id) { + $output = array('posts' => array()); + + $posts = postsInThreadByID($id); + foreach ($posts as $post) { + array_push($output['posts'], buildSinglePostJSON($post)); + } + + return encodeJSON($output); +} \ No newline at end of file diff --git a/inc/json.php b/inc/json.php deleted file mode 100644 index 2ce210e..0000000 --- a/inc/json.php +++ /dev/null @@ -1,76 +0,0 @@ - $thread['id'], 'subject' => $thread['subject'], 'bumped' => $thread['bumped'])); - } - - $threads_json[] = $output; - if (version_compare(phpversion(), '5.4.0', '>')) { - return json_encode($threads_json, JSON_PRETTY_PRINT); - } else { - return json_encode($threads_json); - } -} - -function buildCatalogJSON() { - $output['threads'] = array(); - - $threads = allThreads(); - - foreach ($threads as $thread) { - $replies = postsInThreadByID($thread['id']); - $images = imagesInThreadByID($thread['id']); - - if($thread['name'] == '') { - array_push($output['threads'], array('id' => $thread['id'], 'parent' => $thread['parent'], 'timestamp' => $thread['timestamp'], 'bumped' => $thread['bumped'], 'name' => 'Anonymous', 'tripcode' => $thread['tripcode'], 'subject' => $thread['subject'], 'message' => $thread['message'], 'file' => $thread['file'], 'file_hex' => $thread['file_hex'], 'file_original' => $thread['file_original'], 'file_size' => $thread['file_size'], 'file_size_formated' => $thread['file_size_formatted'], 'image_width' => $thread['image_width'], 'image_height' => $thread['image_height'], 'thumb' => $thread['thumb'], 'thumb_width' => $thread['thumb_width'], 'thumb_height' => $thread['thumb_height'], 'stickied' => $thread['stickied'], 'moderated' => $thread['moderated'], 'locked' => $thread['locked'], 'replies' => count($replies) - 1, 'images' => $images)); - } else { - array_push($output['threads'], array('id' => $thread['id'], 'parent' => $thread['parent'], 'timestamp' => $thread['timestamp'], 'bumped' => $thread['bumped'], 'name' => $thread['name'], 'tripcode' => $thread['tripcode'], 'subject' => $thread['subject'], 'message' => $thread['message'], 'file' => $thread['file'], 'file_hex' => $thread['file_hex'], 'file_original' => $thread['file_original'], 'file_size' => $thread['file_size'], 'file_size_formated' => $thread['file_size_formatted'], 'image_width' => $thread['image_width'], 'image_height' => $thread['image_height'], 'thumb' => $thread['thumb'], 'thumb_width' => $thread['thumb_width'], 'thumb_height' => $thread['thumb_height'], 'stickied' => $thread['stickied'], 'moderated' => $thread['moderated'], 'locked' => $thread['locked'], 'replies' => count($replies) - 1, 'images' => $images)); - } - } - - $threads_json[] = $output; - if (version_compare(phpversion(), '5.4.0', '>')) { - return json_encode($threads_json, JSON_PRETTY_PRINT); - } else { - return json_encode($threads_json); - } -} - -function buildThreadNoJSON($id) { - $output = array(); - - $threads = allThreads(); - - foreach ($threads as $thread) { - $replies = postsInThreadByID($id); - if($thread['parent'] == 0 && $thread['id'] == $id) { - if($thread['name'] == '') { - $output = array('posts' => [array('id' => $thread['id'], 'parent' => $thread['parent'], 'timestamp' => $thread['timestamp'], 'bumped' => $thread['bumped'], 'name' => 'Anonymous', 'tripcode' => $thread['tripcode'], 'subject' => $thread['subject'], 'message' => $thread['message'], 'file' => $thread['file'], 'file_hex' => $thread['file_hex'], 'file_original' => $thread['file_original'], 'file_size' => $thread['file_size'], 'file_size_formated' => $thread['file_size_formatted'], 'image_width' => $thread['image_width'], 'image_height' => $thread['image_height'], 'thumb' => $thread['thumb'], 'thumb_width' => $thread['thumb_width'], 'thumb_height' => $thread['thumb_height'], 'stickied' => $thread['stickied'], 'moderated' => $thread['moderated'], 'locked' => $thread['locked'])]); - } else { - $output = array('posts' => [array('id' => $thread['id'], 'parent' => $thread['parent'], 'timestamp' => $thread['timestamp'], 'bumped' => $thread['bumped'], 'name' => $thread['name'], 'tripcode' => $thread['tripcode'], 'subject' => $thread['subject'], 'message' => $thread['message'], 'file' => $thread['file'], 'file_hex' => $thread['file_hex'], 'file_original' => $thread['file_original'], 'file_size' => $thread['file_size'], 'file_size_formated' => $thread['file_size_formatted'], 'image_width' => $thread['image_width'], 'image_height' => $thread['image_height'], 'thumb' => $thread['thumb'], 'thumb_width' => $thread['thumb_width'], 'thumb_height' => $thread['thumb_height'], 'stickied' => $thread['stickied'], 'moderated' => $thread['moderated'], 'locked' => $thread['locked'])]); - } - } - } - foreach ($replies as $reply) { - if($reply['parent'] == $id) { - if($thread['name'] == '') { - array_push($output['posts'], array('id' => $reply['id'], 'parent' => $reply['parent'], 'timestamp' => $reply['timestamp'], 'bumped' => $reply['bumped'], 'name' => 'Anonymous', 'tripcode' => $reply['tripcode'], 'subject' => $reply['subject'], 'message' => $reply['message'], 'file' => $reply['file'], 'file_hex' => $reply['file_hex'], 'file_original' => $reply['file_original'], 'file_size' => $reply['file_size'], 'file_size_formated' => $reply['file_size_formatted'], 'image_width' => $reply['image_width'], 'image_height' => $reply['image_height'], 'thumb' => $reply['thumb'], 'thumb_width' => $reply['thumb_width'], 'thumb_height' => $reply['thumb_height'], 'moderated' => $reply['moderated'])); - } else { - array_push($output['posts'], array('id' => $reply['id'], 'parent' => $reply['parent'], 'timestamp' => $reply['timestamp'], 'bumped' => $reply['bumped'], 'name' => $reply['name'], 'tripcode' => $reply['tripcode'], 'subject' => $reply['subject'], 'message' => $reply['message'], 'file' => $reply['file'], 'file_hex' => $reply['file_hex'], 'file_original' => $reply['file_original'], 'file_size' => $reply['file_size'], 'file_size_formated' => $reply['file_size_formatted'], 'image_width' => $reply['image_width'], 'image_height' => $reply['image_height'], 'thumb' => $reply['thumb'], 'thumb_width' => $reply['thumb_width'], 'thumb_height' => $reply['thumb_height'], 'moderated' => $reply['moderated'])); - } - } - } - - if (version_compare(phpversion(), '5.4.0', '>')) { - return json_encode($output, JSON_PRETTY_PRINT); - } else { - return json_encode($output); - } -} \ No newline at end of file diff --git a/settings.default.php b/settings.default.php index 3e0821b..78d6118 100644 --- a/settings.default.php +++ b/settings.default.php @@ -1,12 +1,15 @@ array('jpg'), 'image/pjpeg' => array('jpg'), 'image/png' => array('png'), 'image/gif' => array('gif')); -# 'application/x-shockwave-flash' => array('swf', 'swf_thumbnail.png'); -# 'video/webm' => array('webm'); // Video uploads require mediainfo and ffmpegthumbnailer (see README for instructions) -# 'audio/webm' => array('webm'); -# 'video/mp4' => array('mp4'); -# 'audio/mp4' => array('mp4'); +// 'application/x-shockwave-flash' => array('swf', 'swf_thumbnail.png'); +// 'video/webm' => array('webm'); // Video uploads require mediainfo and ffmpegthumbnailer (see README for instructions) +// 'audio/webm' => array('webm'); +// 'video/mp4' => array('mp4'); +// 'audio/mp4' => array('mp4'); // oEmbed APIs // Empty array to disable