<?php
/** 
* @package	uForum 
* @file		users.php
* @version	$Id$ 
* @copyright	2009(c) PioDer <pioder@wp.pl>
* @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/classes/class_topic.php');
include('./includes/misc_functions.php');
include('./includes/classes/class_forum.php');
$start = TimeGeneration();
include('./lngs/'.DefaultLang().'/main.php');
SessDelInvalid();	
SessRegister();
SessDeleteOld();
$default_skin = ViewSkinName();	
//
//generate output pages
//
if ($_SESSION['uid']>0)
{
	$limiter = User::UserInformation($_SESSION['uid'], 'limit_users');
}
else
{
	$limiter = $forum_config['limit_users'];
}
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;
}
$count = DataBase::fetch(DataBase::sql_query("SELECT COUNT(`u_id`) as `u_id` 
FROM ".USERS_TABLE,GENERAL,'Could not obtain count amout of users'));
$count = $count['u_id'] -1;
$count = ceil($count / $limiter);
if(isset($_GET['page']) && ($_GET['page']>$count))
{
	message_forum($lng['invalidpage'],'index.php');
}
//
//end generating pages
//
if (isset($_COOKIE['users_desc'], $_POST['desc']))
{
	unset($_COOKIE['users_desc']);
}
if (isset($_POST['sort'],$_COOKIE['users_sort']))
{
	unset($_COOKIE['users_sort']);
}
if (!isset($_COOKIE['users_desc']))
{
	if (isset($_POST['desc']))
	{
   		switch($_POST['desc'])
   		{
       			case 'yes':
       			{
	           		@setcookie('users_desc','desc',time()+3600);
				$_COOKIE['users_desc'] = 'desc';
				$desc = 'DESC';	
				break;
			}
			case 'no':
			{
	    			@setcookie('users_desc','no',time()+3600);
				$_COOKIE['users_desc'] = 'no';
				$desc = '';
				break;
			}
		}
	}
	else
	{
   		@setcookie('users_desc','no',time()+3600);
		$_COOKIE['users_desc'] = 'no';
		$desc = '';
	}
}
else
{
  	$desc = ($_COOKIE['users_desc']=='desc') ? 'DESC' : '';
}
if (!isset($_COOKIE['users_sort']))
{
	if (isset($_POST['sort']))
	{
   		switch($_POST['sort'])
   		{
       			case 'regdate':
			{
				@setcookie('users_sort','regdate',time()+3600);
				$_COOKIE['users_sort'] = 'regdate';
				break;	
			}
			case 'lastvisit':
			{
				@setcookie('users_sort','lastvisit',time()+3600);
				$_COOKIE['users_sort'] = 'lastvisit';
				break;	
			}
			case 'uname':
			{
				@setcookie('users_sort','uname',time()+3600);
				$_COOKIE['users_sort'] = 'uname';
				break;	
			}
			case 'posts':
			{
				@setcookie('users_sort','posts',time()+3600);
				$_COOKIE['users_sort'] = 'posts';
				break;	
			}	
		}
	}
	else
	{
		@setcookie('users_sort','regdate',time()+3600);
		$_COOKIE['users_sort'] = 'regdate';
	}
}
//add skin variables
$skin = array(
'ldesc' => $lng['desc'],
'desc_yes_option'=>(($_COOKIE['users_desc']=='desc') || ((isset ($_POST['desc'])) && ($_POST['desc']=='yes'))) ? 'selected="selected"' : '',
'desc_no_option'=>(($_COOKIE['users_desc']=='no') || ((isset ($_POST['desc'])) && ($_POST['desc']=='no'))) ? 'selected="selected"' : '',
'regdate_option'=>(($_COOKIE['users_sort']=='regdate') || ((isset ($_POST['sort'])) && ($_POST['sort']=='posts'))) ? 'selected="selected"' : '',
'lastvisit_option'=>(($_COOKIE['users_sort']=='lastvisit') || ((isset ($_POST['sort'])) && ($_POST['sort']=='lastvisit'))) ? 'selected="selected"' : '',
'posts_option'=>(($_COOKIE['users_sort']=='posts') || ((isset ($_POST['sort'])) && ($_POST['sort']=='posts'))) ? 'selected="selected"' : '',
'uname_option'=>(($_COOKIE['users_sort']=='uname') || ((isset ($_POST['sort'])) && ($_POST['sort']=='uname'))) ? 'selected="selected"' : '',
'lselectusers'=>$lng['sort_u_by'],
'ltitle'=>$lng['lusers'],
'lyes'=>$lng['yes'],
'lno'=>$lng['no'],
'lregdate'=>$lng['luregister'],
'llastvisit'=>$lng['lulastvisit'],
'lposts'=>$lng['posts'],
'luname'=>$lng['user_name'],
'lgo'=>$lng['lgo']
);
$skin = array_push_associative($skin, GenerateHeader($lng['lusers'],'</a>&gt; <a href="users.php" class="navigator">'.$lng['lusers']));
//do it!
include('./skins/'.$default_skin.'/overall_header.tpl');
include('./skins/'.$default_skin.'/users_body.tpl');
switch($_COOKIE['users_sort'])
{
	case 'regdate':
	{
		$sql = "SELECT `u_id`,`nick`, `rank`, `regdate`, `lastvisit`, `posts` FROM `".USERS_TABLE."` WHERE `u_id`>0 ORDER BY `regdate` $desc $limit;";
		break;	
	}
	case 'lastvisit':
	{
		$sql = "SELECT `u_id`,`nick`, `rank`, `regdate`, `lastvisit`, `posts` FROM `".USERS_TABLE."` WHERE `u_id`>0 ORDER BY `lastvisit` $desc $limit;";
		break;	
	}
	case 'uname':
	{
		$sql = "SELECT `u_id`,`nick`, `rank`, `regdate`, `lastvisit`, `posts` FROM `".USERS_TABLE."` WHERE `u_id`>0 ORDER BY `nick` $desc $limit;";
		break;	
	}
	case 'posts':
	{
		$sql = "SELECT `u_id`,`nick`, `rank`, `regdate`, `lastvisit`, `posts` FROM `".USERS_TABLE."` WHERE `u_id`>0 ORDER BY `posts` $desc $limit;";
		break;	
	}
}
$query = DataBase::sql_query($sql,CRITICAL,'Could not obtain user information.');
while($result = DataBase::fetch($query))
{
	$skin = array(
	'id'=>$result['u_id'],
	'uname'=>Topic::UserName($result['nick'], $result['rank']),
	'regdate'=>date('d-m-Y, G:i',$result['regdate']),
	'lastvisit'=>($result['lastvisit']!='0') ? date('d-m-Y, G:i',$result['lastvisit']) : $lng['never'],
	'posts'=>$result['posts']
	);
		include('./skins/'.$default_skin.'/users_item_add_body.tpl');
}
$skin = array(
'option_pages'=>AddPages(),
'lwith'=>$lng['with'],
'lpage'=>$lng['page'],
'lpages'=>$count
);
include('./skins/'.$default_skin.'/users_end_body.tpl');
if($_SESSION['uid']>0)
{
	if(RANK==2)
	{
		$skin['pa_link']='<a href="admin/index.php" class="fsmall"><b>'.$lng['pa_link'].'</b></a>';
	}
	else
	{
		$skin['pa_link']='';
	}
}
else
{
	$skin['pa_link']=''; 
}
$stop = TimeGeneration();
$skin['queries'] =  ShowQueries($start, $stop);
include('./skins/'.$default_skin.'/overall_footer.tpl');
?>