<?php
/** 
* @package	Dynamic Script Forum 
* @file		posting.php
* @version	1.0.x, 08-03-2007,17:39 
* @copyright	2008(c) PioDer <pioder@wp.pl>
* @link 	http://pioder.gim2przemysl.int.pl/dsf.html
* @license	GNU GPL v3
**/
define('IN_uF', true);
//include files
include('./config.php');
include('./includes/constants.php');
include('./includes/class_db.php');
include('./includes/class_error.php');
//connect to database
DataBase::db_connect();
include('./includes/sessions.php');
include('./includes/class_user.php');
include('./common.php');
include('./includes/class_overall.php');
include('./includes/class_forum.php');
include('./includes/class_posting.php');
include('./includes/class_topic.php');
include('./includes/classes/secure.php');
include('./lngs/'.Over::DefaultLang().'/main.php');
$start = Over::TimeGeneration();
$default_skin = Over::ViewSkinName();
sess_del_invalid($_SESSION['uid']);	
sess_register($_SESSION['uid']);
sess_delete_old();
$msg='';
if ($_SESSION['uid']<1)
{
	message_forum($lng['youarenotlogd'],'login.php?mode=login');
}
foreach ($_POST as $name => $value)
{
	if ($forum_config['use_censorlist'])
	{
		$_POST[$name] = Secure::UseCensorlist($value);
	}
}
$start = Over::TimeGeneration();
//add skin variables
$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'],
);
$skin = array_push_associative($skin, Over::generate_header('',''));
$stop = Over::TimeGeneration();
$skin['queries'] =  Over::ShowQueries($start, $stop);
if(RANK=='2')
{
	$skin['pa_link']='<a href="admin/index.php" class="fsmall"><b>'.$lng['pa_link'].'</b></a>';
}
else
{
	$skin['pa_link']='';
}
switch(trim($_GET['mode']))
{
	/**
	*
	functions for reply: normal, editing, quoting...
	*	
	**/
	case 'qpost': //quote post
	{
		$msg ='';
		if (isset($_POST['textedit']))
		{
			if (($_POST['textedit'] !='' ) and (strlen(trim($_POST['textedit']))>10))
			{
				if (isset($_COOKIE['antiflood_time']))
				{
					if ($_COOKIE['antiflood_time']>time())
					{
						message_forum($lng['antiflood_enabled'],$_SERVER['REQUEST_URI']);
					}
				}
				else
				{
					setcookie('antiflood_time',(time() + $forum_config['time_antiflood']));
				}
				$_POST['textedit'] = Secure::TagsReplace($_POST['textedit']);
				$id =  Post::NewPost(intval($_GET['t']),$_POST['textedit'], $_SESSION['uid']);
				$count = ceil(($id / 15 ));
				if ($count >1)
				{
	  				$page= '&amp;page='.$count;
				}
				else
				{
	    				$page='';
				}
				$stop = Over::TimeGeneration();
				message_forum($lng['post_is_saved'],'topic.php?t='.$_GET['t'].$page.'#'.$id);
			}
			else
			{
				$message =  $lng['perror_1'];
				$msg = './skins/'.$default_skin.'/post_error_body.tpl';
			}
		}	
		else
		{
			$_POST['textedit']='[quote]'.stripslashes(Topic::PostInformation($_GET['id'],'text')).'[/quote]';
		}
		Secure::topic_exists(intval($_GET['t']));
		Secure::TopicLocked(intval($_GET['t']));
		$skin['mainpage'] = $lng['quote'];
		$skin['lmainpage'] = '</a>&gt; <a href="posting.php?mode=qpost&amp;id='.$_GET['id'].'&amp;t='.$_GET['t'].'" 
	    	  class="navigator">'.$lng['quote'];
		$skin['f_id'] = $_GET['id'];
		$skin['action'] = 'posting.php?mode=qpost&amp;id='.$_GET['id'].'&amp;t='.$_GET['t'];
		if ($msg=='')
		{
			$msg='./skins/'.$default_skin.'/blank.tpl';
		}		
		include('./skins/'.$default_skin.'/overall_header.tpl');
		include('./skins/'.$default_skin.'/posting_body.tpl');
		include('./skins/'.$default_skin.'/overall_footer.tpl');
		break;
	}
	case 'edit': //edit post
	{
		if ((User::RankAdminMod($_SESSION['uid'])) or ($_SESSION['uid']==Topic::PostInformation($_GET['id'],'u_id')))
		{
			$msg ='';
			if (isset($_POST['textedit']))
			{
				if (($_POST['textedit'] !='' ) and (strlen(trim($_POST['textedit']))>10))
				{
					if (isset($_COOKIE['antiflood_time']))
					{
						if ($_COOKIE['antiflood_time']>time())
						{
							message_forum($lng['antiflood_enabled'],$_SERVER['REQUEST_URI']);
						}
					}
					else
					{
						setcookie('antiflood_time',(time() + $forum_config['time_antiflood']));
					}
					$_POST['textedit'] = Secure::TagsReplace($_POST['textedit']);
					Post::EditPost(intval($_GET['id']),$_POST['textedit']);
					$count = ceil((Topic::PostInformation(intval($_GET['id']),'tp_id') / 15 ));
					if ($count >1)
					{
	  					$page= '&amp;page='.$count;
					}
					else
					{
	    					$page='';
					}
					$stop = Over::TimeGeneration();
					message_forum($lng['post_is_saved'],'topic.php?t='.Topic::PostInformation(intval($_GET['id']),'t_id').$page.'#p'.$_GET['id']);
			}
			else
			{
				$message =  $lng['perror_1'];
				$msg = './skins/'.$default_skin.'/post_error_body.tpl';
			}
			}
			else
			{
				$_POST['textedit']=stripslashes(Topic::PostInformation($_GET['id'],'text'));
			}
		}
		$skin['mainpage'] = $lng['editpost'];
		$skin['lmainpage'] = '</a>&gt; <a href="posting.php?mode=edit&amp;id='.intval($_GET['id']).'" 
		    	  class="navigator">'.$lng['editpost'];
		$skin['f_id'] = intval($_GET['id']);
		$skin['action'] = 'posting.php?mode=edit&amp;id='.intval($_GET['id']);
		if ($msg=='')
		{
			$msg='./skins/'.$default_skin.'/blank.tpl';
		}
		include('./skins/'.$default_skin.'/overall_header.tpl');
		include('./skins/'.$default_skin.'/posting_body.tpl');
		include('./skins/'.$default_skin.'/overall_footer.tpl');
		break;
	}
	
	case 'rpost': //reply post
	{
		$msg ='';
		if (isset($_POST['textedit']))
		{
			if (($_POST['textedit'] !='' ) and (strlen(trim($_POST['textedit']))>10))
			{
				if (isset($_COOKIE['antiflood']))
				{
					if ($_COOKIE['antiflood_time']>time())
					{
						message_forum($lng['antiflood_enabled'],$_SERVER['REQUEST_URI']);
					}
				}
				else
				{
					setcookie('antiflood_time',(time() + $forum_config['time_antiflood']));
				}
				$_POST['textedit'] = Secure::TagsReplace($_POST['textedit']);
				$id =  Post::NewPost(intval($_GET['id']),$_POST['textedit'], $_SESSION['uid']);
				$count =ceil(($id / 15 ));
				if ($count >1)
				{
	  				$page= '&amp;page='.$count;
				}
				else
				{
	    				$page='';
				}
				$stop = Over::TimeGeneration();
				message_forum($lng['post_is_saved'],'topic.php?t='.$_GET['id'].$page.'#p'.$id);
			}
			else
			{
				$message =  $lng['perror_1'];
				$msg = './skins/'.$default_skin.'/post_error_body.tpl';
			}
		}
		else
		{
			$_POST['textedit']='';
		}
		Secure::topic_exists(intval($_GET['id']));
		Secure::TopicLocked(intval($_GET['id']));
		$skin['mainpage'] = $lng['answer'].': '.Topic::TopicInformation(intval($_GET['id']),'name');
		$skin['lmainpage'] = '</a>&gt; <a href="posting.php?mode=rpost&amp;id='.intval($_GET['id']).'" 
		    class="navigator">'.$lng['answer'].': </a><a href="topic.php?t='.$_GET['id'].'" 
		    class="navigator">'.Topic::TopicInformation(intval($_GET['id']),'name');
		$skin['f_id'] = $_GET['id'];
		$skin['action'] = 'posting.php?mode=rpost&amp;id='.$_GET['id'];
		if ($msg=='')
		{
			$msg='./skins/'.$default_skin.'/blank.tpl';
		}
		include('./skins/'.$default_skin.'/overall_header.tpl');
		include('./skins/'.$default_skin.'/posting_body.tpl');
		include('./skins/'.$default_skin.'/overall_footer.tpl');		
		break;
	}
	/**
	*
	Select new TOPIC
	*
	**/
	case 'ntopic': //new topic
	{
		$msg ='';
		if ((isset($_POST['textedit'])) and (isset($_POST['ntopic'])))
		{
			if (($_POST['textedit'] !='' ) and ($_POST['ntopic']!='') and (strlen(trim($_POST['textedit']))>10) and (strlen(trim($_POST['ntopic']))>5))
			{
				$_POST['textedit'] = Secure::TagsReplace($_POST['textedit']);
				if (isset($_COOKIE['antiflood_time']))
				{
					if ($_COOKIE['antiflood_time']>time())
					{
						message_forum($lng['antiflood_enabled'],$_SERVER['REQUEST_URI']);
					}
				}
				else
				{
					setcookie('antiflood_time',(time() + $forum_config['time_antiflood']));
				}
				$_POST['ntopic'] = strip_tags(addslashes($_POST['ntopic']));
				if (isset($_POST['topic_sticky']))
				{
		    			$_POST['topic_sticky']='1';	
				}
				else
				{
		    			$_POST['topic_sticky']='0';
				}
				$last = Post::NewTopic($_POST['textedit'],$_POST['ntopic'], intval($_GET['f']), $_SESSION['uid'], $_POST['topic_sticky']);
				$stop = Over::TimeGeneration();
				message_forum($lng['post_is_saved'],'topic.php?t='.$last);
			}
			else
			{
				$message =  $lng['perror_1'];
				$msg = './skins/'.$default_skin.'/post_error_body.tpl';
			}
		}
		else
		{
			$_POST['ntopic'] = '';
			$_POST['textedit']='';
		}
		$sql = "SELECT `lock`, `name`, `f_id` FROM ".FORUMS_TABLE." WHERE `f_id`='".intval($_GET['f'])."'";
		$forum = mysql_fetch_array(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');
		}
		$skin['mainpage'] = $lng['writetopic'];
		$skin['action'] = $_SERVER['REQUEST_URI'];
		$skin['lmainpage'] = '</a>&gt; <a href="forum.php?f='.$_GET['f'].'" class="navigator">'.$forum['name'].'</a> &gt;<a href="posting.php?mode=ntopic&amp;f='.$_GET['f'].'" class="navigator">'.$lng['writetopic'];
		$skin['f_id']=$_GET['f'];
		if ($msg=='')
		{
			$msg='./skins/'.$default_skin.'/blank.tpl';
		}
		include('./skins/'.$default_skin.'/overall_header.tpl');
		include('./skins/'.$default_skin.'/posting_body.tpl');
		include('./skins/'.$default_skin.'/overall_footer.tpl');
		break;
	}
	// if no mode... :D
	default:
	{
		$stop = Over::TimeGeneration();
		message_forum($lng['invalidmode'],'index.php');
		break;
	}			
}