<?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_Destacado = "SELECT Titulo, Autor, Resumen, Publicada FROM noticias WHERE destacada LIKE '%true%'";
$Destacado = mysql_query($query_Destacado, $poblets) or die(mysql_error());
$row_Destacado = mysql_fetch_assoc($Destacado);
$totalRows_Destacado = mysql_num_rows($Destacado);

$maxRows_noticias = 4;
$pageNum_noticias = 0;
if (isset($_GET['pageNum_noticias'])) {
  $pageNum_noticias = $_GET['pageNum_noticias'];
}
$startRow_noticias = $pageNum_noticias * $maxRows_noticias;

mysql_select_db($database_poblets, $poblets);
$query_noticias = "SELECT Id, Titulo, Autor, Fecha, Resumen, Noticia, Publicada, destacada FROM noticias WHERE destacada LIKE '%false%' AND Publicada LIKE '%true%' ORDER BY Fecha DESC";
$query_limit_noticias = sprintf("%s LIMIT %d, %d", $query_noticias, $startRow_noticias, $maxRows_noticias);
$noticias = mysql_query($query_limit_noticias, $poblets) or die(mysql_error());
$row_noticias = mysql_fetch_assoc($noticias);

if (isset($_GET['totalRows_noticias'])) {
  $totalRows_noticias = $_GET['totalRows_noticias'];
} else {
  $all_noticias = mysql_query($query_noticias);
  $totalRows_noticias = mysql_num_rows($all_noticias);
}
$totalPages_noticias = ceil($totalRows_noticias/$maxRows_noticias)-1;
?><?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/admin.php";
  $MM_redirectLoginFailed = "documentos.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>Portal de castellar-Oliveral</title>
<link href="index.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="container">
<div id="top_header"><img src="img/logo.png" alt="Poblets" width="130" height="25" /></a></div>
<div id="header"></div>
<table id="sub_header" width="100%" cellpadding="2" cellspacing="0">
<tr>
<?php include("menutop.php"); ?>
</tr>
</table>
<span id="menuizcont">
<?php include("menu.php"); ?>
<form id="formlogin" name="formlogin" method="POST" action="<?php echo $loginFormAction; ?>">
<table id="tablalogin" style="width:100%">
  <tr>
  
    <td colspan="2">
<span style="padding-right: 10px;"><img src="img/botones/registro.png" alt="registro" /></span><span style="font-size:20px; font-weight:bold;">06.</span><span style="font-weight:bold; padding:20px;"><?php if(isset($_GET['login']) && $_GET['login'] == 0) {
echo "<span style='color:red;'>ERROR <br /></span>"; 
} ?>Acceso para usuarios registrados</span>
</td>
    </tr>
  <tr >
    <td class="sublogin">Nombre de usuario</td>
    <td class="sublogin">
        <input name="ususario" type="text" class="tablafill" id="ususario" size="8" />
</td>
  </tr>
  <tr>
    <td class="sublogin">Contraseña</td>
    <td class="sublogin"><label>
      <input name="password" type="password" class="tablafill" id="password" size="8" />
    </label></td>
  </tr>
  <tr>
    <td class="sublogin"></td>
    <td class="sublogin"><input name="login" type="submit" id="login" value="Acceder" class="tablafill"/></td>
  </tr>
    <tr>
    
    <td colspan="2" class="sublogin"><a href="registro.php">Registrate ahora!</a></td>
  </tr>
</table>

</form>
</span>
<span id="dercont">
<?php 
$dir = "./admin/docs/"; 
$directorio=opendir($dir); 
echo "<div class='tite'>DOCUMENTOS</div>"; 
while ($archivo = readdir($directorio)){ 
 if($archivo=='.' or $archivo=='..'){ 
 echo ""; 
 } else { 
 
 $enlace = $dir.$archivo; 
     echo "<div class='documentos'> <a href=$enlace target='_blank'>$archivo</a></div>";
 } 
 } 
closedir($directorio); 
?> 
</span>
<div id="footer">
   <div align="center">Portal de la pedanía Castellar-Oliveral. CMSPoblets, creado por Javier Julián Olmos.
  </div></div>
</div>

</body>
</html>
<?php
mysql_free_result($Destacado);

mysql_free_result($noticias);
?>
