Drupal investigation

IbanValidator.php 13KB

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