dolibarr 18.0.6
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 = 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}
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.
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=false)
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.