diff --git a/imgboard.php b/imgboard.php index 1071f8e..4ce6fdc 100644 --- a/imgboard.php +++ b/imgboard.php @@ -42,7 +42,7 @@ foreach ($writedirs as $dir) { } $includes = array("inc/defines.php", "inc/functions.php", "inc/html.php"); -if (in_array(TINYIB_DBMODE, array('flatfile', 'mysql', 'sqlite'))) { +if (in_array(TINYIB_DBMODE, array('flatfile', 'mysql', 'mysqli', 'sqlite'))) { $includes[] = 'inc/database_' . TINYIB_DBMODE . '.php'; } else { fancyDie("Unknown database mode specificed"); diff --git a/inc/database_mysqli.php b/inc/database_mysqli.php new file mode 100644 index 0000000..568dc0f --- /dev/null +++ b/inc/database_mysqli.php @@ -0,0 +1,254 @@ + 0; +} + +function insertPost($post) { + global $link; + mysqli_query($link, "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'] . "', '" . mysqli_real_escape_string($link, $post['name']) . "', '" . mysqli_real_escape_string($link, $post['tripcode']) . "', '" . mysqli_real_escape_string($link, $post['email']) . "', '" . mysqli_real_escape_string($link, $post['nameblock']) . "', '" . mysqli_real_escape_string($link, $post['subject']) . "', '" . mysqli_real_escape_string($link, $post['message']) . "', '" . mysqli_real_escape_string($link, $post['password']) . "', '" . $post['file'] . "', '" . $post['file_hex'] . "', '" . mysqli_real_escape_string($link, $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 mysqli_insert_id($link); +} + +function bumpThreadByID($id) { + global $link; + mysqli_query($link, "UPDATE `" . TINYIB_DBPOSTS . "` SET `bumped` = " . time() . " WHERE `id` = " . $id . " LIMIT 1"); +} + +function countThreads() { + global $link; + return mysqli_result(mysqli_query($link, "SELECT COUNT(*) FROM `" . TINYIB_DBPOSTS . "` WHERE `parent` = 0"), 0, 0); +} + +function allThreads() { + global $link; + $threads = array(); + $result = mysqli_query($link, "SELECT * FROM `" . TINYIB_DBPOSTS . "` WHERE `parent` = 0 ORDER BY `bumped` DESC"); + if ($result) { + while ($thread = mysqli_fetch_assoc($result)) { + $threads[] = $thread; + } + } + return $threads; +} + +function numRepliesToThreadByID($id) { + global $link; + return mysqli_result(mysqli_query($link, "SELECT COUNT(*) FROM `" . TINYIB_DBPOSTS . "` WHERE `parent` = " . $id), 0, 0); +} + +function postsInThreadByID($id) { + global $link; + $posts = array(); + $result = mysqli_query($link, "SELECT * FROM `" . TINYIB_DBPOSTS . "` WHERE `id` = " . $id . " OR `parent` = " . $id . " ORDER BY `id` ASC"); + if ($result) { + while ($post = mysqli_fetch_assoc($result)) { + $posts[] = $post; + } + } + return $posts; +} + +function postsByHex($hex) { + global $link; + $posts = array(); + $result = mysqli_query($link, "SELECT `id`, `parent` FROM `" . TINYIB_DBPOSTS . "` WHERE `file_hex` = '" . mysqli_real_escape_string($link, $hex) . "' LIMIT 1"); + if ($result) { + while ($post = mysqli_fetch_assoc($result)) { + $posts[] = $post; + } + } + return $posts; +} + +function latestPosts() { + global $link; + $posts = array(); + $result = mysqli_query($link, "SELECT * FROM `" . TINYIB_DBPOSTS . "` ORDER BY `timestamp` DESC LIMIT 10"); + if ($result) { + while ($post = mysqli_fetch_assoc($result)) { + $posts[] = $post; + } + } + return $posts; +} + +function deletePostByID($id) { + global $link; + $posts = postsInThreadByID($id); + foreach ($posts as $post) { + if ($post['id'] != $id) { + deletePostImages($post); + mysqli_query($link, "DELETE FROM `" . TINYIB_DBPOSTS . "` WHERE `id` = " . $post['id'] . " LIMIT 1"); + } else { + $thispost = $post; + } + } + if (isset($thispost)) { + if ($thispost['parent'] == TINYIB_NEWTHREAD) { + @unlink('res/' . $thispost['id'] . '.html'); + } + deletePostImages($thispost); + mysqli_query($link, "DELETE FROM `" . TINYIB_DBPOSTS . "` WHERE `id` = " . $thispost['id'] . " LIMIT 1"); + } +} + +function trimThreads() { + global $link; + if (TINYIB_MAXTHREADS > 0) { + $result = mysqli_query($link, "SELECT `id` FROM `" . TINYIB_DBPOSTS . "` WHERE `parent` = 0 ORDER BY `bumped` DESC LIMIT " . TINYIB_MAXTHREADS . ", 10"); + if ($result) { + while ($post = mysqli_fetch_assoc($result)) { + deletePostByID($post['id']); + } + } + } +} + +function lastPostByIP() { + global $link; + $replies = mysqli_query($link, "SELECT * FROM `" . TINYIB_DBPOSTS . "` WHERE `ip` = '" . $_SERVER['REMOTE_ADDR'] . "' ORDER BY `id` DESC LIMIT 1"); + if ($replies) { + while ($post = mysqli_fetch_assoc($replies)) { + return $post; + } + } +} + +# 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"); + if ($result) { + while ($ban = mysqli_fetch_assoc($result)) { + return $ban; + } + } +} + +function banByIP($ip) { + global $link; + $result = mysqli_query($link, "SELECT * FROM `" . TINYIB_DBBANS . "` WHERE `ip` = '" . mysqli_real_escape_string($link, $ip) . "' LIMIT 1"); + if ($result) { + while ($ban = mysqli_fetch_assoc($result)) { + return $ban; + } + } +} + +function allBans() { + global $link; + $bans = array(); + $result = mysqli_query($link, "SELECT * FROM `" . TINYIB_DBBANS . "` ORDER BY `timestamp` DESC"); + if ($result) { + while ($ban = mysqli_fetch_assoc($result)) { + $bans[] = $ban; + } + } + return $bans; +} + +function insertBan($ban) { + global $link; + mysqli_query($link, "INSERT INTO `" . TINYIB_DBBANS . "` (`ip`, `timestamp`, `expire`, `reason`) VALUES ('" . mysqli_real_escape_string($link, $ban['ip']) . "')"); + return mysqli_insert_id($link); +} + +function clearExpiredBans() { + global $link; + $result = mysqli_query($link, "SELECT * FROM `" . TINYIB_DBBANS . "` WHERE `expire` > 0 AND `expire` <= " . time()); + if ($result) { + while ($ban = mysqli_fetch_assoc($result)) { + mysqli_query($link, "DELETE FROM `" . TINYIB_DBBANS . "` WHERE `id` = " . $ban['id'] . " LIMIT 1"); + } + } +} + +function deleteBanByID($id) { + global $link; + mysqli_query($link, "DELETE FROM `" . TINYIB_DBBANS . "` WHERE `id` = " . mysqli_real_escape_string($link, $id) . " LIMIT 1"); +} + +function mysqli_result($res, $row, $field = 0) { + $res->data_seek($row); + $datarow = $res->fetch_array(); + return $datarow[$field]; +} diff --git a/inc/flatfile/flatfile.php b/inc/flatfile/flatfile.php index 91dee7e..d94b66a 100644 --- a/inc/flatfile/flatfile.php +++ b/inc/flatfile/flatfile.php @@ -450,6 +450,7 @@ class SimpleWhereClause extends WhereClause { var $operator; var $value; var $compare_type; + /**#@-*/ /** diff --git a/inc/html.php b/inc/html.php index f9dab57..815cd27 100644 --- a/inc/html.php +++ b/inc/html.php @@ -577,6 +577,16 @@ function manageStatus() { $threads = countThreads(); $bans = count(allBans()); $info = $threads . ' ' . plural('thread', $threads) . ', ' . $bans . ' ' . plural('ban', $bans); + $output = ''; + + if ($isadmin && TINYIB_DBMODE == 'mysql' && function_exists('mysqli_connect')) { // Recommend MySQLi + $output .= << + Notice +

TINYIB_DBMODE is currently mysql in settings.php, but MySQLi is installed. Please change it to mysqli. This will not affect your data.

+ +EOF; + } $post_html = ''; $posts = latestPosts(); @@ -588,7 +598,7 @@ function manageStatus() { $post_html .= '' . buildPost($post, TINYIB_INDEXPAGE) . '
'; } - $output = << Status diff --git a/settings.default.php b/settings.default.php index 33629a8..658e7eb 100644 --- a/settings.default.php +++ b/settings.default.php @@ -18,9 +18,9 @@ define('TINYIB_LOGO', ""); // Logo HTML define('TINYIB_TRIPSEED', ""); // Enter some random text - Used when generating secure tripcodes - Must not change once set define('TINYIB_ADMINPASS', ""); // Text entered at the manage prompt to gain administrator access define('TINYIB_MODPASS', ""); // Moderators only have access to delete posts ["" to disable] -define('TINYIB_DBMODE', "flatfile"); // Choose: flatfile / mysql / sqlite (flatfile is not recommended for popular sites) +define('TINYIB_DBMODE', "flatfile"); // Choose: flatfile / mysql / mysqli / sqlite (flatfile is not recommended for popular sites) -// Note: The following only apply when TINYIB_DBMODE is set to mysql +// Note: The following only apply when TINYIB_DBMODE is set to mysql or mysqli (recommended) define('TINYIB_DBHOST', "localhost"); define('TINYIB_DBUSERNAME', ""); define('TINYIB_DBPASSWORD', "");