101 lines
2.5 KiB
PHP
101 lines
2.5 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];
|
|
}
|
|
|
|
//------------------------------------------
|
|
//-------- 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,
|
|
"IsValid" => (validateVIN($vin) ? "Yes" : "No"),
|
|
"Manufacturer" => getManufacturer(getWMI($vin)),
|
|
"year" => getYear(getVIS($vin))
|
|
];
|
|
}
|
|
else {
|
|
$messages = [
|
|
"IsValid" => "No"
|
|
];
|
|
}
|
|
|
|
//------------------------------------------
|
|
//JSON_ENCODE
|
|
//------------------------------------------
|
|
$messages = json_encode($messages, JSON_UNESCAPED_UNICODE);
|
|
|
|
//Send results
|
|
echo $messages;
|
|
|
|
?>
|