26 ottobre 2011
<?php
/*
Author: Zordan Marco <marco[at]customsoft.it>
Version: 1.00
The latest version of ContrDati can be obtained from:
Metodi:
Booleano($sDato,$sDefault=true) : Restituisce valore booleano
CodiceFiscale($sDato) : Restituisce array($nErrore,$sDato).
Dominio($sDato,$nLun=100) : Restituisce il dominio o "" se non è corretto
Email($sDato,$nLun=100) : Restituisce l'email o -1 (non è email) o -2 (non esiste dominio)
Ip($sDato) : Restituisce l'ip se è un ip valido
Link($sDato,$nLun=100) : Restituisce un link con http:// o mailto://
Numero($sDato,$sDefault=0,$bDb=true) : Restituisce valore numerico
PartitaIva($sDato) : Restituisce array($nErrore,$sDato)
Stringa($sDato,$nLun,$bDb=true) : Restituisce un valore stringa
Url($sDato,$nLun=100) : Restituisce l'Url codificato
* *********************************************** */
class ContrDati {
/* * ** Public variables *** */
/* user definable vars */
var $sEmailCheckEmail = "";
###################################
# verifica che sia un valore booleano
# ed imposta il default se vuoto
function Booleano($sDato, $sDefault = true) {
if ($sDato === true || $sDato === "true" || $sDato === "True" ||
$sDato === "on" || $sDato === "checked" || $sDato === "t" ||
$sDato === "v" || $sDato === "vero") {
//echo $sDato;
return true;
} else {
return false;
}
}
###################################
# verifica che sia un codice fiscale
# restituisce un array con codice errore ed il dato
# Codici errore:
# -2: codice di controllo non corretto
# -1: formato codice fiscale non corretto
# 0 : nessun errore
# >0: numero di caratteri che mancano (min 16 - max 15)
# >16:numero di caratteri totali se superiore a 16 (max 16)
function CodiceFiscale($sDato) {
$nLungMax = 16;
$nCheck = 0;
$sDato = strtoupper(trim('' . $sDato));
$nLungDato = strlen($sDato);
if ($nLungDato < $nLungMax) {
$nErrore = $nLungMax - $nLungDato;
} else {
if ($nLungDato > $nLungMax) {
$nErrore = $nLungDato;
} else {
if (preg_match("/\b[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]\b/", $sDato)) {
$sSet1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$sSet2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
$sSetPari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$sSetDisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
$sCheck = 0;
for ($y = 0; $y <= 14; $y += 2) {
$nCheck += strpos($sSetDisp, substr($sSet2, strpos($sSet1, substr($sDato, $y, 1)), 1));
}
for ($y = 1; $y <= 13; $y += 2) {
$nCheck += strpos($sSetPari, substr($sSet2, strpos($sSet1, substr($sDato, $y, 1)), 1));
}
if (($nCheck % 26) != (ord(substr($sDato, -1)) - ord("A"))) {
$nErrore = -2;
} else {
$nErrore = 0;
}
} else {
$nErrore = "-1";
}
}
}
return array($nErrore, $sDato);
}
###################################
# verifica che sia una dominio
function Dominio($sDato, $nLun = 100) {
$sDato = trim('' . $sDato);
if (intval($nLun) < strlen($sDato)) {
$sDato = substr($sDato, 0, $nLun);
} if (strlen($sDato) > 0) {
if (preg_match("`^(([a-z0-9_-]+\.)*)(([a-z0-9-]{2,})\.)([a-z0-9]{2,6})$`iU", $sDato, $aUrl)) {
return $sDato;
} else {
return "";
}
} // if (strlen($sDato) > 0) {
}
###################################
# verifica che sia una mail
function Email($sDato, $nLun = 100) {
$sDato = trim('' . $sDato);
if (intval($nLun) < strlen($sDato)) {
$sDato = substr($sDato, 0, $nLun);
} if (strlen($sDato) > 0) {
$this->sEmailCheckEmail = $sDato;
if ((preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/', $sDato)) ||
(preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/', $sDato))) {
$aHost = explode('@', $sDato);
if ($aHost != '') {
if (checkdnsrr($aHost[1] . '.', 'MX')) {
return $sDato;
}
if (checkdnsrr($aHost[1] . '.', 'A')) {
return $sDato;
}
if (checkdnsrr($aHost[1] . '.', 'CNAME')) {
return $sDato;
}
}
return "-2";
} else {
return "-1";
}
} // if (strlen($sDato) > 0) {
}
###################################
# verifica che sia una IP
function Ip($sDato) {
$sDato = trim('' . $sDato);
if (strlen($sDato) > 6) {
if (preg_match("/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/", $sDato)) {
return $sDato;
} else {
return "";
}
}
}
###################################
# verifica che sia un link http o mailto
function Link($sDato, $nLun = 100) {
$sDato = urlencode(trim('' . $sDato));
if (intval($nLun) < strlen($sDato)) {
$sDato = substr($sDato, 0, $nLun);
} echo $sDato;
echo "da sistemare";
exit;
if (strlen($sDato) > 0) {
if (!(substr($sDato, 0, 7) == "http://" || substr($sDato, 0, 8) == "https://")) {
if (!substr($sDato, 0, 7) == "mailto:") {
if (preg_match("\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", $sDato)) {
return "mailto:" . $sDato;
} else {
return "http://" . $sDato;
}
} // if (!substrr($sStringa, 7)="mailto:") {
} // if (!(substrr($sStringa, 7)="http://" || substr($sStringa, 0, 8)=="https://")) {
} // if (strlen($sStringa) > 0) {
}
###################################
# verifica che sia un numero
# mette . se da savare nel db al posto della virgola
# e se non è numero mette il default
function Numero($sDato, $sDefault = 0, $bDb = true) {
$sDato = trim('' . $sDato);
if ($bDb) {
$sDato = str_replace(',', '.', $sDato);
} else {
$sDato = str_replace('.', ',', $sDato);
}
if (!is_numeric($sDato)) {
$sDato = $sDefault;
}
return $sDato;
}
###################################
# verifica che sia una partita iva valida
# restituisce un array con codice errore ed il dato
# Codici errore:
# -1: formato partita iva non corretto
# 0 : nessun errore
# >0: numero di cifre che mancano (min 11)
# >11:numero di cifre totali se superiore a 11 (max 11)
function PartitaIva($sDato) {
$nLungMax = 11;
$sDato = trim('' . $sDato);
$nLungDato = strlen($sDato);
if ($nLungDato < $nLungMax) {
$nErrore = $nLungMax - $nLungDato;
} else {
if ($nLungDato > $nLungMax) {
$nErrore = $nLungDato;
} else {
if (!is_numeric($sDato)) {
$nErrore = "-1";
} else {
$nErrore = "0";
}
}
}
return array($nErrore, $sDato);
}
###################################
# verifica che sia una stringa della lunghezza massima data
# ed aggiunge eventuali \ prima di salvarla nel db
function Stringa($sDato, $nLun, $bDb = true) {
$sDato = trim('' . $sDato);
#if (get_magic_quotes_gpc()) {
# $sDato = stripslashes($sDato);
#}
if ((int) $nLun < strlen($sDato)) {
$sDato = substr($sDato, 0, $nLun);
} if (strlen($sDato) > 0) {
if (!is_numeric($sDato) && $bDb) {
return (string) addslashes($sDato);
} else {
return (string) $sDato;
}
}
}
###################################
# verifica che sia una url
function Url($sDato, $nLun = 100) {
$sDato = trim('' . $sDato);
if (intval($nLun) < strlen($sDato)) {
$sDato = substr($sDato, 0, $nLun);
} if (strlen($sDato) > 0) {
#print_r(parse_url($sDato));
if (preg_match("`^((http|https|ftp):\/\/)?" .
"(" .
"([A-Z0-9][A-Z0-9_-]*)?" .
"(\.[A-Z0-9][A-Z0-9_-]*)+" .
")" .
"(\.[A-Z0-9]{2,6})" .
"(\/)?" .
"([A-Z0-9])?$`iU", $sDato, $aUrl)) {
// print_r($aUrl);
return $sDato;
} else {
return "";
}
} // if (strlen($sStringa) > 0) {
}
function Telefono($sDato) {
// RESTITUISCE:
// -1 Dato accettabile;
// 0 Dato non accettabile: non formato correto, numero italiano;
// 1 Dato non accettabile: non formato correto, numero straniero;
// 2 Dato non accettabile: non formato correto, numero non riconosciuto;
//
//"\d\d (0[1-9]{1,3}|333|334|335|336|337|338|339|330".
// "|360|368|340|347|348|349|320|328|329|380|388|389|392) [0-9]{5,7}"
$sCaratNonTel = array('-', '/', '(', ')', ' ');
$sDato = trim(str_replace($sCaratNonTel, ' ', $sDato));
$sCaratteriValidi = "0123456789 +";
if (strlen($sDato) === strspn($sDato, $sCaratteriValidi)) {
//echo $sValore;
if ((strpos($sDato, '+') == 0 || strpos($sDato, '00') == 0) && strpos($sDato, ' ', 0) != -1) {
if ((strpos($sDato, '39') == 1) || (strpos($sDato, '39') == 2)) {
// Num italiano
$sRegola = "/^";
$sRegola .= "(\+|00)39\s?";
$sRegola .= "(0[1-9]{1,3}|330|333|334|335|336|337|338|339|" .
"360|363|366|368|340|343|346|347|348|349|" .
"320|323|328|329|380|383|388|389|390|391|392|393)";
$sRegola .= "\s?[\s0-9]{5,10}";
$sRegola .= "/";
//echo $sRegola;
if (preg_match($sRegola, $sDato)) {
return -1;
} else {
// Non correttamente formattato manca + e gli spazi
return 0;
}
} else {
// Num straniero
return -1;
}
} else {
// Num italiano senza prefisso internazionale
$sRegola = "/^";
$sRegola .= "(0[1-9]{1,3}|330|333|334|335|336|337|338|339|" .
"360|363|366|368|340|343|346|347|348|349|" .
"320|323|328|329|380|383|388|389|390|391|392|393)";
$sRegola .= "\s?[\s0-9]{5,10}";
$sRegola .= "/";
//echo $sRegola;
if (preg_match($sRegola, $sDato)) {
return -1;
} else {
// Non correttamente formattato manca + e gli spazi
return 2;
}
}
} else {
return 1; // Caratteri non validi
}
}
}
Commenti Facebook
