2013-02-25 21:16:55 +00:00
|
|
|
<?php
|
|
|
|
|
2012-06-18 08:00:14 +00:00
|
|
|
/**
|
2013-02-25 21:16:55 +00:00
|
|
|
* ImageTools Class
|
|
|
|
* Thumbnail aide, mostly
|
|
|
|
*/
|
|
|
|
class ImageTools {
|
|
|
|
/**
|
|
|
|
* Store processed links to avoid repetition
|
|
|
|
* @var array 'URL' => 'Parsed URL'
|
|
|
|
*/
|
2013-04-30 18:18:07 +00:00
|
|
|
private static $Storage = array();
|
2012-06-18 08:00:14 +00:00
|
|
|
|
2013-02-25 21:16:55 +00:00
|
|
|
/**
|
|
|
|
* We use true as an extra property to make the domain an array key
|
|
|
|
* @var array $Hosts Array of image hosts
|
|
|
|
*/
|
2013-04-30 18:18:07 +00:00
|
|
|
private static $Hosts = array(
|
2013-02-25 21:16:55 +00:00
|
|
|
'whatimg.com' => true,
|
|
|
|
'imgur.com' => true
|
|
|
|
);
|
2013-02-22 08:00:24 +00:00
|
|
|
|
2013-02-25 21:16:55 +00:00
|
|
|
/**
|
|
|
|
* Blacklisted sites
|
|
|
|
* @var array $Blacklist Array of blacklisted hosts
|
|
|
|
*/
|
2013-04-30 18:18:07 +00:00
|
|
|
private static $Blacklist = array(
|
2013-02-25 21:16:55 +00:00
|
|
|
'tinypic.com'
|
|
|
|
);
|
2013-02-22 08:00:24 +00:00
|
|
|
|
2013-02-25 21:16:55 +00:00
|
|
|
/**
|
|
|
|
* Array of image hosts that provide thumbnailing
|
|
|
|
* @var array $Thumbs
|
|
|
|
*/
|
2013-04-30 18:18:07 +00:00
|
|
|
private static $Thumbs = array(
|
2013-02-25 21:16:55 +00:00
|
|
|
'i.imgur.com' => true,
|
|
|
|
'whatimg.com' => true
|
|
|
|
);
|
2013-02-22 08:00:24 +00:00
|
|
|
|
2013-02-25 21:16:55 +00:00
|
|
|
/**
|
|
|
|
* Array of extensions
|
|
|
|
* @var array $Extensions
|
|
|
|
*/
|
2013-04-30 18:18:07 +00:00
|
|
|
private static $Extensions = array(
|
2013-02-25 21:16:55 +00:00
|
|
|
'jpg' => true,
|
|
|
|
'jpeg' => true,
|
|
|
|
'png' => true,
|
|
|
|
'gif' => true
|
|
|
|
);
|
|
|
|
|
2013-10-23 08:01:03 +00:00
|
|
|
/**
|
|
|
|
* Array of user IDs whose avatars have been checked for size
|
|
|
|
* @var array $CheckedAvatars
|
|
|
|
*/
|
|
|
|
private static $CheckedAvatars = array();
|
|
|
|
private static $CheckedAvatars2 = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of user IDs whose donor icons have been checked for size
|
|
|
|
* @var array $CheckedDonorIcons
|
|
|
|
*/
|
|
|
|
private static $CheckedDonorIcons = array();
|
|
|
|
|
2013-02-25 21:16:55 +00:00
|
|
|
/**
|
|
|
|
* Checks from our list of valid hosts
|
|
|
|
* @param string $Host Domain/host to check
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function valid_host($Host) {
|
|
|
|
return !empty(self::$Hosts[$Host]) && self::$Hosts[$Host] === true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a link's host is (not) good, otherwise displays an error.
|
|
|
|
* @param string $Url Link to an image
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-05-27 08:00:58 +00:00
|
|
|
public static function blacklisted($Url, $ShowError = true) {
|
2013-02-25 21:16:55 +00:00
|
|
|
foreach (self::$Blacklist as &$Value) {
|
2013-06-24 08:00:28 +00:00
|
|
|
$Blacklisted = stripos($Url, $Value);
|
|
|
|
if ($Blacklisted !== false) {
|
2013-02-25 21:16:55 +00:00
|
|
|
$ParsedUrl = parse_url($Url);
|
2013-05-30 08:00:30 +00:00
|
|
|
if ($ShowError) {
|
2013-05-27 08:00:58 +00:00
|
|
|
error($ParsedUrl['host'] . ' is not an allowed image host. Please use a different host.');
|
|
|
|
}
|
2013-02-25 21:16:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks to see if a link has a thumbnail
|
|
|
|
* @param string $Url Link to an image
|
|
|
|
* @return string|false Matched host or false
|
|
|
|
*/
|
2013-04-30 18:18:07 +00:00
|
|
|
private static function thumbnailable($Url) {
|
2013-02-25 21:16:55 +00:00
|
|
|
$ParsedUrl = parse_url($Url);
|
|
|
|
return !empty(self::$Thumbs[$ParsedUrl['host']]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks an extension
|
|
|
|
* @param string $Ext Extension to check
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-04-30 18:18:07 +00:00
|
|
|
private static function valid_extension($Ext) {
|
2013-02-25 21:16:55 +00:00
|
|
|
// return @self::$Extensions[$Ext] === true;
|
2013-06-24 08:00:28 +00:00
|
|
|
return !empty(self::$Extensions[$Ext]) && (self::$Extensions[$Ext] === true);
|
2013-02-25 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores a link with a (thumbnail) link
|
|
|
|
* @param type $Link
|
|
|
|
* @param type $Processed
|
|
|
|
*/
|
2013-04-30 18:18:07 +00:00
|
|
|
private static function store($Link, $Processed) {
|
2013-02-25 21:16:55 +00:00
|
|
|
self::$Storage[$Link] = $Processed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves an entry from our storage
|
|
|
|
* @param type $Link
|
|
|
|
* @return boolean|string Returns false if no match
|
|
|
|
*/
|
2013-04-30 18:18:07 +00:00
|
|
|
private static function get_stored($Link) {
|
2013-02-25 21:16:55 +00:00
|
|
|
if (isset(self::$Storage[$Link])) {
|
|
|
|
return self::$Storage[$Link];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-30 18:18:07 +00:00
|
|
|
* Checks if URL points to a whatimg thumbnail.
|
2013-02-25 21:16:55 +00:00
|
|
|
*/
|
2013-04-30 18:18:07 +00:00
|
|
|
private static function has_whatimg_thumb($Url) {
|
|
|
|
return (strpos($Url, '_thumb') !== false);
|
2013-02-25 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-30 18:18:07 +00:00
|
|
|
* Cleans up imgur URL if it already has a modifier attached to the end of it.
|
|
|
|
*/
|
|
|
|
private static function clean_imgur_url($Url) {
|
2013-06-13 08:01:05 +00:00
|
|
|
$Extension = pathinfo($Url, PATHINFO_EXTENSION);
|
|
|
|
$Full = preg_replace('/\.[^.]*$/', '', $Url);
|
|
|
|
$Base = substr($Full, 0, strrpos($Full, '/'));
|
|
|
|
$Path = substr($Full, strrpos($Full, '/') + 1);
|
|
|
|
if (strlen($Path) == 6) {
|
|
|
|
$Last = $Path[strlen($Path) - 1];
|
|
|
|
if ($Last == 'm' || $Last == 'l' || $Last == 's' || $Last == 'h' || $Last == 'b') {
|
|
|
|
$Path = substr($Path, 0, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "$Base/$Path.$Extension";
|
2013-02-25 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-30 18:18:07 +00:00
|
|
|
* Replaces the extension.
|
2013-02-25 21:16:55 +00:00
|
|
|
*/
|
2013-04-30 18:18:07 +00:00
|
|
|
private static function replace_extension($String, $Extension) {
|
2013-06-13 08:01:05 +00:00
|
|
|
return preg_replace('/\.[^.]*$/', $Extension, $String);
|
2013-02-25 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create image proxy URL
|
2013-03-05 08:00:26 +00:00
|
|
|
* @param string $Url image URL
|
2013-10-23 08:01:03 +00:00
|
|
|
* @param bool/string $CheckSize - accepts one of false, "avatar", "avatar2", or "donoricon"
|
|
|
|
* @param bool/string/number $UserID - user ID for avatars and donor icons
|
2013-03-05 08:00:26 +00:00
|
|
|
* @return image proxy URL
|
2013-02-25 21:16:55 +00:00
|
|
|
*/
|
2013-10-23 08:01:03 +00:00
|
|
|
public static function proxy_url($Url, $CheckSize, $UserID, &$ExtraInfo) {
|
2013-02-25 21:16:55 +00:00
|
|
|
global $SSL;
|
2013-10-23 08:01:03 +00:00
|
|
|
|
|
|
|
if ($UserID) {
|
|
|
|
$ExtraInfo = "&userid=$UserID";
|
|
|
|
if ($CheckSize === 'avatar' && !isset(self::$CheckedAvatars[$UserID])) {
|
|
|
|
$ExtraInfo .= "&type=$CheckSize";
|
|
|
|
self::$CheckedAvatars[$UserID] = true;
|
|
|
|
} elseif ($CheckSize === 'avatar2' && !isset(self::$CheckedAvatars2[$UserID])) {
|
|
|
|
$ExtraInfo .= "&type=$CheckSize";
|
|
|
|
self::$CheckedAvatars2[$UserID] = true;
|
|
|
|
} elseif ($CheckSize === 'donoricon' && !isset(self::$CheckedDonorIcons[$UserID])) {
|
|
|
|
$ExtraInfo .= "&type=$CheckSize";
|
|
|
|
self::$CheckedDonorIcons[$UserID] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($SSL ? 'https' : 'http') . '://' . SITE_URL . "/image.php?c=1&i=" . urlencode($Url);
|
2013-02-25 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2013-04-30 18:18:07 +00:00
|
|
|
/**
|
|
|
|
* Determine the image URL. This takes care of the image proxy and thumbnailing.
|
|
|
|
* @param string $Url
|
|
|
|
* @param bool $Thumb
|
2013-10-23 08:01:03 +00:00
|
|
|
* @param bool/string $CheckSize - accepts one of false, "avatar", "avatar2", or "donoricon"
|
|
|
|
* @param bool/string/number $UserID - user ID for avatars and donor icons
|
2013-04-30 18:18:07 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-10-23 08:01:03 +00:00
|
|
|
public static function process($Url, $Thumb = false, $CheckSize = false, $UserID = false) {
|
2013-04-30 18:18:07 +00:00
|
|
|
if (empty($Url)) {
|
|
|
|
return '';
|
|
|
|
}
|
2013-10-23 08:01:03 +00:00
|
|
|
|
2013-10-22 08:01:07 +00:00
|
|
|
if ($Found = self::get_stored($Url . ($Thumb ? '_thumb' : ''))) {
|
2013-04-30 18:18:07 +00:00
|
|
|
return $Found;
|
|
|
|
}
|
2013-02-25 21:16:55 +00:00
|
|
|
|
2013-04-30 18:18:07 +00:00
|
|
|
$ProcessedUrl = $Url;
|
|
|
|
if ($Thumb) {
|
|
|
|
$Extension = pathinfo($Url, PATHINFO_EXTENSION);
|
|
|
|
if (self::thumbnailable($Url) && self::valid_extension($Extension)) {
|
|
|
|
if (strpos($Url, 'whatimg') !== false && !self::has_whatimg_thumb($Url)) {
|
|
|
|
$ProcessedUrl = self::replace_extension($Url, '_thumb.' . $Extension);
|
|
|
|
} elseif (strpos($Url, 'imgur') !== false) {
|
|
|
|
$ProcessedUrl = self::replace_extension(self::clean_imgur_url($Url), 'm.' . $Extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-18 08:00:14 +00:00
|
|
|
|
2013-10-23 08:01:03 +00:00
|
|
|
$ExtraInfo = '';
|
2013-05-01 08:00:16 +00:00
|
|
|
if (check_perms('site_proxy_images')) {
|
2013-10-23 08:01:03 +00:00
|
|
|
$ProcessedUrl = self::proxy_url($ProcessedUrl, $CheckSize, $UserID, $ExtraInfo);
|
2013-04-30 18:18:07 +00:00
|
|
|
}
|
2013-05-01 08:00:16 +00:00
|
|
|
self::store($Url . ($Thumb ? '_thumb' : ''), $ProcessedUrl);
|
2013-10-23 08:01:03 +00:00
|
|
|
return $ProcessedUrl . $ExtraInfo;
|
2013-04-30 18:18:07 +00:00
|
|
|
}
|
2012-06-22 08:00:10 +00:00
|
|
|
|
2013-04-30 18:18:07 +00:00
|
|
|
/**
|
|
|
|
* Cover art thumbnail in browse, on artist pages etc.
|
|
|
|
* @global array $CategoryIcons
|
|
|
|
* @param string $Url
|
|
|
|
* @param int $CategoryID
|
|
|
|
*/
|
|
|
|
public static function cover_thumb($Url, $CategoryID) {
|
|
|
|
global $CategoryIcons;
|
|
|
|
if ($Url) {
|
|
|
|
$Src = self::process($Url, true);
|
|
|
|
$Lightbox = self::process($Url);
|
|
|
|
} else {
|
|
|
|
$Src = STATIC_SERVER . 'common/noartwork/' . $CategoryIcons[$CategoryID - 1];
|
|
|
|
$Lightbox = $Src;
|
2013-04-18 08:00:54 +00:00
|
|
|
}
|
2013-04-30 18:18:07 +00:00
|
|
|
?>
|
|
|
|
<img src="<?=$Src?>" width="90" height="90" alt="Cover" onclick="lightbox.init('<?=$Lightbox?>', 90)" />
|
|
|
|
<?
|
2013-04-18 08:00:54 +00:00
|
|
|
}
|
2012-06-18 08:00:14 +00:00
|
|
|
}
|