dolibarr 20.0.0
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
29{
33 public $db;
34
38 public $outputLang;
39
44 public $error;
45
46
53 public function __construct($db, $outputLang = null)
54 {
55 global $langs;
56
57 if (empty($outputLang)) {
58 $this->outputLang = $langs;
59 } else {
60 $this->outputLang = $outputLang;
61 }
62
63 if (!is_object($this->outputLang) || !method_exists($this->outputLang, 'load')) {
64 return;
65 }
66
67 $this->outputLang->loadLangs(array('validate', 'errors'));
68
69 $this->db = $db;
70 }
71
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 = 0)
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 {
325 $this->error = $this->outputLang->trans('RequireValidExistingElement');
326 }
327 } else {
328 $this->error = $this->outputLang->trans('BadSetupOfFieldClassNotFoundForValidation');
329 }
330 } else {
331 $this->error = $this->outputLang->trans('BadSetupOfFieldFileNotFound');
332 }
333 } else {
334 $this->error = $this->outputLang->trans('BadSetupOfField');
335 }
336 return false;
337 }
338
348 public function isFetchableElement($id, $element_type)
349 {
350 // TODO use newObjectByElement() introduce in V20 by PR #30036 for better errors management
351 $elementProperty = getElementProperties($element_type);
352 return $this->isFetchable($id, $elementProperty['classname'], $elementProperty['classpath'].'/'.$elementProperty['classfile'].'.class.php');
353 }
354}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.