<?php
/*															*/
/* File: index.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.
/*															*/
	
	if ($_GET['show_forum'])
		$_COOKIE['show_forum'] = $_GET['show_forum'];
		
	if ($_GET['show_forum'] == "none")
		$_COOKIE['show_forum'] = 0;
	
	if (!$_COOKIE['lang'])
		$_COOKIE['lang'] = "en";
	
	if ($_GET['lang'])
		$_COOKIE['lang'] = $_GET['lang'];
	
	setcookie("lang", $_COOKIE['lang'], time()+2592000);
	setcookie("show_forum", $_COOKIE['show_forum'], time()+2592000);

	session_start();
	
	require_once("lang.php");
	
	// important DO NOT remove
	$php_self = $_SERVER["PHP_SELF"]; 
	
	//init stuff
	// 
	// TODO: 
	// set your server, username and passwd here
	//
	$mysql_server = "";
	$mysql_user = "";
	$mysql_passwd = "";
	
	$connection = mysql_connect($mysql_server, $mysql_user, $mysql_passwd)    
		or die($_LANG['mysql_connect_error'] . mysql_error());
	
	// 
	// TODO: 
	// set your database name here
	//
	$database_name = "";
	
	mysql_select_db($database_name, $connection)
		or die($_LANG['mysql_db_select_error'] . mysql_error());
	
	// parse user post and get data
	
	if (!(isset($_SESSION['user_login'])))
		$_SESSION['user_login'] = 0;

	// class definitions
	require_once("definitions.php");
	$user = 0;
	
	// helper functions 
	require_once("functions.php");
	
	// parse user post and get data
	if ($_POST['submit'] && $_POST['login'] && $_POST['passwd'])
	{
		if ($user_id = VerifyLogin($_POST['login'], $_POST['passwd']))
		{
			$_SESSION['user_login'] = $user_id;
			SendAuthCookie($_SESSION['user_login']);
		}
		else
			$login_failed = 1;
	}
	
	if ($_COOKIE['auth_login'])
	{
		if ($user_id = VerifyAuthLogin($_COOKIE['auth_login']))
		{
			$_SESSION['user_login'] = $user_id;
			SendAuthCookie($_SESSION['user_login']);
		}
	}
	
	if ($_GET['log_out'])
	{	
		SendAuthCookie();
		$_SESSION['user_login'] = 0;
	}
	
	if ($_SESSION['user_login'])
		$user = new UserOptions($_SESSION['user_login']);
	
	else
		$user = new UserOptions;
	
	// end of init stuff
	require_once("head.php");
	
	if ($_POST['submit'] && $_POST['create_account']) 
	{
		if (ValidateNewAccountData($_POST['name'], $_POST['passwd'], $_POST['passwd_confirm'], $_POST['email'], $_POST['www']))
		{
			AddNewAccount($_POST['name'], $_POST['passwd'], $_POST['email'], $_POST['www']);
		}
	}
	
	if ($_POST['submit'] && $_POST['create_forum']) 
	{
		if (ValidateForumData($_POST['name'], $_POST['name_id'], $_POST['comment']))
		{
			AddNewForum($_POST['name'], $_POST['name_id'], $_POST['comment']);
		}
	}
	
	if ($_POST['new_topic'] && $_POST['submit'] && $_SESSION['user_login'] && $_COOKIE['show_forum'] && $user->Check($_OPTION['posting_allowed']))
	{
		if (ValidatePostData($_POST['title'], $_POST['body']))
		{
			PostMessage($_COOKIE['show_forum'], 0, $_SESSION['user_login'], $_POST['title'], $_POST['body']);
		}
	}
	
	if ($_POST['reply'] && $_POST['submit'] && $_POST['parent'] && $_SESSION['user_login'] && $_COOKIE['show_forum'] && $user->Check($_OPTION['posting_allowed']))
	{
		if (ValidatePostData($_POST['title'], $_POST['body']))
		{
			PostMessage($_COOKIE['show_forum'], $_POST['parent'], $_SESSION['user_login'], $_POST['title'], $_POST['body']);
		}
	}
	
	if ($_POST['update_account'] && $_SESSION['user_login'])
	{
		UpdateAccount($_SESSION['user_login'], $_POST['oldpasswd'], $_POST['passwd'], $_POST['passwd_confirm'], $_POST['email'], $_POST['www'], $_POST['signature'], $_POST['comment'], getOptions($_SESSION['user_login'], $_POST['dont_quote_replies'], $_POST['public_email'], $_POST['sign_posts'], $_POST['reply_notification']));
	}
	
	if ($user->Check($_OPTION['admin']))
	{
		if ($_GET['update_user_post_stats'])
			UpdateUserPostStats();
			
		if ($_POST['delete_forum'] && $_POST['submit'])
		{
			DeleteForum($_POST['delete_forum']);
		}
	
		if ($_POST['moderate'] && $_POST['title'] && $_POST['body'] && $_COOKIE['show_forum'])
		{
			UpdatePost($_COOKIE['show_forum'], $_POST['moderate'], $_POST['title'], $_POST['body']);
		}
	
		if ($_GET['delete'] && $_COOKIE['show_forum'])
		{
			DeletePost($_COOKIE['show_forum'], $_GET['delete']);
		}
	
		if ($_GET['bann'] && $_COOKIE['show_forum'])
		{
			BannPost($_COOKIE['show_forum'], $_GET['bann']);
		}
	
		if ($_GET['unbann'] && $_COOKIE['show_forum'])
		{
			UnBannPost($_COOKIE['show_forum'], $_GET['unbann']);
		}
	
		if ($_GET['bann_posts'])
		{
			BannUserPosts($_GET['bann_posts']);
		}
	
		if ($_GET['unbann_posts'])
		{
			UnBannUserPosts($_GET['unbann_posts']);
		}
	
		if ($_GET['delete_posts'])
		{
			DeleteUserPosts($_GET['delete_posts']);
		}
	
		if ($_GET['bann_account'])
		{
			BannAccount($_GET['bann_account']);
		}
	
		if ($_GET['unbann_account'])
		{
			UnBannAccount($_GET['unbann_account']);
		}
	
		if ($_GET['disable_account'])
		{
			DisableAccount($_GET['disable_account']);
		}
	
		if ($_GET['enable_account'])
		{
			EnableAccount($_GET['enable_account']);
		}
	
		if ($_GET['delete_account'])
		{
			DeleteAccount($_GET['delete_account']);
		}
	}
	
//#########################################
// end of user parsing user input
//#########################################
	
	//misc information for the end-user
	
	if ($login_failed)
	{
		echo "<DIV CLASS=\"warn\">",$_LANG['failed_login'],"</DIV><br>";
	}
	
	// end of misc info
	
	if ($_SESSION['user_login'])
	{
		$userdata = getUserData($_SESSION['user_login'], "name,posts,topics_started,created");
		$posts_perday = postPerDay($userdata['created'], $userdata['posts']);
		$topics_perday = postPerDay($userdata['created'], $userdata['topics_started']);
		
		echo "<DIV class=\"sub-heading\">",$_LANG['welcome_1'],"<B>",$userdata['name'],"</B>, ",$userdata['posts'],$_LANG['welcome_2'],$posts_perday,$_LANG['welcome_3'],$userdata['topics_started']," ";
		echo $userdata['topics_started'] == 1 ? $_LANG['welcome_4_1'] : $_LANG['welcome_4_2'];
		echo " (",$topics_perday,$_LANG['welcome_5'],"</DIV><BR>";
		$padd = 0;
	}
	else
		$padd = 6;
	
	echo "<TABLE WIDTH=\"100%\" HEIGHT=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\" BORDER=\"0\" class=\"main\"><TR><TD valign=top style=\"padding-top:",$padd,"px\">";
	echo "<A HREF=\"$php_self?show_forum=none\">",$_LANG['show_all_forums'],"</A>&nbsp;&nbsp;&nbsp;";
	
	if ($_COOKIE['show_forum'])
		echo "<A HREF=\"$php_self?show_forum=",$_COOKIE['show_forum'],"\">",$_LANG['show_topics'],"</A>&nbsp;&nbsp;&nbsp;";
	
	if (!$_SESSION['user_login']) {
		echo "<A HREF=\"$php_self?create_account=1\">",$_LANG['create_new_account'],"</A>&nbsp;&nbsp;&nbsp;";
	}	
	else
	{
		if ($_COOKIE['show_forum'])
		{
				echo "<A HREF=\"$php_self?new_topic=1\">",$_LANG['create_new_topic'],"</A>&nbsp;&nbsp;&nbsp;";
		}
		
	}
	if ($_GET['post'] && $_COOKIE['show_forum'])
	{	
		$parent = getParent($_COOKIE['show_forum'], $_GET['post']);
		echo "<A HREF=\"$php_self?post=$parent\">",$_LANG['up_one_level'],"</A>&nbsp;&nbsp;&nbsp;";
	}
	
	echo "</td><td ALIGN=\"right\" valign=top style=\"padding-top:0px\">";
	
	if ($_SESSION['user_login'])
	{	
		if ($user->Check($_OPTION['admin']))
		{	
			echo "[ <A HREF=\"$php_self?admin_panel=1\">",$_LANG['admin_panel'],"</A> ]&nbsp;&nbsp;&nbsp;"; 
		}
		echo "[ <A HREF=\"$php_self?manage_account=1\">",$_LANG['manage_account'],"</A> ]&nbsp;&nbsp;&nbsp;";
		echo "[ <A HREF=\"$php_self?log_out=1\">",$_LANG['log_out'],"</A> ]&nbsp;&nbsp;&nbsp;";
	}
	else
	{
	?> <FORM ACTION="<?php echo $php_self;?>" METHOD="POST"><?php echo $_LANG['user'];?>:<INPUT TYPE="TEXT" NAME="login"> <?php echo $_LANG['passwd'];?>:<INPUT TYPE="PASSWORD" NAME="passwd"> <INPUT TYPE="SUBMIT" NAME="submit" VALUE="login"></FORM><a href="<?php echo $php_self;?>?&forgot_passwd=1">zapomniałeś hasła?</A><?php
	}
	
	echo "</TD></TR></TABLE>";
	
	echo "<br>";
	
	// parse GET and POST	
	
	if ($_GET['forgot_passwd'])
	{
		ExpandForgetPassword();
	}
	else if ($_GET['get_passwd'] && $_POST['login'] && $_POST['submit'])
	{
		RetrievePassword($_POST['login']);
	}
	else if ($_GET['authorize'] && $_GET['data'])
	{
		Authorize($_GET['authorize'], $_GET['data']);
	}
	else if ($_GET['create_account']) {
		ExpandCreateAccountForm();	
	}
	else if ($_GET['admin_panel'] && $user->Check($_OPTION['admin']))
	{
		ExpandAdminPanel();
	}
	else if ($_GET['new_topic'])
	{
		ExpandNewTopicForm();
	}
	else if ($_GET['users'] && $user->Check($_OPTION['admin']))
	{
		ShowUsers();
	}
	else if ($_GET['user_profile'])
	{
		ShowUserProfile(getUserData($_GET['user_profile'], "*"));
	}
	else if ($_GET['reply'] && $_COOKIE['show_forum'])
	{
		if ($_SESSION['user_login'])
			ExpandReplyForm($_COOKIE['show_forum'], $_GET['reply'], !$user->Check($_OPTION['dont_quote_replies']));
		else
			ExpandLoginForm();	
	}
	else if ($_GET['moderate'] && $_COOKIE['show_forum'])
	{
		ExpandModeratePost($_COOKIE['show_forum'], $_GET['moderate']);
	}
	else if ($_GET['post'] && $_COOKIE['show_forum'])
	{
		$data = getPostTree($_COOKIE['show_forum'], $_GET['post']);
		echo "<div class=\"main-heading\">",$_LANG['thread_tree'],"</div>";
		ShowPostTree($data);
		echo "<br>";
		echo "<div class=\"main-heading\">",$_LANG['post_bodies'],"</div>";
		
		ShowPostBodies($data);
	}
	else if ($_GET['manage_account'] && $_SESSION['user_login'] && !$user->Check($_OPTION['account_disabled']))
	{
		ExpandEditAccountForm($_SESSION['user_login']);
	}
	else if ($_COOKIE['show_forum'])
	{
		$forum_name = getForumName($_COOKIE['show_forum']);
		echo "<div class=\"main-heading\">$forum_name - ",$_LANG['latest_topics'],"</div>";
		if (!isset($_GET['page']))
			$_GET['page'] = 0;
		
		$data = getTopics($_COOKIE['show_forum']);
		ShowTopics($data, $_GET['page']);
	}
	else if (!$_COOKIE['show_forum'])
	{	
		echo "<div class=\"main-heading\">All Forums</div>";
		ShowAllForums(getAllForumsData());
	}
	
	require_once("foot.php");

	if (!$_SESSION['user_login']) {	
		session_unset();
		session_destroy();
	}
		
	mysql_close($connection);
?>