* @link    		http://www.pioder.pl/
* @license	see LICENSE.txt
**/
define('IN_uF', true);
//include files
require('./config.php');
require('./includes/constants.php');
require('./includes/db.php');
require('./includes/errors.php');
//connect to database
DataBase::db_connect();
require('./includes/sessions.php');
require('./includes/classes/class_user.php');
require('./common.php');
require('./includes/misc_functions.php');
require('./includes/classes/class_forum.php');
require('./includes/classes/class_posting.php');
require('./includes/classes/class_topic.php');
require('./includes/classes/secure.php');
require('./lngs/'.DefaultLang().'/main.php');
$start = TimeGeneration();
$default_skin = ViewSkinName();
SessDelInvalid();	
SessRegister();
SessDeleteOld();
$msg='';
if ($_SESSION['uid']<1)
{
	message_forum($lng['youarenotlogd'],'login.php?mode=login');
}
$start = TimeGeneration();
//add skin variables
$skin = array();
$skin = array_push_assoc($skin, GenerateHeader('',''));
$stop = TimeGeneration();
$skin['queries'] =  ShowQueries($start, $stop);
if (isset($_POST['textedit']))
{
	$errors = false;
	//check post form
	if (($_POST['textedit'] =='' ) or (strlen(trim($_POST['textedit']))<10))
	{
		$errors = true;
		$message = $lng['perror_1'];
	}
	
	if ($_GET['mode'] == 'ntopic')
	{
   		if (($_POST['ntopic']!='') or (strlen(trim($_POST['ntopic']))>5))
   		{
			$errors = true;
			$message = $lng['perror_3'];
		}
	}
	
	//antiflood lock
	if (isset($_COOKIE['antiflood_time']))
	{
		if ($_COOKIE['antiflood_time']>$_SERVER['REQUEST_TIME'])
		{
			message_forum($lng['antiflood_enabled'],$_SERVER['REQUEST_URI']);
		}
	}
	else
	{
		setcookie('antiflood_time',($_SERVER['REQUEST_TIME'] + $forum_config['time_antiflood']));
	}
	
	//prepare post...
	if (!$errors)
	{
   		$_POST['textedit'] = Secure::TagsReplace($_POST['textedit']);
   		if ($_GET['mode']=='ntopic')
   		{
			$_POST['ntopic'] = Secure::UseCensorlist(strip_tags(trim($_POST['ntopic'])));
			if (isset($_POST['topic_sticky']))
			{
		    		$_POST['topic_sticky']='1';	
			}
			else
			{
		    		$_POST['topic_sticky']='0';
			}
		}
	
	
		//do it!
		switch ($_GET['mode'])
		{
			case 'rpost':
			{
				$id =  Post::NewPost(intval($_GET['id']),$_POST['textedit'], $_SESSION['uid']);
				$count =ceil(($id / 15 ));
				if ($count >1)
				{
		  			$page= '&page='.$count;
				}
				else
				{
	    				$page='';
				}
				$redirect_url = 'topic.php?t='.$_GET['id'].$page.'#p'.$id;
				break;
			}
  
			case 'qpost':
			{
				$id =  Post::NewPost(intval($_GET['id']),$_POST['textedit'], $_SESSION['uid']);
				$count =ceil(($id / 15 ));
				if ($count >1)
				{
		  			$page= '&page='.$count;
				}
				else
				{
		    			$page='';
				}
				$redirect_url = 'topic.php?t='.$_GET['id'].$page.'#p'.$id;
			
				break;
			}
		
			case 'edit':
			{
    				Post::EditPost(intval($_GET['id']),$_POST['textedit']);
				$count = ceil((Topic::PostInformation(intval($_GET['id']),'tp_id') / 15 ));
				if ($count >1)
				{
	  				$page= '&page='.$count;
				}
				else
				{
		    			$page='';
				}
				$redirect_url = 'topic.php?t='.Topic::PostInformation(intval($_GET['id']),'t_id').$page.'#p'.$_GET['id'];
			
				break;
			}
		
			case 'ntopic':
			{
				$last = Post::NewTopic($_POST['textedit'],$_POST['ntopic'], intval($_GET['f']), $_SESSION['uid'], $_POST['topic_sticky']);
				$redirect_url = 'topic.php?t='.$last;
				break;
			}
		}
		//redirecting to topic page...
		$stop = TimeGeneration();
		message_forum($lng['post_is_saved'], $redirect_url);
	}
	else
	{
   		$msg = './skins/'.$default_skin.'/post_error_body.tpl';
	}
}
else
{
	switch($_GET['mode'])
	{
   		case 'qpost':
   		{
       			$_POST['textedit'] = (empty($_POST['textedit'])) ? '[quote]'.stripslashes(Topic::PostInformation($_GET['id'],'text')).'[/quote]' : $_POST['textedit'];	
			break;
		}
		
		case 'edit':
		{
    			$_POST['textedit'] = (empty($_POST['textedit'])) ? stripslashes(Topic::PostInformation($_GET['id'],'text')) : $_POST['textedit'];
    			break;
		}
		
		case 'rpost':
		{
			$_POST['textedit'] = (empty($_POST['textedit'])) ? '' : $_POST['textedit'];
			break;    
		}
		
		case 'ntopic':
		{
    			$_POST['textedit'] = (empty($_POST['textedit'])) ? '' : $_POST['textedit'];
    			$_POST['ntopic'] = (empty($_POST['ntopic'])) ? '' : $_POST['ntopic'];
    			break;
		}
	}
}
//generating output page
if ($_GET['mode'] == 'rpost' || $_GET['mode'] == 'qpost')
{
	Secure::topic_exists(intval($_GET['id']));
	Secure::TopicLocked(intval($_GET['id']));
}
if ($_GET['mode'] == 'ntopic')
{
	$sql = "SELECT `lock`, `name`, `f_id` FROM ".FORUMS_TABLE." WHERE `f_id`='".intval($_GET['f'])."'";
	$forum = DataBase::fetch(DataBase::sql_query($sql, GENERAL, 'Could not obtain forum information'));
	if($forum['name']=='')
	{
		message_forum($lng['no_forum'], 'index.php');
	}
	if($forum['lock']=='1')
	{
		message_forum($lng['no_posting_forum_locked'],'index.php', 5);
	}
}
if ($_GET['mode']=='edit')
{
 	if (Topic::PostInformation(trim(strip_tags($_GET['id'])), 'p_id')== '')
 	{
    		message_forum($lng['no_message'], 'index.php');
	}
	
	if ((!User::RankAdminMod($_SESSION['uid'])) or ($_SESSION['uid']!=Topic::PostInformation($_GET['id'],'u_id')))
	{
		message_forum($lng['perror_2'], 'index.php');
 	}
}
switch ($_GET['mode'])
{
	case 'ntopic':
	{
		$window_title = $lng['writetopic'];
		$navigator_title = ''.$forum['name'].' » '.$lng['writetopic'];
		break;
	}
	
	case 'edit':
	{
  		$window_title = $lng['editpost'];
  		$navigator_title = ''.$lng['editpost'];
 	  	break;
	}
	
	case 'rpost':
	{
		$window_title = $lng['answer'].': '.Topic::TopicInformation(intval($_GET['id']),'name');
		$navigator_title = ''.$lng['answer'].': '.Topic::TopicInformation(intval($_GET['id']),'name');
		break;
	}
	
	case 'qpost':
	{
		$window_title = $lng['quote'];
		$navigator_title = ''.$lng['quote'];
		break;
	}
	
	// if no mode... :D
	default:
	{
		$stop = TimeGeneration();
		message_forum($lng['invalidmode'],'index.php');
		break;
	}
}
$skin = array_push_assoc($skin, GenerateHeader($window_title, $navigator_title));
$skin = array_push_assoc($skin, array(
'smiles'=>Post::SmilesShow(),
//labels
'lsmiles'=>$lng['smiles'],
'ltopicname'=>$lng['ltopicname'],
'lsave'=>$lng['save'],
'lreset'=>$lng['reset'],
'lmsg'=>$lng['message'],
'lsticky_topic'=>$lng['sticky_topic'],
'lwritetopic'=>$lng['writetopic'],
));
if ($msg=='')
{
	$msg='./skins/'.$default_skin.'/blank.tpl';
}
//do it!
require('./skins/'.$default_skin.'/overall_header.tpl');
require('./skins/'.$default_skin.'/posting_body.tpl');
require('./skins/'.$default_skin.'/overall_footer.tpl');
?>