validate.class.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. /**
  18. * \file htdocs/core/class/validate.class.php
  19. * \ingroup core
  20. * \brief File for Utils class
  21. */
  22. /**
  23. * Class toolbox to validate values
  24. */
  25. class Validate
  26. {
  27. /**
  28. * @var DoliDb Database handler (result of a new DoliDB)
  29. */
  30. public $db;
  31. /**
  32. * @var Translate $outputLang
  33. */
  34. public $outputLang;
  35. /**
  36. * @var string Error string
  37. * @see $errors
  38. */
  39. public $error;
  40. /**
  41. * Constructor
  42. *
  43. * @param DoliDB $db Database handler
  44. * @param Translate $outputLang Output lang for error
  45. */
  46. public function __construct($db, $outputLang = null)
  47. {
  48. global $langs;
  49. if (empty($outputLang)) {
  50. $this->outputLang = $langs;
  51. } else {
  52. $this->outputLang = $outputLang;
  53. }
  54. if (!is_object($this->outputLang) || !method_exists($this->outputLang, 'load')) {
  55. return false;
  56. }
  57. $this->outputLang->loadLangs(array('validate', 'errors'));
  58. $this->db = $db;
  59. }
  60. /**
  61. * Use to clear errors msg or other ghost vars
  62. * @return null
  63. */
  64. protected function clear()
  65. {
  66. $this->error = '';
  67. }
  68. /**
  69. * Use to clear errors msg or other ghost vars
  70. *
  71. * @param string $errMsg your error message
  72. * @return null
  73. */
  74. protected function setError($errMsg)
  75. {
  76. $this->error = $errMsg;
  77. }
  78. /**
  79. * Check for e-mail validity
  80. *
  81. * @param string $email e-mail address to validate
  82. * @param int $maxLength string max length
  83. * @return boolean Validity is ok or not
  84. */
  85. public function isEmail($email, $maxLength = false)
  86. {
  87. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  88. $this->error = $this->outputLang->trans('RequireValidEmail');
  89. return false;
  90. }
  91. return true;
  92. }
  93. /**
  94. * Check for price validity
  95. *
  96. * @param string $price Price to validate
  97. * @return boolean Validity is ok or not
  98. */
  99. public function isPrice($price)
  100. {
  101. if (!preg_match('/^[0-9]{1,10}(\.[0-9]{1,9})?$/ui', $price)) {
  102. $this->error = $this->outputLang->trans('RequireValidValue');
  103. return false;
  104. }
  105. return true;
  106. }
  107. /**
  108. * Check for timestamp validity
  109. *
  110. * @param string|int $stamp timestamp to validate
  111. * @return boolean Validity is ok or not
  112. */
  113. public function isTimestamp($stamp)
  114. {
  115. if (!is_numeric($stamp) && (int) $stamp == $stamp) {
  116. $this->error = $this->outputLang->trans('RequireValidDate');
  117. return false;
  118. }
  119. return true;
  120. }
  121. /**
  122. * Check for phone validity
  123. *
  124. * @param string $phone Phone string to validate
  125. * @return boolean Validity is ok or not
  126. */
  127. public function isPhone($phone)
  128. {
  129. if (!preg_match('/^[+0-9. ()-]*$/ui', $phone)) {
  130. $this->error = $this->outputLang->trans('RequireValidPhone');
  131. return false;
  132. }
  133. return true;
  134. }
  135. /**
  136. * Check for string max length validity
  137. *
  138. * @param string $string to validate
  139. * @param int $length max length
  140. * @return boolean Validity is ok or not
  141. */
  142. public function isMaxLength($string, $length)
  143. {
  144. if (strlen($string) > $length) {
  145. $this->error = $this->outputLang->trans('RequireMaxLength', $length);
  146. return false;
  147. }
  148. return true;
  149. }
  150. /**
  151. * Check for string not empty
  152. *
  153. * @param string $string to validate
  154. * @return boolean Validity is ok or not
  155. */
  156. public function isNotEmptyString($string)
  157. {
  158. if (!strlen($string)) {
  159. $this->error = $this->outputLang->trans('RequireANotEmptyValue');
  160. return false;
  161. }
  162. return true;
  163. }
  164. /**
  165. * Check for string min length validity
  166. *
  167. * @param string $string to validate
  168. * @param int $length max length
  169. * @return boolean Validity is ok or not
  170. */
  171. public function isMinLength($string, $length)
  172. {
  173. if (strlen($string) < $length) {
  174. $this->error = $this->outputLang->trans('RequireMinLength', $length);
  175. return false;
  176. }
  177. return true;
  178. }
  179. /**
  180. * Check url validity
  181. *
  182. * @param string $url to validate
  183. * @return boolean Validity is ok or not
  184. */
  185. public function isUrl($url)
  186. {
  187. if (!filter_var($url, FILTER_VALIDATE_URL)) {
  188. $this->error = $this->outputLang->trans('RequireValidUrl');
  189. return false;
  190. }
  191. return true;
  192. }
  193. /**
  194. * Check Duration validity
  195. *
  196. * @param mixed $duration to validate
  197. * @return boolean Validity is ok or not
  198. */
  199. public function isDuration($duration)
  200. {
  201. if (!is_int($duration) && $duration >= 0) {
  202. $this->error = $this->outputLang->trans('RequireValidDuration');
  203. return false;
  204. }
  205. return true;
  206. }
  207. /**
  208. * Check numeric validity
  209. *
  210. * @param mixed $string to validate
  211. * @return boolean Validity is ok or not
  212. */
  213. public function isNumeric($string)
  214. {
  215. if (!is_numeric($string)) {
  216. $this->error = $this->outputLang->trans('RequireValidNumeric');
  217. return false;
  218. }
  219. return true;
  220. }
  221. /**
  222. * Check for boolean validity
  223. *
  224. * @param boolean $bool Boolean to validate
  225. * @return boolean Validity is ok or not
  226. */
  227. public function isBool($bool)
  228. {
  229. if (!(is_null($bool) || is_bool($bool) || preg_match('/^[0|1]{1}$/ui', $bool))) {
  230. $this->error = $this->outputLang->trans('RequireValidBool');
  231. return false;
  232. }
  233. return true;
  234. }
  235. /**
  236. * Check for all values in db
  237. *
  238. * @param array $values Boolean to validate
  239. * @param string $table the db table name without $this->db->prefix()
  240. * @param string $col the target col
  241. * @return boolean Validity is ok or not
  242. * @throws Exception
  243. */
  244. public function isInDb($values, $table, $col)
  245. {
  246. if (!is_array($values)) {
  247. $value_arr = array($values);
  248. } else {
  249. $value_arr = $values;
  250. }
  251. if (!count($value_arr)) {
  252. $this->error = $this->outputLang->trans('RequireValue');
  253. return false;
  254. }
  255. foreach ($value_arr as $val) {
  256. $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
  257. $resql = $this->db->query($sql);
  258. if ($resql) {
  259. $obj = $this->db->fetch_object($resql);
  260. if ($obj) {
  261. continue;
  262. }
  263. }
  264. // If something was wrong
  265. $this->error = $this->outputLang->trans('RequireValidExistingElement');
  266. return false;
  267. }
  268. return true;
  269. }
  270. /**
  271. * Check for all values in db
  272. *
  273. * @param integer $id of element
  274. * @param string $classname the class name
  275. * @param string $classpath the class path
  276. * @return boolean Validity is ok or not
  277. * @throws Exception
  278. */
  279. public function isFetchable($id, $classname, $classpath)
  280. {
  281. if (!empty($classpath)) {
  282. if (dol_include_once($classpath)) {
  283. if ($classname && class_exists($classname)) {
  284. /** @var CommonObject $object */
  285. $object = new $classname($this->db);
  286. if (!is_callable(array($object, 'fetch')) || !is_callable(array($object, 'isExistingObject'))) {
  287. $this->error = $this->outputLang->trans('BadSetupOfFieldFetchNotCallable');
  288. return false;
  289. }
  290. if (!empty($object->table_element) && $object->isExistingObject($object->table_element, $id)) {
  291. return true;
  292. } else { $this->error = $this->outputLang->trans('RequireValidExistingElement'); }
  293. } else { $this->error = $this->outputLang->trans('BadSetupOfFieldClassNotFoundForValidation'); }
  294. } else { $this->error = $this->outputLang->trans('BadSetupOfFieldFileNotFound'); }
  295. } else { $this->error = $this->outputLang->trans('BadSetupOfField'); }
  296. return false;
  297. }
  298. }