2 namespace wcf\data\user\avatar
;
3 use wcf\system\exception\SystemException
;
4 use wcf\system\request\LinkHandler
;
9 * Represents a gravatar.
12 * @copyright 2001-2014 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @package com.woltlab.wcf
15 * @subpackage data.user.avatar
16 * @category Community Framework
17 * @see http://www.gravatar.com
19 class Gravatar
extends DefaultAvatar
{
24 const GRAVATAR_BASE
= 'http://gravatar.com/avatar/%s?s=%d&r=g&d=%s';
27 * gravatar local cache location
30 const GRAVATAR_CACHE_LOCATION
= 'images/avatars/gravatars/%s-%s.png';
33 * gravatar expire time (days)
36 const GRAVATAR_CACHE_EXPIRE
= 7;
45 * gravatar e-mail address
48 public $gravatar = '';
51 * urls of this gravatar
54 protected $url = array();
57 * Creates a new Gravatar object.
59 * @param integer $userID
60 * @param string $gravatar
62 public function __construct($userID, $gravatar) {
63 $this->userID
= $userID;
64 $this->gravatar
= $gravatar;
68 * @see \wcf\data\user\avatar\IUserAvatar::getURL()
70 public function getURL($size = null) {
71 if ($size === null) $size = $this->size
;
73 if (!isset($this->url
[$size])) {
74 // try to use cached gravatar
75 $cachedFilename = sprintf(self
::GRAVATAR_CACHE_LOCATION
, md5(mb_strtolower($this->gravatar
)), $size);
76 if (file_exists(WCF_DIR
.$cachedFilename) && filemtime(WCF_DIR
.$cachedFilename) > (TIME_NOW
- (self
::GRAVATAR_CACHE_EXPIRE
* 86400))) {
77 $this->url
[$size] = WCF
::getPath().$cachedFilename;
80 $this->url
[$size] = LinkHandler
::getInstance()->getLink('GravatarDownload', array(
81 'forceFrontend' => true
82 ), 'userID='.$this->userID
.'&size='.$size);
86 return $this->url
[$size];
90 * Checks a given email address for gravatar support.
92 * @param string $email
95 public static function test($email) {
96 $gravatarURL = sprintf(self
::GRAVATAR_BASE
, md5(mb_strtolower($email)), 80, '404');
98 $tmpFile = FileUtil
::downloadFileFromHttp($gravatarURL, 'gravatar');
102 catch (SystemException
$e) {
108 * @see \wcf\data\user\avatar\IUserAvatar::canCrop()
110 public function canCrop() {