<?php
//prevents caching
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter();
session_start();

require('../config.php');
require('../functions.php');

//check for administrative rights
if (allow_access(Administrators) != "yes")
{
	include ('../check_login.php');
	exit;
}

//make connection to dbase
$connection = @mysql_connect($server, $dbusername, $dbpassword)
			or die(mysql_error());
			
$db = @mysql_select_db($db_name,$connection)
			or die(mysql_error());

//build and issue the query
$sql ="SELECT * FROM $table_name";
$result = @mysql_query($sql,$connection) or die(mysql_error());

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Language" content="es-ES" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Administración - La Factoría Verde</title>
    <link rel="stylesheet" type="text/css" href="estilos.css">
</head>

<body>
<div id="layout">
  <div id="header"> <img style="float: left;" src="../img/lafactoriaverde.gif" alt="La Factoría Verde" />
    <div style="float: right;">Sesión iniciada como <?php echo $_SESSION[user_name]; ?></div>
    
    <?php include('_menu.php'); ?>
  </div>
  <div id="content">
    <div id="search">
      <form method="GET" action="<?php echo $_SERVER['PHP_SELF']."?page=$page&max_results=$max_results&search="; ?>">
        <p>Búsqueda:&nbsp;
          <input type="text" name="search" size="20">
          <input type="submit" value="Buscar" name="B1">
        </p>
      </form>
    </div>
	<br/>

<?php
	if ($user_det == "") {
		if (!isset($_GET['max_results'])) {
			$max_results = 5;
		}
		else {
			$max_results = $_GET['max_results'];
		}

		if (!isset($_GET['page'])) {
			$page = 1;
		}
		else {
			$page = $_GET['page'];
		}

		if (isset($_GET['search'])) {
			$s_string = $_GET['search'];
			$explodeit = explode(" ", "$s_string");
			$c = count($explodeit);
			if ($c > 1) {
				for ($i = 0; $i < $c; $i++) {
					$search = $explodeit[$i] . "+";
				}
			}
			else {
				$search = $s_string;
			}
		}

		echo "Número de resultados por página: <a href=\"" . $_SERVER['PHP_SELF'] . "?page=$page&max_results=5&search=$search\">5</a>";
		echo " <a href=\"" . $_SERVER['PHP_SELF'] . "?page=$page&max_results=10&search=$search\">10</a><br/><br/>";

		// Figure out the limit for the query based
		// on the current page number.

		$from = (($page * $max_results) - $max_results);

		// Perform MySQL query on only the current page number's results
		if (!isset($_GET['search'])) {
			$set_lim = "SELECT * FROM $table_name ORDER BY username LIMIT $from, $max_results";
		}
		else {
			$search = $_GET['search'];
			$set_lim = "SELECT * FROM $table_name WHERE username LIKE '%$search%' || firstname LIKE '%$search%' 
			|| lastname LIKE '%$search%' || group1 LIKE '%$search%' || group2 LIKE '%$search%' || group3 LIKE '%$search%' 
			|| email LIKE '%$search%' ORDER BY username LIMIT $from, $max_results"; 	
		}	

		$set_res = @mysql_query($set_lim, $connection) or die(mysql_error());
		while ($set_lim = mysql_fetch_object($set_res)) {
			$fname = $set_lim->firstname;
			$lname = $set_lim->lastname;
			$uname = $set_lim->username;
			$p_change = $set_lim->pchange;
			$verif_d = $set_lim->verified;
			$last = $set_lim->last_login;
			$re_direct = $set_lim->redirect;
			$groupA = $set_lim->group1;
			$groupB = $set_lim->group2;
			$groupC = $set_lim->group3;
			$e_mail = $set_lim->email;
			if ($p_change == 1) {
				$p_change = "Yes";
			}
			else {
				$p_change = "No";
			}

			if ($verif_d == "0") {
				$verif_d = "No";
			}
			else {
				$verif_d = "Yes";
			}

			echo "<table border=\"1\" width=\"100%\" id=\"table5\">";
			echo "<tr>";
			echo "<td width=\"100\"><strong><a href=\"userinfo.php?user_det=$uname\">$uname</a></strong></td>";
			echo "<td width=\"80\">E-Mail:</font></td>";
			echo "<td><a href=\"mailto:$e_mail\">$e_mail</a></td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td width=\"100\">";
			echo "<a href=\"changeuser.php?username=$uname\">Modificar</a>";
			echo "&nbsp;&nbsp;";
			if ($uname != $_SESSION[user_name]) {
				echo "<a href=\"deleteuser.php?del_user=$uname\" onclick=\"return confirm('¿Desea eliminar el usuario $uname?')\">Eliminar</a>"; 
				/*echo "<a href=\"deleteuser.php?del_user=$uname\">Eliminar</a>";*/
			}
			echo "</td>";
			echo "<td width=\"100\">Nombre:</td>";
			echo "<td>$fname $lname</td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td width=\"100\">&nbsp;</td>";
			echo "<td width=\"100\">Último acceso:</td>";
			echo "<td>$last</td>";
			echo "</tr>";
			echo "</table><br/>";
		}

		// Figure out the total number of results in DB:
		if (!isset($_GET['search'])) {
			$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM authorize") , 0);
		}
		else {
			$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM authorize
				WHERE username LIKE '%$search%' || firstname LIKE '%$search%' 
				|| lastname LIKE '%$search%' || group1 LIKE '%$search%' || group2 LIKE '%$search%' || group3 LIKE '%$search%' 
				|| email LIKE '%$search%'"),0); 
		}

		// Figure out the total number of pages. Always round up using ceil()
		$total_pages = ceil($total_results / $max_results);
		if ($total_pages > 1) {
			// Build Page Number Hyperlinks
			echo "<center>Selecciona una página<br />";
			// Build Previous Link
			if($page > 1) {
				$prev = ($page - 1); 
				echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev&max_results=$max_results&search=$search\">&lt;&lt;&nbsp;&nbsp; </a> "; 
			} 
			
			for($i = 1; $i <= $total_pages; $i++) {
				if(($page) == $i) { 
					echo "$i "; 
				} else { 
					echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&max_results=$max_results&search=$search\">$i</a> "; 
				} 
			} 

			// Build Next Link 
			if($page < $total_pages) { 
				$next = ($page + 1); 
				echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next&max_results=$max_results&search=$search\">&nbsp;&nbsp; &gt;&gt;</a>"; 
			} 
			echo "</center>"; }
		}
	
	
if ($search != "")
{
	if ($ret_max)
	{ $page=$ret_page; $max_results=$ret_max; }
	echo "<center><a href=\"".$_SERVER['PHP_SELF']."?page=$page&max_results=$max_results\">Ver toda la lista</a>";
}else{
echo "<br><br>";
}
?>
    </td>
    </tr>
    </table>
  </div>
  <div id="footer"> </div>
</div>


</body>
</html>