<?php require_once('../Connections/poblets.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_poblets, $poblets);
$query_Recordset1 = "SELECT * FROM users WHERE registro LIKE '%si%'";
$Recordset1 = mysql_query($query_Recordset1, $poblets) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?><?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['ususario'])) {
  $loginUsername=$_POST['ususario'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "Nivel";
  $MM_redirectLoginSuccess = "admin.php";
  $MM_redirectLoginFailed = "index.php?login=0";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_poblets, $poblets);
  	
  $LoginRS__query=sprintf("SELECT `user`, pass, Nivel FROM users WHERE `user`=%s AND pass=%s",
  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $poblets) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
    
    $loginStrGroup  = mysql_result($LoginRS,0,'Nivel');
    
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Bienvenido a CMSPoblets</title>
<link href="../style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php include("cabecera.php"); ?>
<div id="container">
<form id="formlogin" name="formlogin" method="POST" action="<?php echo $loginFormAction; ?>">
<table id="tablalogin">
  <tr>
    <td colspan="4" class="cabeceranoticias" align="center"><?php if(isset($_GET['login']) && $_GET['login'] == 0) {
echo "ERROR - "; 
} ?>
Bienvenido a CMSPoblets
</td>
    </tr>
  <tr >
    <td class="sublogin">Nombre de usuario</td>
    <td colspan="3" class="sublogin">
        <input name="ususario" type="text" class="tablafill" id="ususario" size="32" />
</td>
  </tr>
  <tr>
    <td class="sublogin">Contraseña</td>
    <td colspan="3" class="sublogin"><label>
      <input name="password" type="password" class="tablafill" id="password" size="32" />
    </label></td>
  </tr>
  <tr>
    <td class="sublogin"></td>
    <td class="sublogin"><input name="login" type="submit" id="login" value="Acceder" class="tablafill"/></td>
  </tr>
</table>

</form>
</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
