Gazelle/classes/util.php

139 lines
3.8 KiB
PHP
Raw Normal View History

2012-10-11 08:00:15 +00:00
<?
// This is a file of miscellaneous functions that are called so damn often
// that it'd just be annoying to stick them in namespaces.
/**
* Return true if the given string is numeric.
*
2013-08-21 08:00:53 +00:00
* @param mixed $Str
* @return bool
2012-10-11 08:00:15 +00:00
*/
2013-02-24 08:00:18 +00:00
if (PHP_INT_SIZE === 4) {
function is_number($Str) {
2013-08-21 08:00:53 +00:00
if ($Str === null || $Str === '') {
return false;
}
if (is_int($Str)) {
return true;
}
2013-02-24 08:00:18 +00:00
if ($Str[0] == '-' || $Str[0] == '+') { // Leading plus/minus signs are ok
$Str[0] = 0;
}
return ltrim($Str, "0..9") === '';
}
} else {
function is_number($Str) {
return $Str == strval(intval($Str));
}
2012-10-11 08:00:15 +00:00
}
2013-07-19 08:00:28 +00:00
/**
* Check that some given variables (usually in _GET or _POST) are numbers
*
2013-07-24 08:00:46 +00:00
* @param array $Base array that's supposed to contain all keys to check
2013-07-19 08:00:28 +00:00
* @param array $Keys list of keys to check
* @param mixed $Error error code or string to pass to the error() function if a key isn't numeric
*/
function assert_numbers(&$Base, $Keys, $Error = 0) {
2013-07-24 08:00:46 +00:00
// make sure both arguments are arrays
2013-07-19 08:00:28 +00:00
if (!is_array($Base) || !is_array($Keys)) {
return;
}
foreach ($Keys as $Key) {
2013-07-24 08:00:46 +00:00
if (!isset($Base[$Key]) || !is_number($Base[$Key])) {
2013-07-19 08:00:28 +00:00
error($Error);
}
}
}
2012-10-11 08:00:15 +00:00
/**
* HTML-escape a string for output.
* This is preferable to htmlspecialchars because it doesn't screw up upon a double escape.
*
* @param string $Str
* @return string escaped string.
*/
function display_str($Str) {
2013-04-13 08:00:19 +00:00
if ($Str === NULL || $Str === false || is_array($Str)) {
2012-10-11 08:00:15 +00:00
return '';
}
2013-04-13 08:00:19 +00:00
if ($Str != '' && !is_number($Str)) {
2012-10-11 08:00:15 +00:00
$Str = Format::make_utf8($Str);
2013-06-18 08:00:48 +00:00
$Str = mb_convert_encoding($Str, 'HTML-ENTITIES', 'UTF-8');
$Str = preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,5};)/m", '&amp;', $Str);
2012-10-11 08:00:15 +00:00
$Replace = array(
"'",'"',"<",">",
'&#128;','&#130;','&#131;','&#132;','&#133;','&#134;','&#135;','&#136;',
'&#137;','&#138;','&#139;','&#140;','&#142;','&#145;','&#146;','&#147;',
'&#148;','&#149;','&#150;','&#151;','&#152;','&#153;','&#154;','&#155;',
'&#156;','&#158;','&#159;'
);
$With = array(
'&#39;','&quot;','&lt;','&gt;',
'&#8364;','&#8218;','&#402;','&#8222;','&#8230;','&#8224;','&#8225;','&#710;',
'&#8240;','&#352;','&#8249;','&#338;','&#381;','&#8216;','&#8217;','&#8220;',
'&#8221;','&#8226;','&#8211;','&#8212;','&#732;','&#8482;','&#353;','&#8250;',
'&#339;','&#382;','&#376;'
);
$Str = str_replace($Replace, $With, $Str);
}
return $Str;
}
/**
* Send a message to an IRC bot listening on SOCKET_LISTEN_PORT
*
* @param string $Raw An IRC protocol snippet to send.
*/
function send_irc($Raw) {
$IRCSocket = fsockopen(SOCKET_LISTEN_ADDRESS, SOCKET_LISTEN_PORT);
$Raw = str_replace(array("\n", "\r"), '', $Raw);
fwrite($IRCSocket, $Raw);
fclose($IRCSocket);
}
/**
* Display a critical error and kills the page.
*
* @param string $Error Error type. Automatically supported:
* 403, 404, 0 (invalid input), -1 (invalid request)
* If you use your own string for Error, it becomes the error description.
* @param boolean $Ajax If true, the header/footer won't be shown, just the description.
* @param string $Log If true, the user is given a link to search $Log in the site log.
*/
2013-06-26 08:01:00 +00:00
function error($Error, $Ajax = false, $Log = false) {
2012-10-11 08:00:15 +00:00
global $Debug;
require(SERVER_ROOT.'/sections/error/index.php');
$Debug->profile();
die();
}
/**
2013-05-27 08:00:58 +00:00
* Convenience function. See doc in permissions.class.php
2012-10-11 08:00:15 +00:00
*/
function check_perms($PermissionName, $MinClass = 0) {
return Permissions::check_perms($PermissionName, $MinClass);
}
2013-04-24 08:00:23 +00:00
/*
* Print json status result with an optional message and die.
*/
function json_die($Status, $Message) {
2013-08-14 08:00:46 +00:00
if ($Status == 'success' && $Message) {
2013-05-14 08:00:34 +00:00
print json_encode(array('status' => $Status, 'response' => $Message));
2013-08-14 08:00:46 +00:00
} elseif ($Message) {
2013-05-14 08:00:34 +00:00
print json_encode(array('status' => $Status, 'error' => $Message));
} else {
print json_encode(array('status' => $Status));
}
die();
2013-04-24 08:00:23 +00:00
}
2012-10-11 08:00:15 +00:00
?>