BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validate.class.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /*-- TODO ---------------------------//
  3. Writeup how to use the Validate class, add in support for form id checks
  4. Complete the number and date validation
  5. //-----------------------------------*/
  6. class Validate
  7. {
  8. public $Fields = [];
  9. public function SetFields($FieldName, $Required, $FieldType, $ErrorMessage, $Options = [])
  10. {
  11. $this->Fields[$FieldName]['Type'] = strtolower($FieldType);
  12. $this->Fields[$FieldName]['Required'] = $Required;
  13. $this->Fields[$FieldName]['ErrorMessage'] = $ErrorMessage;
  14. if (!empty($Options['maxlength'])) {
  15. $this->Fields[$FieldName]['MaxLength'] = $Options['maxlength'];
  16. }
  17. if (!empty($Options['minlength'])) {
  18. $this->Fields[$FieldName]['MinLength'] = $Options['minlength'];
  19. }
  20. if (!empty($Options['comparefield'])) {
  21. $this->Fields[$FieldName]['CompareField'] = $Options['comparefield'];
  22. }
  23. if (!empty($Options['allowperiod'])) {
  24. $this->Fields[$FieldName]['AllowPeriod'] = $Options['allowperiod'];
  25. }
  26. if (!empty($Options['allowcomma'])) {
  27. $this->Fields[$FieldName]['AllowComma'] = $Options['allowcomma'];
  28. }
  29. if (!empty($Options['inarray'])) {
  30. $this->Fields[$FieldName]['InArray'] = $Options['inarray'];
  31. }
  32. if (!empty($Options['regex'])) {
  33. $this->Fields[$FieldName]['Regex'] = $Options['regex'];
  34. }
  35. }
  36. public function ValidateForm($ValidateArray)
  37. {
  38. reset($this->Fields);
  39. foreach ($this->Fields as $FieldKey => $Field) {
  40. $ValidateVar = $ValidateArray[$FieldKey];
  41. if ($ValidateVar !== '' || !empty($Field['Required']) || $Field['Type'] === 'date') {
  42. if ($Field['Type'] === 'string') {
  43. if (isset($Field['MaxLength'])) {
  44. $MaxLength = $Field['MaxLength'];
  45. } else {
  46. $MaxLength = 255;
  47. }
  48. if (isset($Field['MinLength'])) {
  49. $MinLength = $Field['MinLength'];
  50. } else {
  51. $MinLength = 1;
  52. }
  53. if (strlen($ValidateVar) > $MaxLength) {
  54. return $Field['ErrorMessage'];
  55. } elseif (strlen($ValidateVar) < $MinLength) {
  56. return $Field['ErrorMessage'];
  57. }
  58. } elseif ($Field['Type'] === 'number') {
  59. if (isset($Field['MaxLength'])) {
  60. $MaxLength = $Field['MaxLength'];
  61. } else {
  62. $MaxLength = '';
  63. }
  64. if (isset($Field['MinLength'])) {
  65. $MinLength = $Field['MinLength'];
  66. } else {
  67. $MinLength = 0;
  68. }
  69. $Match = '0-9';
  70. if (isset($Field['AllowPeriod'])) {
  71. $Match .= '.';
  72. }
  73. if (isset($Field['AllowComma'])) {
  74. $Match .= ',';
  75. }
  76. if (preg_match('/[^'.$Match.']/', $ValidateVar) || strlen($ValidateVar) < 1) {
  77. return $Field['ErrorMessage'];
  78. } elseif ($MaxLength !== '' && $ValidateVar > $MaxLength) {
  79. return $Field['ErrorMessage'].'!!';
  80. } elseif ($ValidateVar < $MinLength) {
  81. return $Field['ErrorMessage']."$MinLength";
  82. }
  83. } elseif ($Field['Type'] === 'email') {
  84. if (isset($Field['MaxLength'])) {
  85. $MaxLength = $Field['MaxLength'];
  86. } else {
  87. $MaxLength = 255;
  88. }
  89. if (isset($Field['MinLength'])) {
  90. $MinLength = $Field['MinLength'];
  91. } else {
  92. $MinLength = 6;
  93. }
  94. if (!preg_match("/^".EMAIL_REGEX."$/i", $ValidateVar)) {
  95. return $Field['ErrorMessage'];
  96. } elseif (strlen($ValidateVar) > $MaxLength) {
  97. return $Field['ErrorMessage'];
  98. } elseif (strlen($ValidateVar) < $MinLength) {
  99. return $Field['ErrorMessage'];
  100. }
  101. } elseif ($Field['Type'] === 'link') {
  102. if (isset($Field['MaxLength'])) {
  103. $MaxLength = $Field['MaxLength'];
  104. } else {
  105. $MaxLength = 255;
  106. }
  107. if (isset($Field['MinLength'])) {
  108. $MinLength = $Field['MinLength'];
  109. } else {
  110. $MinLength = 10;
  111. }
  112. if (!preg_match('/^'.URL_REGEX.'$/i', $ValidateVar)) {
  113. return $Field['ErrorMessage'];
  114. } elseif (strlen($ValidateVar) > $MaxLength) {
  115. return $Field['ErrorMessage'];
  116. } elseif (strlen($ValidateVar) < $MinLength) {
  117. return $Field['ErrorMessage'];
  118. }
  119. } elseif ($Field['Type'] === 'username') {
  120. if (isset($Field['MaxLength'])) {
  121. $MaxLength = $Field['MaxLength'];
  122. } else {
  123. $MaxLength = 20;
  124. }
  125. if (isset($Field['MinLength'])) {
  126. $MinLength = $Field['MinLength'];
  127. } else {
  128. $MinLength = 1;
  129. }
  130. if (!preg_match(USERNAME_REGEX, $ValidateVar)) {
  131. return $Field['ErrorMessage'];
  132. } elseif (strlen($ValidateVar) > $MaxLength) {
  133. return $Field['ErrorMessage'];
  134. } elseif (strlen($ValidateVar) < $MinLength) {
  135. return $Field['ErrorMessage'];
  136. }
  137. } elseif ($Field['Type'] === 'checkbox') {
  138. if (!isset($ValidateArray[$FieldKey])) {
  139. return $Field['ErrorMessage'];
  140. }
  141. } elseif ($Field['Type'] === 'compare') {
  142. if ($ValidateArray[$Field['CompareField']] !== $ValidateVar) {
  143. return $Field['ErrorMessage'];
  144. }
  145. } elseif ($Field['Type'] === 'inarray') {
  146. if (array_search($ValidateVar, $Field['InArray']) === false) {
  147. return $Field['ErrorMessage'];
  148. }
  149. } elseif ($Field['Type'] === 'regex') {
  150. if (!preg_match($Field['Regex'], $ValidateVar)) {
  151. return $Field['ErrorMessage'];
  152. }
  153. }
  154. }
  155. } // while
  156. } // function
  157. public function GenerateJS($FormID)
  158. {
  159. /*
  160. $ReturnJS = "<script type=\"text/javascript\" language=\"javascript\">\r\n";
  161. $ReturnJS .= "function formVal() {\r\n";
  162. $ReturnJS .= " clearErrors('$FormID');\r\n";
  163. reset($this->Fields);
  164. foreach ($this->Fields as $FieldKey => $Field) {
  165. if ($Field['Type'] === 'string') {
  166. $ValItem = ' if ($(\'#'.$FieldKey.'\').raw().value === ""';
  167. if (!empty($Field['MaxLength'])) {
  168. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > '.$Field['MaxLength'];
  169. } else {
  170. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > 255';
  171. }
  172. if (!empty($Field['MinLength'])) {
  173. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < '.$Field['MinLength'];
  174. }
  175. $ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  176. } elseif ($Field['Type'] === 'number') {
  177. $Match = '0-9';
  178. if (!empty($Field['AllowPeriod'])) {
  179. $Match .= '.';
  180. }
  181. if (!empty($Field['AllowComma'])) {
  182. $Match .= ',';
  183. }
  184. $ValItem = ' if ($(\'#'.$FieldKey.'\').raw().value.match(/[^'.$Match.']/) || $(\'#'.$FieldKey.'\').raw().value.length < 1';
  185. if (!empty($Field['MaxLength'])) {
  186. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value/1 > '.$Field['MaxLength'];
  187. }
  188. if (!empty($Field['MinLength'])) {
  189. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value/1 < '.$Field['MinLength'];
  190. }
  191. $ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  192. } elseif ($Field['Type'] === 'email') {
  193. $ValItem = ' if (!validEmail($(\'#'.$FieldKey.'\').raw().value)';
  194. if (!empty($Field['MaxLength'])) {
  195. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > '.$Field['MaxLength'];
  196. } else {
  197. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > 255';
  198. }
  199. if (!empty($Field['MinLength'])) {
  200. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < '.$Field['MinLength'];
  201. } else {
  202. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < 6';
  203. }
  204. $ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  205. } elseif ($Field['Type'] === 'link') {
  206. $ValItem = ' if (!validLink($(\'#'.$FieldKey.'\').raw().value)';
  207. if (!empty($Field['MaxLength'])) {
  208. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > '.$Field['MaxLength'];
  209. } else {
  210. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > 255';
  211. }
  212. if (!empty($Field['MinLength'])) {
  213. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < '.$Field['MinLength'];
  214. } else {
  215. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < 10';
  216. }
  217. $ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  218. } elseif ($Field['Type'] === 'username') {
  219. $ValItem = ' if ($(\'#'.$FieldKey.'\').raw().value.match(/[^a-zA-Z0-9_\-]/)';
  220. if (!empty($Field['MaxLength'])) {
  221. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > '.$Field['MaxLength'];
  222. }
  223. if (!empty($Field['MinLength'])) {
  224. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < '.$Field['MinLength'];
  225. }
  226. $ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  227. } elseif ($Field['Type'] === 'regex') {
  228. $ValItem = ' if (!$(\'#'.$FieldKey.'\').raw().value.match('.$Field['Regex'].')) { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  229. } elseif ($Field['Type'] === 'date') {
  230. $DisplayError = $FieldKey.'month';
  231. if (isset($Field['MinLength']) && $Field['MinLength'] === 3) {
  232. $Day = '$(\'#'.$FieldKey.'day\').raw().value';
  233. $DisplayError .= ",{$FieldKey}day";
  234. } else {
  235. $Day = '1';
  236. }
  237. $DisplayError .= ",{$FieldKey}year";
  238. $ValItemHold = ' if (!validDate($(\'#'.$FieldKey.'month\').raw().value+\'/\'+'.$Day.'+\'/\'+$(\'#'.$FieldKey.'year\').raw().value)) { return showError(\''.$DisplayError.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  239. if (empty($Field['Required'])) {
  240. $ValItem = ' if ($(\'#'.$FieldKey.'month\').raw().value !== ""';
  241. if (isset($Field['MinLength']) && $Field['MinLength'] === 3) {
  242. $ValItem .= ' || $(\'#'.$FieldKey.'day\').raw().value !== ""';
  243. }
  244. $ValItem .= ' || $(\'#'.$FieldKey.'year\').raw().value !== "") {'."\r\n";
  245. $ValItem .= $ValItemHold;
  246. $ValItem .= " }\r\n";
  247. } else {
  248. $ValItem .= $ValItemHold;
  249. }
  250. } elseif ($Field['Type'] === 'checkbox') {
  251. $ValItem = ' if (!$(\'#'.$FieldKey.'\').checked) { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  252. } elseif ($Field['Type'] === 'compare') {
  253. $ValItem = ' if ($(\'#'.$FieldKey.'\').raw().value!==$(\'#'.$Field['CompareField'].'\').raw().value) { return showError(\''.$FieldKey.','.$Field['CompareField'].'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  254. }
  255. if (empty($Field['Required']) && $Field['Type'] !== 'date') {
  256. $ReturnJS .= ' if ($(\'#'.$FieldKey.'\').raw().value!=="") {'."\r\n ";
  257. $ReturnJS .= $ValItem;
  258. $ReturnJS .= " }\r\n";
  259. } else {
  260. $ReturnJS .= $ValItem;
  261. }
  262. $ValItem = '';
  263. }
  264. $ReturnJS .= "}\r\n";
  265. $ReturnJS .= "</script>\r\n";
  266. return $ReturnJS;
  267. */
  268. }
  269. }