summoner.class.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  5. *
  6. * You should have received a copy of the
  7. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  8. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  9. *
  10. * 2019 - 2020 https://://www.bananas-playground.net/projekt/selfpaste
  11. */
  12. /**
  13. * a static helper class
  14. */
  15. class Summoner {
  16. /**
  17. * validate the given string with the given type. Optional check the string
  18. * length
  19. *
  20. * @param string $input The string to check
  21. * @param string $mode How the string should be checked
  22. * @param mixed $limit If int given the string is checked for length
  23. *
  24. * @return bool
  25. *
  26. * @see http://de.php.net/manual/en/regexp.reference.unicode.php
  27. * http://www.sql-und-xml.de/unicode-database/#pc
  28. *
  29. * the pattern replaces all that is allowed. the correct result after
  30. * the replace should be empty, otherwise are there chars which are not
  31. * allowed
  32. */
  33. static function validate($input,$mode='text',$limit=false) {
  34. // check if we have input
  35. $input = trim($input);
  36. if($input == "") return false;
  37. $ret = false;
  38. switch ($mode) {
  39. case 'mail':
  40. if(filter_var($input,FILTER_VALIDATE_EMAIL) === $input) {
  41. return true;
  42. }
  43. else {
  44. return false;
  45. }
  46. break;
  47. case 'url':
  48. if(filter_var($input,FILTER_VALIDATE_URL) === $input) {
  49. return true;
  50. }
  51. else {
  52. return false;
  53. }
  54. break;
  55. case 'nospace':
  56. // text without any whitespace and special chars
  57. $pattern = '/[\p{L}\p{N}]/u';
  58. break;
  59. case 'nospaceP':
  60. // text without any whitespace and special chars
  61. // but with Punctuation other
  62. # http://www.sql-und-xml.de/unicode-database/po.html
  63. $pattern = '/[\p{L}\p{N}\p{Po}\-]/u';
  64. break;
  65. case 'digit':
  66. // only numbers and digit
  67. // warning with negative numbers...
  68. $pattern = '/[\p{N}\-]/';
  69. break;
  70. case 'pageTitle':
  71. // text with whitespace and without special chars
  72. // but with Punctuation
  73. $pattern = '/[\p{L}\p{N}\p{Po}\p{Z}\s-]/u';
  74. break;
  75. # strange. the \p{M} is needed.. don't know why..
  76. case 'filename':
  77. $pattern = '/[\p{L}\p{N}\p{M}\-_\.\p{Zs}]/u';
  78. break;
  79. case 'text':
  80. default:
  81. $pattern = '/[\p{L}\p{N}\p{P}\p{S}\p{Z}\p{M}\s]/u';
  82. }
  83. $value = preg_replace($pattern, '', $input);
  84. if($value === "") {
  85. $ret = true;
  86. }
  87. if(!empty($limit)) {
  88. # isset starts with 0
  89. if(isset($input[$limit])) {
  90. # too long
  91. $ret = false;
  92. }
  93. }
  94. return $ret;
  95. }
  96. /**
  97. * check if a string starts with a given string
  98. *
  99. * @param string $haystack
  100. * @param string $needle
  101. * @return boolean
  102. */
  103. static function startsWith($haystack, $needle) {
  104. $length = strlen($needle);
  105. return (substr($haystack, 0, $length) === $needle);
  106. }
  107. /**
  108. * check if a string ends with a given string
  109. *
  110. * @param string $haystack
  111. * @param string $needle
  112. * @return boolean
  113. */
  114. static function endsWith($haystack, $needle) {
  115. $length = strlen($needle);
  116. if ($length == 0) {
  117. return true;
  118. }
  119. return (substr($haystack, -$length) === $needle);
  120. }
  121. /**
  122. * Simple helper to detect the $_FILES upload status
  123. * Expects the error value from $_FILES['error']
  124. * @param $error
  125. * @return array
  126. */
  127. static function checkFileUploadStatus($error) {
  128. $message = "Unknown upload error";
  129. $status = false;
  130. switch ($error) {
  131. case UPLOAD_ERR_OK:
  132. $message = "There is no error, the file uploaded with success.";
  133. $status = true;
  134. break;
  135. case UPLOAD_ERR_INI_SIZE:
  136. $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
  137. break;
  138. case UPLOAD_ERR_FORM_SIZE:
  139. $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
  140. break;
  141. case UPLOAD_ERR_PARTIAL:
  142. $message = "The uploaded file was only partially uploaded";
  143. break;
  144. case UPLOAD_ERR_NO_FILE:
  145. $message = "No file was uploaded";
  146. break;
  147. case UPLOAD_ERR_NO_TMP_DIR:
  148. $message = "Missing a temporary folder";
  149. break;
  150. case UPLOAD_ERR_CANT_WRITE:
  151. $message = "Failed to write file to disk";
  152. break;
  153. case UPLOAD_ERR_EXTENSION:
  154. $message = "File upload stopped by extension";
  155. break;
  156. }
  157. return array(
  158. 'message' => $message,
  159. 'status' => $status
  160. );
  161. }
  162. /**
  163. * create a short string based on a integer
  164. *
  165. * @see https://www.jwz.org/base64-shortlinks/
  166. * @return string
  167. */
  168. static function b64sl_pack_id($id) {
  169. $id = intval($id);
  170. $ida = ($id > 0xFFFFFFFF ? $id >> 32 : 0); // 32 bit big endian, top
  171. $idb = ($id & 0xFFFFFFFF); // 32 bit big endian, bottom
  172. $id = pack ('N', $ida) . pack ('N', $idb);
  173. $id = preg_replace('/^\000+/', '', "$id"); // omit high-order NUL bytes
  174. $id = base64_encode ($id);
  175. $id = str_replace ('+', '-', $id); // encode URL-unsafe "+" "/"
  176. $id = str_replace ('/', '_', $id);
  177. $id = preg_replace ('/=+$/', '', $id); // omit trailing padding bytes
  178. return $id;
  179. }
  180. /**
  181. * Decode a base64-encoded big-endian integer of up to 64 bits.
  182. *
  183. * @see https://www.jwz.org/base64-shortlinks/
  184. * @param $id
  185. * @return false|int|string|string[]
  186. */
  187. static function b64sl_unpack_id($id) {
  188. $id = str_replace ('-', '+', $id); // decode URL-unsafe "+" "/"
  189. $id = str_replace ('_', '/', $id);
  190. $id = base64_decode ($id);
  191. while (strlen($id) < 8) { $id = "\000$id"; } // pad with leading NULs
  192. $a = unpack ('N*', $id); // 32 bit big endian
  193. $id = ($a[1] << 32) | $a[2]; // pack top and bottom word
  194. return $id;
  195. }
  196. /**
  197. * create based on the given string a path
  198. * each char in string is a dir
  199. * asdef -> a/s/d/e/f/
  200. * @param $string
  201. * @return string
  202. */
  203. static function forwardslashStringToPath($string) {
  204. $ret = '';
  205. if(!empty($string)) {
  206. for ($i = 0; $i < strlen($string); $i++) {
  207. $ret .= $string[$i] . "/";
  208. }
  209. }
  210. return $ret;
  211. }
  212. }