cvs: pear /Validate/Validate US.php
| From: | Tomas V.V.Cox | Date: | Sat, 08 Jun 2002 14:54:12 +0000 |
| Subject: | cvs: pear /Validate/Validate US.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-4497@lists.php.net to get a copy of this message | ||
cox Sat Jun 8 10:54:12 2002 EDT
Added files:
/pear/Validate/Validate US.php
Log:
Validation methods for US scpecific data (currently SSN validation)
By: Brent Cook <busterb@mail.utexas.edu>
Tim Gallagher <timg@sunflowerroad.com>
Index: pear/Validate/Validate/US.php
+++ pear/Validate/Validate/US.php
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt.
|
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Brent Cook <busterb@mail.utexas.edu> |
// | Tim Gallagher <timg@sunflowerroad.com> |
// +----------------------------------------------------------------------+
//
// $Id: US.php,v 1.1 2002/06/08 14:54:12 cox Exp $
//
// Specific validation methods for data used in the United States
//
class ValidateUS
{
/**
* Validates a social security number
* @param string $ssn number to validate
* @param array $high_groups array of highest issued SSN group numbers
* @returns bool
*/
function ssn($ssn, $high_groups = null)
{
// remove any dashes, spaces, returns, tabs or slashes
$ssn = str_replace(array('-','/','
',"\t","\n"), '', $ssn);
// check if this is a 9-digit number
if (!is_numeric($ssn) || !(strlen($ssn) == 9)) {
return false;
}
$area = intval(substr($ssn, 0, 3));
$group = intval(substr($ssn, 3, 2));
$serial = intval(substr($ssn, 5, 4));
if (is_null($high_groups)) {
$high_groups = ValidateUS::ssnGetHighGroups();
}
return ValidateUS::ssnCheck($area, $group, $serial, $high_groups);
}
/**
* Returns a range for a supplied group number, which
* is the middle, two-digit field of a SSN.
* Group numbers are defined as follows:
* 1 - Odd numbers, 01 to 09
* 2 - Even numbers, 10 to 98
* 3 - Even numbers, 02 to 08
* 4 - Odd numbers, 11 to 99
* @param int $groupNumber a group number to check, 00-99
* @return int
*/
function ssnGroupRange($groupNumber)
{
if ($groupNumber < 10) {
// is the number odd?
if ($groupNumber % 2) {
return 1;
} else {
return 3;
}
} else {
// is the number odd?
if ($groupNumber % 2) {
return 4;
} else {
return 2;
}
}
}
/**
* checks if a Social Security Number is valid
* needs the first three digits and first two digits and the
* final four digits as separate integer parameters
* @param int $area 3-digit group in a SSN
* @param int $group 2-digit group in a SSN
* @param int $serial 4-digit group in a SSN
* @param array $high_groups array of highest issued group numbers
* area number=>group number
*/
function ssnCheck($area, $group, $serial, &$high_groups)
{
// perform trivial checks
// no field should contain all zeros
if (!($area && $group && $serial)) {
return false;
}
// check if this area has been assigned yet
if (!($high_group = $high_groups[$area])) {
return false;
}
$high_group_range = ValidateUS::ssnGroupRange($high_group);
$group_range = ValidateUS::ssnGroupRange($group);
// if the assigned range is higher than this group number, we're OK
if ($high_group_range > $group_range) {
return true;
} else {
// if the assigned range is lower than the group number, that's bad
if ($high_group_range < $group_range) {
return false;
} else {
// we must be in the same range, check the actual numbers
if ($high_group >= $group) {
return true;
} else {
return false;
}
}
}
}
/**
* Gets the most current list the highest SSN group numbers issued
* from the Social Security Administration website. This info can be
* cached for performance (and to lessen the load on the SSA website)
*
* @param string $source The SSA highgroup.htm file
* @param bool $is_text Take the $highgroup_htm param as directly the contents
* @returns array
*/
function ssnGetHighGroups($source = 'http://www.ssa.gov/foia/highgroup.htm',
$is_text = false)
{
if (!$is_text) {
if (!$fd = @fopen($source, 'r')) {
trigger_error("Could not access to the SSA High Groups file",
E_USER_WARNING);
return array();
}
$source = '';
while ($data = fread($fp, 2048)) {
$source .= $data;
}
fclose($fd);
}
$search = array ("'<script[^>]*?>.*?</script>'si", //
Strip javascript
"'<[\/\!]*?[^<>]*?>'si", // Strip
html tags
"'([\r\n])[\s]+'", // Strip white space
"'\*'si");
$replace = array ('','','\\1','');
$lines = explode("\n", preg_replace($search, $replace, $source));
$high_groups = array();
foreach ($lines as $line) {
$line = trim($line);
if ((strlen($line) == 3) && is_numeric($line)) {
$current_group = $line;
} elseif ((strlen($line) == 2) && is_numeric($line)) {
$high_groups[$current_group] = $line;
}
}
return $high_groups;
}
}
?>