if (!defined('ACCESO_WEB_LOCAL')) exit(header('Location: http://www.tse.org.gt/uaip/index.php'));
// incluir conexion de base de datos para validar los datos con mysql_real_scape_string
include('condbgt.php');
/* inicio de declaracion de funcion para validar Fechas */
function validarFecha($datoFecha, $separador, $tipo, $formato){
$datoFecha = explode($separador,$datoFecha);
switch ($formato){
case 'dmy':
$dia = $datoFecha[0];
$mes = $datoFecha[1];
$anyo = $datoFecha[2];
break;
case 'ymd':
$dia = $datoFecha[2];
$mes = $datoFecha[1];
$anyo = $datoFecha[0];
break;
}
switch($tipo){
case 'fecha':
if (preg_match("/^[0-9]+$/",$dia) && preg_match("/^[0-9]+$/",$mes) && preg_match("/^[0-9]+$/",$anyo)){
$errorDia = 0;
$errorMes = 0;
$errorAnyo = 0;
$anyoActual = date('Y');
if ($dia > 31){ $errorDia = 1;}
if ($mes > 12){ $errorMes = 1;}
if ($anyo > $anyoActual){ $errorAnyo = 1;}
if (empty($errorDia) && empty($errorMes) && empty($errorAnyo)){
$errorFecha = 0;
} else {
$errorFecha = 1;
}
} else {
$errorFecha = 1;
}
return $errorFecha;
break;
case 'mayorEdad':
if (preg_match("/^[0-9]+$/",$dia) && preg_match("/^[0-9]+$/",$mes) && preg_match("/^[0-9]+$/",$anyo)){
$fechaNac1 = $anyo . $separador . $mes . $separador . $dia;
$fechaNac2 = strtotime($fechaNac1);
$mayorEdad = strtotime('18 years ago');
if ($fechaNac2 > $mayorEdad){
$errorFecha = 1;
} else {
$errorFecha = 0;
}
} else {
$errorFecha = 1;
}
return $errorFecha;
break;
}
}
/* fin de declaracion de funcion para validar Correos electronicos */
/* inicio de declaracion de funcion para convertir Fechas*/
function convertirFecha($convertirFecha, $separadorIni, $separadorFin){
$convertirFecha = explode($separadorIni,$convertirFecha);
$fechaConvertida = $convertirFecha[2] . $separadorFin . $convertirFecha[1] . $separadorFin . $convertirFecha[0];
return $fechaConvertida;
}
/* fin de declaracion de funcion para convertir Fechas*/
/* inicio de declaracion de funcion para validar Correos electronicos */
function validarCorreo($correo){
$exp = "^[a-z'0-9]+([._-][a-z'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
if(eregi($exp,$correo)){
if(checkdnsrr(array_pop(explode("@",$correo)),"MX")){
return $correo;
}else{
// return 'correo no valido';
return $correo;
}
}else{
return 'correo no valido';
}
}
/* fin de declaracion de funcion para validar Correos electronicos */
/* inicio de declaracion de funcion para validar campos de texto */
function validarTexto($texto){
// limpiar codigo
$texto = strip_tags($texto);
$texto = stripslashes($texto);
$texto = htmlspecialchars($texto);
$texto = htmlentities($texto, ENT_QUOTES, 'UTF-8');
$poner1 = array('','','','','','','','','','');
$poner2 = array('á','é','í','ó','ú','Á','É','Í','Ó','Ú','ñ','Ñ');
$quitarVal = array('src','SRC','window','location','javascript','"','iframe','IFRAME','style','STYLE');
$quitarInj1 = array('\x00','\n','\r','\\',',',', ','\x1a','OR',' OR ', '--');
$quitarInj2 = array('SELECT','WHERE','select','where','from','FROM','union','UNION','all', 'ALL');
$quitarInj3 = array('collate','COLLATE','sql','SQL','insert','NULL','null','INSERT','convert','CONVERT');
$quitarInj4 = array('set','SET','update','UPDATE','INTO','drop','DROP','order','ORDER','loadfile');
$quitarTag = array('','/SCRIPT>','<','>');
$quitarSim1 = array('@','/','\\','=','|','&','(',')','¿','?');
$quitarSim2 = array('^','+','*','-','_',',','.',';','#','%');
$quitarSim3 = array('"','$','!','¡','[',']','´','¨','\'');
$quitarVar = array('0x','x0',' ','726F6F74','\0x','\726F6F74','ampamp','',"'", 'into');
$quitarTildes = array('aacute','eacute','iacute','oacute','uacute','Aacute','Eacute','Iacute','Oacute','Uacute','ntilde','Ntilde');
$texto = str_replace($quitarVal,$poner1,$texto);
$texto = str_replace($quitarInj1,$poner1,$texto);
$texto = str_replace($quitarInj2,$poner1,$texto);
$texto = str_replace($quitarInj3,$poner1,$texto);
$texto = str_replace($quitarInj4,$poner1,$texto);
$texto = str_replace($quitarTag,$poner1,$texto);
$texto = str_replace($quitarSim1,$poner1,$texto);
$texto = str_replace($quitarSim2,$poner1,$texto);
$texto = str_replace($quitarSim3,$poner1,$texto);
$texto = str_replace($quitarVar,$poner1,$texto);
$texto = str_replace($quitarTildes,$poner2,$texto);
return $texto;
}
/* fin de declaracion de funcion para validar campos de texto */
/* inicio de declaracion de funcion para validar campos de texto */
function validarTextoS($texto){
// limpiar codigo
$texto = strip_tags($texto);
$texto = stripslashes($texto);
$texto = htmlspecialchars($texto);
$texto = htmlentities($texto, ENT_QUOTES, 'UTF-8');
$poner1 = array('','','','','','','','','','');
$poner2 = array('á','é','í','ó','ú','Á','É','Í','Ó','Ú','ñ','Ñ');
$quitarVal = array('src','SRC','window','location','javascript','"','iframe','IFRAME','style','STYLE');
$quitarInj1 = array('\x00','\n','\r','\\',',',', ','\x1a','OR',' OR ', '--');
$quitarInj2 = array('SELECT','WHERE','select','where','from','FROM','union','UNION','all', 'ALL');
$quitarInj3 = array('collate','COLLATE','sql','SQL','insert','NULL','null','INSERT','convert','CONVERT');
$quitarInj4 = array('set','SET','update','UPDATE','INTO','drop','DROP','order','ORDER','loadfile');
$quitarTag = array('','/SCRIPT>','<','>');
$quitarSim1 = array('@','/','\\','=','|','&','(',')','¿','?');
$quitarSim2 = array('^','+','*',' - ','_',',','.',';','#','%');
$quitarSim3 = array('"','$','!','¡','[',']','´','¨','\'');
$quitarVar = array('0x','x0',' ','726F6F74','\0x','\726F6F74','ampamp','',"'", 'into');
$quitarTildes = array('aacute','eacute','iacute','oacute','uacute','Aacute','Eacute','Iacute','Oacute','Uacute','ntilde','Ntilde');
$texto = str_replace($quitarVal,$poner1,$texto);
$texto = str_replace($quitarInj1,$poner1,$texto);
$texto = str_replace($quitarInj2,$poner1,$texto);
$texto = str_replace($quitarInj3,$poner1,$texto);
$texto = str_replace($quitarInj4,$poner1,$texto);
$texto = str_replace($quitarTag,$poner1,$texto);
$texto = str_replace($quitarSim1,$poner1,$texto);
$texto = str_replace($quitarSim2,$poner1,$texto);
$texto = str_replace($quitarSim3,$poner1,$texto);
$texto = str_replace($quitarVar,$poner1,$texto);
$texto = str_replace($quitarTildes,$poner2,$texto);
return $texto;
}
/* fin de declaracion de funcion para validar campos de texto */
/* inicio de declaracion de funcion para validar campos numericos */
function validarNumero($numero, $longitud){
// limpiar numero
$numero = strip_tags($numero);
$numero = stripslashes($numero);
$numero = htmlspecialchars($numero);
$numero = htmlentities($numero);
$numero = trim($numero);
$poner = array('','','','','','','','','','','','','','','','','','','','','','','','','','','','');
$quitarSim = array(' ','-','_',',','.',';','@','#','"','=','$','!','%','&','/','(',')','?','¡','[',']','*','+','´','¨','<','>','|');
$quitarMay = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','Ñ','O','P','Q','R','S','T','U','V','W','X','Y','Z','°');
$quitarMin = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','ñ','o','p','q','r','s','t','u','v','w','x','y','z','^');
$numero = str_replace($quitarSim,$poner,$numero);
$numero = str_replace($quitarMay,$poner,$numero);
$numero = str_replace($quitarMin,$poner,$numero);
// comprobar numero
if (is_integer($numero)){
$numero = $numero;
} else {
if (preg_match("/^[0-9]+$/",$numero)){ $numero = $numero;} else { $numero = 0;}
}
if ($longitud > 0){
$numero = substr($numero,0,$longitud);
}
// retornar numero
return $numero;
}
/* fin de declaracion de funcion para validar campos numericos */
/* inicio aplicar validaciones a todos los valores POST */
foreach ($_POST as $key => $value) {
if ($key == 's'){
$_POST[$key] = validarTextoS($value);
}
if ($key == 'correo'){
$_POST[$key] = validarCorreo($value);
}
if ($key != 's' && $key != 'correo'){
$_POST[$key] = validarTexto($value);
}
if (PHP_VERSION >= '5.3.0'){
$_POST[$key] = mysql_real_escape_string($_POST[$key]);
} else {
$_POST[$key] = mysql_escape_string($_POST[$key]);
}
}
/* fin aplicar validaciones a todos los valores POST */
/* inicio aplicar validaciones a todos los valores GET */
foreach ($_GET as $key => $value) {
if ($key == 's'){
$_GET[$key] = validarTextoS($value);
}
if ($key == 'correo'){
$_GET[$key] = validarCorreo($value);
}
if ($key != 's' && $key != 'correo'){
$_GET[$key] = validarTexto($value);
}
if (PHP_VERSION >= '5.3.0'){
$_GET[$key] = mysql_real_escape_string($value);
} else {
$_GET[$key] = mysql_escape_string($value);
}
}
/* fin aplicar validaciones a todos los valores GET */
/* inicio aplicar validaciones a todos los valores REQUEST */
foreach ($_REQUEST as $key => $value) {
if ($key == 's'){
$_REQUEST[$key] = validarTextoS($value);
}
if ($key == 'correo'){
$_REQUEST[$key] = validarCorreo($value);
}
if ($key != 's' && $key != 'correo'){
$_REQUEST[$key] = validarTexto($value);
}
if (PHP_VERSION >= '5.3.0'){
$_REQUEST[$key] = mysql_real_escape_string($value);
} else {
$_REQUEST[$key] = mysql_escape_string($value);
}
}
/* fin aplicar validaciones a todos los valores REQUEST */
?>