<?php
  session_save_path(dirname($_SERVER["SCRIPT_FILENAME"])."/sessions");
  session_start();
  session_register("user");
  session_register("pass");
  session_register("querystring");

  function check_admin() {
    global $link;

    $user=$_SESSION["user"];
    $pass=$_SESSION["pass"];
    $result=mysql_query("select * from users where login='".$user."' and password='".$pass."'",$link);
    if($row=mysql_fetch_array($result)) {
      mysql_free_result($result);
      if($user=="admin") return TRUE;
      if($user=="sanz") return TRUE;
    }
    return FALSE;
  }

  function check_valid() {
    global $link;
    global $current_id,$current_name,$current_valid;

    $user=$_SESSION["user"];
    $pass=$_SESSION["pass"];
    $result=mysql_query("select * from users where login='".$user."' and password='".$pass."'",$link);
    if($row=mysql_fetch_array($result)) {
      $current_valid=$row["valido"];
      mysql_free_result($result);
      if($current_valid==0) return FALSE;
      $current_id=$row["id"];
      $current_name=$row["nombre"]." ".$row["apellido1"]." ".$row["apellido2"];
      return TRUE;
    }
    return FALSE;
  }

  function convert_date($date) {
    $year=substr($date,0,4);
    $month=substr($date,5,2);
    $day=substr($date,8,2);
    $newdate=$day."/".$month."/".$year;
    return $newdate;
  }

  function disconnect() {
    global $link;
    mysql_close($link);
  }

  function getParam($index) {
    if(isset($_POST[$index])) return trim(htmlentities($_POST[$index]));
    if(isset($_GET[$index])) return trim(htmlentities($_GET[$index]));
    return "";
  }

  // conexion base de datos
  $link=mysql_pconnect("localhost","segurfor_user","") or die(mysql_error());
  mysql_select_db("segurfor_db",$link);
  // variables globales
  if(isset($_SERVER["QUERY_STRING"])) $querystring=$_SERVER["QUERY_STRING"];
  $page=getParam("page");
  $action=getParam("action");
  $id=getParam("id");
  $orden=getParam("orden");
  $pnum=getParam("pnum");
  $plim=10;
  // variables de usuario
  $current_id="";
  $current_name="[Usuari no registrat]";
  $current_time=date("H:i:s",time());
  $current_date=date("Y-m-d",time());
  $current_valid=0;
  // por si viene del flash
  $flash=0;
  if(!isset($_SERVER["HTTP_REFERER"])) $flash=1;
  else if(substr_count($_SERVER["HTTP_REFERER"],"segurforum")==0) $flash=1;
?>
