<?php
/*															*/
/* File: functions.php											*/
/* (c) 2003,2004 by Łukasz Tomicki <tomicki(at)o2(dot)pl> 	*/
/* Most recent version available @ : http://tomicki.net/	*/
/*															*/
/*
/*  This program is free software; you can redistribute it and/or modify
/*  it under the terms of the GNU General Public License as published by
/*  the Free Software Foundation; either version 2 of the License, or
/*  (at your option) any later version.
/*
/*  This program is distributed in the hope that it will be useful,
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/*  GNU Library General Public License for more details.
/*
/*  You should have received a copy of the GNU General Public License
/*  along with this program; if not, write to the Free Software
/*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
/*															*/

require_once("display.php");
require_once("misc.php");

function RetrievePassword($login)
{
	$query = "select email,passwd from users where name = '$login'";
	$result = mysql_query($query);
	if (!($data = mysql_fetch_array($result)))
	{
		echo "Login nie istnieje.";
		return 0;
	}
	$subject = "Odzysk";
	$email = $data['email'];
	$message = "Your secret password is: ".$data['passwd'];
	$headers = "To: <$email>\r\n";
	$headers .= "From: netblazer Forums <netblazer@netblazer.forums.prv.pl>\r\n";
		
	mail($email, $subject, $message, $headers);
	echo "Email został wysłany.";
}

function VerifyAuthLogin($data)
{
	$separator = ":";
	if (!($pos = strpos($data, $separator)))
		return 0;
	
	$size = strlen($data);
	
	$user = substr($data, 0, $pos);
	$pos += strlen($separator);
	$hash = substr($data, $pos, $size - $pos);
	$result = mysql_query("select options from users where id = '$user' limit 1");
	$data = mysql_fetch_array($result);
	if (calculateAuthValue($user, $data['options']) == $hash)
		return $user;
	
	else 
		return 0;
}

function SendAuthCookie($user = 0)
{
	if (!$user)
		setcookie("auth_login", 0, time()+5);
			
	$result = mysql_query("select options from users where id = '$user' limit 1");
	$data = mysql_fetch_array($result);
	
	$send = $user.":".calculateAuthValue($user, $data['options']);
	setcookie("auth_login", $send, time()+2592000);
}

function VerifyLogin($login, $passwd)
{
	global $_OPTION;
	
	$query = "select id from users where (name = '$login' AND passwd = '$passwd') limit 1";
	$result = mysql_query($query);
		
	if ($data = mysql_fetch_array($result))
	{
		$user = new UserOptions($data['id']);
		
		if (!$user->Check($_OPTION['account_disabled']))
			return $data['id'];
	}
			
	return 0;
}

function ValidateUpdateData($id, $passwd_old, $passwd_new, $passwd_confirm, $email, $www)
{
	$data = getUserData($id, "name");
	$name = $data['name'];
	if ($passwd_old && $passwd_new && $passwd_confirm)
	{
		if (!VerifyLogin($name, $passwd_old))
		{
				echo "<DIV CLASS=\"warn\">old password invalid</DIV><BR>";
				return 0;
		}
		
		if ($passwd_new != $passwd_confirm)
		{
				echo "<DIV CLASS=\"warn\">new passwords don't match</DIV><BR>";
				return 0;
		}
	}
	
	$email = trim($email);
	if (!$email)
	{
		echo "<DIV CLASS=\"warn\">email required!</DIV><BR>";
		return 0;
	}
	if (!strrpos ($email, "@"))
	{
		echo "<DIV CLASS=\"warn\">email not valid!</DIV><BR>";
		return 0;
	}
	if (strlen($email) > 100)
	{
		echo "<DIV CLASS=\"warn\">email too long!</DIV><BR>";
		return 0;
	}
			
	// check website address
	$www = trim($www);
	if ($www && strlen($www) > 100)
	{
		echo "<DIV CLASS=\"warn\">website address too long!</DIV><BR>";
		return 0;
	}
	
	return 1;
}

function ValidateNewAccountData($name, $passwd, $passwd_confirm, $email, $www = 0)
{
			//check name
			if (!$name)
			{
				echo "<DIV CLASS=\"warn\">name required!</DIV><BR>";
				return 0;
			}
			
			$name = trim($name);
			if (strlen($name) > 50)
			{
				echo "<DIV CLASS=\"warn\">name too long!</DIV><BR>";
				return 0;
			}
				
			$query = "select name from users where name = '$name' limit 1";
			$result = mysql_query($query);
			if (mysql_num_rows($result))
			{
				echo "<DIV CLASS=\"warn\">name already in use, choose a different one!</DIV><BR>";
				return 0;
			}
			
			// check passwd
			
			if (!$passwd)
			{
				echo "<DIV CLASS=\"warn\">password required!</DIV><BR>";
				return 0;
			}
			if ($passwd != $passwd_confirm)
			{
				echo "<DIV CLASS=\"warn\">passwords do not match!</DIV><BR>";
				return 0;
			}
			$passwd_length = strlen($passwd);
			if ($passwd_length < 6)
			{
				echo "<DIV CLASS=\"warn\">password too short!</DIV><BR>";
				return 0;
			}
			if ($passwd_length > 40)
			{
				echo "<DIV CLASS=\"warn\">password too long!</DIV><BR>";
				return 0;
			}
			
			//check email

			$email = trim($email);
			if (!$email)
			{
				echo "<DIV CLASS=\"warn\">email required!</DIV><BR>";
				return 0;
			}
			if (!strrpos ($email, "@"))
			{
				echo "<DIV CLASS=\"warn\">email not valid!</DIV><BR>";
				return 0;
			}
			if (strlen($email) > 100)
			{
				echo "<DIV CLASS=\"warn\">email too long!</DIV><BR>";
				return 0;
			}
			
			// check website address
			$www = trim($www);
			if ($www && strlen($www) > 100)
			{
				echo "<DIV CLASS=\"warn\">website address too long!</DIV><BR>";
				return 0;
			}

			return 1;
}

function ValidateForumData($name, $name_id, $comment)
{
	if (!$name)
	{
		echo "<DIV CLASS=\"warn\">Forum name required!</DIV><BR>";
		return 0;
	}
	
	$name = trim($name);
	if (strlen($name) > 100)
	{
		echo "<DIV CLASS=\"warn\">Forum name too long!</DIV><BR>";
		return 0;
	}
	
	if (!$name_id)
	{
		echo "<DIV CLASS=\"warn\">Forum name_id required!</DIV><BR>";
		return 0;
	}
	if ($name_id == "none")
	{
		echo "<DIV CLASS=\"warn\">Forum name_id == 'none'! 'none' is a reserved keyword!</DIV><BR>";
		return 0;
	}
	
	$name_id = trim($name_id);
	if (strlen($name_id) > 50)
	{
		echo "<DIV CLASS=\"warn\">Forum name_id too long!</DIV><BR>";
		return 0;
	}
	
	if (!$comment)
	{
		echo "<DIV CLASS=\"warn\">Forum description required!</DIV><BR>";
		return 0;
	}
	return 1;
}

function ValidatePostData($title, $body)
{	
	if (!$title)
	{
		echo "<DIV CLASS=\"warn\">title required!</DIV><BR>";
		return 0;
	}
	
	$title = trim($title);
	if (strlen($title) > 200)
	{
		echo "<DIV CLASS=\"warn\">title too long!</DIV><BR>";
		return 0;
	}
	if (longwords($title, 40))
	{
		echo "<DIV CLASS=\"warn\">title contains words that are too long!</DIV><BR>";
		return 0;
	}

	if (!$body)
	{
		echo "<DIV CLASS=\"warn\">body required!</DIV><BR>";
		return 0;
	}
	return 1;
}

function PostMessage($forum, $parent, $user, $title, $body)
{
//################################
// warn - user MUST be valid
//################################

	if (!$forum)
		return 0;
	
	$query = "select id from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$data = mysql_fetch_array($result);
	
	$id = $data['id'];
	$posts_table = "posts$id";
	$time = date("Y-m-d H:i:s");
		
	// do some housekeeping on body and title
	
	$body = doBodyClean($body);
	$body = quoteText($body);
	$title = doTitleClean($title);
	
	$userdata = new UserOptions($user);
	
	global $_OPTION;
	
	if ($userdata->Check($_OPTION['sign_posts']))
	{
		$body = sign($user, $body);
	}
	
	if (!$parent)
		$query = "select id from $posts_table where title = '$title' OR body = '$body' limit 1";
	
	else 
		$query = "select id from $posts_table where body = '$body' limit 1";
	
	$result = mysql_query($query);
	
	if (mysql_num_rows($result))
		return 0;
	
	$query  = "select id from $posts_table where author = $user limit 1";
	
	$result = mysql_query($query);
	
	if (!mysql_num_rows($result))
	{
		$query = "select users from forums where name_id = '$forum' limit 1";
		$result = mysql_query($query);
		$data = mysql_fetch_array($result);
		
		++$data['users'];
		
		$query = "update forums set users = ".$data['users']." where name_id = '$forum' limit 1";
		mysql_query($query);
	}
	
	$query = "insert into $posts_table(parent, author, posted, options, title, body, replies, last_reply) values($parent, $user, '$time', 0, '$title', '$body', 0, 0)";
	
	mysql_query($query);
	
	$query = "select id from $posts_table order by id desc limit 1";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);
	setLastReply($forum, $parent, $data['id']);
	
	$query = "select posts, topics_started from users where id = $user limit 1";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);

	++$data['posts'];
	
	if (!$parent)
		++$data['topics_started'];
	
	$query = "update users set posts = ".$data['posts'].", topics_started = ".$data['topics_started']." where id = $user limit 1";

	mysql_query($query);
	
	$query = "select posts, topics from forums where name_id = '$forum' limit 1";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);

	++$data['posts'];
	
	if (!$parent)
		++$data['topics'];
	
	$query = "update forums set posts = ".$data['posts'].", topics = ".$data['topics']." where name_id = '$forum' limit 1";
	
	mysql_query($query);
	
	// send email note to user ( if wanted )
	if (!$parent)
		return 0;
		
	$query = "select author from $posts_table where id = $parent limit 1";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);
	
	if ($data['author'] == $user)
		return 0;  
	
	$userdata = new UserOptions($data['author']);
	global $_OPTION;
	if ($userdata->Check($_OPTION['reply_notification']))
	{
		$subject = $title;
	
		$message = "<html><head><title>$title</title>";
		$message .= "
	<style type=\"text/css\">
	<!--
		.quote1 {
       			font-size: 8pt;
        		font-style: italic;
					}
		.signature {
       		 	font-size: 8pt;
        		font-weight: bold;
					}
	-->
	</style>";
		$message .= "</head><body>";
		$user_data = getUserData($user, "name");
		$message .= "<span style=\"font-family: verdana; font-size: 12px;\">";
		$message .= "<b>Posted by:</b> ".$user_data['name']." ";
		$message .= " <b>on:</b> ".$time."<br>";
		
		$query = "select id from $posts_table where posted = '$time' limit 1";
		$result = mysql_query($query);
		$msg_id = mysql_fetch_array($result);
		$message .= "<b>Message id:</b> <a href=\"http://igr.ibmer.waw.pl/~bluedev/forum/index.php?post=". $msg_id['id']."&show_forum=".$forum."\">"."http://netblazer.forums.prv.pl/index.php?post=". $msg_id['id']."&show_forum=".$forum."</a><br>";
		$message .= "<br>";
		$message .= "<span style=\"font-size: 10pt;\">";
		$message .= "<p>";
		$body = str_replace("\\\"","\"",$body);
		$message .= $body;
		$message .= "</p>";
		$message .= "</span>";
		$message .= "</span>";
		$message .= "</body></html>";
		$data = getUserData($data['author'], "email");
		$email = $data['email'];
		$headers  = "MIME-Version: 1.0\r\n";
		$headers .= "Content-type: text/html; charset=iso-8859-2\r\n";
		$headers .= "To: <$email>\r\n";
		$headers .= "From: netblazer Forums <netblazer@netblazer.forums.prv.pl>\r\n";
		
		mail($email, $subject, $message, $headers);
	}
}

function AddNewForum($name, $name_id, $comment)
{
	$time = date("Y-m-d H:i:s");
	$query = "insert into forums(name, name_id, comment, users, posts, topics, created) values ('$name', '$name_id', '$comment', 0, 0, 0, '$time')";
	
	mysql_query($query);
	
	$query = "select id from forums where name_id = '$name_id' limit 1"; 
	
	$result = mysql_query($query);
	
	$data = mysql_fetch_array($result);
	$id = $data['id'];
	$query = "create table posts$id(id smallint primary key auto_increment, parent smallint, author smallint, posted datetime, options int, title varchar(200), body text, replies smallint, last_reply smallint)";
	
	mysql_query($query);
}

function AddNewAccount($name, $passwd, $email, $www)
{
	global $_OPTION;
	
	$newuser = new UserOptions;
	
	$newuser->Set($_OPTION['posting_allowed']);
	
	$time = date("Y-m-d H:i:s");
	$query = "insert into users(name, passwd, email, www, signature, comment, posts, topics_started, created, options) values('$name', '$passwd', '$email', '$www', '', '',0,0,'$time',".$newuser->options.")";
	
	mysql_query($query);
}

function UpdatePost($forum, $post, $title, $body)
{
	$query = "select id from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$forum_data = mysql_fetch_array($result);
	
	$id = $forum_data['id'];
	$posts_table = "posts$id";
	
	$body = doBodyClean($body);
	$title = doTitleClean($title);
	
	$query = "update $posts_table set title = '$title', body = '$body' where id = $post";
	mysql_query($query);
}

function getUserData($user, $data = "*")
{
	$query = "select $data from users where id = $user limit 1";
	$result = mysql_query($query);
	return ( mysql_fetch_array($result) );
}

function getAllForumStats()
{
	$data = getAllForumsData();
	$stats['forums'] = mysql_num_rows($data);
	while ( $row = mysql_fetch_array($data) )
	{
		$stats['posts'] += $row['posts'];
		$stats['topics'] += $row['topics'];
	}
	$query = "select id from users";
	$result = mysql_query($query);
	$stats['users'] = mysql_num_rows($result);
	
	return ( $stats );
}

function getAllForumsData()
{
	$query = "select * from forums order by id desc";
	return ( mysql_query($query));
}

function getTopics($forum)
{
	$query = "select id from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$data = mysql_fetch_array($result);
	
	$id = $data['id'];
	$posts_table = "posts$id";
	
	$query = "select id,author,posted,options,title,replies,last_reply from $posts_table where parent = 0 order by id desc";
	
	$result = mysql_query($query);
	
	return ( $result );
}

function getParent($forum, $post)
{
	$query = "select id from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$forum_data = mysql_fetch_array($result);
	
	$id = $forum_data['id'];
	$posts_table = "posts$id";
	
	$query = "select parent from $posts_table where id = $post limit 1";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);
	return ( $data['parent'] );
}

function getForumName($forum)
{
	$query = "select name from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$data = mysql_fetch_array($result);
	return ( $data['name'] );
}

function getPostTree($forum, $post)
{
	$query = "select id from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$forum_data = mysql_fetch_array($result);
	
	$id = $forum_data['id'];
	$posts_table = "posts$id";
	
	$query = "select * from $posts_table where id = $post limit 1";

	$result = mysql_query($query);
	
	$row = mysql_fetch_array($result);
	
	if (!$row)
		return 0;
		
	$i = 0;
	$row['more'] = 0;
	$data[$i] = $row;
		++$i;
		
	$query = "select * from $posts_table where parent = $post order by id desc";

	$result = mysql_query($query);

	while ($row = mysql_fetch_array($result))
	{	
		if (!($row['options'] & $_OPTION['banned']))
		{
			$data[$i] = getPostTree($forum, $row['id']);
			$data[$i]['more'] = 1;
			++$i;
		}
	}
	
	return ( $data );
}

function Authorize($user, $code)
{
	$query = "select authorization from users where id = $user";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);
	if ($data['authorization'] == $code)
	{
		$user_options = new UserOptions($user);
		global $_OPTION;
		$user_options->Set($_OPTION['reply_notification']);
		
		$query = "update users set options = ". $user_options->options. " where id = $user";
		mysql_query($query);		
		return 0;
	}
	return -1;
}

function WantAuthorization($user, $email)
{
	$time = time() + $user;
	$query = "update users set authorization = '$time' where id = $user";
	mysql_query($query);
	
	$subject = "Konieczna autoryzacja";
	
	$message = "
	<html>
	<head>
 	<title>Konieczna autoryzacja adresu email.</title>
	</head>
	<body>
	<p>";
	$message .= "Kliknij w link żeby autoryzować adres email.<br>";
	$message .= "http://netblazer.forums.prv.pl/index.php?authorize=$user&data=$time<br>";
	$message .= "<br><br>";
	$message .= "Żądzanie zostało wysłane:<br>";
	$message .= "Date: " . date("F j, Y, g:i a") . "<br>";
	$message .= "User Agent: " . $_SERVER['HTTP_USER_AGENT'] . "<br>"; 
	$message .= "IP: " . $_SERVER['REMOTE_ADDR'] . "<br>";
	$message .= "</p>
	</body>
	</html>";

	$headers  = "MIME-Version: 1.0\r\n";
	$headers .= "Content-type: text/html; charset=iso-8859-2\r\n";
	$headers .= "To: <$email>\r\n";
	$headers .= "From: Admin Forum <tomicki@o2.pl>\r\n";

	mail($email, $subject, $message, $headers);
}

function UpdateAccount($user, $passwd_old, $passwd_new, $passwd_confirm, $email, $www, $signature, $comment, $options)
{
	if (ValidateUpdateData($user, $passwd_old, $passwd_new, $passwd_confirm, $email, $www))
	{
		if ($passwd_old)
			$passwd = $passwd_new;
			
		else 
		{
			$data = getUserData($user, "passwd");
			$passwd = $data['passwd'];
		}
		
		$data = getUserData($user, "signature, comment, options, email");
		
		if ($data['signature'] != $signature)
			$signature = doBodyClean($signature, 0);
			
			
		if ($data['comment'] != $comment)
			$comment = doBodyClean($comment);
		
		global $_OPTION;
		
		if (!($data['options'] & $_OPTION['reply_notification']) && ($options & $_OPTION['reply_notification']))
		{
			WantAuthorization($user, $email);
			$options &= ~$_OPTION['reply_notification'];
		}
		else if (($options & $_OPTION['reply_notification']) && $data['email'] != $email)
		{
			WantAuthorization($user, $email);
			$options &= ~$_OPTION['reply_notification'];
		}
			
		UpdateAccountData($user, $passwd, $email, $www, $signature, $comment, $options);
	}
}

function UpdateAccountData($user, $passwd, $email, $www, $signature, $comment, $options)
{
	$query = "update users set passwd = '$passwd', email = '$email', www = '$www', signature = '$signature', comment = '$comment', options = '$options' where id = $user";
	
	mysql_query($query);
}

function DeleteAccount($user)
{
	DeleteUserPosts($user);
	
	$query = "delete from users where id = $user limit 1";
	mysql_query($query);
	UpdateUserPostStats();
}

function EnableAccount($user)
{
	global $_OPTION;
	
	$query = "select options from users where id = $user limit 1";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);

	$data['options'] &= ~$_OPTION['account_disabled'];

	$query = "update users set options = ".$data['options']." where id = $user limit 1";
	
	mysql_query($query);
}

function DisableAccount($user)
{
	global $_OPTION;
	
	$query = "select options from users where id = $user limit 1";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);

	$data['options'] |= $_OPTION['account_disabled'];

	$query = "update users set options = ".$data['options']." where id = $user limit 1";
	
	mysql_query($query);
}

function UnBannAccount($user)
{
	global $_OPTION;
	
	$query = "select options from users where id = $user limit 1";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);

	$data['options'] |= $_OPTION['posting_allowed'];

	$query = "update users set options = ".$data['options']." where id = $user limit 1";
	
	mysql_query($query);
}

function BannAccount($user)
{
	global $_OPTION;
	
	$query = "select options from users where id = $user limit 1";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);
	
	$data['options'] &= ~$_OPTION['posting_allowed'];

	$query = "update users set options = ".$data['options']." where id = $user limit 1";
	
	mysql_query($query);
}

function DeletePost($forum, $post)
{
	global $_OPTION;
	
	$posts_table = getForum($forum);
	
	$data = getPost($forum, $post, "parent");
	
	if ($data['parent'] == 0)
		--$forum_data['topics'];
		
	--$forum_data['posts'];
	
	$query = "update forums set posts = ".$forum_data['posts'].", topics = ".$forum_data['topics']." where name_id = '$forum' limit 1";
	
	mysql_query($query);
	
	$query = "delete from $posts_table where id = $post limit 1";
	mysql_query($query);
	
	$query = "select id from $posts_table where parent = $post";
	$result = mysql_query($query);
	
	while ($data = mysql_fetch_array($result))
	{
		DeletePost($forum, $data['id']);
	}
	UpdateUserPostStats();
}

function DeleteUserPosts($user)
{
	global $_OPTION;
	
	$query = "select id,name_id from forums";

	$forum_result = mysql_query($query);
	
	while ($forum_data = mysql_fetch_array($forum_result))
	{
		$id = $forum_data['id'];
		$posts_table = "posts$id";
	
		$query = "select id from $posts_table where author = $user order by id limit 1";
		$result = mysql_query($query);
	
		while ($data = mysql_fetch_array($result))
		{	
			DeletePost($forum_data['name_id'], $data['id']);
			$result = mysql_query($query);
		}
	}
	UpdateUserPostStats();
}		

function UnBannUserPosts($user)
{
	global $_OPTION;
	
	$query = "select id,name_id from forums";

	$forum_result = mysql_query($query);
	
	while ($forum_data = mysql_fetch_array($forum_result))
	{
		$id = $forum_data['id'];
		$posts_table = "posts$id";
	
		$query = "select id,options from $posts_table where author = $user order by id";
		$result = mysql_query($query);
	
		while ($data = mysql_fetch_array($result))
		{
			if ($data['options'] & $_OPTION['banned'])
				UnBannPost($forum_data['name_id'], $data['id']);
		}
	}
}	

function BannUserPosts($user)
{
	global $_OPTION;
	
	$query = "select id,name_id from forums";

	$forum_result = mysql_query($query);
	
	while ($forum_data = mysql_fetch_array($forum_result))
	{
		$id = $forum_data['id'];
		$posts_table = "posts$id";
	
		$query = "select id,options from $posts_table where author = $user order by id";
		$result = mysql_query($query);
	
		while ($data = mysql_fetch_array($result))
		{
			if (!($data['options'] & $_OPTION['banned']))
				BannPost($forum_data['name_id'], $data['id']);
		}
	}
}

function BannPost($forum, $post)
{
	global $_OPTION;
	
	$query = "select id from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$forum_data = mysql_fetch_array($result);
	
	$id = $forum_data['id'];
	$posts_table = "posts$id";
	
	$query = "select options from $posts_table where id = $post limit 1";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);
	$data['options'] |= $_OPTION['banned'];
	
	$query = "update $posts_table set options = ".$data['options']." where id = $post limit 1";
	mysql_query($query);
	
	$query = "select id,options from $posts_table where parent = $post";
	$result = mysql_query($query);
	
	while ($data = mysql_fetch_array($result))
	{
		BannPost($forum, $data['id']);
	}
}

function UnBannPost($forum, $post)
{
	global $_OPTION;
	
	$query = "select id from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$forum_data = mysql_fetch_array($result);
	
	$id = $forum_data['id'];
	$posts_table = "posts$id";
	
	$query = "select options from $posts_table where id = $post limit 1";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);
	$data['options'] &= ~$_OPTION['banned'];
	
	$query = "update $posts_table set options = ".$data['options']." where id = $post limit 1";
	mysql_query($query);
	
	$query = "select id,options from $posts_table where parent = $post";
	$result = mysql_query($query);
	
	while ($data = mysql_fetch_array($result))
	{
		UnBannPost($forum, $data['id']);
	}
}	

function DeleteForum($forum)
{
	$query = "select id from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$forum_data = mysql_fetch_array($result);
	
	$id = $forum_data['id'];
	$posts_table = "posts$id";
	$query = "drop table $posts_table";
	mysql_query($query);
	
	$query = "delete from forums where name_id = '$forum'";
	mysql_query($query);
	UpdateUserPostStats();
}

function getLastPost($forum, $user = 0)
{
	$query = "select id from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$forum_data = mysql_fetch_array($result);
	
	$id = $forum_data['id'];
	$posts_table = "posts$id";
	
	$query = "select * from $posts_table order by id desc limit 1";
	
	$result = mysql_query($query);
	$size = mysql_num_rows($result);
	
	if (!$size)
		return 0;
		
	return ( mysql_fetch_array($result) );
}

function setLastReply($forum, $parent, $set)
{
	if (!$parent)
		return 0;
		
	$query = "select id from forums where name_id = '$forum' limit 1";
	
	$result = mysql_query($query);
	$forum_data = mysql_fetch_array($result);
	
	$id = $forum_data['id'];
	$posts_table = "posts$id";
	
	$query = "select parent,replies from $posts_table where id = $parent";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);
	++$data['replies'];
	
	$query = "update $posts_table set last_reply = $set,replies = ".$data['replies']." where id = $parent";
	mysql_query($query);
	
	setLastReply($forum, $data['parent'], $set);
}

function getForum($forum)
{
	$query = "select id from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$forum_data = mysql_fetch_array($result);
	
	$id = $forum_data['id'];
	$posts_table = "posts$id";
	
	return ( $posts_table );
}

function getPercentData($posts, $topics)
{
	$query = "select posts,topics from forums where posts";
	$result = mysql_query($query);
	
	while ($forum_data = mysql_fetch_array($result))
	{
		$data['posts'] += $forum_data['posts'];
		$data['topics'] += $forum_data['topics'];
	}
	
	if ($data['posts'])
		$data['posts'] = round((100 * $posts) / $data['posts'], 3);
		
	if ($data['topics'])
		$data['topics'] = round((100 * $topics) / $data['topics'], 3);
	
	return ( $data );
}

function getPost($forum, $post, $get)
{
	$posts_table = getForum($forum);
	
	$query = "select $get from $posts_table where id = $post limit 1";
	$result = mysql_query($query);
	
	if (!mysql_num_rows($result))
		return 0;
		
	return ( mysql_fetch_array($result) );
}

function getTopMost($forum, $id, $level = 0)
{
	$posts_table = getForum($forum);
	$parent = $id;
	$i = 0;
	while ($id)
	{
		$query = "select id,parent from $posts_table where id = $id";
		$result = mysql_query($query);
		$data = mysql_fetch_array($result);
		$id = $data['parent'] ? $data['parent'] : $data['id'];
		++$i;
		if ($level && $i == $level)
			return ( $id );
	}
	return ( $id );
}

function UpdateUserPostStats()
{
	$query = "select id from users";
	$users = mysql_query($query);
	
	$query = "select id from forums";
	$forums = mysql_query($query);
	
	$i = 0;
	while ( $forum = mysql_fetch_array($forums) )
	{
		$forum_tables[$i] = "posts".$forum['id'];
		
		$query = "select id from ".$forum_tables[$i];
		$result = mysql_query($query);
		$posts = mysql_num_rows($result);
		
		$query = "select id from ".$forum_tables[$i]." where parent = 0";
		$result = mysql_query($query);
		$topics = mysql_num_rows($result);
		
		$query = "update forums set posts = $posts, topics = $topics where id = ".$forum['id'];
		mysql_query($query);
		
		++$i;
	}
	$size = count($forum_tables);
	
	while ( $user = mysql_fetch_array($users) )
	{
		$posts = 0;
		$topics = 0;
		for ($i = 0; $i < $size; ++$i)
		{
			$query = "select id from ".$forum_tables[$i]." where author = ".$user['id'];
			$posts_result = mysql_query($query);
			$posts += mysql_num_rows($posts_result);
			
			$query = "select id from ".$forum_tables[$i]." where parent = 0 AND author = ".$user['id'];
			$topics_result = mysql_query($query);
			$topics += mysql_num_rows($topics_result);
		}
		$query = "update users set posts = $posts, topics_started = $topics where id = ".$user['id'];
		mysql_query($query);
	}
}

?>