dolibarr  20.0.0-alpha
profid.lib.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2022 Frédéric France <frederic.france@netlogic.fr>
4  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  * or see https://www.gnu.org/
19  */
20 
33 function isValidLuhn($str)
34 {
35  $str = (string) $str;
36  $len = dol_strlen($str);
37  $parity = $len % 2;
38  $sum = 0;
39  for ($i = $len - 1; $i >= 0; $i--) {
40  $d = (int) $str[$i];
41  if ($i % 2 == $parity) {
42  if (($d *= 2) > 9) {
43  $d -= 9;
44  }
45  }
46  $sum += $d;
47  }
48  return $sum % 10 == 0;
49 }
50 
51 
59 function isValidSiren($siren)
60 {
61  $siren = trim($siren);
62  $siren = preg_replace('/(\s)/', '', $siren);
63 
64  if (!is_numeric($siren) || dol_strlen($siren) != 9) {
65  return false;
66  }
67 
68  return isValidLuhn($siren);
69 }
70 
71 
79 function isValidSiret($siret)
80 {
81  $siret = trim($siret);
82  $siret = preg_replace('/(\s)/', '', $siret);
83 
84  if (!is_numeric($siret) || dol_strlen($siret) != 14) {
85  return false;
86  }
87 
88  if (isValidLuhn($siret)) {
89  return true;
90  } elseif ((substr($siret, 0, 9) == "356000000") && (array_sum(str_split($siret)) % 5 == 0)) {
96  return true;
97  } else {
98  return false;
99  }
100 }
101 
102 
111 function isValidTinForPT($str)
112 {
113  $str = trim($str);
114  $str = preg_replace('/(\s)/', '', $str);
115 
116  if (preg_match('/(^[0-9]{9}$)/', $str)) {
117  return true;
118  } else {
119  return false;
120  }
121 }
122 
123 
132 function isValidTinForDZ($str)
133 {
134  $str = trim($str);
135  $str = preg_replace('/(\s)/', '', $str);
136 
137  if (preg_match('/(^[0-9]{15}$)/', $str)) {
138  return true;
139  } else {
140  return false;
141  }
142 }
143 
144 
153 function isValidTinForBE($str)
154 {
155  // https://economie.fgov.be/fr/themes/entreprises/banque-carrefour-des/actualites/structure-du-numero
156  $str = trim($str);
157  $str = preg_replace('/(\s)/', '', $str);
158 
159  if (preg_match('/(^[0-1]{1}[0-9]{3}\.[0-9]{3}\.[0-9]{3}$)/', $str)) {
160  return true;
161  } else {
162  return false;
163  }
164 }
165 
166 
177 function isValidTinForES($str)
178 {
179  $str = trim($str);
180  $str = preg_replace('/(\s)/', '', $str);
181  $str = strtoupper($str);
182 
183  //Check format
184  if (!preg_match('/((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)/', $str)) {
185  return 0;
186  }
187 
188  $num = array();
189  for ($i = 0; $i < 9; $i++) {
190  $num[$i] = substr($str, $i, 1);
191  }
192 
193  //Check NIF
194  if (preg_match('/(^[0-9]{8}[A-Z]{1}$)/', $str)) {
195  if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', (int) substr($str, 0, 8) % 23, 1)) {
196  return 1;
197  } else {
198  return -1;
199  }
200  }
201 
202  //algorithm checking type code CIF
203  $sum = (int) $num[2] + (int) $num[4] + (int) $num[6];
204  for ($i = 1; $i < 8; $i += 2) {
205  $sum += intval(substr((string) (2 * (int) $num[$i]), 0, 1)) + intval(substr((string) (2 * (int) $num[$i]), 1, 1));
206  }
207  $n = 10 - (int) substr((string) $sum, strlen((string) $sum) - 1, 1);
208 
209  //Check special NIF
210  if (preg_match('/^[KLM]{1}/', $str)) {
211  if ($num[8] == chr(64 + $n) || $num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', (int) substr($str, 1, 8) % 23, 1)) {
212  return 1;
213  } else {
214  return -1;
215  }
216  }
217 
218  //Check CIF
219  if (preg_match('/^[ABCDEFGHJNPQRSUVW]{1}/', $str)) {
220  if ($num[8] == chr(64 + $n) || $num[8] == substr((string) $n, strlen((string) $n) - 1, 1)) {
221  return 2;
222  } else {
223  return -2;
224  }
225  }
226 
227  //Check NIE T
228  if (preg_match('/^[T]{1}/', $str)) {
229  if ($num[8] == preg_match('/^[T]{1}[A-Z0-9]{8}$/', $str)) {
230  return 3;
231  } else {
232  return -3;
233  }
234  }
235 
236  //Check NIE XYZ
237  if (preg_match('/^[XYZ]{1}/', $str)) {
238  if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', (int) substr(str_replace(array('X', 'Y', 'Z'), array('0', '1', '2'), $str), 0, 8) % 23, 1)) {
239  return 3;
240  } else {
241  return -3;
242  }
243  }
244 
245  //Can not be verified
246  return -4;
247 }
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
isValidSiren($siren)
Check the syntax validity of a SIREN.
Definition: profid.lib.php:59
isValidLuhn($str)
Check if a string passes the Luhn algorithm test.
Definition: profid.lib.php:33
isValidTinForPT($str)
Check the syntax validity of a Portuguese (PT) Tax Identification Number (TIN).
Definition: profid.lib.php:111
isValidSiret($siret)
Check the syntax validity of a SIRET.
Definition: profid.lib.php:79
isValidTinForES($str)
Check the syntax validity of a Spanish (ES) Tax Identification Number (TIN), where:
Definition: profid.lib.php:177
isValidTinForBE($str)
Check the syntax validity of a Belgium (BE) Tax Identification Number (TIN).
Definition: profid.lib.php:153
isValidTinForDZ($str)
Check the syntax validity of an Algerian (DZ) Tax Identification Number (TIN).
Definition: profid.lib.php:132