From 8e5124812f8b3bcf6d1f3df2ffd722d5698d5885 Mon Sep 17 00:00:00 2001 From: tslocum Date: Sat, 4 Dec 2010 00:45:29 -0800 Subject: [PATCH] SQLite support --- .gitignore | 1 + README | 2 +- imgboard.php | 2 + inc/database_sqlite.php | 189 ++++++++++++++++++++++++++++++++++++++++ settings.default.php | 6 +- 5 files changed, 196 insertions(+), 4 deletions(-) create mode 100644 inc/database_sqlite.php diff --git a/.gitignore b/.gitignore index 9471da0..6153208 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ settings.php *.html +*.db .posts .posts.lock \ No newline at end of file diff --git a/README b/README index 1b6c25a..7e7efbd 100644 --- a/README +++ b/README @@ -1,7 +1,7 @@ TinyIB by tslocum http://tslocum.github.com/ -Supports MySQL and flat file database modes. +Supports MySQL, SQLite, and flat file database modes. To install TinyIB: - CD to the directory you wish to install TinyIB diff --git a/imgboard.php b/imgboard.php index 15daabb..d0307aa 100644 --- a/imgboard.php +++ b/imgboard.php @@ -36,6 +36,8 @@ if (TINYIB_DBMODE == 'flatfile') { $includes[] = 'inc/database_flatfile.php'; } elseif (TINYIB_DBMODE == 'mysql') { $includes[] = 'inc/database_mysql.php'; +} elseif (TINYIB_DBMODE == 'sqlite') { + $includes[] = 'inc/database_sqlite.php'; } else { fancyDie("Unknown database mode specificed"); } diff --git a/inc/database_sqlite.php b/inc/database_sqlite.php new file mode 100644 index 0000000..6719816 --- /dev/null +++ b/inc/database_sqlite.php @@ -0,0 +1,189 @@ + 0; +} + +function insertPost($post) { + sqlite_query($GLOBALS["db"], "INSERT INTO " . TINYIB_DBPOSTS . " (parent, timestamp, bumped, ip, name, tripcode, email, nameblock, subject, message, password, file, file_hex, file_original, file_size, file_size_formatted, image_width, image_height, thumb, thumb_width, thumb_height) VALUES (" . $post['parent'] . ", " . time() . ", " . time() . ", '" . $_SERVER['REMOTE_ADDR'] . "', '" . sqlite_escape_string($post['name']) . "', '" . sqlite_escape_string($post['tripcode']) . "', '" . sqlite_escape_string($post['email']) . "', '" . sqlite_escape_string($post['nameblock']) . "', '" . sqlite_escape_string($post['subject']) . "', '" . sqlite_escape_string($post['message']) . "', '" . sqlite_escape_string($post['password']) . "', '" . $post['file'] . "', '" . $post['file_hex'] . "', '" . sqlite_escape_string($post['file_original']) . "', " . $post['file_size'] . ", '" . $post['file_size_formatted'] . "', " . $post['image_width'] . ", " . $post['image_height'] . ", '" . $post['thumb'] . "', " . $post['thumb_width'] . ", " . $post['thumb_height'] . ")"); + return sqlite_last_insert_rowid($GLOBALS["db"]); +} + +function bumpThreadByID($id) { + sqlite_query($GLOBALS["db"], "UPDATE " . TINYIB_DBPOSTS . " SET bumped = " . time() . " WHERE id = " . $id); +} + +function countThreads() { + return sqlite_fetch_single(sqlite_query($GLOBALS["db"], "SELECT COUNT(*) FROM " . TINYIB_DBPOSTS . " WHERE parent = 0")); +} + +function allThreads() { + $threads = array(); + $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT * FROM " . TINYIB_DBPOSTS . " WHERE parent = 0 ORDER BY bumped DESC"), SQLITE_ASSOC); + foreach ($result as $thread) { + $threads[] = $thread; + } + return $threads; +} + +function postsInThreadByID($id) { + $posts = array(); + $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT * FROM " . TINYIB_DBPOSTS . " WHERE id = " . $id . " OR parent = " . $id . " ORDER BY id ASC"), SQLITE_ASSOC); + foreach ($result as $post) { + $posts[] = $post; + } + return $posts; +} + +function latestRepliesInThreadByID($id) { + $posts = array(); + $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT * FROM " . TINYIB_DBPOSTS . " WHERE parent = " . $id . " ORDER BY id DESC LIMIT 3"), SQLITE_ASSOC); + foreach ($result as $post) { + $posts[] = $post; + } + return $posts; +} + +function postsByHex($hex) { + $posts = array(); + $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT id, parent FROM " . TINYIB_DBPOSTS . " WHERE file_hex = '" . sqlite_escape_string($hex) . "' LIMIT 1"), SQLITE_ASSOC); + foreach ($result as $post) { + $posts[] = $post; + } + return $posts; +} + +function deletePostByID($id) { + $posts = postsInThreadByID($id); + foreach ($posts as $post) { + if ($post['id'] != $id) { + deletePostImages($post); + sqlite_query($GLOBALS["db"], "DELETE FROM " . TINYIB_DBPOSTS . " WHERE id = " . $post['id']); + } else { + $thispost = $post; + } + } + if (isset($thispost)) { + if ($thispost['parent'] == 0) { + @unlink('res/' . $thispost['id'] . '.html'); + } + deletePostImages($thispost); + sqlite_query($GLOBALS["db"], "DELETE FROM " . TINYIB_DBPOSTS . " WHERE id = " . $thispost['id']); + } +} + +function trimThreads() { + if (TINYIB_MAXTHREADS > 0) { + $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT id FROM " . TINYIB_DBPOSTS . " WHERE parent = 0 ORDER BY bumped DESC LIMIT " . TINYIB_MAXTHREADS. ", 10"), SQLITE_ASSOC); + foreach ($result as $post) { + deletePostByID($post['id']); + } + } +} + +function lastPostByIP() { + $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT * FROM " . TINYIB_DBPOSTS . " WHERE ip = '" . $_SERVER['REMOTE_ADDR'] . "' ORDER BY id DESC LIMIT 1"), SQLITE_ASSOC); + foreach ($result as $post) { + return $post; + } +} + +# 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) { + return $ban; + } +} + +function banByIP($ip) { + $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT * FROM " . TINYIB_DBBANS . " WHERE ip = '" . sqlite_escape_string($ip) . "' LIMIT 1"), SQLITE_ASSOC); + foreach ($result as $ban) { + return $ban; + } +} + +function allBans() { + $bans = array(); + $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT * FROM " . TINYIB_DBBANS . " ORDER BY timestamp DESC"), SQLITE_ASSOC); + foreach ($result as $ban) { + $bans[] = $ban; + } + return $bans; +} + +function insertBan($ban) { + sqlite_query($GLOBALS["db"], "INSERT INTO " . TINYIB_DBBANS . " (ip, timestamp, expire, reason) VALUES ('" . sqlite_escape_string($ban['ip']) . "', " . time() . ", '" . sqlite_escape_string($ban['expire']) . "', '" . sqlite_escape_string($ban['reason']) . "')"); + return sqlite_last_insert_rowid($GLOBALS["db"]); +} + +function clearExpiredBans() { + $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT * FROM " . TINYIB_DBBANS . " WHERE expire > 0 AND expire <= " . time()), SQLITE_ASSOC); + foreach ($result as $ban) { + sqlite_query($GLOBALS["db"], "DELETE FROM " . TINYIB_DBBANS . " WHERE id = " . $ban['id']); + } +} + +function deleteBanByID($id) { + sqlite_query($GLOBALS["db"], "DELETE FROM " . TINYIB_DBBANS . " WHERE id = " . sqlite_escape_string($id)); +} + +?> \ No newline at end of file diff --git a/settings.default.php b/settings.default.php index d27e96c..db38fb2 100644 --- a/settings.default.php +++ b/settings.default.php @@ -1,4 +1,4 @@ -