dolibarr 21.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 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 (!is_numeric($stamp) && (int) $stamp == $stamp) {
134 $this->error = $this->outputLang->trans('RequireValidDate');
135 return false;
136 }
137 return true;
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 ".$col." FROM ".$this->db->prefix().$table." WHERE ".$col." = '".$this->db->escape($val)."' LIMIT 1"; // more quick than count(*) to check existing of a row
286 $resql = $this->db->query($sql);
287 if ($resql) {
288 $obj = $this->db->fetch_object($resql);
289 if ($obj) {
290 continue;
291 }
292 }
293 // If something was wrong
294 $this->error = $this->outputLang->trans('RequireValidExistingElement');
295 return false;
296 }
297
298 return true;
299 }
300
310 public function isFetchable($id, $classname, $classpath)
311 {
312 if (!empty($classpath)) {
313 if (dol_include_once($classpath)) {
314 if ($classname && class_exists($classname)) {
316 $object = new $classname($this->db);
317 '@phan-var-force CommonObject|User $object'; // User provided to propose a class with 'fetch'
318
319 if (!is_callable(array($object, 'fetch')) || !is_callable(array($object, 'isExistingObject'))) {
320 $this->error = $this->outputLang->trans('BadSetupOfFieldFetchNotCallable');
321 return false;
322 }
323
324 if (!empty($object->table_element) && $object->isExistingObject($object->table_element, $id)) {
325 return true;
326 } else {
327 $this->error = $this->outputLang->trans('RequireValidExistingElement');
328 }
329 } else {
330 $this->error = $this->outputLang->trans('BadSetupOfFieldClassNotFoundForValidation');
331 }
332 } else {
333 $this->error = $this->outputLang->trans('BadSetupOfFieldFileNotFound');
334 }
335 } else {
336 $this->error = $this->outputLang->trans('BadSetupOfField');
337 }
338 return false;
339 }
340
350 public function isFetchableElement($id, $element_type)
351 {
352 // TODO use newObjectByElement() introduce in V20 by PR #30036 for better errors management
353 $elementProperty = getElementProperties($element_type);
354 return $this->isFetchable($id, $elementProperty['classname'], $elementProperty['classpath'].'/'.$elementProperty['classfile'].'.class.php');
355 }
356}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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.
getElementProperties($elementType)
Get an array with properties of an element.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.