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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. # todo: Change this to a switch statement
  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. /**
  159. * Extension Parser
  160. *
  161. * Takes an associative array of file types and extension, e.g.,
  162. * $Archives = [
  163. * '7z' => ['7z'],
  164. * 'bzip2' => ['bz2', 'bzip2'],
  165. * 'gzip' => ['gz', 'gzip', 'tgz', 'tpz'],
  166. * ...
  167. * ];
  168. *
  169. * Then it finds all the extensions in a torrent file list,
  170. * organizes them by file size, and returns the "heaviest" match.
  171. *
  172. * That way, you can have, e.g., 5 GiB FASTQ sequence data in one file,
  173. * and 100 other small files, and get the format of the actual data.
  174. *
  175. * todo: Incorporate into the main function (remove if statements first)
  176. * todo: Make this work with a more robust object than $Tor->file_list()
  177. */
  178. public function ParseExtensions($FileList, $FileTypes)
  179. {
  180. # Make $Tor->file_list() output manageable
  181. $UnNested = array_values($FileList[1]);
  182. /*
  183. $Sorted = usort($UnNested, function ($a, $b) {
  184. return $b[0] <=> $a[0];
  185. });
  186. */
  187. $TopTen = array_slice($UnNested, 0, 10); # Good
  188. $Result = [];
  189. print_r('<pre>');
  190. var_dump($TopTen);
  191. print_r('</pre>');
  192. foreach ($TopTen as $TopTen) {
  193. $Extensions = explode('.', strtolower($TopTen[1]));
  194. $Result = array_filter($Extensions, function ($a) {
  195. foreach ($FileTypes as $FileType) {
  196. in_array($a, $FileType);
  197. }
  198. });
  199. /*
  200. foreach ($FileTypes as $Key => $FileTypes) {
  201. print_r('<pre>');
  202. var_dump($UnNested);
  203. var_dump( $FileTypes[$Key]);
  204. print_r('</pre>');
  205. }
  206. */
  207. }
  208. /*
  209. foreach ($TopTen as $TopTen) {
  210. $Extensions = explode('.', strtolower($TopTen[1]));
  211. #foreach ($FileTypes as $Key => $FileTypes) {
  212. print_r('<pre>');
  213. #$Result = (in_array($Extensions, $FileTypes)) ? $FileTypes[$Key] : false;
  214. var_dump($Extensions);
  215. var_dump(array_intersect($Extensions, $FileTypes));
  216. print_r('</pre>');
  217. #}
  218. print_r('<pre>');
  219. //var_dump(array_intersect($Extensions, $FileTypes));
  220. //var_dump($Extensions);
  221. print_r('</pre>');
  222. }
  223. */
  224. print_r('<pre>');
  225. #var_dump(array_intersect($UnNested, $FileTypes));
  226. #print_r($Sorted);
  227. print_r($Result);
  228. print_r('</pre>');
  229. /*
  230. while ($Result === false) {
  231. foreach ($UnNested as $Key => $UnNested) {
  232. $Exploded = explode('.', strtolower($UnNested[1]));
  233. foreach ($Needles as $Key => $Needle) {
  234. $ID = array_search($Exploded, $Needle);
  235. var_dump($Needle[$ID]);
  236. }
  237. $dump = array_filter($Exploded, function($s){
  238. foreach ($Needles as $Type => $Extension) {
  239. return array_search($s, $Extension);
  240. }
  241. });
  242. #var_dump($dump);
  243. /*
  244. if (array_search($Needle, $Exploded, true)) {
  245. $Result = $Needles;
  246. #break;
  247. }
  248. }
  249. }
  250. */
  251. #var_dump($Result);
  252. /*
  253. #do {
  254. foreach ($UnNested as $UnNested) {
  255. $Exploded = explode('.', strtolower($UnNested[1]));
  256. #var_dump(in_array(vals($names), $Explode));
  257. }
  258. /*
  259. if (array_intersect($names, $Exploded)) {
  260. $result = array_search($Exploded, $names);
  261. }
  262. *
  263. }
  264. #} while ($result !== false);
  265. print_r($result);
  266. */
  267. }
  268. public function GenerateJS($FormID)
  269. {
  270. /*
  271. $ReturnJS = "<script type=\"text/javascript\" language=\"javascript\">\r\n";
  272. $ReturnJS .= "function formVal() {\r\n";
  273. $ReturnJS .= " clearErrors('$FormID');\r\n";
  274. reset($this->Fields);
  275. foreach ($this->Fields as $FieldKey => $Field) {
  276. if ($Field['Type'] === 'string') {
  277. $ValItem = ' if ($(\'#'.$FieldKey.'\').raw().value === ""';
  278. if (!empty($Field['MaxLength'])) {
  279. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > '.$Field['MaxLength'];
  280. } else {
  281. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > 255';
  282. }
  283. if (!empty($Field['MinLength'])) {
  284. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < '.$Field['MinLength'];
  285. }
  286. $ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  287. } elseif ($Field['Type'] === 'number') {
  288. $Match = '0-9';
  289. if (!empty($Field['AllowPeriod'])) {
  290. $Match .= '.';
  291. }
  292. if (!empty($Field['AllowComma'])) {
  293. $Match .= ',';
  294. }
  295. $ValItem = ' if ($(\'#'.$FieldKey.'\').raw().value.match(/[^'.$Match.']/) || $(\'#'.$FieldKey.'\').raw().value.length < 1';
  296. if (!empty($Field['MaxLength'])) {
  297. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value/1 > '.$Field['MaxLength'];
  298. }
  299. if (!empty($Field['MinLength'])) {
  300. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value/1 < '.$Field['MinLength'];
  301. }
  302. $ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  303. } elseif ($Field['Type'] === 'email') {
  304. $ValItem = ' if (!validEmail($(\'#'.$FieldKey.'\').raw().value)';
  305. if (!empty($Field['MaxLength'])) {
  306. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > '.$Field['MaxLength'];
  307. } else {
  308. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > 255';
  309. }
  310. if (!empty($Field['MinLength'])) {
  311. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < '.$Field['MinLength'];
  312. } else {
  313. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < 6';
  314. }
  315. $ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  316. } elseif ($Field['Type'] === 'link') {
  317. $ValItem = ' if (!validLink($(\'#'.$FieldKey.'\').raw().value)';
  318. if (!empty($Field['MaxLength'])) {
  319. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > '.$Field['MaxLength'];
  320. } else {
  321. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > 255';
  322. }
  323. if (!empty($Field['MinLength'])) {
  324. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < '.$Field['MinLength'];
  325. } else {
  326. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < 10';
  327. }
  328. $ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  329. } elseif ($Field['Type'] === 'username') {
  330. $ValItem = ' if ($(\'#'.$FieldKey.'\').raw().value.match(/[^a-zA-Z0-9_\-]/)';
  331. if (!empty($Field['MaxLength'])) {
  332. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > '.$Field['MaxLength'];
  333. }
  334. if (!empty($Field['MinLength'])) {
  335. $ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < '.$Field['MinLength'];
  336. }
  337. $ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  338. } elseif ($Field['Type'] === 'regex') {
  339. $ValItem = ' if (!$(\'#'.$FieldKey.'\').raw().value.match('.$Field['Regex'].')) { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  340. } elseif ($Field['Type'] === 'date') {
  341. $DisplayError = $FieldKey.'month';
  342. if (isset($Field['MinLength']) && $Field['MinLength'] === 3) {
  343. $Day = '$(\'#'.$FieldKey.'day\').raw().value';
  344. $DisplayError .= ",{$FieldKey}day";
  345. } else {
  346. $Day = '1';
  347. }
  348. $DisplayError .= ",{$FieldKey}year";
  349. $ValItemHold = ' if (!validDate($(\'#'.$FieldKey.'month\').raw().value+\'/\'+'.$Day.'+\'/\'+$(\'#'.$FieldKey.'year\').raw().value)) { return showError(\''.$DisplayError.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  350. if (empty($Field['Required'])) {
  351. $ValItem = ' if ($(\'#'.$FieldKey.'month\').raw().value !== ""';
  352. if (isset($Field['MinLength']) && $Field['MinLength'] === 3) {
  353. $ValItem .= ' || $(\'#'.$FieldKey.'day\').raw().value !== ""';
  354. }
  355. $ValItem .= ' || $(\'#'.$FieldKey.'year\').raw().value !== "") {'."\r\n";
  356. $ValItem .= $ValItemHold;
  357. $ValItem .= " }\r\n";
  358. } else {
  359. $ValItem .= $ValItemHold;
  360. }
  361. } elseif ($Field['Type'] === 'checkbox') {
  362. $ValItem = ' if (!$(\'#'.$FieldKey.'\').checked) { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  363. } elseif ($Field['Type'] === 'compare') {
  364. $ValItem = ' if ($(\'#'.$FieldKey.'\').raw().value!==$(\'#'.$Field['CompareField'].'\').raw().value) { return showError(\''.$FieldKey.','.$Field['CompareField'].'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
  365. }
  366. if (empty($Field['Required']) && $Field['Type'] !== 'date') {
  367. $ReturnJS .= ' if ($(\'#'.$FieldKey.'\').raw().value!=="") {'."\r\n ";
  368. $ReturnJS .= $ValItem;
  369. $ReturnJS .= " }\r\n";
  370. } else {
  371. $ReturnJS .= $ValItem;
  372. }
  373. $ValItem = '';
  374. }
  375. $ReturnJS .= "}\r\n";
  376. $ReturnJS .= "</script>\r\n";
  377. return $ReturnJS;
  378. */
  379. }
  380. }