<?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>Añadir nuevo usuario - La Factoría Verde</title>
    <link rel="stylesheet" type="text/css" href="estilos.css">
</head>
<body>
<div id="layout">
  <div id="header"> <img src="../img/lafactoriaverde.gif" alt="La Factoría Verde" />
    <div style="float: right;">Sesión iciada como <?php echo $_SESSION[user_name]; ?></div>
    <?php include('_menu.php'); ?>
  </div>
  <div id="content">
<?php
//check for authority to view this page
if (allow_access(Administrators) != "yes")
{
	echo "<br/><br/><br/><center><p class='error'>No tienes autorización para esta función.</p></center>";
}

//check required fields
if (($_POST[username] == ""))
{
	echo "<br/><br/><br/><center><p class='error'>El campo 'Usuario' no puede estar en blanco. <a href=\"#\" onClick=\"history.go(-1)\">Vuelve atrás</a> y cámbialo.</p></center>";
	exit;
}

//check the password length
$pass_len = password_check($min_pass, $max_pass, $_POST[password]);
if ($pass_len == "no")
{
	echo "<br/><br/><br/><center><p class='error'>La contraseña debe tener una longitud entre $min_pass y $max_pass caracteres. <a href=\"#\" onClick=\"history.go(-1)\">Vuelve atrás</a> y cámbiala.</p></center>";
	exit;
}

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

//make query to database
$sql ="SELECT * FROM $table_name WHERE username= '$_POST[username]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());

//get the number of rows in the result set
$num = mysql_num_rows($result);

//check if that username already exists
if ($num != 0) {
	echo "<p>El usuario '".$_POST[username]."'ya existe.</p>";
	echo "<p><a href=\"#\" onClick=\"history.go(-1)\">Prueba con otro nombre de usuario.</a></p>";
	echo "$_POST[username]";
	exit;
} else {
	//or add it to the database
	$sql_add = "INSERT INTO $table_name (firstname, lastname, username, password, group1, group2, group3, 
				pchange, email, redirect, verified, last_login) VALUES
				('$_POST[firstname]', '$_POST[lastname]', '$_POST[username]', password('$_POST[password]'), 
				'$_POST[group1]', '$_POST[group2]', '$_POST[group3]', '$_POST[pchange]', '$_POST[email]',
				'$_POST[redirect]', '1', 'last_login()')";
	
	
	$result = @mysql_query($sql_add,$connection) or die(mysql_error());
}

/*if ($_POST[email_user] == "Yes")
{
	$mailheaders = "From: $domain\n";
	$mailheaders .= "Dear $_POST[firstname] $_POST[lastname],\n";
	$mailheaders .= "\n";
	$mailheaders .= "An account has been created for you at $domain.\n";
	$mailheaders .= "Please log in with the following account information:\n";
	$mailheaders .= "Username: $_POST[username]\n";
	$mailheaders .= "Password: $_POST[password]\n";
	$mailheaders .= "\n";
	$mailheaders .= "Please login at:\n";
	$mailheaders .= "$base_dir/login.html\n";
	$mailheaders .= "Should you have any complications, please email the System Administrator at:\n";
	$mailheaders .= "$adminemail\n";

	$to = "$_POST[email]";
	$subject = "Your account has been created !!";

	mail($to, $subject, $mailheaders, "From: No Reply <$adminemail>\n");
}*/

?>

<h2>Usuario añadido correctamente</h2>
<br/>
<table id="table1" width="100%" >
	<tr>
		<td width="177">Nombre:</td>
		<td><?php echo $_POST[firstname]; ?></td>
	</tr>
	<tr>
		<td width="177">Apellidos:</td>
		<td><?php echo $_POST[lastname]; ?></td>
	</tr>
	<tr>
		<td width="177">Usuario:</td>
		<td><?php echo $_POST[username]; ?></td>
	</tr>
	<tr>
		<td width="177">Contraseña:</td>
		<td><?php echo $_POST[password]; ?></td>
	</tr>
	<tr>
	<tr>
		<td width="177">E-Mail:</td>
		<td><?php echo $_POST[email]; ?></td>
	</tr>
		<td width="177">Grupo:</td>
		<td><?php echo $_POST[group1]; ?>&nbsp;</td>
	</tr>
	<tr>
		<td width="177">&nbsp;</td>
		<td><?php echo $_POST[group2]; ?>&nbsp;</td>
	</tr>
	<tr>
		<td width="177">&nbsp;</td>
		<td><?php echo $_POST[group3]; ?>&nbsp;</td>
	</tr>
	<tr>
		<td width="177">Redirigir a:</td>
		<td><?php echo $_POST[redirect]; ?></td>
	</tr>
	<tr>
		<td width="177">Cambio de contraseña:</td>
		<td><?php if($_POST[pchange] == "1"){ $ans1="Si"; } else { $ans1="No"; } echo $ans1; ?></td>
	</tr>
	<tr>
		<td width="177">Enviar e-mail al usuario:</td>
		<td><?php echo $_POST[email_user]; ?></td>
	</tr>
</table>
	<br/>
    <center>
      <a href="<?php echo "adminpage.php"; ?>">Volver a la lista</a>
    </center>
	</div>
  <div id="footer"> </div>
</div>
</body>
</html>