summoner.class.php 6.9 KB

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