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

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