<?
////////////////////////////////////////////////////
// Conciliación: gestión de conciliación de activos
// Version: 1.0
//
// Copyright (C) 2014  Eustotem Multimedia, S.L.
//
// License: MIT, see LICENSE
////////////////////////////////////////////////////


include("../conf/connections.php");
class ValidationServices{

	function isEmpty($param){
		return $param=='';
	}
	function isNotEmpty($param){
		return $param!='';
	}
	function required($param){
		return $param!='';
	}
	
	
	function isNumeric($param,$val=NULL){
		if($val=='null'){
			return (is_numeric($param) || $param=='');
		}else{
			return is_numeric($param);
		}
	}
	
	
	function isBoolean($param){
		return ($param=="1" || $param=="0" || strtolower($param)=="true" || strtolower($param)=="false");
	}
	
	
	function isEmail($param){
		if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $param) || ($val['params']=='null' && $param=='')) {
			return true;
		}else {
			return false;
		}	
	}
	
	
	function isURL($param){
		if(eregi("(https?|ftp|file)://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?", $param) || ($val['params']=='null' && $param=='')) {
			return true;
		}else {
			return false;
		}	
	}
	
	
	function isValidIP($param){
		if(ip2long($param)) {
			return true;
		}else {
			return false;
		}	
	}
	
	
	function regexp($param,$val){
		if (preg_match($val, $param)) {  
			return true;  
		}else {  
			return false;  
		} 	
	}
	
	
	function moreThan($param1,$param2){
		return (float)$param>=(float)$param2;
	}
	
	
	function lessThan($param,$param2){
		return (float)$param<=(float)$param2;
	}
	
	
	function between($param,$limit1,$limit2){
		if((float)$param>=(float)$limit1 && (float)$param<=(float)$limit2){
			return true;
		}else{
			return false;
		}
	}
	
	
	function equal($param,$val){
		return $param!=(string)$val;
	}
	
	
	function notEqual($param,$val){
		return $param!=(string)$val;
	}
	
	
	function isUnique($campo,$tabla,$valor,$tipo){
		$comillas=$tipo=="string" ? "'" : "";;
		$cadena="SELECT * FROM $tabla WHERE $campo={$comillas}$param{$comillas}";
		$q=mysql_query($cadena) or die(mysql_error());
		return (mysql_num_rows($q)==0);
	}
	
	
	function validExtension($param,$val){
		$exts=explode(",",(string)$val);
		$ext = substr(($t=strrchr($param,'.'))!==false?$t:'',1);
		return in_array($ext,$exts);
	}
	
	
	function fileSize($param,$val){
		
	}
	
	
	function len($param,$val){
		$caracteres=(int)$val;
		return strlen($param)<=$caracteres;
	}
	
	
	function contains($param,$val){
		$needle=(string)$val;
		return (stripos($param,$needle)!==false);
	}
	
	
	function notContains($param,$val){
		$needle=(string)$val;
		return (stripos($param,$needle)===false);
	}
	
	
	
	function isValidDate($param){
		$fecha=$this->parseDate($param);
		return strtotime($fecha)!==false;
	}
	
	function dateGreaterThan($param,$val){
		$f1=strtotime((string)$val);
		$fecha=strtotime($this->parseDate($param));
		return $fecha>$f1;
	}
	
	function dateLowerThan($param,$val){
		$f1=strtotime((string)$val);
		$fecha=strtotime($this->parseDate($param));
		return $fecha<$f1;
	}
	
	function dateBetween($param,$val){
		$fechas=explode(",",(string)$val);
		$f1=strtotime($fechas[0]);
		$f2=strtotime($fechas[1]);
		$f=strtotime($this->parseDate($param));
		return ($f>$f1 && $f<$f2) ? true : false;
	}

	function dateNotBetween($param,$val){
		$fechas=explode(",",(string)$val);
		$f1=strtotime($fechas[0]);
		$f2=strtotime($fechas[1]);
		$f=strtotime($this->parseDate($param));
		return ($f>$f1 && $f<$f2) ? false : true;
	}


	function parseDate($value){
		$temp=explode(" ", $value);
		$fecha=explode("/",$temp[0]);
		$pattern=implode("",explode("/",CAL_DATE_FORMAT));
		$yuyu=$fecha[strripos($pattern,"Y")] . "-" . $fecha[strripos($pattern,"m")] . "-" . $fecha[strripos($pattern,"d")];
		if($temp[1]!=NULL) $yuyu.=" " . $temp[1];
		return $yuyu;
	}
}
?>