Created
October 6, 2011 21:58
-
-
Save ChrisMcKee/1268814 to your computer and use it in GitHub Desktop.
PHP Utility Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RDM_Utility { | |
public static function generateGUID() { | |
$guid = ''; | |
$time_low = str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT) . str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT); | |
$time_mid = str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT); | |
$time_high_and_version = mt_rand(0, 255); | |
$time_high_and_version = $time_high_and_version & hexdec('0f'); | |
$time_high_and_version = $time_high_and_version ^ hexdec('40'); // Sets the version number to 4 in the high byte | |
$time_high_and_version = str_pad(dechex($time_high_and_version), 2, '0', STR_PAD_LEFT); | |
$clock_seq_hi_and_reserved = mt_rand(0, 255); | |
$clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved & hexdec('3f'); | |
$clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved ^ hexdec('80'); // Sets the variant for this GUID type to '10x' | |
$clock_seq_hi_and_reserved = str_pad(dechex($clock_seq_hi_and_reserved), 2, '0', STR_PAD_LEFT); | |
$clock_seq_low = str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT); | |
$node = str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT) . str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT) . str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT); | |
$guid = $time_low . '-' . $time_mid . '-' . $time_high_and_version . $clock_seq_hi_and_reserved . '-' . $clock_seq_low . '-' . $node; | |
return $guid; | |
} | |
/** | |
Validate an email address. | |
Provide email address (raw input) | |
Returns true if the email address has the email | |
address format and the domain exists. | |
*/ | |
public static function isValidEmail(&$email) { | |
$isValid = true; | |
$atIndex = strrpos($email, "@"); | |
if (is_bool($atIndex) && !$atIndex) { | |
$isValid = false; | |
} else { | |
$domain = substr($email, $atIndex + 1); | |
$local = substr($email, 0, $atIndex); | |
$localLen = strlen($local); | |
$domainLen = strlen($domain); | |
if ($localLen < 1 || $localLen > 64) { | |
// local part length exceeded | |
$isValid = false; | |
} else if ($domainLen < 1 || $domainLen > 255) { | |
// domain part length exceeded | |
$isValid = false; | |
} else if ($local[0] == '.' || $local[$localLen - 1] == '.') { | |
// local part starts or ends with '.' | |
$isValid = false; | |
} else if (preg_match('/\\.\\./', $local)) { | |
// local part has two consecutive dots | |
$isValid = false; | |
} else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { | |
// character not valid in domain part | |
$isValid = false; | |
} else if (preg_match('/\\.\\./', $domain)) { | |
// domain part has two consecutive dots | |
$isValid = false; | |
} else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\", "", $local))) { | |
// character not valid in local part unless | |
// local part is quoted | |
if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\", "", $local))) { | |
$isValid = false; | |
} | |
} | |
if ($isValid && !(checkdnsrr($domain, "MX") || checkdnsrr($domain, "A"))) { | |
// domain not found in DNS | |
$isValid = false; | |
} | |
} | |
return $isValid; | |
} | |
public static function isUKPostcode(&$toCheck) { | |
// Permitted letters depend upon their position in the postcode. | |
$alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1 | |
$alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2 | |
$alpha3 = "[abcdefghjkpmnrstuvwxy]"; // Character 3 | |
$alpha4 = "[abehmnprvwxy]"; // Character 4 | |
$alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5 | |
// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA with a space | |
$pcexp[0] = '/^(' . $alpha1 . '{1}' . $alpha2 . '{0,1}[0-9]{1,2})([[:space:]]{0,})([0-9]{1}' . $alpha5 . '{2})$/'; | |
// Expression for postcodes: ANA NAA | |
$pcexp[1] = '/^(' . $alpha1 . '{1}[0-9]{1}' . $alpha3 . '{1})([[:space:]]{0,})([0-9]{1}' . $alpha5 . '{2})$/'; | |
// Expression for postcodes: AANA NAA | |
$pcexp[2] = '/^(' . $alpha1 . '{1}' . $alpha2 . '{1}[0-9]{1}' . $alpha4 . ')([[:space:]]{0,})([0-9]{1}' . $alpha5 . '{2})$/'; | |
// Exception for the special postcode GIR 0AA | |
$pcexp[3] = '/^(gir)([[:space:]]{0,})(0aa)$/'; | |
// Standard BFPO numbers | |
$pcexp[4] = '/^(bfpo)([[:space:]]{0,})([0-9]{1,4})$/'; | |
// c/o BFPO numbers | |
$pcexp[5] = '/^(bfpo)([[:space:]]{0,})(c\/o([[:space:]]{0,})[0-9]{1,3})$/'; | |
// Overseas Territories | |
$pcexp[6] = '/^([a-z]{4})([[:space:]]{0,})(1zz)$/'; | |
// Anquilla | |
$pcexp[7] = '/^ai-2640$/'; | |
// Load up the string to check, converting into lowercase | |
$postcode = strtolower($toCheck); | |
// Assume we are not going to find a valid postcode | |
$valid = false; | |
// Check the string against the six types of postcodes | |
foreach ($pcexp as $regexp) { | |
if (preg_match($regexp, $postcode, $matches)) { | |
// Load new postcode back into the form element | |
$postcode = strtoupper($matches[1] . ' ' . $matches [3]); | |
// Take account of the special BFPO c/o format | |
$postcode = preg_replace('/C\/O([[:space:]]{0,})/', 'c/o ', $postcode); | |
// Take acount of special Anquilla postcode format (a pain, but that's the way it is) | |
if (preg_match($pcexp[7], strtolower($toCheck), $matches)) | |
$postcode = 'AI-2640'; | |
// Remember that we have found that the code is valid and break from loop | |
$valid = true; | |
break; | |
} | |
} | |
// Return with the reformatted valid postcode in uppercase if the postcode was | |
// valid | |
if ($valid) { | |
$toCheck = $postcode; | |
return true; | |
} | |
else | |
return false; | |
} | |
public static function isUKTelephone(&$strTelephoneNumber, &$intError, &$strError) { | |
// Copy the parameter and strip out the spaces | |
$strTelephoneNumberCopy = str_replace(' ', '', $strTelephoneNumber); | |
// Convert into a string and check that we were provided with something | |
if (empty($strTelephoneNumberCopy)) { | |
$intError = 1; | |
$strError = 'Telephone number not provided'; | |
return false; | |
} | |
// Don't allow country codes to be included (assumes a leading "+") | |
if (mb_ereg('^(\+)[\s]*(.*)$', $strTelephoneNumberCopy)) { | |
$intError = 2; | |
$strError = 'UK telephone number without the country code, please'; | |
return false; | |
} | |
// Remove hyphens - they are not part of a telephine number | |
$strTelephoneNumberCopy = str_replace('-', '', $strTelephoneNumberCopy); | |
// Now check that all the characters are digits | |
if (!mb_ereg('^[0-9]{10,11}$', $strTelephoneNumberCopy)) { | |
$intError = 3; | |
$strError = 'UK telephone numbers should contain 10 or 11 digits'; | |
return false; | |
} | |
// Now check that the first digit is 0 | |
if (!mb_ereg('^0[0-9]{9,10}$', $strTelephoneNumberCopy)) { | |
$intError = 4; | |
$strError = 'The telephone number should start with a 0'; | |
return false; | |
} | |
// Check the string against the numbers allocated for dramas | |
// Expression for numbers allocated to dramas | |
$tnexp[0] = '^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$'; | |
$tnexp[1] = '^02079460[0-9]{3}$'; | |
$tnexp[2] = '^01914980[0-9]{3}$'; | |
$tnexp[3] = '^02890180[0-9]{3}$'; | |
$tnexp[4] = '^02920180[0-9]{3}$'; | |
$tnexp[5] = '^01632960[0-9]{3}$'; | |
$tnexp[6] = '^07700900[0-9]{3}$'; | |
$tnexp[7] = '^08081570[0-9]{3}$'; | |
$tnexp[8] = '^09098790[0-9]{3}$'; | |
$tnexp[9] = '^03069990[0-9]{3}$'; | |
foreach ($tnexp as $regexp) { | |
if (mb_ereg($regexp, $strTelephoneNumberCopy, $matches)) { | |
$intError = 5; | |
$strError = 'The telephone number is either invalid or inappropriate'; | |
return false; | |
} | |
} | |
// Finally, check that the telephone number is appropriate. | |
if (!mb_ereg('^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$', $strTelephoneNumberCopy)) { | |
$intError = 5; | |
$strError = 'The telephone number is either invalid or inappropriate'; | |
return false; | |
} | |
// Seems to be valid - return the stripped telephone number | |
$strTelephoneNumber = $strTelephoneNumberCopy; | |
$intError = 0; | |
$strError = ''; | |
return true; | |
} | |
static $random = ''; | |
// generates a random password | |
// By default of length 12 having special characters | |
static function generate_password($length = 12, $special_chars=true) { | |
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; | |
if ($special_chars) | |
$chars .= '!@#$%^&*_-()'; | |
$password = ''; | |
for ($i = 0; $i < $length; $i++) | |
$password .= substr($chars, self::generate_random_number(0, strlen($chars) - 1), 1); | |
return $password; | |
} | |
// generates a random number between $min and $max | |
static function generate_random_number($min=0, $max=0) { | |
$seed = mt_rand(); | |
// generate $random | |
// special thing about random is that it is 32(md5) + 40(sha1) + 40(sha1) = 112 long | |
// hence if we cut the 1st 8 characters everytime, we can get upto 14 random numbers | |
// each time the length of $random decreases and when it is less than 8, new 112 long $random is generated | |
if (strlen(self::$random) < 8) { | |
self::$random = md5(uniqid(microtime() . mt_rand(), true) . $seed); | |
self::$random .= sha1(self::$random); | |
self::$random .= sha1(self::$random . $seed); | |
} | |
// take first 8 characters | |
$value = substr(self::$random, 0, 8); | |
// strip first 8 character, leaving remainder for next call | |
self::$random = substr(self::$random, 8); | |
$value = abs(hexdec($value)); | |
// Reduce the value to be within the min - max range. 4294967295 = 0xffffffff = max random number | |
if ($max != 0) | |
$value = $min + (($max - $min + 1) * ($value / (4294967295 + 1))); | |
return abs(intval($value)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment