<?php
class McAvatar_Listener
{
public static function init(XenForo_Dependencies_Abstract $dependencies, array $data)
{
XenForo_Template_Helper_Core::$helperCallbacks['avatar'] = array('McAvatar_Listener', 'helperAvatarUrl');
}
public static function helperAvatarUrl($user, $size, $forceType = null, $canonical = false)
{
if (!is_array($user))
{
$user = array();
}
if ($forceType)
{
switch ($forceType)
{
case 'default':
case 'custom':
break;
default:
$forceType = null;
break;
}
}
$url = self::getAvatarUrl($user, $size, $forceType);
if ($canonical)
{
$url = XenForo_Link::convertUriToAbsoluteUri($url, true);
}
return htmlspecialchars($url);
}
public static function getAvatarUrl(array $user, $size, $forceType = '')
{
if (!empty($user['user_id']) && $forceType != 'default')
{
if ($user['gravatar'] && $forceType != 'custom')
{
return self::_getGravatarUrl($user, $size);
}
else if (!empty($user['avatar_date']))
{
return self::_getCustomAvatarUrl($user, $size);
}
}
return self::_getDefaultAvatarUrl($user, $size);
}
public static function getAvatarUrls(array $user)
{
$urls = array();
foreach (XenForo_Model_Avatar::getSizes() AS $sizeCode => $maxDimensions)
{
$urls[$sizeCode] = self::getAvatarUrl($user, $sizeCode);
}
return $urls;
}
protected static function _getDefaultAvatarUrl(array $user, $size)
{
if ($user['is_banned'])
{
$baimg = imagecreatefrompng("http://minotar.net/helm/{$user['username']}/".XenForo_Model_Avatar::getSizeFromCode($size).".png");
$imgsize = getimagesize("http://minotar.net/helm/{$user['username']}/".XenForo_Model_Avatar::getSizeFromCode($size).".png");
imagesetthickness($baimg, 3);
imageline($baimg, 0, 0, $imgsize[0], $imgsize[1], $gray);
imageline($baimg, 0, $imgsize[1], $imgsize[0], 0, $gray);
imagefilter($baimg, IMG_FILTER_GRAYSCALE);
imagepng($baimg,"images/banned_{$user['username']}".XenForo_Model_Avatar::getSizeFromCode($size).".png");
return "images/banned_{$user['username']}".XenForo_Model_Avatar::getSizeFromCode($size).".png";
imagedestroy($baimg);
}
else
{
return "http://minotar.net/helm/{$user['username']}/".XenForo_Model_Avatar::getSizeFromCode($size).".png";
}
}
protected static function _getCustomAvatarUrl(array $user, $size)
{
$group = floor($user['user_id'] / 1000);
if($user['is_banned'])
{
$baimg = imagecreatefromjpeg(XenForo_Application::$externalDataUrl."/avatars/$size/$group/$user[user_id].jpg");
$imgsize = getimagesize(XenForo_Application::$externalDataUrl."/avatars/$size/$group/$user[user_id].jpg");
imagesetthickness($baimg, 3);
imageline($baimg, 0, 0, $imgsize[0], $imgsize[1], $gray);
imageline($baimg, 0, $imgsize[1], $imgsize[0], 0, $gray);
imagefilter($baimg, IMG_FILTER_GRAYSCALE);
imagepng($baimg,"images/banned_{$user['username']}".XenForo_Model_Avatar::getSizeFromCode($size).".png");
return "images/banned_{$user['username']}".XenForo_Model_Avatar::getSizeFromCode($size).".png";
imagedestroy($baimg);
}
else
{
return XenForo_Application::$externalDataUrl . "/avatars/$size/$group/$user[user_id].jpg?$user[avatar_date]";
}
}
protected static function _getGravatarUrl(array $user, $size, $default = '')
{
$md5 = md5($user['gravatar']);
if ($default === '')
{
$default = '&d=' . urlencode(XenForo_Application::get('options')->boardUrl . '/' . self::_getDefaultAvatarUrl($user, $size));
}
else if (!empty($default))
{
$default = '&d=' . urlencode($default);
}
if (is_string($size))
{
$size = XenForo_Model_Avatar::getSizeFromCode($size);
}
return (XenForo_Application::$secure ? 'https://secure' : 'http://www')
. ".gravatar.com/avatar/{$md5}.jpg?s={$size}{$default}";
}
}
?>