dolibarr  17.0.3
validate.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2021 John BOTELLA <john.botella@atm-consulting.fr>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
28 class Validate
29 {
30 
34  public $db;
35 
39  public $outputLang;
40 
45  public $error;
46 
47 
54  public function __construct($db, $outputLang = null)
55  {
56  global $langs;
57 
58  if (empty($outputLang)) {
59  $this->outputLang = $langs;
60  } else {
61  $this->outputLang = $outputLang;
62  }
63 
64  if (!is_object($this->outputLang) || !method_exists($this->outputLang, 'load')) {
65  return false;
66  }
67 
68  $this->outputLang->loadLangs(array('validate', 'errors'));
69 
70  $this->db = $db;
71  }
72 
77  protected function clear()
78  {
79  $this->error = '';
80  }
81 
88  protected function setError($errMsg)
89  {
90  $this->error = $errMsg;
91  }
92 
100  public function isEmail($email, $maxLength = false)
101  {
102  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
103  $this->error = $this->outputLang->trans('RequireValidEmail');
104  return false;
105  }
106  return true;
107  }
108 
115  public function isPrice($price)
116  {
117  if (!preg_match('/^[0-9]{1,10}(\.[0-9]{1,9})?$/ui', $price)) {
118  $this->error = $this->outputLang->trans('RequireValidValue');
119  return false;
120  }
121  return true;
122  }
123 
130  public function isTimestamp($stamp)
131  {
132  if (!is_numeric($stamp) && (int) $stamp == $stamp) {
133  $this->error = $this->outputLang->trans('RequireValidDate');
134  return false;
135  }
136  return true;
137  }
138 
145  public function isPhone($phone)
146  {
147  if (!preg_match('/^[+0-9. ()-]*$/ui', $phone)) {
148  $this->error = $this->outputLang->trans('RequireValidPhone');
149  return false;
150  }
151  return true;
152  }
153 
161  public function isMaxLength($string, $length)
162  {
163  if (strlen($string) > $length) {
164  $this->error = $this->outputLang->trans('RequireMaxLength', $length);
165  return false;
166  }
167  return true;
168  }
169 
176  public function isNotEmptyString($string)
177  {
178  if (!strlen($string)) {
179  $this->error = $this->outputLang->trans('RequireANotEmptyValue');
180  return false;
181  }
182  return true;
183  }
184 
192  public function isMinLength($string, $length)
193  {
194  if (strlen($string) < $length) {
195  $this->error = $this->outputLang->trans('RequireMinLength', $length);
196  return false;
197  }
198  return true;
199  }
200 
207  public function isUrl($url)
208  {
209  if (!filter_var($url, FILTER_VALIDATE_URL)) {
210  $this->error = $this->outputLang->trans('RequireValidUrl');
211  return false;
212  }
213  return true;
214  }
215 
222  public function isDuration($duration)
223  {
224  if (!is_int($duration) && $duration >= 0) {
225  $this->error = $this->outputLang->trans('RequireValidDuration');
226  return false;
227  }
228  return true;
229  }
230 
237  public function isNumeric($string)
238  {
239  if (!is_numeric($string)) {
240  $this->error = $this->outputLang->trans('RequireValidNumeric');
241  return false;
242  }
243  return true;
244  }
245 
252  public function isBool($bool)
253  {
254  if (!(is_null($bool) || is_bool($bool) || preg_match('/^[0|1]{1}$/ui', $bool))) {
255  $this->error = $this->outputLang->trans('RequireValidBool');
256  return false;
257  }
258  return true;
259  }
260 
270  public function isInDb($values, $table, $col)
271  {
272  if (!is_array($values)) {
273  $value_arr = array($values);
274  } else {
275  $value_arr = $values;
276  }
277 
278  if (!count($value_arr)) {
279  $this->error = $this->outputLang->trans('RequireValue');
280  return false;
281  }
282 
283  foreach ($value_arr as $val) {
284  $sql = "SELECT ".$col." FROM ".$this->db->prefix().$table." WHERE ".$col." = '".$this->db->escape($val)."' LIMIT 1"; // more quick than count(*) to check existing of a row
285  $resql = $this->db->query($sql);
286  if ($resql) {
287  $obj = $this->db->fetch_object($resql);
288  if ($obj) {
289  continue;
290  }
291  }
292  // If something was wrong
293  $this->error = $this->outputLang->trans('RequireValidExistingElement');
294  return false;
295  }
296 
297  return true;
298  }
299 
309  public function isFetchable($id, $classname, $classpath)
310  {
311  if (!empty($classpath)) {
312  if (dol_include_once($classpath)) {
313  if ($classname && class_exists($classname)) {
315  $object = new $classname($this->db);
316 
317  if (!is_callable(array($object, 'fetch')) || !is_callable(array($object, 'isExistingObject'))) {
318  $this->error = $this->outputLang->trans('BadSetupOfFieldFetchNotCallable');
319  return false;
320  }
321 
322  if (!empty($object->table_element) && $object->isExistingObject($object->table_element, $id)) {
323  return true;
324  } else { $this->error = $this->outputLang->trans('RequireValidExistingElement'); }
325  } else { $this->error = $this->outputLang->trans('BadSetupOfFieldClassNotFoundForValidation'); }
326  } else { $this->error = $this->outputLang->trans('BadSetupOfFieldFileNotFound'); }
327  } else { $this->error = $this->outputLang->trans('BadSetupOfField'); }
328  return false;
329  }
330 }
Validate\isPrice
isPrice($price)
Check for price validity.
Definition: validate.class.php:115
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1032
Validate\isMaxLength
isMaxLength($string, $length)
Check for string max length validity.
Definition: validate.class.php:161
Validate\clear
clear()
Use to clear errors msg or other ghost vars.
Definition: validate.class.php:77
Validate\isTimestamp
isTimestamp($stamp)
Check for timestamp validity.
Definition: validate.class.php:130
Validate\isNumeric
isNumeric($string)
Check numeric validity.
Definition: validate.class.php:237
Validate\isMinLength
isMinLength($string, $length)
Check for string min length validity.
Definition: validate.class.php:192
Validate\isBool
isBool($bool)
Check for boolean validity.
Definition: validate.class.php:252
Validate\isInDb
isInDb($values, $table, $col)
Check for all values in db.
Definition: validate.class.php:270
Validate
Class toolbox to validate values.
Definition: validate.class.php:28
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
Validate\isUrl
isUrl($url)
Check url validity.
Definition: validate.class.php:207
Validate\__construct
__construct($db, $outputLang=null)
Constructor.
Definition: validate.class.php:54
Validate\isEmail
isEmail($email, $maxLength=false)
Check for e-mail validity.
Definition: validate.class.php:100
Validate\isDuration
isDuration($duration)
Check Duration validity.
Definition: validate.class.php:222
Validate\setError
setError($errMsg)
Use to clear errors msg or other ghost vars.
Definition: validate.class.php:88
Validate\isPhone
isPhone($phone)
Check for phone validity.
Definition: validate.class.php:145
Validate\isNotEmptyString
isNotEmptyString($string)
Check for string not empty.
Definition: validate.class.php:176