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.3KB

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