helper.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2013-2020 Johannes 'Banana' Keßler
  5. *
  6. * https://www.bananas-playground.net
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  10. *
  11. * You should have received a copy of the
  12. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  13. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  14. */
  15. /**
  16. * Static helper class
  17. *
  18. */
  19. class Helper {
  20. /**
  21. * validate the given string with the given type. Optional check the string
  22. * length
  23. *
  24. * @see http://de.php.net/manual/en/regexp.reference.unicode.php
  25. * http://www.sql-und-xml.de/unicode-database/#pc
  26. *
  27. * the pattern replaces all that is allowed. the correct result after
  28. * the replace should be empty, otherwise are there chars which are not
  29. * allowed
  30. *
  31. * @param string $input The string to check
  32. * @param string $mode How the string should be checked
  33. * @param mixed $limit If int given the string is checked for length
  34. *
  35. * @return bool
  36. */
  37. static function validate($input,$mode='text',$limit=false) {
  38. // check if we have input
  39. $input = trim($input);
  40. if($input == "") return false;
  41. $ret = false;
  42. switch ($mode) {
  43. case 'mail':
  44. if(filter_var($input,FILTER_VALIDATE_EMAIL) === $input) {
  45. return true;
  46. }
  47. else {
  48. return false;
  49. }
  50. break;
  51. case 'url':
  52. if(filter_var($input,FILTER_VALIDATE_URL) === $input) {
  53. return true;
  54. }
  55. else {
  56. return false;
  57. }
  58. break;
  59. case 'nospace':
  60. // text without any whitespace and special chars
  61. $pattern = '/[\p{L}\p{N}]/u';
  62. break;
  63. case 'nospaceP':
  64. // text without any whitespace and special chars
  65. // but with Punctuation
  66. $pattern = '/[\p{L}\p{N}\p{Po}]/u';
  67. break;
  68. case 'digit':
  69. // only numbers and digit
  70. $pattern = '/[\p{Nd}]/';
  71. break;
  72. case 'pageTitle':
  73. // text with whitespace and without special chars
  74. // but with Punctuation
  75. $pattern = '/[\p{L}\p{N}\p{Po}\p{Z}\s]/u';
  76. break;
  77. # strange. the \p{M} is needed.. don't know why..
  78. case 'filename':
  79. $pattern = '/[\p{L}\p{N}\p{M}\-_\.\p{Zs}]/u';
  80. break;
  81. case 'text':
  82. default:
  83. $pattern = '/[\p{L}\p{N}\p{P}\p{S}\p{Z}\p{M}\s]/u';
  84. }
  85. $value = preg_replace($pattern, '', $input);
  86. #if($input === $value) {
  87. if($value === "") {
  88. $ret = true;
  89. }
  90. if(!empty($limit)) {
  91. # isset starts with 0
  92. if(isset($input[$limit])) {
  93. # too long
  94. $ret = false;
  95. }
  96. }
  97. return $ret;
  98. }
  99. }