dolibarr 24.0.0-beta
validate.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2021 John BOTELLA <john.botella@atm-consulting.fr>
3 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
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;
66 }
67
68 $this->outputLang->loadLangs(array('validate', 'errors'));
69
70 $this->db = $db;
71 }
72
78 protected function clear()
79 {
80 $this->error = '';
81 }
82
89 protected function setError($errMsg)
90 {
91 $this->error = $errMsg;
92 }
93
101 public function isEmail($email, $maxLength = 0)
102 {
103 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
104 $this->error = $this->outputLang->trans('RequireValidEmail');
105 return false;
106 }
107 return true;
108 }
109
116 public function isPrice($price)
117 {
118 if (!preg_match('/^[0-9]{1,10}(\.[0-9]{1,9})?$/ui', $price)) {
119 $this->error = $this->outputLang->trans('RequireValidValue');
120 return false;
121 }
122 return true;
123 }
124
131 public function isTimestamp($stamp)
132 {
133 if (preg_match('/^\d{9,}$/', (string) $stamp)) {
134 return true;
135 }
136 $this->error = $this->outputLang->trans('RequireValidDate');
137 return false;
138 }
139
146 public function isPhone($phone)
147 {
148 if (!preg_match('/^[+0-9. ()-]*$/ui', $phone)) {
149 $this->error = $this->outputLang->trans('RequireValidPhone');
150 return false;
151 }
152 return true;
153 }
154
162 public function isMaxLength($string, $length)
163 {
164 if (strlen($string) > $length) {
165 $this->error = $this->outputLang->trans('RequireMaxLength', $length);
166 return false;
167 }
168 return true;
169 }
170
177 public function isNotEmptyString($string)
178 {
179 if (!strlen($string)) {
180 $this->error = $this->outputLang->trans('RequireANotEmptyValue');
181 return false;
182 }
183 return true;
184 }
185
193 public function isMinLength($string, $length)
194 {
195 if (strlen($string) < $length) {
196 $this->error = $this->outputLang->trans('RequireMinLength', $length);
197 return false;
198 }
199 return true;
200 }
201
208 public function isUrl($url)
209 {
210 if (!filter_var($url, FILTER_VALIDATE_URL)) {
211 $this->error = $this->outputLang->trans('RequireValidUrl');
212 return false;
213 }
214 return true;
215 }
216
223 public function isDuration($duration)
224 {
225 if (!is_int($duration) && $duration >= 0) {
226 $this->error = $this->outputLang->trans('RequireValidDuration');
227 return false;
228 }
229 return true;
230 }
231
238 public function isNumeric($string)
239 {
240 if (!is_numeric($string)) {
241 $this->error = $this->outputLang->trans('RequireValidNumeric');
242 return false;
243 }
244 return true;
245 }
246
253 public function isBool($bool)
254 {
255 if (!(is_null($bool) || is_bool($bool) || preg_match('/^[0|1]{1}$/ui', $bool))) {
256 $this->error = $this->outputLang->trans('RequireValidBool');
257 return false;
258 }
259 return true;
260 }
261
271 public function isInDb($values, $table, $col)
272 {
273 if (!is_array($values)) {
274 $value_arr = array($values);
275 } else {
276 $value_arr = $values;
277 }
278
279 if (!count($value_arr)) {
280 $this->error = $this->outputLang->trans('RequireValue');
281 return false;
282 }
283
284 foreach ($value_arr as $val) {
285 $sql = "SELECT ".$this->db->sanitize($col)." FROM ".$this->db->prefix().$this->db->sanitize($table);
286 $sql .= " WHERE ".$this->db->sanitize($col)." = '".$this->db->escape($val)."' LIMIT 1"; // more quick than count(*) to check existing of a row
287 $resql = $this->db->query($sql);
288 if ($resql) {
289 $obj = $this->db->fetch_object($resql);
290 if ($obj) {
291 continue;
292 }
293 }
294 // If something was wrong
295 $this->error = $this->outputLang->trans('RequireValidExistingElement');
296 return false;
297 }
298
299 return true;
300 }
301
311 public function isFetchable($id, $classname, $classpath)
312 {
313 if (!empty($classpath)) {
314 if (dol_include_once($classpath)) {
315 if ($classname && class_exists($classname)) {
317 $object = new $classname($this->db);
318 '@phan-var-force CommonObject|User $object'; // User provided to propose a class with 'fetch'
319
320 if (!is_callable(array($object, 'fetch')) || !is_callable(array($object, 'isExistingObject'))) {
321 $this->error = $this->outputLang->trans('BadSetupOfFieldFetchNotCallable');
322 return false;
323 }
324
325 if (!empty($object->table_element) && $object->isExistingObject($object->table_element, $id)) {
326 return true;
327 } else {
328 $this->error = $this->outputLang->trans('RequireValidExistingElement');
329 }
330 } else {
331 $this->error = $this->outputLang->trans('BadSetupOfFieldClassNotFoundForValidation');
332 }
333 } else {
334 $this->error = $this->outputLang->trans('BadSetupOfFieldFileNotFound');
335 }
336 } else {
337 $this->error = $this->outputLang->trans('BadSetupOfField');
338 }
339 return false;
340 }
341
351 public function isFetchableElement($id, $element_type)
352 {
353 // TODO use newObjectByElement() introduce in V20 by PR #30036 for better errors management
354 $elementProperty = getElementProperties($element_type);
355 return $this->isFetchable($id, $elementProperty['classname'], $elementProperty['classpath'].'/'.$elementProperty['classfile'].'.class.php');
356 }
357}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class toolbox to validate values.
isMinLength($string, $length)
Check for string min length validity.
isNotEmptyString($string)
Check for string not empty.
isNumeric($string)
Check numeric validity.
isDuration($duration)
Check Duration validity.
isInDb($values, $table, $col)
Check for all values in db.
isMaxLength($string, $length)
Check for string max length validity.
__construct($db, $outputLang=null)
Constructor.
clear()
Use to clear errors msg or other ghost vars.
isFetchableElement($id, $element_type)
Check for all values in db for an element.
setError($errMsg)
Use to clear errors msg or other ghost vars.
isPrice($price)
Check for price validity.
isBool($bool)
Check for boolean validity.
isTimestamp($stamp)
Check for timestamp validity.
isEmail($email, $maxLength=0)
Check for e-mail validity.
isPhone($phone)
Check for phone validity.
isUrl($url)
Check url validity.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.