-
Notifications
You must be signed in to change notification settings - Fork 1
/
caldera-forms-iban-validator.php
93 lines (76 loc) · 2.68 KB
/
caldera-forms-iban-validator.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
<?php
/*
* Plugin Name: Caldera Forms IBAN Validator
* Description: Validator for iban field.
* Author: MOEWE
* Author URI: https://www.moewe.io
* Text Domain: caldera-forms-iban-validator
*/
add_filter('caldera_forms_get_form_processors', 'caldera_forms_iban_validator_processor');
function caldera_forms_iban_validator_processor($processors)
{
$processors['caldera_forms_iban_validator'] = array(
'name' => __('IBAN Validator', 'caldera-forms-iban-validator'),
'description' => __('Processor to validate IBAN field.'),
'pre_processor' => 'caldera_forms_iban_validator_pre_processor',
'template' => __DIR__ . '/templates/caldera_forms_iban_validator_processor.config.php'
);
return $processors;
}
function caldera_forms_iban_validator_pre_processor($config, $form)
{
$cf_iban_slug = Caldera_Forms::do_magic_tags($config['cf_iban_slug']);
foreach ($form['fields'] as $field) {
if ($field['slug'] == $cf_iban_slug) {
$cf_iban_field_id = $field['ID'];
break;
}
}
if (!$cf_iban_field_id) {
return;
}
$raw_data = Caldera_Forms::get_submission_data($form);
$iban_field_value = $raw_data[$cf_iban_field_id];
if(!caldera_forms_iban_validator_check_iban($iban_field_value)) {
return array(
'note' => __('IBAN ungültig: Die Berechnung der Prüfziffer hat ergeben, dass die Eingabe keine gültige IBAN ist.','caldera-forms-iban-validator'),
'type' => 'error'
);
}
return;
}
function caldera_forms_iban_validator_check_iban($iban) {
// Normalize input (remove spaces and make upcase)
$iban = strtoupper(str_replace(' ', '', $iban));
if (preg_match('/^[A-Z]{2}[0-9]{2}[A-Z0-9]{1,30}$/', $iban)) {
$country = substr($iban, 0, 2);
$check = intval(substr($iban, 2, 2));
$account = substr($iban, 4);
// To numeric representation
$search = range('A','Z');
foreach (range(10,35) as $tmp)
$replace[]=strval($tmp);
$numstr=str_replace($search, $replace, $account.$country.'00');
// Calculate checksum
$checksum = intval(substr($numstr, 0, 1));
for ($pos = 1; $pos < strlen($numstr); $pos++) {
$checksum *= 10;
$checksum += intval(substr($numstr, $pos,1));
$checksum %= 97;
}
return ((98-$checksum) == $check);
} else
return false;
}
function caldera_forms_iban_validator_fields()
{
return array(
array(
'id' => 'cf_iban_slug',
'label' => 'IBAN Slug',
'type' => 'text',
'required' => true,
'magic' => false,
),
);
}