-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontrolla_cf.php
More file actions
83 lines (80 loc) · 2.11 KB
/
controlla_cf.php
File metadata and controls
83 lines (80 loc) · 2.11 KB
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
<?php
/**
* Controlla codice fiscale.
* @author Umberto Salsi <salsi@icosaedro.it>
* @version 2012-05-12
* @param string $cf Codice fiscale costituito da 16 caratteri. Non
* sono ammessi caratteri di spazio, per cui i campi di input dell'utente
* dovrebbero essere trimmati preventivamente. La stringa vuota e' ammessa,
* cioe' il dato viene considerato opzionale.
* @return
* 0 -> CF OK
* 1 -> stringa vuota
* 2 -> errore lunghezza CF
* 3 -> caratteri non previsti
* 4 -> controcodice sbagliato
*/
function ControllaCF($cf)
{
$cf = trim($cf);
if( $cf === '' )
return 1;
if( strlen($cf) != 16 )
return 2;
$cf = strtoupper($cf);
if( preg_match("/^[A-Z0-9]+\$/", $cf) != 1 )
return 3;
$s = 0;
for( $i = 1; $i <= 13; $i += 2 ){
$c = $cf[$i];
if( strcmp($c, "0") >= 0 and strcmp($c, "9") <= 0 )
$s += ord($c) - ord('0');
else
$s += ord($c) - ord('A');
}
for( $i = 0; $i <= 14; $i += 2 ){
$c = $cf[$i];
switch( $c ){
case '0': $s += 1; break;
case '1': $s += 0; break;
case '2': $s += 5; break;
case '3': $s += 7; break;
case '4': $s += 9; break;
case '5': $s += 13; break;
case '6': $s += 15; break;
case '7': $s += 17; break;
case '8': $s += 19; break;
case '9': $s += 21; break;
case 'A': $s += 1; break;
case 'B': $s += 0; break;
case 'C': $s += 5; break;
case 'D': $s += 7; break;
case 'E': $s += 9; break;
case 'F': $s += 13; break;
case 'G': $s += 15; break;
case 'H': $s += 17; break;
case 'I': $s += 19; break;
case 'J': $s += 21; break;
case 'K': $s += 2; break;
case 'L': $s += 4; break;
case 'M': $s += 18; break;
case 'N': $s += 20; break;
case 'O': $s += 11; break;
case 'P': $s += 3; break;
case 'Q': $s += 6; break;
case 'R': $s += 8; break;
case 'S': $s += 12; break;
case 'T': $s += 14; break;
case 'U': $s += 16; break;
case 'V': $s += 10; break;
case 'W': $s += 22; break;
case 'X': $s += 25; break;
case 'Y': $s += 24; break;
case 'Z': $s += 23; break;
/*. missing_default: .*/
}
}
if( chr($s%26 + ord('A')) != $cf[15] )
return 4;
return 0;
}