forked from globalcitizen/php-iban
/
php-iban.php
489 lines (430 loc) · 16.7 KB
/
php-iban.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
# PHP IBAN - http://code.google.com/p/php-iban - LGPLv3
# Global flag by request
$__disable_iiban_gmp_extension=false;
# Verify an IBAN number. Returns true or false.
# NOTE: Input can be printed 'IIBAN xx xx xx...' or 'IBAN xx xx xx...' or machine 'xxxxx' format.
function verify_iban($iban) {
# First convert to machine format.
$iban = iban_to_machine_format($iban);
# Get country of IBAN
$country = iban_get_country_part($iban);
# Test length of IBAN
if(strlen($iban)!=iban_country_get_iban_length($country)) { return false; }
# Get checksum of IBAN
$checksum = iban_get_checksum_part($iban);
# Get country-specific IBAN format regex
$regex = '/'.iban_country_get_iban_format_regex($country).'/';
# Check regex
if(preg_match($regex,$iban)) {
# Regex passed, check checksum
if(!iban_verify_checksum($iban)) {
return false;
}
}
else {
return false;
}
# Otherwise it 'could' exist
return true;
}
# Convert an IBAN to machine format. To do this, we
# remove IBAN from the start, if present, and remove
# non basic roman letter / digit characters
function iban_to_machine_format($iban) {
# Uppercase and trim spaces from left
$iban = ltrim(strtoupper($iban));
# Remove IIBAN or IBAN from start of string, if present
$iban = preg_replace('/^I?IBAN/','',$iban);
# Remove all non basic roman letter / digit characters
$iban = preg_replace('/[^a-zA-Z0-9]/','',$iban);
return $iban;
}
# Convert an IBAN to human format. To do this, we
# simply insert spaces right now, as per the ECBS
# (European Committee for Banking Standards)
# recommendations available at:
# http://www.europeanpaymentscouncil.eu/knowledge_bank_download.cfm?file=ECBS%20standard%20implementation%20guidelines%20SIG203V3.2.pdf
function iban_to_human_format($iban) {
# First verify validity, or return
if(!verify_iban($iban)) { return false; }
# Remove all spaces
$iban = str_replace(' ','',$iban);
# Add spaces every four characters
return wordwrap($iban,4,' ',true);
}
# Get the country part from an IBAN
function iban_get_country_part($iban) {
$iban = iban_to_machine_format($iban);
return substr($iban,0,2);
}
# Get the checksum part from an IBAN
function iban_get_checksum_part($iban) {
$iban = iban_to_machine_format($iban);
return substr($iban,2,2);
}
# Get the BBAN part from an IBAN
function iban_get_bban_part($iban) {
$iban = iban_to_machine_format($iban);
return substr($iban,4);
}
# Check the checksum of an IBAN - code modified from Validate_Finance PEAR class
function iban_verify_checksum($iban) {
# convert to machine format
$iban = iban_to_machine_format($iban);
# move first 4 chars (countrycode and checksum) to the end of the string
$tempiban = substr($iban, 4).substr($iban, 0, 4);
# subsitutute chars
$tempiban = iban_checksum_string_replace($tempiban);
# mod97-10
$result = iban_mod97_10($tempiban);
# checkvalue of 1 indicates correct IBAN checksum
if ($result != 1) {
return false;
}
return true;
}
# Find the correct checksum for an IBAN
# $iban The IBAN whose checksum should be calculated
function iban_find_checksum($iban) {
$iban = iban_to_machine_format($iban);
# move first 4 chars to right
$left = substr($iban,0,2) . '00'; # but set right-most 2 (checksum) to '00'
$right = substr($iban,4);
# glue back together
$tmp = $right . $left;
# convert letters using conversion table
$tmp = iban_checksum_string_replace($tmp);
# get mod97-10 output
$checksum = iban_mod97_10_checksum($tmp);
# return 98 minus the mod97-10 output, left zero padded to two digits
return str_pad((98-$checksum),2,'0',STR_PAD_LEFT);
}
# Set the correct checksum for an IBAN
# $iban IBAN whose checksum should be set
function iban_set_checksum($iban) {
$iban = iban_to_machine_format($iban);
return substr($iban,0,2) . iban_find_checksum($iban) . substr($iban,4);
}
# Character substitution required for IBAN MOD97-10 checksum validation/generation
# $s Input string (IBAN)
function iban_checksum_string_replace($s) {
$iban_replace_chars = range('A','Z');
foreach (range(10,35) as $tempvalue) { $iban_replace_values[]=strval($tempvalue); }
return str_replace($iban_replace_chars,$iban_replace_values,$s);
}
# Same as below but actually returns resulting checksum
function iban_mod97_10_checksum($numeric_representation) {
$checksum = intval(substr($numeric_representation, 0, 1));
for ($position = 1; $position < strlen($numeric_representation); $position++) {
$checksum *= 10;
$checksum += intval(substr($numeric_representation,$position,1));
$checksum %= 97;
}
return $checksum;
}
# Perform MOD97-10 checksum calculation ('Germanic-level effiency' version - thanks Chris!)
function iban_mod97_10($numeric_representation) {
global $__disable_iiban_gmp_extension;
# prefer php5 gmp extension if available
if(!($__disable_iiban_gmp_extension) && function_exists('gmp_intval')) { return gmp_intval(gmp_mod(gmp_init($numeric_representation, 10),'97')) === 1; }
/*
# old manual processing (~16x slower)
$checksum = intval(substr($numeric_representation, 0, 1));
for ($position = 1; $position < strlen($numeric_representation); $position++) {
$checksum *= 10;
$checksum += intval(substr($numeric_representation,$position,1));
$checksum %= 97;
}
return $checksum;
*/
# new manual processing (~3x slower)
$length = strlen($numeric_representation);
$rest = "";
$position = 0;
while ($position < $length) {
$value = 9-strlen($rest);
$n = $rest . substr($numeric_representation,$position,$value);
$rest = $n % 97;
$position = $position + $value;
}
return ($rest === 1);
}
# Get an array of all the parts from an IBAN
function iban_get_parts($iban) {
return array(
'country' => iban_get_country_part($iban),
'checksum' => iban_get_checksum_part($iban),
'bban' => iban_get_bban_part($iban),
'bank' => iban_get_bank_part($iban),
'country' => iban_get_country_part($iban),
'branch' => iban_get_branch_part($iban),
'account' => iban_get_account_part($iban)
);
}
# Get the Bank ID (institution code) from an IBAN
function iban_get_bank_part($iban) {
$iban = iban_to_machine_format($iban);
$country = iban_get_country_part($iban);
$start = iban_country_get_bankid_start_offset($country);
$stop = iban_country_get_bankid_stop_offset($country);
if($start!=''&&$stop!='') {
$bban = iban_get_bban_part($iban);
return substr($bban,$start,($stop-$start+1));
}
return '';
}
# Get the Branch ID (sort code) from an IBAN
function iban_get_branch_part($iban) {
$iban = iban_to_machine_format($iban);
$country = iban_get_country_part($iban);
$start = iban_country_get_branchid_start_offset($country);
$stop = iban_country_get_branchid_stop_offset($country);
if($start!=''&&$stop!='') {
$bban = iban_get_bban_part($iban);
return substr($bban,$start,($stop-$start+1));
}
return '';
}
# Get the (branch-local) account ID from an IBAN
function iban_get_account_part($iban) {
$iban = iban_to_machine_format($iban);
$country = iban_get_country_part($iban);
$start = iban_country_get_branchid_stop_offset($country);
if($start=='') {
$start = iban_country_get_bankid_stop_offset($country);
}
if($start!='') {
$bban = iban_get_bban_part($iban);
return substr($bban,$start+1);
}
return '';
}
# Get the name of an IBAN country
function iban_country_get_country_name($iban_country) {
return _iban_country_get_info($iban_country,'country_name');
}
# Get the domestic example for an IBAN country
function iban_country_get_domestic_example($iban_country) {
return _iban_country_get_info($iban_country,'domestic_example');
}
# Get the BBAN example for an IBAN country
function iban_country_get_bban_example($iban_country) {
return _iban_country_get_info($iban_country,'bban_example');
}
# Get the BBAN format (in SWIFT format) for an IBAN country
function iban_country_get_bban_format_swift($iban_country) {
return _iban_country_get_info($iban_country,'bban_format_swift');
}
# Get the BBAN format (as a regular expression) for an IBAN country
function iban_country_get_bban_format_regex($iban_country) {
return _iban_country_get_info($iban_country,'bban_format_regex');
}
# Get the BBAN length for an IBAN country
function iban_country_get_bban_length($iban_country) {
return _iban_country_get_info($iban_country,'bban_length');
}
# Get the IBAN example for an IBAN country
function iban_country_get_iban_example($iban_country) {
return _iban_country_get_info($iban_country,'iban_example');
}
# Get the IBAN format (in SWIFT format) for an IBAN country
function iban_country_get_iban_format_swift($iban_country) {
return _iban_country_get_info($iban_country,'iban_format_swift');
}
# Get the IBAN format (as a regular expression) for an IBAN country
function iban_country_get_iban_format_regex($iban_country) {
return _iban_country_get_info($iban_country,'iban_format_regex');
}
# Get the IBAN length for an IBAN country
function iban_country_get_iban_length($iban_country) {
return _iban_country_get_info($iban_country,'iban_length');
}
# Get the BBAN Bank ID start offset for an IBAN country
function iban_country_get_bankid_start_offset($iban_country) {
return _iban_country_get_info($iban_country,'bban_bankid_start_offset');
}
# Get the BBAN Bank ID stop offset for an IBAN country
function iban_country_get_bankid_stop_offset($iban_country) {
return _iban_country_get_info($iban_country,'bban_bankid_stop_offset');
}
# Get the BBAN Branch ID start offset for an IBAN country
function iban_country_get_branchid_start_offset($iban_country) {
return _iban_country_get_info($iban_country,'bban_branchid_start_offset');
}
# Get the BBAN Branch ID stop offset for an IBAN country
function iban_country_get_branchid_stop_offset($iban_country) {
return _iban_country_get_info($iban_country,'bban_branchid_stop_offset');
}
# Get the registry edition for an IBAN country
function iban_country_get_registry_edition($iban_country) {
return _iban_country_get_info($iban_country,'registry_edition');
}
# Is the IBAN country one official issued by SWIFT?
function iban_country_get_country_swift_official($iban_country) {
return _iban_country_get_info($iban_country,'country_swift_official');
}
# Is the IBAN country a SEPA member?
function iban_country_is_sepa($iban_country) {
return _iban_country_get_info($iban_country,'country_sepa');
}
# Get the list of all IBAN countries
function iban_countries() {
global $_iban_registry;
return array_keys($_iban_registry);
}
# Given an incorrect IBAN, return an array of zero or more checksum-valid
# suggestions for what the user might have meant, based upon common
# mistranscriptions.
function iban_mistranscription_suggestions($incorrect_iban) {
# abort on ridiculous length input (but be liberal)
$length = strlen($incorrect_iban);
if($length<5 || $length>34) { return array('(supplied iban length insane)'); }
# abort if mistranscriptions data is unable to load
if(!_iban_load_mistranscriptions()) { return array('(failed to load mistranscriptions)'); }
# init
global $_iban_mistranscriptions;
$suggestions = array();
# we have a string of approximately IBAN-like length.
# ... now let's make suggestions.
$numbers = array('0','1','2','3','4','5','6','7','8','9');
for($i=0;$i<$length;$i++) {
# get the character at this position
$character = substr($incorrect_iban,$i,1);
# for each known transcription error resulting in this character
foreach($_iban_mistranscriptions[$character] as $possible_origin) {
# if we're:
# - in the first 2 characters (country) and the possible replacement
# is a letter
# - in the 3rd or 4th characters (checksum) and the possible
# replacement is a number
# - later in the string
if(($i<2 && !in_array($possible_origin,$numbers)) ||
($i>=2 && $i<=3 && in_array($possible_origin,$numbers)) ||
$i>3) {
# construct a possible IBAN using this possible origin for the
# mistranscribed character, replaced at this position only
$possible_iban = substr($incorrect_iban,0,$i) . $possible_origin . substr($incorrect_iban,$i+1);
# if the checksum passes, return it as a possibility
if(verify_iban($possible_iban)) {
array_push($suggestions,$possible_iban);
}
}
}
}
# now we check for the type of mistransposition case where all of
# the characters of a certain type within a string were mistransposed.
# - first generate a character frequency table
$char_freqs = array();
for($i=0;$i<strlen($incorrect_iban);$i++) {
if(!isset($char_freqs[substr($incorrect_iban,$i,1)])) {
$char_freqs[substr($incorrect_iban,$i,1)] = 1;
}
else {
$char_freqs[substr($incorrect_iban,$i,1)]++;
}
}
# - now, for each of the characters in the string...
foreach($char_freqs as $char=>$freq) {
# if the character occurs more than once
if($freq>1) {
# check the 'all occurrences of <char> were mistranscribed' case
foreach($_iban_mistranscriptions[$char] as $possible_origin) {
$possible_iban = str_replace($char,$possible_origin,$incorrect_iban);
if(verify_iban($possible_iban)) {
array_push($suggestions,$possible_iban);
}
}
}
}
return $suggestions;
}
##### internal use functions - safe to ignore ######
# Load the IBAN registry from disk.
global $_iban_registry;
$_iban_registry = array();
_iban_load_registry();
function _iban_load_registry() {
global $_iban_registry;
# if the registry is not yet loaded, or has been corrupted, reload
if(!is_array($_iban_registry) || count($_iban_registry)<1) {
$data = file_get_contents(dirname(__FILE__) . '/registry.txt');
$lines = explode("\n",$data);
array_shift($lines); # drop leading description line
# loop through lines
foreach($lines as $line) {
if($line!='') {
# split to fields
$old_display_errors_value = ini_get('display_errors');
ini_set('display_errors',false);
$old_error_reporting_value = ini_get('error_reporting');
ini_set('error_reporting',false);
list($country,$country_name,$domestic_example,$bban_example,$bban_format_swift,$bban_format_regex,$bban_length,$iban_example,$iban_format_swift,$iban_format_regex,$iban_length,$bban_bankid_start_offset,$bban_bankid_stop_offset,$bban_branchid_start_offset,$bban_branchid_stop_offset,$registry_edition,$country_sepa,$country_swift_official) = explode('|',$line);
ini_set('display_errors',$old_display_errors_value);
ini_set('error_reporting',$old_error_reporting_value);
# assign to registry
$_iban_registry[$country] = array(
'country' => $country,
'country_name' => $country_name,
'country_sepa' => $country_sepa,
'domestic_example' => $domestic_example,
'bban_example' => $bban_example,
'bban_format_swift' => $bban_format_swift,
'bban_format_regex' => $bban_format_regex,
'bban_length' => $bban_length,
'iban_example' => $iban_example,
'iban_format_swift' => $iban_format_swift,
'iban_format_regex' => $iban_format_regex,
'iban_length' => $iban_length,
'bban_bankid_start_offset' => $bban_bankid_start_offset,
'bban_bankid_stop_offset' => $bban_bankid_stop_offset,
'bban_branchid_start_offset' => $bban_branchid_start_offset,
'bban_branchid_stop_offset' => $bban_branchid_stop_offset,
'registry_edition' => $registry_edition,
'country_swift_official' => $country_swift_official
);
}
}
}
}
# Get information from the IBAN registry by example IBAN / code combination
function _iban_get_info($iban,$code) {
$country = iban_get_country_part($iban);
return _iban_country_get_info($country,$code);
}
# Get information from the IBAN registry by country / code combination
function _iban_country_get_info($country,$code) {
global $_iban_registry;
$country = strtoupper($country);
$code = strtolower($code);
if(array_key_exists($country,$_iban_registry)) {
if(array_key_exists($code,$_iban_registry[$country])) {
return $_iban_registry[$country][$code];
}
}
return false;
}
# Load common mistranscriptions from disk.
function _iban_load_mistranscriptions() {
global $_iban_mistranscriptions;
# do not reload if already present
if(is_array($_iban_mistranscriptions) && count($_iban_mistranscriptions) == 36) { return true; }
$_iban_mistranscriptions = array();
$file = dirname(__FILE__) . '/mistranscriptions.txt';
if(!file_exists($file) || !is_readable($file)) { return false; }
$data = file_get_contents($file);
$lines = explode("\n",$data);
foreach($lines as $line) {
# match lines with ' c-<x> = <something>' where x is a word-like character
if(preg_match('/^ *c-(\w) = (.*?)$/',$line,$matches)) {
# normalize the character to upper case
$character = strtoupper($matches[1]);
# break the possible origins list at '/', strip quotes & spaces
$chars = explode(' ',str_replace('"','',preg_replace('/ *?\/ *?/','',$matches[2])));
# assign as possible mistranscriptions for that character
$_iban_mistranscriptions[$character] = $chars;
}
}
return true;
}
?>