Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / validator / Constraints / IbanValidator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Constraints;
13
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
16 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17
18 /**
19  * @author Manuel Reinhard <manu@sprain.ch>
20  * @author Michael Schummel
21  * @author Bernhard Schussek <bschussek@gmail.com>
22  *
23  * @see http://www.michael-schummel.de/2007/10/05/iban-prufung-mit-php/
24  */
25 class IbanValidator extends ConstraintValidator
26 {
27     /**
28      * IBAN country specific formats.
29      *
30      * The first 2 characters from an IBAN format are the two-character ISO country code.
31      * The following 2 characters represent the check digits calculated from the rest of the IBAN characters.
32      * The rest are up to thirty alphanumeric characters for
33      * a BBAN (Basic Bank Account Number) which has a fixed length per country and,
34      * included within it, a bank identifier with a fixed position and a fixed length per country
35      *
36      * @see https://www.swift.com/sites/default/files/resources/iban_registry.pdf
37      *
38      * @var array
39      */
40     private static $formats = array(
41         'AD' => 'AD\d{2}\d{4}\d{4}[\dA-Z]{12}', // Andorra
42         'AE' => 'AE\d{2}\d{3}\d{16}', // United Arab Emirates
43         'AL' => 'AL\d{2}\d{8}[\dA-Z]{16}', // Albania
44         'AO' => 'AO\d{2}\d{21}', // Angola
45         'AT' => 'AT\d{2}\d{5}\d{11}', // Austria
46         'AX' => 'FI\d{2}\d{6}\d{7}\d{1}', // Aland Islands
47         'AZ' => 'AZ\d{2}[A-Z]{4}[\dA-Z]{20}', // Azerbaijan
48         'BA' => 'BA\d{2}\d{3}\d{3}\d{8}\d{2}', // Bosnia and Herzegovina
49         'BE' => 'BE\d{2}\d{3}\d{7}\d{2}', // Belgium
50         'BF' => 'BF\d{2}\d{23}', // Burkina Faso
51         'BG' => 'BG\d{2}[A-Z]{4}\d{4}\d{2}[\dA-Z]{8}', // Bulgaria
52         'BH' => 'BH\d{2}[A-Z]{4}[\dA-Z]{14}', // Bahrain
53         'BI' => 'BI\d{2}\d{12}', // Burundi
54         'BJ' => 'BJ\d{2}[A-Z]{1}\d{23}', // Benin
55         'BL' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // Saint Barthelemy
56         'BR' => 'BR\d{2}\d{8}\d{5}\d{10}[A-Z][\dA-Z]', // Brazil
57         'CG' => 'CG\d{2}\d{23}', // Congo
58         'CH' => 'CH\d{2}\d{5}[\dA-Z]{12}', // Switzerland
59         'CI' => 'CI\d{2}[A-Z]{1}\d{23}', // Ivory Coast
60         'CM' => 'CM\d{2}\d{23}', // Cameron
61         'CR' => 'CR\d{2}\d{3}\d{14}', // Costa Rica
62         'CV' => 'CV\d{2}\d{21}', // Cape Verde
63         'CY' => 'CY\d{2}\d{3}\d{5}[\dA-Z]{16}', // Cyprus
64         'CZ' => 'CZ\d{2}\d{20}', // Czech Republic
65         'DE' => 'DE\d{2}\d{8}\d{10}', // Germany
66         'DO' => 'DO\d{2}[\dA-Z]{4}\d{20}', // Dominican Republic
67         'DK' => 'DK\d{2}\d{4}\d{10}', // Denmark
68         'DZ' => 'DZ\d{2}\d{20}', // Algeria
69         'EE' => 'EE\d{2}\d{2}\d{2}\d{11}\d{1}', // Estonia
70         'ES' => 'ES\d{2}\d{4}\d{4}\d{1}\d{1}\d{10}', // Spain (also includes Canary Islands, Ceuta and Melilla)
71         'FI' => 'FI\d{2}\d{6}\d{7}\d{1}', // Finland
72         'FO' => 'FO\d{2}\d{4}\d{9}\d{1}', // Faroe Islands
73         'FR' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // France
74         'GF' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // French Guyana
75         'GB' => 'GB\d{2}[A-Z]{4}\d{6}\d{8}', // United Kingdom of Great Britain and Northern Ireland
76         'GE' => 'GE\d{2}[A-Z]{2}\d{16}', // Georgia
77         'GI' => 'GI\d{2}[A-Z]{4}[\dA-Z]{15}', // Gibraltar
78         'GL' => 'GL\d{2}\d{4}\d{9}\d{1}', // Greenland
79         'GP' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // Guadeloupe
80         'GR' => 'GR\d{2}\d{3}\d{4}[\dA-Z]{16}', // Greece
81         'GT' => 'GT\d{2}[\dA-Z]{4}[\dA-Z]{20}', // Guatemala
82         'HR' => 'HR\d{2}\d{7}\d{10}', // Croatia
83         'HU' => 'HU\d{2}\d{3}\d{4}\d{1}\d{15}\d{1}', // Hungary
84         'IE' => 'IE\d{2}[A-Z]{4}\d{6}\d{8}', // Ireland
85         'IL' => 'IL\d{2}\d{3}\d{3}\d{13}', // Israel
86         'IR' => 'IR\d{2}\d{22}', // Iran
87         'IS' => 'IS\d{2}\d{4}\d{2}\d{6}\d{10}', // Iceland
88         'IT' => 'IT\d{2}[A-Z]{1}\d{5}\d{5}[\dA-Z]{12}', // Italy
89         'JO' => 'JO\d{2}[A-Z]{4}\d{4}[\dA-Z]{18}', // Jordan
90         'KW' => 'KW\d{2}[A-Z]{4}\d{22}', // KUWAIT
91         'KZ' => 'KZ\d{2}\d{3}[\dA-Z]{13}', // Kazakhstan
92         'LB' => 'LB\d{2}\d{4}[\dA-Z]{20}', // LEBANON
93         'LI' => 'LI\d{2}\d{5}[\dA-Z]{12}', // Liechtenstein (Principality of)
94         'LT' => 'LT\d{2}\d{5}\d{11}', // Lithuania
95         'LU' => 'LU\d{2}\d{3}[\dA-Z]{13}', // Luxembourg
96         'LV' => 'LV\d{2}[A-Z]{4}[\dA-Z]{13}', // Latvia
97         'MC' => 'MC\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // Monaco
98         'MD' => 'MD\d{2}[\dA-Z]{2}[\dA-Z]{18}', // Moldova
99         'ME' => 'ME\d{2}\d{3}\d{13}\d{2}', // Montenegro
100         'MF' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // Saint Martin (French part)
101         'MG' => 'MG\d{2}\d{23}', // Madagascar
102         'MK' => 'MK\d{2}\d{3}[\dA-Z]{10}\d{2}', // Macedonia, Former Yugoslav Republic of
103         'ML' => 'ML\d{2}[A-Z]{1}\d{23}', // Mali
104         'MQ' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // Martinique
105         'MR' => 'MR13\d{5}\d{5}\d{11}\d{2}', // Mauritania
106         'MT' => 'MT\d{2}[A-Z]{4}\d{5}[\dA-Z]{18}', // Malta
107         'MU' => 'MU\d{2}[A-Z]{4}\d{2}\d{2}\d{12}\d{3}[A-Z]{3}', // Mauritius
108         'MZ' => 'MZ\d{2}\d{21}', // Mozambique
109         'NC' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // New Caledonia
110         'NL' => 'NL\d{2}[A-Z]{4}\d{10}', // The Netherlands
111         'NO' => 'NO\d{2}\d{4}\d{6}\d{1}', // Norway
112         'PF' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // French Polynesia
113         'PK' => 'PK\d{2}[A-Z]{4}[\dA-Z]{16}', // Pakistan
114         'PL' => 'PL\d{2}\d{8}\d{16}', // Poland
115         'PM' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // Saint Pierre et Miquelon
116         'PS' => 'PS\d{2}[A-Z]{4}[\dA-Z]{21}', // Palestine, State of
117         'PT' => 'PT\d{2}\d{4}\d{4}\d{11}\d{2}', // Portugal (plus Azores and Madeira)
118         'QA' => 'QA\d{2}[A-Z]{4}[\dA-Z]{21}', // Qatar
119         'RE' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // Reunion
120         'RO' => 'RO\d{2}[A-Z]{4}[\dA-Z]{16}', // Romania
121         'RS' => 'RS\d{2}\d{3}\d{13}\d{2}', // Serbia
122         'SA' => 'SA\d{2}\d{2}[\dA-Z]{18}', // Saudi Arabia
123         'SE' => 'SE\d{2}\d{3}\d{16}\d{1}', // Sweden
124         'SI' => 'SI\d{2}\d{5}\d{8}\d{2}', // Slovenia
125         'SK' => 'SK\d{2}\d{4}\d{6}\d{10}', // Slovak Republic
126         'SM' => 'SM\d{2}[A-Z]{1}\d{5}\d{5}[\dA-Z]{12}', // San Marino
127         'SN' => 'SN\d{2}[A-Z]{1}\d{23}', // Senegal
128         'TF' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // French Southern Territories
129         'TL' => 'TL\d{2}\d{3}\d{14}\d{2}', // Timor-Leste
130         'TN' => 'TN59\d{2}\d{3}\d{13}\d{2}', // Tunisia
131         'TR' => 'TR\d{2}\d{5}[\dA-Z]{1}[\dA-Z]{16}', // Turkey
132         'UA' => 'UA\d{2}\d{6}[\dA-Z]{19}', // Ukraine
133         'VG' => 'VG\d{2}[A-Z]{4}\d{16}', // Virgin Islands, British
134         'WF' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // Wallis and Futuna Islands
135         'XK' => 'XK\d{2}\d{4}\d{10}\d{2}', // Republic of Kosovo
136         'YT' => 'FR\d{2}\d{5}\d{5}[\dA-Z]{11}\d{2}', // Mayotte
137     );
138
139     /**
140      * {@inheritdoc}
141      */
142     public function validate($value, Constraint $constraint)
143     {
144         if (!$constraint instanceof Iban) {
145             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Iban');
146         }
147
148         if (null === $value || '' === $value) {
149             return;
150         }
151
152         if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
153             throw new UnexpectedTypeException($value, 'string');
154         }
155
156         $value = (string) $value;
157
158         // Remove spaces and convert to uppercase
159         $canonicalized = str_replace(' ', '', strtoupper($value));
160
161         // The IBAN must contain only digits and characters...
162         if (!ctype_alnum($canonicalized)) {
163             $this->context->buildViolation($constraint->message)
164                 ->setParameter('{{ value }}', $this->formatValue($value))
165                 ->setCode(Iban::INVALID_CHARACTERS_ERROR)
166                 ->addViolation();
167
168             return;
169         }
170
171         // ...start with a two-letter country code
172         $countryCode = substr($canonicalized, 0, 2);
173
174         if (!ctype_alpha($countryCode)) {
175             $this->context->buildViolation($constraint->message)
176                 ->setParameter('{{ value }}', $this->formatValue($value))
177                 ->setCode(Iban::INVALID_COUNTRY_CODE_ERROR)
178                 ->addViolation();
179
180             return;
181         }
182
183         // ...have a format available
184         if (!array_key_exists($countryCode, self::$formats)) {
185             $this->context->buildViolation($constraint->message)
186                 ->setParameter('{{ value }}', $this->formatValue($value))
187                 ->setCode(Iban::NOT_SUPPORTED_COUNTRY_CODE_ERROR)
188                 ->addViolation();
189
190             return;
191         }
192
193         // ...and have a valid format
194         if (!preg_match('/^'.self::$formats[$countryCode].'$/', $canonicalized)
195         ) {
196             $this->context->buildViolation($constraint->message)
197                 ->setParameter('{{ value }}', $this->formatValue($value))
198                 ->setCode(Iban::INVALID_FORMAT_ERROR)
199                 ->addViolation();
200
201             return;
202         }
203
204         // Move the first four characters to the end
205         // e.g. CH93 0076 2011 6238 5295 7
206         //   -> 0076 2011 6238 5295 7 CH93
207         $canonicalized = substr($canonicalized, 4).substr($canonicalized, 0, 4);
208
209         // Convert all remaining letters to their ordinals
210         // The result is an integer, which is too large for PHP's int
211         // data type, so we store it in a string instead.
212         // e.g. 0076 2011 6238 5295 7 CH93
213         //   -> 0076 2011 6238 5295 7 121893
214         $checkSum = self::toBigInt($canonicalized);
215
216         // Do a modulo-97 operation on the large integer
217         // We cannot use PHP's modulo operator, so we calculate the
218         // modulo step-wisely instead
219         if (1 !== self::bigModulo97($checkSum)) {
220             $this->context->buildViolation($constraint->message)
221                 ->setParameter('{{ value }}', $this->formatValue($value))
222                 ->setCode(Iban::CHECKSUM_FAILED_ERROR)
223                 ->addViolation();
224         }
225     }
226
227     private static function toBigInt($string)
228     {
229         $chars = str_split($string);
230         $bigInt = '';
231
232         foreach ($chars as $char) {
233             // Convert uppercase characters to ordinals, starting with 10 for "A"
234             if (ctype_upper($char)) {
235                 $bigInt .= (ord($char) - 55);
236
237                 continue;
238             }
239
240             // Simply append digits
241             $bigInt .= $char;
242         }
243
244         return $bigInt;
245     }
246
247     private static function bigModulo97($bigInt)
248     {
249         $parts = str_split($bigInt, 7);
250         $rest = 0;
251
252         foreach ($parts as $part) {
253             $rest = ($rest.$part) % 97;
254         }
255
256         return $rest;
257     }
258 }