validate.class.php 7.7 KB

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