137 lines
3.7 KiB
PHP
137 lines
3.7 KiB
PHP
<?php
|
|
defined($security_key) or exit;
|
|
|
|
//------------------------------------------
|
|
// VIN Decoder
|
|
//------------------------------------------
|
|
//--------------START ---------------------
|
|
// translated from JS (kevinboutin on 3/11/18) to PHP
|
|
// https://gist.github.com/kevboutin/3ac029e336fc7cafd20c05adda42ffa5
|
|
//------------------------------------------
|
|
/* Transliterate VIN characters for validation
|
|
function transliterate($c) {
|
|
$index = strpos('0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ', $c);
|
|
return $index % 10;
|
|
}
|
|
// Determine the check digit to validate the VIN
|
|
function getCheckDigit($vin) {
|
|
$map = '0123456789X';
|
|
$weights = '8765432X098765432';
|
|
$sum = 0;
|
|
for ($i = 0; $i < 17; ++$i) {
|
|
$sum += transliterate($vin[$i]) * strpos($map, $weights[$i]);
|
|
}
|
|
return $map[$sum % 11];
|
|
}
|
|
// Validate the VIN
|
|
function validateVIN($vin) {
|
|
if (strlen($vin) !== 17) return false;
|
|
return getCheckDigit($vin) === $vin[8];
|
|
}*/
|
|
|
|
function validateVIN($vin) {
|
|
|
|
$vin = strtolower($vin);
|
|
if (!preg_match('/^[^\Wioq]{17}$/', $vin)) {
|
|
return false;
|
|
}
|
|
$weights = array(8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2);
|
|
$transliterations = array(
|
|
"a" => 1, "b" => 2, "c" => 3, "d" => 4,
|
|
"e" => 5, "f" => 6, "g" => 7, "h" => 8,
|
|
"j" => 1, "k" => 2, "l" => 3, "m" => 4,
|
|
"n" => 5, "p" => 7, "r" => 9, "s" => 2,
|
|
"t" => 3, "u" => 4, "v" => 5, "w" => 6,
|
|
"x" => 7, "y" => 8, "z" => 9
|
|
);
|
|
$sum = 0;
|
|
for($i = 0 ; $i < strlen($vin) ; $i++ ) { // loop through characters of VIN
|
|
// add transliterations * weight of their positions to get the sum
|
|
$check_char = substr($vin, $i, 1);
|
|
if(!is_numeric($check_char)) {
|
|
$sum += $transliterations[$check_char] * $weights[$i];
|
|
} else {
|
|
$sum += $check_char * $weights[$i];
|
|
}
|
|
}
|
|
|
|
// find checkdigit by taking the mod of the sum
|
|
$checkdigit = $sum % 11;
|
|
if($checkdigit == 10) { // checkdigit of 10 is represented by "X"
|
|
$checkdigit = "x";
|
|
}
|
|
$actual_checkdigit = substr($vin, 8, 1);
|
|
return ($checkdigit == $actual_checkdigit);
|
|
}
|
|
|
|
//------------------------------------------
|
|
//-------- END --------------------------
|
|
//------------------------------------------
|
|
|
|
//GET WMI - world manufacturer ID
|
|
function getWMI($vin){
|
|
//WMI = first 3 digits
|
|
$wmi = substr($vin, 0, 3);
|
|
return $wmi;
|
|
}
|
|
|
|
// VDS - vehicle descriptor
|
|
function getVDS($vin){
|
|
//WMI = first 3 digits
|
|
$vds = substr($vin, 3, 5);
|
|
return $vds;
|
|
}
|
|
|
|
// VIS - serialnumber
|
|
function getVIS($vin){
|
|
//WMI = first 3 digits
|
|
$vis = substr($vin, 9, 8);
|
|
return $vis;
|
|
}
|
|
|
|
// Get the manufacturing country of the vehicle
|
|
function getCountry($wmi) {
|
|
include './settings/settingsvin.php';
|
|
return $countries[substr($wmi, 0, 2)] ?? 'Unknown';
|
|
}
|
|
|
|
// Get the manufacturer of the vehicle
|
|
function getManufacturer($wmi) {
|
|
include './settings/settingsvin.php';
|
|
return $manufacturers[$wmi] ?? 'Unknown';
|
|
}
|
|
|
|
function getYear($vis){
|
|
include './settings/settingsvin.php';
|
|
//first character = year
|
|
return $years[substr($vis, 0, 1)] ?? 'Unknown';
|
|
}
|
|
|
|
|
|
// Check if get_content has 17 characters, then expect VIN
|
|
if (strlen($get_content) == 17){
|
|
$vin = strtoupper($get_content);
|
|
|
|
$messages = [
|
|
"VIN" => $vin,
|
|
"Manufacturer" => getManufacturer(getWMI($vin)),
|
|
"year" => getYear(getVIS($vin))
|
|
];
|
|
}
|
|
else {
|
|
$messages = [
|
|
"VIN" => $vin,
|
|
"Manufacturer" => "Unknown",
|
|
"year" => "Unknown"
|
|
];
|
|
}
|
|
|
|
//------------------------------------------
|
|
//JSON_ENCODE
|
|
//------------------------------------------
|
|
$messages = json_encode($messages, JSON_UNESCAPED_UNICODE);
|
|
|
|
//Send results
|
|
echo $messages;
|
|
|
|
?>
|