summoner.class.php 6.1 KB

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