Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. // This is a file of miscellaneous functions that are called so damn often
  3. // that it'd just be annoying to stick them in namespaces.
  4. /**
  5. * Return true if the given string is numeric.
  6. *
  7. * @param mixed $Str
  8. * @return bool
  9. */
  10. if (PHP_INT_SIZE === 4) {
  11. function is_number($Str)
  12. {
  13. if ($Str === null || $Str === '') {
  14. return false;
  15. }
  16. if (is_int($Str)) {
  17. return true;
  18. }
  19. if ($Str[0] == '-' || $Str[0] == '+') { // Leading plus/minus signs are ok
  20. $Str[0] = 0;
  21. }
  22. return ltrim($Str, "0..9") === '';
  23. }
  24. } else {
  25. function is_number($Str)
  26. {
  27. return $Str == strval(intval($Str));
  28. }
  29. }
  30. function is_date($Date)
  31. {
  32. list($Y, $M, $D) = explode('-', $Date);
  33. if (checkdate($M, $D, $Y)) {
  34. return true;
  35. }
  36. return false;
  37. }
  38. /**
  39. * Check that some given variables (usually in _GET or _POST) are numbers
  40. *
  41. * @param array $Base array that's supposed to contain all keys to check
  42. * @param array $Keys list of keys to check
  43. * @param mixed $Error error code or string to pass to the error() function if a key isn't numeric
  44. */
  45. function assert_numbers(&$Base, $Keys, $Error = 0)
  46. {
  47. // make sure both arguments are arrays
  48. if (!is_array($Base) || !is_array($Keys)) {
  49. return;
  50. }
  51. foreach ($Keys as $Key) {
  52. if (!isset($Base[$Key]) || !is_number($Base[$Key])) {
  53. error($Error);
  54. }
  55. }
  56. }
  57. /**
  58. * Return true, false or null, depending on the input value's "truthiness" or "non-truthiness"
  59. *
  60. * @param $Value the input value to check for truthiness
  61. * @return true if $Value is "truthy", false if it is "non-truthy" or null if $Value was not
  62. * a bool-like value
  63. */
  64. function is_bool_value($Value)
  65. {
  66. if (is_bool($Value)) {
  67. return $Value;
  68. }
  69. if (is_string($Value)) {
  70. switch (strtolower($Value)) {
  71. case 'true':
  72. case 'yes':
  73. case 'on':
  74. case '1':
  75. return true;
  76. case 'false':
  77. case 'no':
  78. case 'off':
  79. case '0':
  80. return false;
  81. }
  82. }
  83. if (is_numeric($Value)) {
  84. if ($Value == 1) {
  85. return true;
  86. } elseif ($Value == 0) {
  87. return false;
  88. }
  89. }
  90. return null;
  91. }
  92. /**
  93. * HTML-escape a string for output.
  94. * This is preferable to htmlspecialchars because it doesn't screw up upon a double escape.
  95. *
  96. * @param string $Str
  97. * @return string escaped string.
  98. */
  99. function display_str($Str)
  100. {
  101. if ($Str === null || $Str === false || is_array($Str)) {
  102. return '';
  103. }
  104. if ($Str != '' && !is_number($Str)) {
  105. $Str = Format::make_utf8($Str);
  106. $Str = mb_convert_encoding($Str, 'HTML-ENTITIES', 'UTF-8');
  107. $Str = preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,6};)/m", '&amp;', $Str);
  108. $Replace = array(
  109. "'",'"',"<",">",
  110. '&#128;','&#130;','&#131;','&#132;','&#133;','&#134;','&#135;','&#136;',
  111. '&#137;','&#138;','&#139;','&#140;','&#142;','&#145;','&#146;','&#147;',
  112. '&#148;','&#149;','&#150;','&#151;','&#152;','&#153;','&#154;','&#155;',
  113. '&#156;','&#158;','&#159;'
  114. );
  115. $With = array(
  116. '&#39;','&quot;','&lt;','&gt;',
  117. '&#8364;','&#8218;','&#402;','&#8222;','&#8230;','&#8224;','&#8225;','&#710;',
  118. '&#8240;','&#352;','&#8249;','&#338;','&#381;','&#8216;','&#8217;','&#8220;',
  119. '&#8221;','&#8226;','&#8211;','&#8212;','&#732;','&#8482;','&#353;','&#8250;',
  120. '&#339;','&#382;','&#376;'
  121. );
  122. $Str = str_replace($Replace, $With, $Str);
  123. }
  124. return $Str;
  125. }
  126. /**
  127. * Send a message to an IRC bot listening on SOCKET_LISTEN_PORT
  128. *
  129. * @param string $Raw An IRC protocol snippet to send.
  130. */
  131. function send_irc($Raw)
  132. {
  133. // check if IRC is enabled
  134. if (!FEATURE_IRC) {
  135. return;
  136. }
  137. $IRCSocket = fsockopen(SOCKET_LISTEN_ADDRESS, SOCKET_LISTEN_PORT);
  138. $Raw = str_replace(array("\n", "\r"), '', $Raw);
  139. fwrite($IRCSocket, $Raw);
  140. fclose($IRCSocket);
  141. }
  142. /**
  143. * Display a critical error and kills the page.
  144. *
  145. * @param string $Error Error type. Automatically supported:
  146. * 403, 404, 0 (invalid input), -1 (invalid request)
  147. * If you use your own string for Error, it becomes the error description.
  148. * @param boolean $NoHTML If true, the header/footer won't be shown, just the description.
  149. * @param string $Log If true, the user is given a link to search $Log in the site log.
  150. */
  151. function error($Error, $NoHTML = false, $Log = false)
  152. {
  153. global $Debug;
  154. require(SERVER_ROOT.'/sections/error/index.php');
  155. $Debug->profile();
  156. die();
  157. }
  158. /**
  159. * Convenience function. See doc in permissions.class.php
  160. */
  161. function check_perms($PermissionName, $MinClass = 0)
  162. {
  163. return Permissions::check_perms($PermissionName, $MinClass);
  164. }
  165. function get_permissions_for_user($UserID, $CustomPermissions = false)
  166. {
  167. return Permissions::get_permissions_for_user($UserID, $CustomPermissions = false);
  168. }
  169. /**
  170. * Print JSON status result with an optional message and die.
  171. * DO NOT USE THIS FUNCTION!
  172. */
  173. function json_die($Status, $Message)
  174. {
  175. json_print($Status, $Message);
  176. die();
  177. }
  178. /**
  179. * Print JSON status result with an optional message.
  180. */
  181. function json_print($Status, $Message)
  182. {
  183. if ($Status == 'success' && $Message) {
  184. print json_encode(array('status' => $Status, 'response' => $Message));
  185. } elseif ($Message) {
  186. print json_encode(array('status' => $Status, 'error' => $Message));
  187. } else {
  188. print json_encode(array('status' => $Status, 'response' => []));
  189. }
  190. }
  191. /**
  192. * Print the site's URL including the appropriate URI scheme, including the trailing slash
  193. */
  194. function site_url()
  195. {
  196. return 'https://' . SITE_DOMAIN . '/';
  197. }