Gazelle/image.php

280 lines
7.7 KiB
PHP
Raw Normal View History

2011-03-28 14:21:28 +00:00
<?
/*-- Image Start Class ---------------------------------*/
/*------------------------------------------------------*/
2013-06-26 08:01:00 +00:00
/* Simplified version of script_start, used for the */
/* sitewide image proxy. */
2011-03-28 14:21:28 +00:00
/*------------------------------------------------------*/
/********************************************************/
2011-10-24 08:00:10 +00:00
error_reporting(E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR);
2011-03-28 14:21:28 +00:00
2013-04-13 08:00:19 +00:00
if (isset($_SERVER['http_if_modified_since'])) {
2011-03-28 14:21:28 +00:00
header("Status: 304 Not Modified");
die();
}
2013-06-26 08:01:00 +00:00
header('Expires: '.date('D, d-M-Y H:i:s \U\T\C', time() + 3600 * 24 * 120)); // 120 days
header('Last-Modified: '.date('D, d-M-Y H:i:s \U\T\C', time()));
2011-03-28 14:21:28 +00:00
2013-06-26 08:01:00 +00:00
require('classes/config.php'); // The config contains all site wide configuration information as well as memcached rules
2011-03-28 14:21:28 +00:00
2013-04-13 08:00:19 +00:00
if (!extension_loaded('gd')) {
error('nogd');
}
2011-03-28 14:21:28 +00:00
2013-06-26 08:01:00 +00:00
require(SERVER_ROOT.'/classes/cache.class.php'); // Require the caching class
require(SERVER_ROOT.'/classes/encrypt.class.php'); // Require the encryption class
2011-03-28 14:21:28 +00:00
require(SERVER_ROOT.'/classes/regex.php');
2013-06-26 08:01:00 +00:00
$Cache = NEW CACHE($MemcachedServers); // Load the caching class
$Enc = NEW CRYPT; // Load the encryption class
2011-03-28 14:21:28 +00:00
2013-04-13 08:00:19 +00:00
if (isset($_COOKIE['session'])) {
2013-06-26 08:01:00 +00:00
$LoginCookie = $Enc->decrypt($_COOKIE['session']);
2013-04-13 08:00:19 +00:00
}
if (isset($LoginCookie)) {
2013-06-26 08:01:00 +00:00
list($SessionID, $UserID) = explode('|~|', $Enc->decrypt($LoginCookie));
2011-03-28 14:21:28 +00:00
$UserID = (int)$UserID;
2013-06-26 08:01:00 +00:00
$UserInfo = $Cache->get_value("user_info_$UserID");
2011-03-28 14:21:28 +00:00
$Permissions = $Cache->get_value('perm_'.$UserInfo['PermissionID']);
}
function check_perms($PermissionName) {
global $Permissions;
return (isset($Permissions['Permissions'][$PermissionName])) ? true : false;
}
function error($Type) {
2013-04-13 08:00:19 +00:00
header('Content-type: image/gif');
die(file_get_contents(SERVER_ROOT.'/sections/image/'.$Type.'.gif'));
2011-03-28 14:21:28 +00:00
}
function invisible($Image) {
$Count = imagecolorstotal($Image);
2013-04-13 08:00:19 +00:00
if ($Count == 0) {
return false;
}
2011-03-28 14:21:28 +00:00
$TotalAlpha = 0;
2013-04-13 08:00:19 +00:00
for ($i = 0; $i < $Count; ++$i) {
2013-06-26 08:01:00 +00:00
$Color = imagecolorsforindex($Image, $i);
2011-03-28 14:21:28 +00:00
$TotalAlpha += $Color['alpha'];
}
return (($TotalAlpha/$Count) == 127) ? true : false;
2012-10-11 08:00:15 +00:00
2011-03-28 14:21:28 +00:00
}
function is_number($Str) {
$Return = true;
2013-04-13 08:00:19 +00:00
if ($Str < 0) {
$Return = false;
}
2011-03-28 14:21:28 +00:00
// We're converting input to a int, then string and comparing to original
$Return = ($Str == strval(intval($Str)) ? true : false);
return $Return;
}
function verysmall($Image) {
return ((imagesx($Image) * imagesy($Image)) < 25) ? true : false;
}
function image_type($Data) {
2013-06-26 08:01:00 +00:00
if (!strncmp($Data, 'GIF', 3)) {
return 'gif';
}
2013-06-26 08:01:00 +00:00
if (!strncmp($Data, pack('H*', '89504E47'), 4)) {
return 'png';
}
2013-06-26 08:01:00 +00:00
if (!strncmp($Data, pack('H*', 'FFD8'), 2)) {
return 'jpeg';
}
2013-06-26 08:01:00 +00:00
if (!strncmp($Data, 'BM', 2)) {
return 'bmp';
}
2013-06-26 08:01:00 +00:00
if (!strncmp($Data, 'II', 2) || !strncmp($Data, 'MM', 2)) {
return 'tiff';
}
}
2011-03-28 14:21:28 +00:00
function image_height($Type, $Data) {
$Length = strlen($Data);
global $URL, $_GET;
2013-04-13 08:00:19 +00:00
switch ($Type) {
2011-03-28 14:21:28 +00:00
case 'jpeg':
// See http://www.obrador.com/essentialjpeg/headerinfo.htm
$i = 4;
$Data = (substr($Data, $i));
$Block = unpack('nLength', $Data);
$Data = substr($Data, $Block['Length']);
2013-04-13 08:00:19 +00:00
$i += $Block['Length'];
2011-03-28 14:21:28 +00:00
$Str []= "Started 4, + ".$Block['Length'];
2013-04-13 08:00:19 +00:00
while ($Data != '') { // iterate through the blocks until we find the start of frame marker (FFC0)
2011-03-28 14:21:28 +00:00
$Block = unpack('CBlock/CType/nLength', $Data); // Get info about the block
2013-06-26 08:01:00 +00:00
if ($Block['Block'] != '255') { // We should be at the start of a new block
break;
}
2013-04-13 08:00:19 +00:00
if ($Block['Type'] != '192') { // C0
$Data = substr($Data, $Block['Length'] + 2); // Next block
2013-06-26 08:01:00 +00:00
$Str []= "Started $i, + ".($Block['Length'] + 2);
2013-04-13 08:00:19 +00:00
$i += ($Block['Length'] + 2);
2011-03-28 14:21:28 +00:00
} else { // We're at the FFC0 block
$Data = substr($Data, 5); // Skip FF C0 Length(2) precision(1)
2013-04-13 08:00:19 +00:00
$i += 5;
2011-03-28 14:21:28 +00:00
$Height = unpack('nHeight', $Data);
return $Height['Height'];
}
}
break;
case 'gif':
$Data = substr($Data, 8);
$Height = unpack('vHeight', $Data);
return $Height['Height'];
case 'png':
$Data = substr($Data, 20);
$Height = unpack('NHeight', $Data);
return $Height['Height'];
default:
return 0;
}
}
2013-06-06 08:01:03 +00:00
function send_pm($ToID, $FromID, $Subject, $Body, $ConvID = '') {
2011-03-28 14:21:28 +00:00
global $DB, $Cache;
2013-04-13 08:00:19 +00:00
if ($ToID == 0) {
2011-03-28 14:21:28 +00:00
// Don't allow users to send messages to the system
return;
}
2013-04-13 08:00:19 +00:00
if ($ConvID == '') {
2013-06-06 08:01:03 +00:00
$DB->query("
INSERT INTO pm_conversations (Subject)
VALUES ('$Subject')");
2011-03-28 14:21:28 +00:00
$ConvID = $DB->inserted_id();
2013-06-06 08:01:03 +00:00
$DB->query("
INSERT INTO pm_conversations_users
(UserID, ConvID, InInbox, InSentbox, SentDate, ReceivedDate, UnRead)
VALUES
2013-06-26 08:01:00 +00:00
('$ToID', '$ConvID', '1', '0', '".sqltime()."', '".sqltime()."', '1')");
2011-03-28 14:21:28 +00:00
if ($FromID != 0) {
2013-06-06 08:01:03 +00:00
$DB->query("
INSERT INTO pm_conversations_users
(UserID, ConvID, InInbox, InSentbox, SentDate, ReceivedDate, UnRead)
VALUES
2013-06-26 08:01:00 +00:00
('$FromID', '$ConvID', '0', '1', '".sqltime()."', '".sqltime()."', '0')");
2011-03-28 14:21:28 +00:00
}
} else {
2013-06-06 08:01:03 +00:00
$DB->query("
UPDATE pm_conversations_users
SET
2013-06-26 08:01:00 +00:00
InInbox = '1',
UnRead = '1',
ReceivedDate = '".sqltime()."'
WHERE UserID = '$ToID'
AND ConvID = '$ConvID'");
2011-03-28 14:21:28 +00:00
2013-06-06 08:01:03 +00:00
$DB->query("
UPDATE pm_conversations_users
SET
2013-06-26 08:01:00 +00:00
InSentbox = '1',
SentDate = '".sqltime()."'
WHERE UserID = '$FromID'
AND ConvID = '$ConvID'");
2011-03-28 14:21:28 +00:00
}
2013-06-06 08:01:03 +00:00
$DB->query("
INSERT INTO pm_messages
(SenderID, ConvID, SentDate, Body)
VALUES
('$FromID', '$ConvID', '".sqltime()."', '$Body')");
2011-03-28 14:21:28 +00:00
// Clear the caches of the inbox and sentbox
2013-06-06 08:01:03 +00:00
/*$DB->query("
SELECT UnRead
FROM pm_conversations_users
2013-06-26 08:01:00 +00:00
WHERE ConvID = '$ConvID'
AND UserID = '$ToID'");
2013-06-06 08:01:03 +00:00
*/
$DB->query("
SELECT COUNT(ConvID)
FROM pm_conversations_users
WHERE UnRead = '1'
2013-06-26 08:01:00 +00:00
AND UserID = '$ToID'
2013-06-06 08:01:03 +00:00
AND InInbox = '1'");
2011-03-28 14:21:28 +00:00
list($UnRead) = $DB->next_record(MYSQLI_BOTH, FALSE);
2013-06-26 08:01:00 +00:00
$Cache->cache_value("inbox_new_$ToID", $UnRead);
2011-03-28 14:21:28 +00:00
//if ($UnRead == 0) {
2013-06-26 08:01:00 +00:00
// $Cache->increment("inbox_new_$ToID");
2011-03-28 14:21:28 +00:00
//}
return $ConvID;
}
function send_irc($Raw) {
$IRCSocket = fsockopen(SOCKET_LISTEN_ADDRESS, SOCKET_LISTEN_PORT);
fwrite($IRCSocket, $Raw);
fclose($IRCSocket);
}
2011-08-27 08:00:05 +00:00
function display_str($Str) {
2013-04-13 08:00:19 +00:00
if ($Str === NULL || $Str === false || is_array($Str)) {
2011-08-27 08:00:05 +00:00
return '';
}
2013-04-13 08:00:19 +00:00
if ($Str != '' && !is_number($Str)) {
$Str = make_utf8($Str);
2013-06-26 08:01:00 +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);
2011-08-27 08:00:05 +00:00
$Replace = array(
"'",'"',"<",">",
2013-06-26 08:01:00 +00:00
'&#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;'
2011-08-27 08:00:05 +00:00
);
2013-04-13 08:00:19 +00:00
$With = array(
2011-08-27 08:00:05 +00:00
'&#39;','&quot;','&lt;','&gt;',
2013-06-26 08:01:00 +00:00
'&#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;'
2011-08-27 08:00:05 +00:00
);
2013-06-26 08:01:00 +00:00
$Str = str_replace($Replace, $With, $Str);
2011-08-27 08:00:05 +00:00
}
return $Str;
}
function make_utf8($Str) {
2013-04-13 08:00:19 +00:00
if ($Str != '') {
if (is_utf8($Str)) {
$Encoding = 'UTF-8';
}
if (empty($Encoding)) {
2013-06-06 08:01:03 +00:00
$Encoding = mb_detect_encoding($Str, 'UTF-8, ISO-8859-1');
2013-04-13 08:00:19 +00:00
}
if (empty($Encoding)) {
$Encoding = 'ISO-8859-1';
}
if ($Encoding == 'UTF-8') {
return $Str;
} else {
2013-06-06 08:01:03 +00:00
return @mb_convert_encoding($Str, 'UTF-8', $Encoding);
2013-04-13 08:00:19 +00:00
}
2011-08-27 08:00:05 +00:00
}
}
function is_utf8($Str) {
return preg_match('%^(?:
[\x09\x0A\x0D\x20-\x7E] // ASCII
| [\xC2-\xDF][\x80-\xBF] // non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] // excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} // straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] // excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} // planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} // planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} // plane 16
)*$%xs', $Str
);
}
2011-03-28 14:21:28 +00:00
require(SERVER_ROOT.'/sections/image/index.php');
?>