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
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public static function blacklisted($Url) {
|
|
|
|
foreach (self::$Blacklist as &$Value) {
|
|
|
|
if (stripos($Url, $Value)) {
|
|
|
|
$ParsedUrl = parse_url($Url);
|
|
|
|
error($ParsedUrl['host'] . ' is not an allowed image host. Please use a different host.');
|
|
|
|
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;
|
|
|
|
return !empty(self::$Extensions[$Ext]) && self::$Extensions[$Ext] === true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
$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) {
|
|
|
|
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
|
|
|
|
* @return image proxy URL
|
2013-02-25 21:16:55 +00:00
|
|
|
*/
|
|
|
|
public static function proxy_url($Url) {
|
|
|
|
global $SSL;
|
2013-04-30 18:18:07 +00:00
|
|
|
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
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function process($Url, $Thumb = false) {
|
|
|
|
global $LoggedUser;
|
|
|
|
if (empty($Url)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if (($Found = self::get_stored($Url . ($Thumb ? '_thumb' : '')))) {
|
|
|
|
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-04-30 18:18:07 +00:00
|
|
|
if (isset($LoggedUser['Permissions'])) {
|
|
|
|
/*
|
|
|
|
* We only want to apply the proxy and store the processed URL if the
|
|
|
|
* permissions were loaded before. This is necessary because self::process
|
|
|
|
* is used in Users::user_info which is called in script_start.php before
|
|
|
|
* the permissions are loaded, causing the own avatar to always load without
|
|
|
|
* proxy later on.
|
|
|
|
*/
|
|
|
|
if (check_perms('site_proxy_images')) {
|
|
|
|
$ProcessedUrl = self::proxy_url($ProcessedUrl);
|
|
|
|
}
|
2012-06-22 08:00:10 +00:00
|
|
|
|
2013-04-30 18:18:07 +00:00
|
|
|
self::store($Url . ($Thumb ? '_thumb' : ''), $ProcessedUrl);
|
|
|
|
}
|
|
|
|
return $ProcessedUrl;
|
|
|
|
}
|
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
|
|
|
}
|