Gazelle/sections/image/index.php

135 lines
4.3 KiB
PHP
Raw Normal View History

2011-03-28 14:21:28 +00:00
<?
2013-05-04 08:00:48 +00:00
if (!check_perms('site_proxy_images')) {
2013-07-04 08:00:56 +00:00
img_error('forbidden');
2013-05-04 08:00:48 +00:00
}
2014-04-25 08:00:52 +00:00
2011-08-27 08:00:05 +00:00
$URL = isset($_GET['i']) ? htmlspecialchars_decode($_GET['i']) : null;
2011-03-28 14:21:28 +00:00
2013-05-04 08:00:48 +00:00
if (!extension_loaded('openssl') && strtoupper($URL[4]) == 'S') {
2013-07-04 08:00:56 +00:00
img_error('badprotocol');
2013-05-04 08:00:48 +00:00
}
2011-03-28 14:21:28 +00:00
2013-10-22 08:01:07 +00:00
if (!preg_match('/^'.IMAGE_REGEX.'/is', $URL, $Matches)) {
2013-07-04 08:00:56 +00:00
img_error('invalid');
2011-03-28 14:21:28 +00:00
}
2013-05-04 08:00:48 +00:00
if (isset($_GET['c'])) {
2013-10-23 08:01:03 +00:00
list($Data, $FileType) = $Cache->get_value('image_cache_'.md5($URL));
2011-03-28 14:21:28 +00:00
$Cached = true;
}
2013-05-04 08:00:48 +00:00
if (!isset($Data) || !$Data) {
2011-03-28 14:21:28 +00:00
$Cached = false;
2013-10-22 08:01:07 +00:00
$Data = @file_get_contents($URL, 0, stream_context_create(array('http' => array('timeout' => 15))));
2013-05-04 08:00:48 +00:00
if (!$Data || empty($Data)) {
2013-07-04 08:00:56 +00:00
img_error('timeout');
2011-03-28 14:21:28 +00:00
}
2013-10-23 08:01:03 +00:00
$FileType = image_type($Data);
if ($FileType && function_exists("imagecreatefrom$FileType")) {
2011-03-28 14:21:28 +00:00
$Image = imagecreatefromstring($Data);
2013-05-04 08:00:48 +00:00
if (invisible($Image)) {
2013-07-04 08:00:56 +00:00
img_error('invisible');
2011-03-28 14:21:28 +00:00
}
2013-05-04 08:00:48 +00:00
if (verysmall($Image)) {
2013-07-04 08:00:56 +00:00
img_error('small');
2011-03-28 14:21:28 +00:00
}
}
2013-04-19 08:00:55 +00:00
if (isset($_GET['c']) && strlen($Data) < 262144) {
2013-10-23 08:01:03 +00:00
$Cache->cache_value('image_cache_'.md5($URL), array($Data, $FileType), 3600 * 24 * 7);
2011-03-28 14:21:28 +00:00
}
}
2013-10-23 08:01:03 +00:00
// Reset avatar, add mod note
function reset_image($UserID, $Type, $AdminComment, $PrivMessage) {
if ($Type === 'avatar') {
$CacheKey = "user_info_$UserID";
$DBTable = 'users_info';
2014-01-30 08:02:17 +00:00
$DBColumn = 'Avatar';
2013-10-23 08:01:03 +00:00
$PMSubject = 'Your avatar has been automatically reset';
} elseif ($Type === 'avatar2') {
$CacheKey = "donor_info_$UserID";
$DBTable = 'donor_rewards';
2014-01-30 08:02:17 +00:00
$DBColumn = 'SecondAvatar';
2013-10-23 08:01:03 +00:00
$PMSubject = 'Your second avatar has been automatically reset';
} elseif ($Type === 'donoricon') {
$CacheKey = "donor_info_$UserID";
$DBTable = 'donor_rewards';
2014-01-30 08:02:17 +00:00
$DBColumn = 'CustomIcon';
2013-10-23 08:01:03 +00:00
$PMSubject = 'Your donor icon has been automatically reset';
}
$UserInfo = G::$Cache->get_value($CacheKey, true);
if ($UserInfo !== false) {
if ($UserInfo[$DBColumn] === '') {
// This image has already been reset
return;
}
$UserInfo[$DBColumn] = '';
G::$Cache->cache_value($CacheKey, $UserInfo, 2592000); // cache for 30 days
}
// reset the avatar or donor icon URL
G::$DB->query("
UPDATE $DBTable
SET $DBColumn = ''
WHERE UserID = '$UserID'");
// write comment to staff notes
G::$DB->query("
UPDATE users_info
SET AdminComment = CONCAT('".sqltime().' - '.db_string($AdminComment)."\n\n', AdminComment)
WHERE UserID = '$UserID'");
// clear cache keys
G::$Cache->delete_value($CacheKey);
Misc::send_pm($UserID, 0, $PMSubject, $PrivMessage);
}
2011-03-28 14:21:28 +00:00
// Enforce avatar rules
2013-10-23 08:01:03 +00:00
if (isset($_GET['type']) && isset($_GET['userid'])) {
$ValidTypes = array('avatar', 'avatar2', 'donoricon');
if (!is_number($_GET['userid']) || !in_array($_GET['type'], $ValidTypes)) {
2013-05-04 08:00:48 +00:00
die();
}
2013-10-23 08:01:03 +00:00
$UserID = $_GET['userid'];
$Type = $_GET['type'];
if ($Type === 'avatar' || $Type === 'avatar2') {
$MaxFileSize = 256 * 1024; // 256 kB
$MaxImageHeight = 400; // pixels
$TypeName = $Type === 'avatar' ? 'avatar' : 'second avatar';
} elseif ($Type === 'donoricon') {
$MaxFileSize = 64 * 1024; // 64 kB
$MaxImageHeight = 100; // pixels
$TypeName = 'donor icon';
}
2013-10-23 08:01:03 +00:00
$Height = image_height($FileType, $Data);
if (strlen($Data) > $MaxFileSize || $Height > $MaxImageHeight) {
2011-03-28 14:21:28 +00:00
// Sometimes the cached image we have isn't the actual image
2013-05-04 08:00:48 +00:00
if ($Cached) {
2013-10-22 08:01:07 +00:00
$Data2 = @file_get_contents($URL, 0, stream_context_create(array('http' => array('timeout' => 15))));
2011-03-28 14:21:28 +00:00
} else {
$Data2 = $Data;
}
2013-10-23 08:01:03 +00:00
if (strlen($Data2) > $MaxFileSize || image_height($FileType, $Data2) > $MaxImageHeight) {
2013-05-27 08:00:58 +00:00
require_once(SERVER_ROOT.'/classes/mysql.class.php');
2013-10-23 08:01:03 +00:00
require_once(SERVER_ROOT.'/classes/time.class.php');
2011-03-28 14:21:28 +00:00
$DBURL = db_string($URL);
2013-10-23 08:01:03 +00:00
$AdminComment = ucfirst($TypeName)." reset automatically (Size: ".number_format((strlen($Data)) / 1024)." kB, Height: ".$Height."px). Used to be $DBURL";
$PrivMessage = SITE_NAME." has the following requirements for {$TypeName}s:\n\n".
"[b]".ucfirst($TypeName)."s must not exceed ".($MaxFileSize / 1024)." kB or be vertically longer than {$MaxImageHeight}px.[/b]\n\n".
"Your $TypeName at $DBURL has been found to exceed these rules. As such, it has been automatically reset. You are welcome to reinstate your $TypeName once it has been resized down to an acceptable size.";
reset_image($UserID, $Type, $AdminComment, $PrivMessage);
2011-03-28 14:21:28 +00:00
}
}
}
2013-10-23 08:01:03 +00:00
if (!isset($FileType)) {
2013-07-04 08:00:56 +00:00
img_error('timeout');
2011-03-28 14:21:28 +00:00
}
2014-04-25 08:00:52 +00:00
header("Content-type: image/$FileType");
2011-03-28 14:21:28 +00:00
echo $Data;