<?php
/**
* WEID<=>OID Converter
* (c) Webfan.de, ViaThinkSoft
**/
class WeidOidConverter {
protected static function weLuhnGetCheckDigit($str) {
$wrkstr = str_replace('-', '', $str); // remove separators
for ($i=0; $i<36; $i++) {
}
$parity = $nbdigits & 1;
$sum = 0;
for ($n=$nbdigits-1; $n>=0; $n--) {
$digit = $wrkstr[$n];
if (($n & 1) != $parity) $digit *= 2;
if ($digit > 9) $digit -= 9;
$sum += $digit;
}
return ($sum%10) == 0 ? 0 : 10-($sum%10);
}
public static function weid2oid(&$weid, $namespace='weid:', $base='1-3-6-1-4-1-SZ5-8') {
if (stripos($weid, $namespace) !== 0) return false; // wrong namespace
$weid = explode(':', $weid, 2)[1]; // remove namespace
$expected_checksum = self::weLuhnGetCheckDigit(implode('-',$elements));
if ($actual_checksum != '?') {
if ($actual_checksum != $expected_checksum) return false; // wrong checksum
} else {
// If checksum is '?', it will be replaced by the actual checksum,
// e.g. weid:EXAMPLE-? becomes weid:EXAMPLE-3
}
foreach ($elements as &$arc) {
//$arc = strtoupper(base_convert($arc, 36, 10));
$arc = strtoupper(self::base_convert_bigint($arc, 36, 10));
}
return $oidstr;
}
public static function oid2weid($oid, $namespace='weid:', $base='1-3-6-1-4-1-SZ5-8') {
foreach ($elements as &$arc) {
//$arc = strtoupper(base_convert($arc, 10, 36));
$arc = strtoupper(self::base_convert_bigint($arc, 10, 36));
}
$weidstr = implode('-', $elements);
if (stripos($weidstr.'-', $base.'-') !== 0) return false; // wrong base
return $namespace . substr($weidstr.'-'.self::weLuhnGetCheckDigit($weidstr), strlen($base.'-'));
}
protected static function base_convert_bigint($numstring, $frombase, $tobase) {
$frombase_str = '';
for ($i=0; $i<$frombase; $i++) {
}
$tobase_str = '';
for ($i=0; $i<$tobase; $i++) {
}
$result = '';
for ($i = 0; $i < $length; $i++) {
$number[$i] = stripos($frombase_str, $numstring[$i]);
}
do { // Loop until whole number is converted
$divide = 0;
$newlen = 0;
for ($i = 0; $i < $length; $i++) { // Perform division manually (which is why this works with big numbers)
$divide = $divide * $frombase + $number[$i];
if ($divide >= $tobase) {
$number[$newlen++] = (int)($divide / $tobase);
$divide = $divide % $tobase;
} else if ($newlen > 0) {
$number[$newlen++] = 0;
}
}
$length = $newlen;
$result = $tobase_str[$divide] . $result; // Divide is basically $numstring % $tobase (i.e. the new character)
}
while ($newlen != 0);
return $result;
}
}
# --- TESTCASE ---
/*
$lines = file('E:\__fastphp\weid_test.txt');
$cnt = 0;
foreach ($lines as $line) {
$line = trim($line);
if (empty($line)) continue;
list($testcase_oid, $testcase_weid) = explode("\t", $line);
$weid = WeidOidConverter::oid2weid($testcase_oid);
if ($weid != $testcase_weid) echo "OID2WEID ERROR: '$testcase_oid' = '$weid' (should be: '$testcase_weid')\n";
$oid = WeidOidConverter::weid2oid($testcase_weid);
if ($oid != $testcase_oid) echo "WEID2OID ERROR: '$testcase_weid' = '$oid' (should be: '$testcase_oid')\n";
$cnt++;
}
echo "Done, checked $cnt testcases!\n";
$weid = 'weid:EXAMPLE-?';
echo "Autocomplete test: '$weid' => '".WeidOidConverter::weid2oid($weid)."' (checksum corrected to: '$weid')\n";
*/