Iban.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. #require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * Validates IBAN Numbers (International Bank Account Numbers)
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Validate_Iban extends Zend_Validate_Abstract
  34. {
  35. const NOTSUPPORTED = 'ibanNotSupported';
  36. const FALSEFORMAT = 'ibanFalseFormat';
  37. const CHECKFAILED = 'ibanCheckFailed';
  38. /**
  39. * Validation failure message template definitions
  40. *
  41. * @var array
  42. */
  43. protected $_messageTemplates = array(
  44. self::NOTSUPPORTED => "Unknown country within the IBAN '%value%'",
  45. self::FALSEFORMAT => "'%value%' has a false IBAN format",
  46. self::CHECKFAILED => "'%value%' has failed the IBAN check",
  47. );
  48. /**
  49. * Optional locale
  50. *
  51. * @var string|Zend_Locale|null
  52. */
  53. protected $_locale;
  54. /**
  55. * IBAN regexes by region
  56. *
  57. * @var array
  58. */
  59. protected $_ibanregex = array(
  60. 'AD' => '/^AD[0-9]{2}[0-9]{8}[A-Z0-9]{12}$/',
  61. 'AE' => '/^AE[0-9]{2}[0-9]{3}[0-9]{16}$/',
  62. 'AL' => '/^AL[0-9]{2}[0-9]{8}[A-Z0-9]{16}$/',
  63. 'AT' => '/^AT[0-9]{2}[0-9]{5}[0-9]{11}$/',
  64. 'AZ' => '/^AZ[0-9]{2}[0-9]{4}[A-Z0-9]{20}$/',
  65. 'BA' => '/^BA[0-9]{2}[0-9]{6}[0-9]{10}$/',
  66. 'BE' => '/^BE[0-9]{2}[0-9]{3}[0-9]{9}$/',
  67. 'BG' => '/^BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}$/',
  68. 'BH' => '/^BH[0-9]{2}[A-Z]{4}[A-Z0-9]{14}$/',
  69. 'BR' => '/^BR[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z]{1}[A-Z0-9]{1}$/',
  70. 'CH' => '/^CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}$/',
  71. 'CR' => '/^CR[0-9]{2}[0-9]{3}[0-9]{14}$/',
  72. 'CS' => '/^CS[0-9]{2}[0-9]{3}[0-9]{15}$/',
  73. 'CY' => '/^CY[0-9]{2}[0-9]{8}[A-Z0-9]{16}$/',
  74. 'CZ' => '/^CZ[0-9]{2}[0-9]{4}[0-9]{16}$/',
  75. 'DE' => '/^DE[0-9]{2}[0-9]{8}[0-9]{10}$/',
  76. 'DK' => '/^DK[0-9]{2}[0-9]{4}[0-9]{10}$/',
  77. 'DO' => '/^DO[0-9]{2}[A-Z0-9]{4}[0-9]{20}$/',
  78. 'EE' => '/^EE[0-9]{2}[0-9]{4}[0-9]{12}$/',
  79. 'ES' => '/^ES[0-9]{2}[0-9]{8}[0-9]{12}$/',
  80. 'FR' => '/^FR[0-9]{2}[0-9]{10}[A-Z0-9]{11}[0-9]{2}$/',
  81. 'FI' => '/^FI[0-9]{2}[0-9]{6}[0-9]{8}$/',
  82. 'FO' => '/^FO[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}$/',
  83. 'GB' => '/^GB[0-9]{2}[A-Z]{4}[0-9]{14}$/',
  84. 'GE' => '/^GE[0-9]{2}[A-Z]{2}[0-9]{16}$/',
  85. 'GI' => '/^GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}$/',
  86. 'GL' => '/^GL[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}$/',
  87. 'GR' => '/^GR[0-9]{2}[0-9]{7}[A-Z0-9]{16}$/',
  88. 'GT' => '/^GT[0-9]{2}[A-Z0-9]{4}[A-Z0-9]{20}$/',
  89. 'HR' => '/^HR[0-9]{2}[0-9]{7}[0-9]{10}$/',
  90. 'HU' => '/^HU[0-9]{2}[0-9]{7}[0-9]{1}[0-9]{15}[0-9]{1}$/',
  91. 'IE' => '/^IE[0-9]{2}[A-Z0-9]{4}[0-9]{6}[0-9]{8}$/',
  92. 'IL' => '/^IL[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{13}$/',
  93. 'IS' => '/^IS[0-9]{2}[0-9]{4}[0-9]{18}$/',
  94. 'IT' => '/^IT[0-9]{2}[A-Z]{1}[0-9]{10}[A-Z0-9]{12}$/',
  95. 'KW' => '/^KW[0-9]{2}[A-Z]{4}[0-9]{3}[0-9]{22}$/',
  96. 'KZ' => '/^KZ[A-Z]{2}[0-9]{2}[0-9]{3}[A-Z0-9]{13}$/',
  97. 'LB' => '/^LB[0-9]{2}[0-9]{4}[A-Z0-9]{20}$/',
  98. 'LI' => '/^LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}$/',
  99. 'LU' => '/^LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}$/',
  100. 'LT' => '/^LT[0-9]{2}[0-9]{5}[0-9]{11}$/',
  101. 'LV' => '/^LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}$/',
  102. 'MC' => '/^MC[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$/',
  103. 'MD' => '/^MD[0-9]{2}[A-Z0-9]{20}$/',
  104. 'ME' => '/^ME[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}$/',
  105. 'MK' => '/^MK[0-9]{2}[0-9]{3}[A-Z0-9]{10}[0-9]{2}$/',
  106. 'MR' => '/^MR13[0-9]{5}[0-9]{5}[0-9]{11}[0-9]{2}$/',
  107. 'MU' => '/^MU[0-9]{2}[A-Z]{4}[0-9]{2}[0-9]{2}[0-9]{12}[0-9]{3}[A-Z]{2}$/',
  108. 'MT' => '/^MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}$/',
  109. 'NL' => '/^NL[0-9]{2}[A-Z]{4}[0-9]{10}$/',
  110. 'NO' => '/^NO[0-9]{2}[0-9]{4}[0-9]{7}$/',
  111. 'PK' => '/^PK[0-9]{2}[A-Z]{4}[0-9]{16}$/',
  112. 'PL' => '/^PL[0-9]{2}[0-9]{8}[0-9]{16}$/',
  113. 'PS' => '/^PS[0-9]{2}[A-Z]{4}[0-9]{21}$/',
  114. 'PT' => '/^PT[0-9]{2}[0-9]{8}[0-9]{13}$/',
  115. 'RO' => '/^RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}$/',
  116. 'RS' => '/^RS[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}$/',
  117. 'SA' => '/^SA[0-9]{2}[0-9]{2}[A-Z0-9]{18}$/',
  118. 'SE' => '/^SE[0-9]{2}[0-9]{3}[0-9]{17}$/',
  119. 'SI' => '/^SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}$/',
  120. 'SK' => '/^SK[0-9]{2}[0-9]{4}[0-9]{16}$/',
  121. 'SM' => '/^SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}$/',
  122. 'TN' => '/^TN[0-9]{2}[0-9]{5}[0-9]{15}$/',
  123. 'TR' => '/^TR[0-9]{2}[0-9]{5}[A-Z0-9]{17}$/',
  124. 'VG' => '/^VG[0-9]{2}[A-Z]{4}[0-9]{16}$/'
  125. );
  126. /**
  127. * Sets validator options
  128. *
  129. * @param string|Zend_Config|Zend_Locale $locale OPTIONAL
  130. */
  131. public function __construct($locale = null)
  132. {
  133. if ($locale instanceof Zend_Config) {
  134. $locale = $locale->toArray();
  135. }
  136. if (is_array($locale)) {
  137. if (array_key_exists('locale', $locale)) {
  138. $locale = $locale['locale'];
  139. } else {
  140. $locale = null;
  141. }
  142. }
  143. if (empty($locale)) {
  144. #require_once 'Zend/Registry.php';
  145. if (Zend_Registry::isRegistered('Zend_Locale')) {
  146. $locale = Zend_Registry::get('Zend_Locale');
  147. }
  148. }
  149. if ($locale !== null) {
  150. $this->setLocale($locale);
  151. }
  152. }
  153. /**
  154. * Returns the locale option
  155. *
  156. * @return string|Zend_Locale|null
  157. */
  158. public function getLocale()
  159. {
  160. return $this->_locale;
  161. }
  162. /**
  163. * Sets the locale option
  164. *
  165. * @param string|Zend_Locale $locale
  166. * @throws Zend_Locale_Exception
  167. * @throws Zend_Validate_Exception
  168. * @return Zend_Validate_Date provides a fluent interface
  169. */
  170. public function setLocale($locale = null)
  171. {
  172. if ($locale !== false) {
  173. #require_once 'Zend/Locale.php';
  174. $locale = Zend_Locale::findLocale($locale);
  175. if (strlen($locale) < 4) {
  176. #require_once 'Zend/Validate/Exception.php';
  177. throw new Zend_Validate_Exception('Region must be given for IBAN validation');
  178. }
  179. }
  180. $this->_locale = $locale;
  181. return $this;
  182. }
  183. /**
  184. * Defined by Zend_Validate_Interface
  185. *
  186. * Returns true if $value is a valid IBAN
  187. *
  188. * @param string $value
  189. * @return boolean
  190. */
  191. public function isValid($value)
  192. {
  193. $value = strtoupper($value);
  194. $this->_setValue($value);
  195. if (empty($this->_locale)) {
  196. $region = substr($value, 0, 2);
  197. } else {
  198. $region = new Zend_Locale($this->_locale);
  199. $region = $region->getRegion();
  200. }
  201. if (!array_key_exists($region, $this->_ibanregex)) {
  202. $this->_setValue($region);
  203. $this->_error(self::NOTSUPPORTED);
  204. return false;
  205. }
  206. if (!preg_match($this->_ibanregex[$region], $value)) {
  207. $this->_error(self::FALSEFORMAT);
  208. return false;
  209. }
  210. $format = substr($value, 4) . substr($value, 0, 4);
  211. $format = str_replace(
  212. array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  213. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'),
  214. array('10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22',
  215. '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'),
  216. $format);
  217. $temp = intval(substr($format, 0, 1));
  218. $len = strlen($format);
  219. for ($x = 1; $x < $len; ++$x) {
  220. $temp *= 10;
  221. $temp += intval(substr($format, $x, 1));
  222. $temp %= 97;
  223. }
  224. if ($temp != 1) {
  225. $this->_error(self::CHECKFAILED);
  226. return false;
  227. }
  228. return true;
  229. }
  230. }