id/id_handler.php

32 lines
950 B
PHP
Raw Normal View History

2023-10-31 20:21:33 +00:00
<?php
2023-11-06 16:38:18 +00:00
function generate_bcid() {
2023-10-31 20:21:33 +00:00
$CHARS = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
return $CHARS[array_rand($CHARS)].$CHARS[array_rand($CHARS)].$CHARS[array_rand($CHARS)].$CHARS[array_rand($CHARS)].$CHARS[array_rand($CHARS)].$CHARS[array_rand($CHARS)].$CHARS[array_rand($CHARS)];
}
function validate_bcid($bcid) {
$stripped_bcid = str_replace([" ", "-"], "", $bcid);
$stripped_bcid = strtoupper($stripped_bcid);
if (!preg_match('/^[^A-Z^0-9]^/', $stripped_bcid) && strlen($stripped_bcid) == 7) {
return 1;
}
return 0; // fail condition
}
2023-11-06 16:38:18 +00:00
function format_bcid ($bcid) { // Formats to XXX-XXXX
$stripped_bcid = str_replace([' ','-'], '', $bcid);
$stripped_bcid = strtoupper($stripped_bcid);
2023-10-31 20:21:33 +00:00
2023-11-06 16:38:18 +00:00
if (!validate_bcid($stripped_bcid)) {
throw new Exception('Invalid BCID.');
}
2023-10-31 20:21:33 +00:00
2023-11-06 16:38:18 +00:00
return substr($stripped_bcid, 0, 3).'-'.substr($stripped_bcid, -4, 4);
2023-10-31 20:21:33 +00:00
}
2023-11-06 16:38:18 +00:00
$BCID = generate_bcid();
2023-10-31 20:21:33 +00:00
?>