|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
<?php
/*************************************************
ContrDati - Classe per il controllo dei dati in entrata
Author: Zordan Marco <marco[at]customsoft.it>
Version: 1.00
The latest version of ContrDati can be obtained from:
http://customsofts.it/softwares-scripts-opensource/script-php/contrdati-per-la-validazione-dei-dati-in-entrata/
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
}
}
} |