summoner.class.php 6.4 KB

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