2009-09-20 02:53:15 +00:00
< ? php
2014-06-24 19:51:22 +00:00
if ( ! defined ( 'TINYIB_BOARD' )) {
die ( '' );
}
2009-09-20 02:53:15 +00:00
2013-04-16 03:30:41 +00:00
if ( ! function_exists ( 'mysql_connect' )) {
fancyDie ( " MySQL library is not installed " );
}
2010-12-08 20:50:24 +00:00
$link = mysql_connect ( TINYIB_DBHOST , TINYIB_DBUSERNAME , TINYIB_DBPASSWORD );
2009-09-20 02:53:15 +00:00
if ( ! $link ) {
fancyDie ( " Could not connect to database: " . mysql_error ());
}
2010-12-08 20:50:24 +00:00
$db_selected = mysql_select_db ( TINYIB_DBNAME , $link );
2009-09-20 02:53:15 +00:00
if ( ! $db_selected ) {
fancyDie ( " Could not select database: " . mysql_error ());
}
// Create the posts table if it does not exist
2010-12-03 10:11:14 +00:00
if ( mysql_num_rows ( mysql_query ( " SHOW TABLES LIKE ' " . TINYIB_DBPOSTS . " ' " )) == 0 ) {
2014-07-20 19:12:00 +00:00
mysql_query ( $posts_sql );
2009-09-20 02:53:15 +00:00
}
// Create the bans table if it does not exist
2010-12-03 10:11:14 +00:00
if ( mysql_num_rows ( mysql_query ( " SHOW TABLES LIKE ' " . TINYIB_DBBANS . " ' " )) == 0 ) {
2014-07-20 19:12:00 +00:00
mysql_query ( $bans_sql );
2009-09-20 02:53:15 +00:00
}
# Post Functions
function uniquePosts () {
2010-12-03 10:11:14 +00:00
$row = mysql_fetch_row ( mysql_query ( " SELECT COUNT(DISTINCT(`ip`)) FROM " . TINYIB_DBPOSTS ));
2009-09-20 02:53:15 +00:00
return $row [ 0 ];
}
function postByID ( $id ) {
2010-12-03 10:11:14 +00:00
$result = mysql_query ( " SELECT * FROM ` " . TINYIB_DBPOSTS . " ` WHERE `id` = ' " . mysql_real_escape_string ( $id ) . " ' LIMIT 1 " );
2011-01-07 08:56:59 +00:00
if ( $result ) {
while ( $post = mysql_fetch_assoc ( $result )) {
return $post ;
}
2009-09-20 02:53:15 +00:00
}
}
function threadExistsByID ( $id ) {
2014-10-17 01:12:52 +00:00
return mysql_result ( mysql_query ( " SELECT COUNT(*) FROM ` " . TINYIB_DBPOSTS . " ` WHERE `id` = ' " . mysql_real_escape_string ( $id ) . " ' AND `parent` = 0 AND `moderated` = 1 LIMIT 1 " ), 0 , 0 ) > 0 ;
2009-09-20 02:53:15 +00:00
}
function insertPost ( $post ) {
2014-10-17 01:12:52 +00:00
mysql_query ( " 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`, `moderated`) VALUES ( " . $post [ 'parent' ] . " , " . time () . " , " . time () . " , ' " . $_SERVER [ 'REMOTE_ADDR' ] . " ', ' " . mysql_real_escape_string ( $post [ 'name' ]) . " ', ' " . mysql_real_escape_string ( $post [ 'tripcode' ]) . " ', ' " . mysql_real_escape_string ( $post [ 'email' ]) . " ', ' " . mysql_real_escape_string ( $post [ 'nameblock' ]) . " ', ' " . mysql_real_escape_string ( $post [ 'subject' ]) . " ', ' " . mysql_real_escape_string ( $post [ 'message' ]) . " ', ' " . mysql_real_escape_string ( $post [ 'password' ]) . " ', ' " . $post [ 'file' ] . " ', ' " . $post [ 'file_hex' ] . " ', ' " . mysql_real_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' ] . " , " . $post [ 'moderated' ] . " ) " );
2009-09-20 02:53:15 +00:00
return mysql_insert_id ();
}
2014-10-17 01:12:52 +00:00
function approvePostByID ( $id ) {
mysql_query ( " UPDATE ` " . TINYIB_DBPOSTS . " ` SET `moderated` = 1 WHERE `id` = " . $id . " LIMIT 1 " );
}
2015-08-21 07:45:01 +00:00
function stickyThreadByID ( $id , $setsticky ) {
mysql_query ( " UPDATE ` " . TINYIB_DBPOSTS . " ` SET `stickied` = ' " . mysql_real_escape_string ( $setsticky ) . " ' WHERE `id` = " . $id . " LIMIT 1 " );
}
2009-09-20 02:53:15 +00:00
function bumpThreadByID ( $id ) {
2010-12-03 10:11:14 +00:00
mysql_query ( " UPDATE ` " . TINYIB_DBPOSTS . " ` SET `bumped` = " . time () . " WHERE `id` = " . $id . " LIMIT 1 " );
2009-09-20 02:53:15 +00:00
}
2013-04-16 02:50:52 +00:00
function countThreads () {
2014-10-17 01:12:52 +00:00
return mysql_result ( mysql_query ( " SELECT COUNT(*) FROM ` " . TINYIB_DBPOSTS . " ` WHERE `parent` = 0 AND `moderated` = 1 " ), 0 , 0 );
2009-09-20 02:53:15 +00:00
}
2014-06-24 19:51:22 +00:00
function allThreads () {
2009-09-20 02:53:15 +00:00
$threads = array ();
2015-08-21 07:45:01 +00:00
$result = mysql_query ( " SELECT * FROM ` " . TINYIB_DBPOSTS . " ` WHERE `parent` = 0 AND `moderated` = 1 ORDER BY `stickied` DESC, `bumped` DESC " );
2011-01-07 08:56:59 +00:00
if ( $result ) {
while ( $thread = mysql_fetch_assoc ( $result )) {
$threads [] = $thread ;
}
2009-09-20 02:53:15 +00:00
}
return $threads ;
}
2013-04-16 02:50:52 +00:00
function numRepliesToThreadByID ( $id ) {
2014-10-17 01:12:52 +00:00
return mysql_result ( mysql_query ( " SELECT COUNT(*) FROM ` " . TINYIB_DBPOSTS . " ` WHERE `parent` = " . $id . " AND `moderated` = 1 " ), 0 , 0 );
2013-04-16 02:50:52 +00:00
}
2014-10-17 01:12:52 +00:00
function postsInThreadByID ( $id , $moderated_only = true ) {
2009-09-20 02:53:15 +00:00
$posts = array ();
2014-10-17 01:12:52 +00:00
$result = mysql_query ( " SELECT * FROM ` " . TINYIB_DBPOSTS . " ` WHERE (`id` = " . $id . " OR `parent` = " . $id . " ) " . ( $moderated_only ? " AND `moderated` = 1 " : " " ) . " ORDER BY `id` ASC " );
2011-01-07 08:56:59 +00:00
if ( $result ) {
while ( $post = mysql_fetch_assoc ( $result )) {
$posts [] = $post ;
}
2009-09-20 02:53:15 +00:00
}
return $posts ;
}
2014-06-24 19:51:22 +00:00
function postsByHex ( $hex ) {
2009-09-20 02:53:15 +00:00
$posts = array ();
2014-10-17 01:12:52 +00:00
$result = mysql_query ( " SELECT `id`, `parent` FROM ` " . TINYIB_DBPOSTS . " ` WHERE `file_hex` = ' " . mysql_real_escape_string ( $hex ) . " ' AND `moderated` = 1 LIMIT 1 " );
2011-01-07 08:56:59 +00:00
if ( $result ) {
while ( $post = mysql_fetch_assoc ( $result )) {
$posts [] = $post ;
}
2009-09-20 02:53:15 +00:00
}
return $posts ;
}
2014-10-17 01:12:52 +00:00
function latestPosts ( $moderated = true ) {
2011-08-07 07:24:39 +00:00
$posts = array ();
2014-10-17 01:12:52 +00:00
$result = mysql_query ( " SELECT * FROM ` " . TINYIB_DBPOSTS . " ` WHERE `moderated` = " . ( $moderated ? '1' : '0' ) . " ORDER BY `timestamp` DESC LIMIT 10 " );
2011-08-07 07:24:39 +00:00
if ( $result ) {
while ( $post = mysql_fetch_assoc ( $result )) {
$posts [] = $post ;
}
}
return $posts ;
}
2014-06-24 19:51:22 +00:00
function deletePostByID ( $id ) {
2014-10-17 01:12:52 +00:00
$posts = postsInThreadByID ( $id , false );
2009-09-20 02:53:15 +00:00
foreach ( $posts as $post ) {
if ( $post [ 'id' ] != $id ) {
deletePostImages ( $post );
2010-12-03 10:11:14 +00:00
mysql_query ( " DELETE FROM ` " . TINYIB_DBPOSTS . " ` WHERE `id` = " . $post [ 'id' ] . " LIMIT 1 " );
2009-09-20 02:53:15 +00:00
} else {
$thispost = $post ;
}
2010-06-22 07:19:06 +00:00
}
if ( isset ( $thispost )) {
2011-08-07 07:24:39 +00:00
if ( $thispost [ 'parent' ] == TINYIB_NEWTHREAD ) {
2010-06-22 07:19:06 +00:00
@ unlink ( 'res/' . $thispost [ 'id' ] . '.html' );
}
2009-09-20 02:53:15 +00:00
deletePostImages ( $thispost );
2010-12-03 10:11:14 +00:00
mysql_query ( " DELETE FROM ` " . TINYIB_DBPOSTS . " ` WHERE `id` = " . $thispost [ 'id' ] . " LIMIT 1 " );
2009-09-20 02:53:15 +00:00
}
}
function trimThreads () {
2010-12-03 10:11:14 +00:00
if ( TINYIB_MAXTHREADS > 0 ) {
2015-08-21 07:45:01 +00:00
$result = mysql_query ( " SELECT `id` FROM ` " . TINYIB_DBPOSTS . " ` WHERE `parent` = 0 AND `moderated` = 1 ORDER BY `stickied` DESC, `bumped` DESC LIMIT " . TINYIB_MAXTHREADS . " , 10 " );
2011-01-07 08:56:59 +00:00
if ( $result ) {
while ( $post = mysql_fetch_assoc ( $result )) {
deletePostByID ( $post [ 'id' ]);
}
2009-09-20 02:53:15 +00:00
}
}
}
2014-06-24 19:51:22 +00:00
function lastPostByIP () {
2010-12-03 10:11:14 +00:00
$replies = mysql_query ( " SELECT * FROM ` " . TINYIB_DBPOSTS . " ` WHERE `ip` = ' " . $_SERVER [ 'REMOTE_ADDR' ] . " ' ORDER BY `id` DESC LIMIT 1 " );
2011-01-07 08:56:59 +00:00
if ( $replies ) {
while ( $post = mysql_fetch_assoc ( $replies )) {
return $post ;
}
2009-09-20 02:53:15 +00:00
}
}
# Ban Functions
function banByID ( $id ) {
2010-12-03 10:11:14 +00:00
$result = mysql_query ( " SELECT * FROM ` " . TINYIB_DBBANS . " ` WHERE `id` = ' " . mysql_real_escape_string ( $id ) . " ' LIMIT 1 " );
2011-01-07 08:56:59 +00:00
if ( $result ) {
while ( $ban = mysql_fetch_assoc ( $result )) {
return $ban ;
}
2009-09-20 02:53:15 +00:00
}
}
function banByIP ( $ip ) {
2010-12-03 10:11:14 +00:00
$result = mysql_query ( " SELECT * FROM ` " . TINYIB_DBBANS . " ` WHERE `ip` = ' " . mysql_real_escape_string ( $ip ) . " ' LIMIT 1 " );
2011-01-07 08:56:59 +00:00
if ( $result ) {
while ( $ban = mysql_fetch_assoc ( $result )) {
return $ban ;
}
2009-09-20 02:53:15 +00:00
}
}
function allBans () {
$bans = array ();
2010-12-03 10:11:14 +00:00
$result = mysql_query ( " SELECT * FROM ` " . TINYIB_DBBANS . " ` ORDER BY `timestamp` DESC " );
2011-01-07 08:56:59 +00:00
if ( $result ) {
while ( $ban = mysql_fetch_assoc ( $result )) {
$bans [] = $ban ;
}
2009-09-20 02:53:15 +00:00
}
return $bans ;
}
function insertBan ( $ban ) {
2010-12-03 10:11:14 +00:00
mysql_query ( " INSERT INTO ` " . TINYIB_DBBANS . " ` (`ip`, `timestamp`, `expire`, `reason`) VALUES (' " . mysql_real_escape_string ( $ban [ 'ip' ]) . " ', " . time () . " , ' " . mysql_real_escape_string ( $ban [ 'expire' ]) . " ', ' " . mysql_real_escape_string ( $ban [ 'reason' ]) . " ') " );
2009-09-20 02:53:15 +00:00
return mysql_insert_id ();
}
function clearExpiredBans () {
2010-12-03 10:11:14 +00:00
$result = mysql_query ( " SELECT * FROM ` " . TINYIB_DBBANS . " ` WHERE `expire` > 0 AND `expire` <= " . time ());
2011-01-07 08:56:59 +00:00
if ( $result ) {
while ( $ban = mysql_fetch_assoc ( $result )) {
mysql_query ( " DELETE FROM ` " . TINYIB_DBBANS . " ` WHERE `id` = " . $ban [ 'id' ] . " LIMIT 1 " );
}
2009-09-20 02:53:15 +00:00
}
}
function deleteBanByID ( $id ) {
2010-12-03 10:11:14 +00:00
mysql_query ( " DELETE FROM ` " . TINYIB_DBBANS . " ` WHERE `id` = " . mysql_real_escape_string ( $id ) . " LIMIT 1 " );
2009-09-20 02:53:15 +00:00
}