<?
/*
$type 		= $req[0];
$post_name 	= $req[1];
$value 		= trim($_POST[$post_name]);
$min_len		= $req[2];
$config		= $req[3];
*/

global $X;

switch($type) {
	default: //normal string
	case 'text':
	case 'textarea':
		if(strlen($value)<$min_len) return false;
	break;


	case 'email':
		if($min_len>0 || !empty($value)) {
			if(empty($value)) return false;
			$pattern="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
			if(!preg_match("#^".str_replace('#','\#',$pattern)."$#i", $value)) {
				$this->MESSAGE_TYPE='email';
				return false;
			}
		}
	break;


	case 'link':
		if($min_len>0 || !empty($value)) {
			if(empty($value)) return false;
			$value=str_replace('://www.', '://', $value);
//			$pattern="((((H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\://)?(www.|[a-zA-Z0-9-].)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&amp;\(\)%\$#\=~_\-]+))*";
			$pattern="((((H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\://)?(www.)?[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\:\;\?\'\\\+&amp;\(\)%\$!#\=~_\-]+))*";
			if(!preg_match("#^".str_replace('#','\#',$pattern)."$#i", $value)) {
				$this->MESSAGE_TYPE='link';
				return false;
			}
		}
	break;


	case 'domena':
		if($min_len>0 || !empty($value)) {
			if(empty($value)) return false;
			if(substr($value,0,7)=='http://' || substr($value,0,7)=='HTTP://') $value=str_replace(array('http://','HTTP://'), array('',''), $value);
			if(substr($value,0,8)=='https://' || substr($value,0,8)=='HTTPS://') $value=str_replace(array('https://','HTTPS://'), array('',''), $value);
			if(substr($value,0,4)=='www.' || substr($value,0,4)=='WWW.') $value=str_replace(array('www.','WWW.'), array('',''), $value);
//			$pattern="([a-zA-Z0-9-].)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*";
			$pattern="[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*";
			if(!preg_match("#^".str_replace('#','\#',$pattern)."$#i", $value)) {
				$this->MESSAGE_TYPE='link';
				return false;
			}
		}
	break;


	case 'domena2':
		if($min_len>0 || !empty($value)) {
			if(empty($value)) return false;
			if(substr($value,0,7)=='http://' || substr($value,0,7)=='HTTP://') $value=str_replace(array('http://','HTTP://'), array('',''), $value);
			if(substr($value,0,8)=='https://' || substr($value,0,8)=='HTTPS://') $value=str_replace(array('https://','HTTPS://'), array('',''), $value);
			if(substr($value,0,4)=='www.' || substr($value,0,4)=='WWW.') $value=str_replace(array('www.','WWW.'), array('',''), $value);

//			$pattern="([a-zA-Z0-9-].)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&amp;\(\)%\$#\=~_\-]+))*";
			$pattern="[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&amp;\(\)%\$!#\=~_\-]+))*";

			if(!preg_match("#^".str_replace('#','\#',$pattern)."$#i", $value)) {
				$this->MESSAGE_TYPE='link';
				return false;
			}
		}
	break;


	case 'kod':
		if($min_len>0 || !empty($value)) {
			$pattern="^([0-9]{2})-([0-9]{3})$";
			if(!preg_match("#^".str_replace('#','\#',$pattern)."$#i", $value)) return false;
		}
	break;


	case 'kod2':
		if($min_len>0 || !empty($value)) {
			$pattern="^([0-9-]{4,})$";
			if(!preg_match("#^".str_replace('#','\#',$pattern)."$#i", $value)) return false;
		}
	break;


	case 'data':
		if($min_len>0 || !empty($value)) {
			$pattern="^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$";
			if(!preg_match("#^".str_replace('#','\#',$pattern)."$#i", $value)) return false;
		}
	break;


	case 'czas':
		if($min_len>0 || !empty($value)) {
			$pattern="^(1?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$";
			if(!preg_match("#^".str_replace('#','\#',$pattern)."$#i", $value)) return false;
		}
	break;


	case 'data18':
		if($min_len>0 || !empty($value)) {
			$pattern="^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$";
			if(!preg_match("#^".str_replace('#','\#',$pattern)."$#i", $value)) return false;
			$ile=time()-strtotime($value);
			if(@floor($ile/31556927.29)<18 || (int)$ile==0) {
				$this->MESSAGE_TYPE='18lat';
				return false;
			}
		}
	break;


	case 'telefon':
		if($min_len>0 || !empty($value)) {
			$value=str_replace(array(' ','(',')','-',',','.','+'), array('','','','','','',''), $value);
			$value=preg_replace("/.*[^0-9].*/", "", $value); // "/[^0-9]/"
			if(strlen($value)<9) return false;
		}
	break;


	case 'nip':
		if($min_len>0 || !empty($value)) {
			if(($value=='1234567890' || $value=='0123456789') && $min_len>0) return false;
			$value=str_replace('-','',$value);
			$value=str_replace(' ','',$value);
			if(strlen($value)!=10 && $min_len>=0) return false;
			if($value=='0000000000') return false;
			$steps=array(6,5,7,2,3,4,5,6,7);
			if(!empty($value) && !is_numeric($value)) return false;
			for($x=0; $x<9; $x++) $sum_nb+=$steps[$x]*$value[$x];
			$sum_m=$sum_nb%11;
			if($sum_m==10) $sum_m=0; //?????
			if($sum_m!=$value[9]) return false;
		}
	break;


	case 'regon':
		if($min_len>0 || !empty($value)) {
			$value = str_replace(array('-',''), array(' ',''), $value);
			if((strlen($value)!=9 || !is_numeric($value)) && $min_len>0) return false;
			$steps=array(8,9,2,3,4,5,6,7);
			for($x=0; $x<8; $x++) $sum_nb+=$steps[$x]*$value[$x];
			$sum_m=$sum_nb%11;
			if($sum_m==10) $sum_m=0;
			if($sum_m!=$value[8]) return false;
		}
	break;


	case 'pesel':
		if($min_len>0 || !empty($value)) {
			if((strlen($pesel)!=11 || !is_numeric($value)) && $min_len>0) return false;
			if($value*1==0) return false;
			$steps=array(1,3,7,9,1,3,7,9,1,3);
			for($x=0; $x<10; $x++) $sum_nb+=$steps[$x]*$value[$x];
			$sum_m=10-$sum_nb%10;
			if($sum_m==10) $sum_c=0; else $sum_c=$sum_m;
			if($sum_c!=$value[10]) return false;
		}
	break;


	case 'image':
	case 'file':
	case 'flash_upload':
		if($min_len>0 || !empty($value)) {
			if(empty($this->ID)) $this->ID=substr($_GET['form'],0,-5);

			if(!empty($_SESSION['flash_upload'][$this->ID.'_form'][$post_name]['config'])) {
				//flash upload
				$config	 = $_SESSION['flash_upload'][$this->ID.'_form'][$post_name]['config'];
				$uploaded = $_SESSION['flash_upload'][$this->ID.'_form'][$post_name]['uploaded'];
				if(count($uploaded)>0) foreach($uploaded as $upl_file) {
					$ext=$X->FILE->get_ext(substr($upl_file,0,-4));
					if(@in_array($ext, @explode('|', $config['ext']))) $passed_uploaded[]=$upl_file;
				}
				if(count($passed_uploaded)+count($_POST[$post_name.'_exists'])-count($_POST[$post_name.'_del']) < $min_len) return false;
			} else {
				//normal upload
				$ext=$X->FILE->get_ext($value);
				if(empty($value) && empty($_POST[$post_name.'_exists'])) return false;
				if(!empty($value) || $minlen>0 || empty($_POST[$post_name.'_exists'])) {
					if(!@in_array($ext, @explode('|', $config['ext']))) {
						$this->MESSAGE_TYPE='extension';
						return false;
					}
				}
			}
		}
	break;


	case 'int':
		if(!is_numeric($value) || $value<=0 || stristr($value,'.')) return false;
	break;

	case 'int_zero':
		if(!is_numeric($value) || $value<0 || stristr($value,'.')) return false;
	break;

	case 'int_negative':
		if(!is_numeric($value) || stristr($value,'.')) return false;
	break;

	case 'int10':
		if(!is_numeric($value) || $value<=0 || $value>10 || stristr($value,'.')) return false;
	break;

	case 'int010':
		if(!is_numeric($value) || $value<0 || $value>10 || stristr($value,'.')) return false;
	break;


	case 'cena':
	case 'float':
		if(!is_numeric($value) || $value<=0) return false;
	break;

	case 'float_zero':
		if(!is_numeric($value) || $value<0) return false;
	break;

	case 'float_negative':
		if(!is_numeric($value)) return false;
	break;


	case 'wzrost':
		if(!is_numeric($value) || (int)$value<=99 || (int)$value>299) return false;
	break;


	case 'radio':
		if(empty($value) || $value===0) {
//			$this->FOCUS_DATA=' $("#'.$this->FORM.'").find("[name=\''.$post_name.'\']:first").focus(); ';
			return false;
		}
	break;


	case 'select':
		if(empty($value) || $value===0) return false;
	break;

	case 'multiselect':
		$post_name=str_replace('[]','',$post_name);
		if(count($_POST[$post_name])==0) return false;
	break;

	case 'checkbox':
		if(empty($value) || $value===0) return false;
	break;


	case 'checkbox_array':
		$post_name=str_replace('[]','',$post_name);
		if(count($_POST[$post_name])==0) {
//			$this->FOCUS_DATA=' $("#'.$this->FORM.'").find("[name=\''.$post_name.'\']:first").focus(); ';
			return false;
		}
	break;


	case 'promocja_data':
		if($_POST['promocja']==1) {
			$pattern="^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$";
			if(!preg_match("#^".str_replace('#','/#',$pattern)."$#i", $value)) return false;
		}
	break;
	case 'promocja_cena':
		if($_POST['promocja']==1) {
			if(!is_numeric($value) || $value<=0) return false;
		}
	break;
}







//Uniq & SQL Addon ///////////////////////////////////////////////////
if($config['unique']==1) {
	if(!empty($config['cond']) || !empty($config['cond-eval'])) {
		if(!empty($config['cond-eval'])) {
			$e=$config['cond-eval'];
			if(count($e['replace'])>0) foreach($e['replace'] as $rep_from => $rep_to) {
				if(!empty($rep_to)) {
					eval("\$rep_evaled=($rep_to);");
					$e['query']=str_replace($rep_from, $rep_evaled, $e['query']);
				}
			}
			$custom_cond=' AND '.$e['query'];
		} else {
			$custom_cond=' AND '.$config['cond'];
		}
	}
	if((int)$_POST['row_id']>0) $row_id=' AND id!="'.(int)$_POST['row_id'].'" ';
	$custom_col=$post_name;
	if(!empty($config['col'])) $custom_col=$config['col'];

	$query=' SELECT '.$custom_col.' FROM '.$config['table'].' WHERE '.$custom_col.'="'.$value.'" '.$custom_cond.$row_id.' ';

} else if(!empty($config['query'])) {
	if(!empty($config['query_eval'])) {
		if(count($config['query_eval']['replace'])>0) foreach($config['query_eval']['replace'] as $rep_from => $rep_to) {
			if(!empty($rep_to)) {
				eval("\$rep_evaled=($rep_to);");
				$config['query']=str_replace($rep_from, $rep_evaled, $config['query']);
			}
		}
	}
	$query=$config['query'];
}

if(!empty($query)) {
	$unique_test=$X->DB->query($query);
	if($config['inverse']==1) {
		if($X->DB->num_rows($unique_test)==0) {
			$this->MESSAGE_TYPE=(!empty($config['message']) ? $config['message'] : 'unique_inverse');
			return false;
		}
	} else {
		if($X->DB->num_rows($unique_test)>0) {
			$this->MESSAGE_TYPE=(!empty($config['message']) ? $config['message'] : 'unique');
			return false;
		}
	}
}



return true; //validation SUCCESS
?>