tinyib/inc/database/sqlite3.php
2020-10-14 09:37:15 -07:00

195 lines
6.2 KiB
PHP

<?php
if (!defined('TINYIB_BOARD')) {
die('');
}
// Post Functions
function uniquePosts() {
global $db;
return $db->querySingle("SELECT COUNT(ip) FROM (SELECT DISTINCT ip FROM " . TINYIB_DBPOSTS . ")");
}
function postByID($id) {
global $db;
$result = $db->query("SELECT * FROM " . TINYIB_DBPOSTS . " WHERE id = '" . $db->escapeString($id) . "' LIMIT 1");
while ($post = $result->fetchArray()) {
return $post;
}
}
function threadExistsByID($id) {
global $db;
return $db->querySingle("SELECT COUNT(*) FROM " . TINYIB_DBPOSTS . " WHERE id = '" . $db->escapeString($id) . "' AND parent = 0 LIMIT 1") > 0;
}
function insertPost($post) {
global $db;
$db->exec("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'] . "', '" . $db->escapeString($post['name']) . "', '" . $db->escapeString($post['tripcode']) . "', '" . $db->escapeString($post['email']) . "', '" . $db->escapeString($post['nameblock']) . "', '" . $db->escapeString($post['subject']) . "', '" . $db->escapeString($post['message']) . "', '" . $db->escapeString($post['password']) . "', '" . $post['file'] . "', '" . $post['file_hex'] . "', '" . $db->escapeString($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 $db->lastInsertRowID();
}
function approvePostByID($id) {
global $db;
$db->exec("UPDATE " . TINYIB_DBPOSTS . " SET moderated = 1 WHERE id = " . $id);
}
function bumpThreadByID($id) {
global $db;
$db->exec("UPDATE " . TINYIB_DBPOSTS . " SET bumped = " . time() . " WHERE id = " . $id);
}
function stickyThreadByID($id, $setsticky) {
global $db;
$db->exec("UPDATE " . TINYIB_DBPOSTS . " SET stickied = '" . $db->escapeString($setsticky) . "' WHERE id = " . $id);
}
function lockThreadByID($id, $setlock) {
global $db;
$db->exec("UPDATE " . TINYIB_DBPOSTS . " SET locked = '" . $db->escapeString($setlock) . "' WHERE id = " . $id);
}
function countThreads() {
global $db;
return $db->querySingle("SELECT COUNT(*) FROM " . TINYIB_DBPOSTS . " WHERE parent = 0");
}
function allThreads() {
global $db;
$threads = array();
$result = $db->query("SELECT * FROM " . TINYIB_DBPOSTS . " WHERE parent = 0 ORDER BY stickied DESC, bumped DESC");
while ($thread = $result->fetchArray()) {
$threads[] = $thread;
}
return $threads;
}
function numRepliesToThreadByID($id) {
global $db;
return $db->querySingle("SELECT COUNT(*) FROM " . TINYIB_DBPOSTS . " WHERE parent = " . $id);
}
function postsInThreadByID($id, $moderated_only = true) {
global $db;
$posts = array();
$result = $db->query("SELECT * FROM " . TINYIB_DBPOSTS . " WHERE id = " . $id . " OR parent = " . $id . " ORDER BY id ASC");
while ($post = $result->fetchArray()) {
$posts[] = $post;
}
return $posts;
}
function imagesInThreadByID($id, $moderated_only = true) {
$images = 0;
$posts = postsInThreadByID($id, false);
foreach ($posts as $post) {
if ($post['file'] != '') {
$images++;
}
}
return $images;
}
function postsByHex($hex) {
global $db;
$posts = array();
$result = $db->query("SELECT id, parent FROM " . TINYIB_DBPOSTS . " WHERE file_hex = '" . $db->escapeString($hex) . "' LIMIT 1");
while ($post = $result->fetchArray()) {
$posts[] = $post;
}
return $posts;
}
function latestPosts($moderated = true) {
global $db;
$posts = array();
$result = $db->query("SELECT * FROM " . TINYIB_DBPOSTS . " ORDER BY timestamp DESC LIMIT 10");
while ($post = $result->fetchArray()) {
$posts[] = $post;
}
return $posts;
}
function deletePostByID($id) {
global $db;
$posts = postsInThreadByID($id, false);
foreach ($posts as $post) {
if ($post['id'] != $id) {
deletePostImages($post);
$db->exec("DELETE FROM " . TINYIB_DBPOSTS . " WHERE id = " . $post['id']);
} else {
$thispost = $post;
}
}
if (isset($thispost)) {
if ($thispost['parent'] == TINYIB_NEWTHREAD) {
@unlink('res/' . $thispost['id'] . '.html');
}
deletePostImages($thispost);
$db->exec("DELETE FROM " . TINYIB_DBPOSTS . " WHERE id = " . $thispost['id']);
}
}
function trimThreads() {
global $db;
if (TINYIB_MAXTHREADS > 0) {
$result = $db->query("SELECT id FROM " . TINYIB_DBPOSTS . " WHERE parent = 0 ORDER BY stickied DESC, bumped DESC LIMIT " . TINYIB_MAXTHREADS . ", 10");
while ($post = $result->fetchArray()) {
deletePostByID($post['id']);
}
}
}
function lastPostByIP() {
global $db;
$result = $db->query("SELECT * FROM " . TINYIB_DBPOSTS . " WHERE ip = '" . $_SERVER['REMOTE_ADDR'] . "' ORDER BY id DESC LIMIT 1");
while ($post = $result->fetchArray()) {
return $post;
}
}
// Ban Functions
function banByID($id) {
global $db;
$result = $db->query("SELECT * FROM " . TINYIB_DBBANS . " WHERE id = '" . $db->escapeString($id) . "' LIMIT 1");
while ($ban = $result->fetchArray()) {
return $ban;
}
}
function banByIP($ip) {
global $db;
$result = $db->query("SELECT * FROM " . TINYIB_DBBANS . " WHERE ip = '" . $db->escapeString($ip) . "' LIMIT 1");
while ($ban = $result->fetchArray()) {
return $ban;
}
}
function allBans() {
global $db;
$bans = array();
$result = $db->query("SELECT * FROM " . TINYIB_DBBANS . " ORDER BY timestamp DESC");
while ($ban = $result->fetchArray()) {
$bans[] = $ban;
}
return $bans;
}
function insertBan($ban) {
global $db;
$db->exec("INSERT INTO " . TINYIB_DBBANS . " (ip, timestamp, expire, reason) VALUES ('" . $db->escapeString($ban['ip']) . "', " . time() . ", '" . $db->escapeString($ban['expire']) . "', '" . $db->escapeString($ban['reason']) . "')");
return $db->lastInsertRowID();
}
function clearExpiredBans() {
global $db;
$result = $db->query("SELECT * FROM " . TINYIB_DBBANS . " WHERE expire > 0 AND expire <= " . time());
while ($ban = $result->fetchArray()) {
$db->exec("DELETE FROM " . TINYIB_DBBANS . " WHERE id = " . $ban['id']);
}
}
function deleteBanByID($id) {
global $db;
$db->exec("DELETE FROM " . TINYIB_DBBANS . " WHERE id = " . $db->escapeString($id));
}