<?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.
// +------------------------------------------------------------------------+

$showm = '6';
include('header.php');

$action = (int) $_GET['a'];
$userid = (int) trim($_GET['uid']);

$page = (int) $_GET['page'];

if(empty($page))
	$page = 1;
$limit = 50;	//	users per page
$from = $page * $limit - ($limit);

$filter = '';
$filters = array('power'); 
$filter_value = '';

if(in_array(strtolower($_GET['filter']), $filters) !== false)
{
	$filter = strtolower($_GET['filter']);
	$filter_value = $_GET['fv'];
}

// Action buttons
if ($_POST['Submit'] == 'Activate checked' && (is_admin() || is_moderator()))
{
	$user_ids = $_POST['user_ids'];
	if (count($user_ids) > 0)
	{
		$sql = "UPDATE pm_users 
				SET power = '". U_ACTIVE ."' 
				WHERE id IN (". implode(',', $user_ids) .") 
				  AND power = '". U_INACTIVE ."'";
		$result = @mysql_query($sql);
		
		if ( ! $result)
		{
			$info_msg = '<div class="info_msg_errimg">There was an error while updating your database.<br />MySQL returned: '.mysql_error().'</div>';
		}
		else
		{
			$info_msg = '<div class="info_msg_ok">The selected user accounts have been updated.</div>';
		}
	}
	else
	{	
		$info_msg = '<div class="info_msg">No items were selected!</div>';
	}
}
else if ($_POST['Submit'] == 'Delete checked' && (is_admin() || is_moderator()))
{
	$user_ids = $_POST['user_ids'];
	$total = count($user_ids);
	
	// exclude self;
	if ($total > 0)
	{
		foreach ($user_ids as $k => $id)
		{
			if ($userdata['id'] == $id)
			{
				unset($user_ids[$k]);
				$total--;
				break;
			}
		}
	}
	
	if ($total > 0)
	{
		if (is_admin())
		{
			$sql = "DELETE FROM pm_users 
					WHERE id IN (". implode(',', $user_ids) .") 
					  AND power != '". U_ADMIN ."'";
		}
		else // is moderator
		{
			$sql = "DELETE FROM pm_users 
					WHERE id IN (". implode(',', $user_ids) .") 
					  AND (power = '".U_ACTIVE."'
					  	   OR power = '".U_INACTIVE."')"; 
		}
		
		$result = @mysql_query($sql);
		if ( ! $result)
		{
			$info_msg = '<div class="info_msg_errimg">There was an error while updating your database.<br />MySQL returned: '.mysql_error().'</div>';
		}
		else
		{
			$affected_rows = mysql_affected_rows();
			if ($affected_rows == 0)
			{
				$info_msg = '<div class="info_msg_ok">No user accounts deleted.</div>';
			}
			else if ($affected_rows > 1)
			{
				$info_msg = '<div class="info_msg_ok">'. $affected_rows .' user accounts have been deleted.</div>';
			}
			else
			{
				$info_msg = '<div class="info_msg_ok">1 user account deleted.</div>';
			}
		}
	}
	else
	{	
		$info_msg = '<div class="info_msg">No items were selected!</div>';
	}
}
else if ($_POST['Submit'] == 'Delete checked' && !(is_admin() || is_moderator()))
{
	$info_msg = '<div class="alert-warning">Sorry, only Administrators are allowed to perform this action.</div>';
}


// DELETE A USER
if($action == 1) 
{ 
	$query = mysql_query("SELECT * FROM pm_users WHERE id = '".$userid."'");
	$result = mysql_fetch_array($query);
		
	if (is_moderator() && in_array($result['power'], array(U_ADMIN, U_MODERATOR, U_EDITOR)))
	{
		$info_msg = '<div class="info_msg_err">You can\'t delete this account!</div>';
	}
	else if ($result['power'] == U_ADMIN) 
	{
		$info_msg = '<div class="info_msg_err">You can\'t delete the administrator\'s account!</div>';
	} 
	else 
	{
		$result = @mysql_query("DELETE FROM pm_users WHERE id = '".$userid."'");
		if(!$result)
		{
			$info_msg = '<div class="info_msg_err">An error occurred!<br />MySQL Reported: '.mysql_error().'</div>';
		}
		else
			$info_msg = '<div class="info_msg_ok">User ID #<b>'.$userid.'</b> has been deleted from the database.</div>';
	}
}

//	Search
if(!empty($_POST['submit']) || !empty($_GET['submit']))
{
	$search_query = ($_POST['keywords'] != '') ? trim($_POST['keywords']) : trim($_GET['keywords']);
	$search_type = ($_POST['search_type'] != '') ? $_POST['search_type'] : $_GET['search_type'];
	$members = a_list_users($search_query, $search_type,  $from, $limit, $page);
	$total_members = $members['total'];
} 
else 
{
	$total_members = count_entries('pm_users', $filter, $filter_value);
	if($total_members - $from == 1)
		$page--;
		
	$members = a_list_users('', '', $from, $limit, $page, $filter, $filter_value);
	if($total_members - $from == 1)
		$page++;
}

// generate smart pagination
$filename = 'members.php';
$uri = $_SERVER['REQUEST_URI'];
$uri = explode('?', $uri);

$pagination = '';
$pagination = a_generate_smart_pagination($page, $total_members, $limit, 1, $filename, $uri[1]);


?>
<div id="sub_menu">
<ul>
<li><a href="banlist.php">Manage ban list</a></li>
<?php
if (is_admin())
{
	?>
	
	<li><a href="members_export.php" class="hintanchor" onMouseover="showhint('The exported CSV can be used with \'<strong>FaceBook</strong> Friend Import\', \'<strong>Outlook 2007</strong>\', \'<strong>Gmail</strong> Contacts\' and so on. You can use this to keep in touch with your members and to create new communities around \'<strong><?php echo _SITENAME; ?></strong>\' on social sites.', this, event, '500px')">Export to CSV</a></li>
	
	<?php
}
?>
</ul>
</div>
<div id="content">
<h2>Users</h2>

<?php echo $info_msg; ?>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
  <tr>
    <td width="420" valign="top">
	<form name="search" action="members.php" method="get" onsubmit="return validateSearch('true');">
	<input type="text" name="keywords" value="<?php echo $_GET['keywords']; ?>" />
	<select name="search_type">
	 <option value="username" <?php echo ($_GET['search_type'] == "username") ? 'selected="selected"' : ''; ?> >Username</option>
	 <option value="fullname" <?php echo ($_GET['search_type'] == "fullname") ? 'selected="selected"' : ''; ?> >Full Name</option>
	 <option value="email" <?php echo ($_GET['search_type'] == "email") ? 'selected="selected"' : ''; ?> >Email</option>
	 <option value="ip" <?php echo ($_GET['search_type'] == "ip") ? 'selected="selected"' : ''; ?> >IP Address</option>
	</select> &nbsp;
	<input name="submit" type="submit" value="Search" class="inputbutton" /><br />
	</form>
	</td>
    <td width="763" valign="top">
	</td>
    <td width="220" valign="top"><div class="totals">Total members: <?php echo $total_members; ?></div>
	</td>
  </tr>
  <tr>
  	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td align="right" valign="top">
	 <div id="sort_by">
	 <small><strong>Filter: </strong></small>&nbsp;
	 <form name="power_filter" action="members.php" method="get">
	 <input type="hidden" name="filter" value="power" />
	 <select name="fv" onChange="submit()">
	 	<option value="">by user level ...</option>
	 	<option value="1" <?php if ($filter_value == '1') echo 'selected="selected"'; ?> >Admin</option>
		<option value="3" <?php if ($filter_value == '3') echo 'selected="selected"'; ?> >Moderator</option>
		<option value="4" <?php if ($filter_value == '4') echo 'selected="selected"'; ?> >Editor</option>
		<option value="0" <?php if ($filter_value == '0') echo 'selected="selected"'; ?> >Regular user</option>
		<option value="2" <?php if ($filter_value == '2') echo 'selected="selected"'; ?> >Inactive</option>
	 </select>
	 </form>
	 <a href="members.php" title="Remove filter"><img src="images/ico_filter_remove.gif" alt="Remove filter" width="21" height="15" border="0" align="absmiddle" /></a>
	 </div>
	</td>
  </tr>
</table>

<form name="users_checkboxes" id="users_checkboxes" action="members.php?page=<?php echo $page;?>&filter=<?php echo $filter;?>&fv=<?php echo $filter_value;?>" method="post">
<table id="usersSortTable" class="tablesorter" cellspacing="1">
 <thead>
  <tr>
   <th align="center" width="3%"><input type="checkbox" name="checkall" onclick="checkUncheckAll(this);"/></th>
   <th width="50">ID</th>
   <th>Username</th>
   <th>Full name</th>
   <th>Register date</th>
   <th>Last Login</th>
   <th>Email</th>
   <th>IP</th>
   <th >Status/Action</th>
  </tr>
 </thead>
 <tbody>
  <?php echo $members['users']; ?>
 </tbody>
</table>

<!-- bottom action buttons -->
<table>
    <tr style="background-color: #fff;">
        <td align="left">
            <input type="submit" name="Submit" value="Activate checked" class="approvebutton" />
        </td>
		<td align="left">
	 		<input type="submit" name="Submit" value="Delete checked" class="deletebutton" onClick="return confirm_delete_all();" /> 
		</td>
    </tr>
</table>
</form>

<div align="center"><?php echo $pagination; ?></div>
</div>
<?php
include('footer.php');
?>