<?php
  ini_set("session.bug_compat_42","Off");

  session_save_path(dirname($_SERVER["SCRIPT_FILENAME"])."/sessions");
  session_start();
  session_register("user");
  session_register("pass");
  session_register("login");
  session_register("querystring");
  session_register("votos");

  function create_password() {
    $valids="abcdefghijklmnopqrstuvwxyz1234567890";
    $length=5;
    $password="";
    for($i=0;$i<$length;$i++) {
      $password=$password.substr($valids,rand(0,strlen($valids)-1),1);
    }
    return $password;
  }

  function is_admin($login) {
    if($login=="admin") return TRUE;
    if($login=="sanz") return TRUE;
    return FALSE;
  }

  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);
      return is_admin($user);
    }
    return FALSE;
  }

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


	if (!isset($_SESSION["norevalidate"])) 
	{
		if (isset($_SESSION["password_olvidado_datos"])) unset($_SESSION["password_olvidado_datos"]);
	    $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;
    	}else {
		    $result=mysql_query("select * from users where login='".$user."'");
		    if (mysql_num_rows($result)>1) mysql_free_result($result); // problemo
		    else if($row=mysql_fetch_assoc($result)) {
		    	$_SESSION["norevalidate"]=true;
		    	$_SESSION["password_olvidado_datos"]=$row;
		    	//error_reporting(E_ALL);
		    	mysql_free_result($result);
		    	header("Location: inicio.php?page=passperduda");
		    	die;
		    }     	    
    	}
	    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 string_date($date) {
    $year=substr($date,0,4);
    $month=substr($date,5,2);
    $day=substr($date,8,2);
    $months=array('de Gener', 'de Febrer', 'de Mar&ccedil;', 'd\'Abril', 'de Maig', 'de Juny', 'de Juliol', 'd\'Agost', 'de Setembre', 'd\'Octubre', 'de Novembre', 'de Desembre');
    $newdate=$day." ".$months[$month-1]." del ".$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 "";
  }

  function show_permision() {
    global $current_valid;

    if($_SESSION["login"]==0) return;
    $_SESSION["login"]=0;
    if($current_valid) return;
    if($_SESSION["user"]=="" && $_SESSION["pass"]=="") return;
    echo "<script language=javascript type=text/javascript>\n";
    echo "  alert(\"Acces denegat\");\n";
    echo "</script>\n";
  }

  // conexion base de datos
  include("config.php");
  $link=mysql_pconnect($dbhost,$dbuser,$dbpass) or die(mysql_error());
  mysql_select_db($dbname,$link);
  // variables globales
  if(isset($_SERVER["QUERY_STRING"])) $querystring=$_SERVER["QUERY_STRING"];
  $page=getParam("page");
  $action=getParam("action");
  $id=getParam("id");
  $grupo=getParam("grupo");
  $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($_GET["flash"])) $flash=getParam("flash");
  //else if(!isset($_SERVER["HTTP_REFERER"])) $flash=1;
  //else if(substr_count($_SERVER["HTTP_REFERER"],"elcol-legi")==0 &&
          //substr_count($_SERVER["HTTP_REFERER"],"segurforum")==0) $flash=1;

  function getValor($tag,$def="") {
    global $link;
    $query="SELECT valor FROM literales WHERE tag='$tag'";
    $result=mysql_query($query,$link) or die(mysql_error());
    $row=mysql_fetch_assoc($result);
    mysql_free_result($result);
    $valor=$row["valor"];
    if($valor=="") $valor=$def;
    if($valor!="") if(file_exists("../admin/files/".$valor)) $valor="../admin/files/".$valor;
    if($valor!="") if(file_exists("img/".$valor)) $valor="img/".$valor;
	$valor=html_entity_decode($valor);
    return $valor;
  }
?>
