76 lines
2.0 KiB
PHP
76 lines
2.0 KiB
PHP
<?php
|
|
|
|
$iban = $_REQUEST["value"];
|
|
|
|
|
|
function validateIBAN($iban) {
|
|
// Entferne Leerzeichen und mache alles zu Großbuchstaben
|
|
$iban = strtoupper(str_replace(' ', '', $iban));
|
|
|
|
// Länder und deren spezifische IBAN-Längen
|
|
$ibanLengths = [
|
|
'DE' => 22, 'AT' => 20, 'CH' => 21, 'BE' => 16, 'FR' => 27, 'GB' => 22,
|
|
'IT' => 27, 'ES' => 24, 'NL' => 18, 'PL' => 28, 'SE' => 24, 'PT' => 25,
|
|
'LU' => 20, 'NO' => 15, 'DK' => 18, 'FI' => 18, 'IE' => 22, 'GR' => 27,
|
|
'CZ' => 24, 'HU' => 28, 'SK' => 24, 'SI' => 19
|
|
// Du kannst hier noch mehr Länder hinzufügen
|
|
];
|
|
|
|
// Prüfen, ob der IBAN mindestens 2 Zeichen für das Land enthält
|
|
if (strlen($iban) < 2) {
|
|
return false;
|
|
}
|
|
|
|
// Extrahiere den Ländercode
|
|
$countryCode = substr($iban, 0, 2);
|
|
|
|
// Prüfe, ob das Land bekannt ist
|
|
if (!array_key_exists($countryCode, $ibanLengths)) {
|
|
return false;
|
|
}
|
|
|
|
// Prüfe, ob die Länge korrekt ist
|
|
if (strlen($iban) !== $ibanLengths[$countryCode]) {
|
|
return false;
|
|
}
|
|
|
|
// Bewege die ersten 4 Zeichen ans Ende
|
|
$ibanRearranged = substr($iban, 4) . substr($iban, 0, 4);
|
|
|
|
// Ersetze Buchstaben durch Zahlen (A=10, B=11, ..., Z=35)
|
|
$ibanNumeric = '';
|
|
foreach (str_split($ibanRearranged) as $char) {
|
|
if (ctype_alpha($char)) {
|
|
$ibanNumeric .= ord($char) - 55;
|
|
} else {
|
|
$ibanNumeric .= $char;
|
|
}
|
|
}
|
|
|
|
// Modulo 97 rechnen (stückweise wegen sehr großer Zahlen)
|
|
$remainder = $ibanNumeric;
|
|
while (strlen($remainder) > 2) {
|
|
$block = substr($remainder, 0, 9);
|
|
$remainder = (int)$block % 97 . substr($remainder, strlen($block));
|
|
}
|
|
|
|
return ((int)$remainder % 97) === 1;
|
|
}
|
|
|
|
|
|
|
|
if (validateIBAN($iban))
|
|
{
|
|
$status = "success";
|
|
} else
|
|
{
|
|
$status = "failure";
|
|
}
|
|
|
|
$data = array( 'status' => $status, 'response' => ['IBAN nicht gültig bzw. falsch!'] );
|
|
|
|
header('Content-type: application/json');
|
|
echo json_encode( $data );
|
|
|
|
?>
|