* @link 	http://pioder.gim2przemysl.int.pl/
* @license	GNU GPL v3
**/
define('IN_uF', true);
//include files
include('./config.php');
include('./includes/constants.php');
include('./includes/db.php');
include('./includes/errors.php');
//connect to database
DataBase::db_connect();
include('./includes/sessions.php');
include('./includes/classes/class_user.php');
include('./common.php');
include('./includes/misc_functions.php');
include('./includes/classes/class_forum.php');
include('./includes/classes/class_topic.php');
include('./includes/classes/secure.php');
$default_lang = DefaultLang();
include('./lngs/'.$default_lang.'/main.php');
$start = TimeGeneration();
$default_skin = ViewSkinName();
SessDelInvalid();	
SessRegister();
SessDeleteOld();	
$sql = "SELECT ".POSTS_TABLE.".*, ".USERS_TABLE.".* FROM ".POSTS_TABLE." LEFT JOIN ".USERS_TABLE." ON ".USERS_TABLE.".u_id = ".POSTS_TABLE.".u_id ORDER BY `ptime`";
$query = DataBase::sql_query($sql,'GENERAL', 'Could not obtain amout of posts in forum');
while($result = @mysql_fetch_array($query))
{
	$lastpost[$result['t_id']]['tp_id']=$result['tp_id'];
	$lastpost[$result['t_id']]['u_id']=$result['u_id'];
	$lastpost[$result['t_id']]['time']=$result['ptime'];
	$lastpost[$result['t_id']]['user_nick']=$result['nick'];
	$lastpost[$result['t_id']]['user_rank']=$result['rank'];
}
$sql = "SELECT COUNT(*) as `p_id`, `t_id` FROM ".POSTS_TABLE." GROUP BY `t_id`";
$query = DataBase::sql_query($sql,'GENERAL', 'Could not obtain amout of posts in forum');
while($result = @mysql_fetch_array($query))
{
	$count_topic[$result['t_id']]=$result['p_id'];
}
if (!isset($_GET['content']))
{
	header('Location: search.php?content=posts');
}
//
//generate output pages
//
if ($_SESSION['uid']>0)
{
	$limiter = $userdata['limit_ftid'];
}
else
{
	$limiter = $forum_config['limit_ftid'];
}
if (isset($_GET['page'])&&($_GET['page']!=1))
{
	if (!is_numeric($_GET['page']))
	{
		die('Hacking attempt');
	}
	$value = ($_GET['page']-1)*$limiter;
	$limit = 'LIMIT '.$value . ', '.$limiter;
	$page = $_GET['page'];
}
else
{
  $limit = 'LIMIT 0, '.$limiter;
  $page=1;
}
//
//end generating pages
//
if ((isset($_POST['keywords'])) || ($_GET['content']=='lastposts') || ($_GET['content']=='userposts'))
{
	$errors = true;
	switch($_GET['content'])
	{
		case 'lastposts':
		{
			if ($_SESSION['uid']>0)
			{
				$time = time()-129600;
				$sql = "SELECT ".TOPICS_TABLE.".*, ".USERS_TABLE.".* FROM ".TOPICS_TABLE." LEFT JOIN ".USERS_TABLE." ON ".TOPICS_TABLE.".author = ".USERS_TABLE.".u_id WHERE lastpost_time>$time ORDER BY  `sticky` DESC, `lastpost_time` DESC LIMIT 60;";
				$errors = false;
				$window_title = $lng['showlast30posts'];
				$navigator_title = '> '.$lng['showlast30posts'];
				break;
			}
			else
			{
				message_forum($lng['youarenotlogd'],'login.php?mode=login');
				break;
			}
		}
		case 'userposts':
		{
			if (isset($_GET['u']))
			{
				$count = @mysql_fetch_array(DataBase::sql_query("SELECT COUNT(`t_id`) as `t_id` 
				FROM ".TOPICS_TABLE." WHERE `author`='".intval($_GET['u'])."'",'GENERAL','Could not obtain count amout of topics'));
				$count = $count['t_id'];
				$count = ceil($count / $limiter);
				if ($count==0)
				{
					$count +=1;
				}
				if(isset($_GET['page']) && ($_GET['page']>$count))
				{
					message_forum($lng['invalidpage'],'index.php');
				}
				$sql = "SELECT ".TOPICS_TABLE.".*, ".USERS_TABLE.".* FROM ".TOPICS_TABLE." LEFT JOIN ".USERS_TABLE." ON ".TOPICS_TABLE.".author = ".USERS_TABLE.".u_id WHERE `u_id`='".intval($_GET['u'])."' ORDER BY  `sticky` DESC, `lastpost_time` DESC $limit;";
				$errors = false;
				$window_title = $lng['view_user_topics'];
				$navigator_title = '> '.$lng['view_user_topics'];
				break;
			}
			else
			{
				message_forum($lng['no_user'],'index.php');
				break;
			}
		}
		case 'posts':
		{
			if(strlen(trim($_POST['keywords']))>=3)
			{
				$keyword = explode(' ', strip_tags(addslashes($_POST['keywords'])));
				$like_where = ' `text` LIKE \'%'.$keyword[0].'%\'';
				for($i=1; $i$count))
				{
					message_forum($lng['invalidpage'],'index.php');
				}
				$sql = "SELECT ".TOPICS_TABLE.".*, ".USERS_TABLE.".*, ".POSTS_TABLE.".* FROM ".TOPICS_TABLE." LEFT JOIN ".USERS_TABLE." ON ".TOPICS_TABLE.".author = ".USERS_TABLE.".u_id LEFT JOIN ".POSTS_TABLE." ON ".POSTS_TABLE.".t_id = ".TOPICS_TABLE.".t_id WHERE $like_where ORDER BY `sticky` DESC, `lastpost_time` DESC $limit;";
				$window_title = $lng['search_results'];
				$navigator_title = '> '.$lng['search_results'];
				$errors = false;
				
			}
			else
			{
				message_forum($lng['too_short_keywords'],'search.php?content=posts');
			}
			break;
		}
	}
	if (!$errors)
	{
		//add skin variables
		$skin = array(
		'lposts'=>$lng['posts'],
		'llastposts'=>$lng['lastpost'],
		'lposts'=>$lng['posts'],
		'lauthor'=>$lng['author'],
		'llastpost'=>$lng['lastpost'],
		'ltopicname'=>$lng['ltopicname'],
		'lang'=> $default_lang
		);
		$skin = array_push_associative($skin,GenerateHeader($window_title,$navigator_title));
		include('./skins/'.$default_skin.'/overall_header.tpl');
		include('./skins/'.$default_skin.'/forum_body.tpl'); 
		$query = DataBase::sql_query($sql,'GENERAL', 'Could not obtain topics information');
		$value = mysql_num_rows($query);
		if ($value>0)
		{
			$isset_topics = array();
			while($record = mysql_fetch_array($query))
			{
				if (!in_array($record['t_id'], $isset_topics))
				{
					$skin = array(
					't_id'=>$record['t_id'],
					'fname'=>($record['sticky']=='1') ? ''.$lng['sticky'].''.$record['name'] : $record['name'],
					'author'=>Topic::TopicAuthor($record['author']),
					'new_post'=>Topic::LastPostImg(),
					'tposts'=>$count_topic[$record['t_id']],
					'lastpost'=>Topic::LastPostInTopic($record['t_id'])
					);
					include('./skins/'.$default_skin.'/forum_forum_add.tpl');
					array_push($isset_topics, $record['t_id']);
				}	
			}
		}
		else 
		{
			echo ''.$lng['nopost'].'!  | 
';
		}
		if ($_GET['content']=='lastposts')
		{
			echo '';
		}
		else
		{
			$skin = array( 
			'option_pages' => AddPages(),
			'lwith' => $lng['with'],
			'lpage' => $lng['page'],
			'lpages' => $count,
		);
			include('./skins/'.$default_skin.'/search_end_body.tpl');
		} 
	} 	
}
else
{
	$skin = array(
	'mainpage'=>$lng['lsearch'],
	'lsubmit'=>$lng['search'],
	'lreset'=>$lng['reset'],
	'insert_keywords'=>$lng['insert_keywords']
	);
	$window_title = $lng['lsearch'];
	$navigator_title = '> '.$lng['lsearch'];
	$skin = array_push_associative($skin,GenerateHeader($window_title,$navigator_title));
	include('./skins/'.$default_skin.'/overall_header.tpl');
	include('./skins/'.$default_skin.'/search_body.tpl');
}
if ($_SESSION['uid']>0)
{
	if(RANK=='2')
	{
		$skin['pa_link']=''.$lng['pa_link'].'';
	}
	else
	{
		$skin['pa_link']='';
	}
}
else
{
	$skin['pa_link']='';
}
$stop = TimeGeneration();
$skin['queries'] =  ShowQueries($start, $stop);
include('./skins/'.$default_skin.'/overall_footer.tpl');
?>