<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

session_start();
require('config.php');
require_once('include/functions.php');
require_once('include/user_functions.php');
require_once('include/islogged.php');

$page = $_GET['page'];
$sortby = secure_sql($_GET['sortby']);
$order = secure_sql($_GET['order']);
$startwith = trim($_GET['letter']);//urldecode(trim($_GET['letter']));
$act = $_GET['do'];

if($sortby == 'name') {
	$sql_sortby = 'username';
	$order = 'ASC';
}
elseif($sortby == 'country') {
	$sql_sortby = 'country';
	$order = 'ASC';
}
elseif($sortby == 'lastseen') {
	$sql_sortby = 'last_signin';
	$order = 'DESC';
}

$limit = _BROWSER_PAGE;
$meta_title = _SITENAME." - ".$lang['members'];
$meta_description = "";

if($startwith != '')
{
	$is_letter = 1;
	if($startwith != 'other')
		$startwith = substr($startwith, 0, 1);
	if(!ctype_alnum($startwith))
		$is_letter = 0;
}
else
	$is_letter = 0;

if (empty($page) || !is_numeric($page) || $page < 0 ) { $page = 1 ; }

	$x = 2;
	$sql = "SELECT COUNT(*) as total_users FROM pm_users WHERE power IN ('".U_ACTIVE."', '".U_ADMIN."', '".U_MODERATOR."', '".U_EDITOR."')"; 
	if($is_letter == 1 && $startwith != 'other') {
		$sql .= " AND username LIKE '".$startwith."%'";
	}
	elseif($is_letter == 1 && $startwith == 'other')
	{
		for ($i = 97; $i < 123; $i++)
		{
			$sql .= " AND username NOT LIKE '".chr($i)."%'";
		}
	}
	//	Show online users only - MOD
	if(strtolower($act) == 'online') {
		$last_5_min = time() - (2 * 60);
		$sql .= " AND last_signin > '".$last_5_min."'";
	}
	
	$result = @mysql_query($sql);
	$row = @mysql_fetch_assoc($result);
	$total_items = $row['total_users'];
	//$total_items = @mysql_num_rows($result) ; 
	$total_pages = ceil($total_items / $limit) ; 
	$set_limit = $page * $limit - ($limit); 
	if($total_items == 0 && strtolower($act) == 'online') {
			$problem = $lang['memberlist_msg1'];
	}
	elseif($total_items == 0) {
			$problem = $lang['memberlist_msg2'];
	}
	mysql_free_result($result);
// OUTPUT DATA 
			if(isset($sortby) && isset($order)) {
				( empty($sql_sortby) ) ? $sql_sortby = "id" : "";
				( empty($order) ) ? $order = "DESC" : "";
			}
			$sql .= " ORDER BY ".$sql_sortby." $order LIMIT $set_limit, $limit";
			$sql = str_replace('COUNT(*) as total_users', '*', $sql);
			$result = mysql_query($sql) ; 
			$alt = 0; 
			$item = '';
			while ($row = mysql_fetch_array($result)) 
			{ 
			$col = ($alt % 2) ? 'row1' : 'row2';
			$alt++;
			$country = countryid2name($row['country']);
			$islive = islive($row['last_signin']);
			$last_seen = ucwords(time_since($row['last_signin'])).' '.$lang['ago'];
			if($islive == 1) 
				$status = '<img src="'._URL.'/templates/'._TPLFOLDER.'/images/ico_offline.gif"> '.$lang['memberlist_off'];
			else 
				$status = '<img src="'._URL.'/templates/'._TPLFOLDER.'/images/ico_online.gif"> '.$lang['memberlist_on'];

			if($row['avatar'] == "no_avatar.gif" && !empty($row['gender']))
				$row['avatar'] = $row['gender'].".gif";

			$item .= "
			  <tr class=\"$col\">
				<td><a href=\""._URL."/profile."._FEXT."?u=".$row['username']."\"><img src=\""._URL."/uploads/avatars/".$row['avatar']."\"  alt=\"".$row['username']."\" width=\"60\" height=\"60\" border=\"0\" /></a>
				</td>
				<td><a href=\""._URL."/profile."._FEXT."?u=".$row['username']."\">".$row['username']."</a></td>
				<td>".$country."</td>
				<td>".$status."<br />".$last_seen."</td>
			  </tr>";
			//$j++;
			} 
// END OUTPUT DATA
					
// SHOW LONG PAGINATION 
	$page_count_info = sprintf($lang['page'], $page)." ";//.$total_pages;
	if($total_pages == 0)
		$page_count_info .= "1";
	else
		$page_count_info .= $total_pages;
	$pag = '';
	$pag_left = '';
		if ($page > 1) // Prev Page 
		{ 
		$prev_page = $page-1; 
			if($prev_page >= 1) {
				if($is_letter == 1)
					$pag_left .= "<a href=\"memberlist."._FEXT."?page=".$prev_page."&letter=".$startwith."\">&laquo;";
				elseif(strtolower($act) == 'online')
					$pag_left .= "<a href=\"memberlist."._FEXT."?page=".$prev_page."&do=".$act."\">&laquo;";
				else
					$pag_left .= "<a href=\"memberlist."._FEXT."?page=".$prev_page."&sortby=".$sortby."\">&laquo;";
			}
		} 
	   // Page Numbers 
	   if ($page < $total_pages ) 
	   { 
	   $next_page = $page+1;
	   if($is_letter == 1)
	   		$pag_right = "<a href=\"memberlist."._FEXT."?page=".$next_page."&letter=".$startwith."\">&raquo;</a>" ;
		elseif(strtolower($act) == 'online')
			$pag_right = "<a href=\"memberlist."._FEXT."?page=".$next_page."&do=".$act."\">&raquo;</a>" ;
		else
	   		$pag_right = "<a href=\"memberlist."._FEXT."?page=".$next_page."&sortby=".$sortby."\">&raquo;</a>" ; 
	   }  
	$pag .= "</div>";
/// END SHOW LONG PAGINATION

$list_cats = list_categories(0, '');
$smarty->assign('list_categories', $list_cats);
$smarty->assign('problem', $problem);
$smarty->assign('gv_pagenumber', $page);
$smarty->assign('gv_sortby', $sortby);
$smarty->assign('pagination', $pag);
$smarty->assign('page_count_info', $page_count_info);
$smarty->assign('pag_left', $pag_left);
$smarty->assign('pag_right', $pag_right);

$smarty->assign('results', $item);
// --- DEFAULT SYSTEM FILES - DO NOT REMOVE --- //
$smarty->assign('meta_title', $meta_title);
$smarty->assign('meta_description', $meta_description);
$smarty->assign('template_dir', $template_f);
$smarty->display('memberlist.tpl');
?>