From 4ae7fdc10c2e7786daba4b44dc4c93df958008f6 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Sat, 6 Dec 2014 20:23:24 +0100 Subject: [PATCH] Fixed wrong gravatar file extensions --- com.woltlab.wcf/update_2.1.0_alpha_1.sql | 2 + .../action/GravatarDownloadAction.class.php | 41 ++++++++++++++++--- .../files/lib/data/user/UserProfile.class.php | 2 +- .../lib/data/user/avatar/Gravatar.class.php | 13 ++++-- .../user/follow/UserFollowerList.class.php | 2 +- .../visitor/UserProfileVisitorList.class.php | 2 +- wcfsetup/setup/db/install.sql | 1 + 7 files changed, 52 insertions(+), 11 deletions(-) diff --git a/com.woltlab.wcf/update_2.1.0_alpha_1.sql b/com.woltlab.wcf/update_2.1.0_alpha_1.sql index 410eca00b8..36ef05db94 100644 --- a/com.woltlab.wcf/update_2.1.0_alpha_1.sql +++ b/com.woltlab.wcf/update_2.1.0_alpha_1.sql @@ -205,6 +205,8 @@ ALTER TABLE wcf1_template_listener ADD niceValue TINYINT(3) NOT NULL DEFAULT 0; ALTER TABLE wcf1_user_group_option ADD usersOnly TINYINT(1) NOT NULL DEFAULT 0; +ALTER TABLE wcf1_user ADD gravatarFileExtension VARCHAR(3) NOT NULL DEFAULT ''; + /* truncate table to ensure consistency */ DELETE FROM wcf1_user_notification; diff --git a/wcfsetup/install/files/lib/action/GravatarDownloadAction.class.php b/wcfsetup/install/files/lib/action/GravatarDownloadAction.class.php index 00aa0fde87..dde6d5e66d 100644 --- a/wcfsetup/install/files/lib/action/GravatarDownloadAction.class.php +++ b/wcfsetup/install/files/lib/action/GravatarDownloadAction.class.php @@ -7,6 +7,7 @@ use wcf\data\user\UserEditor; use wcf\system\exception\IllegalLinkException; use wcf\system\exception\SystemException; use wcf\util\FileUtil; +use wcf\util\HTTPRequest; /** * Downloads and caches gravatars. @@ -64,8 +65,10 @@ class GravatarDownloadAction extends AbstractAction { parent::execute(); if ($this->user->enableGravatar) { + $fileExtension = ($this->user->gravatarFileExtension ?: 'png'); + // try to use cached gravatar - $cachedFilename = sprintf(Gravatar::GRAVATAR_CACHE_LOCATION, md5(mb_strtolower($this->user->email)), $this->size); + $cachedFilename = sprintf(Gravatar::GRAVATAR_CACHE_LOCATION, md5(mb_strtolower($this->user->email)), $this->size, $fileExtension); if (file_exists(WCF_DIR.$cachedFilename) && filemtime(WCF_DIR.$cachedFilename) > (TIME_NOW - (Gravatar::GRAVATAR_CACHE_EXPIRE * 86400))) { @header('Content-Type: image/png'); @readfile(WCF_DIR.$cachedFilename); @@ -75,12 +78,40 @@ class GravatarDownloadAction extends AbstractAction { // try to download new version $gravatarURL = sprintf(Gravatar::GRAVATAR_BASE, md5(mb_strtolower($this->user->email)), $this->size, GRAVATAR_DEFAULT_TYPE); try { - $tmpFile = FileUtil::downloadFileFromHttp($gravatarURL, 'gravatar'); - copy($tmpFile, WCF_DIR.$cachedFilename); - @unlink($tmpFile); + $request = new HTTPRequest($gravatarURL); + $request->execute(); + $reply = $request->getReply(); + + // get mime type and file extension + $fileExtension = 'png'; + $mimeType = 'image/png'; + if (isset($reply['headers']['Content-Type'])) { + switch ($reply['headers']['Content-Type']) { + case 'image/jpeg': + $mimeType = 'image/jpeg'; + $fileExtension = 'jpg'; + break; + case 'image/gif': + $mimeType = 'image/gif'; + $fileExtension = 'gif'; + break; + } + } + + // save file + $cachedFilename = sprintf(Gravatar::GRAVATAR_CACHE_LOCATION, md5(mb_strtolower($this->user->email)), $this->size, $fileExtension); + file_put_contents(WCF_DIR.$cachedFilename, $reply['body']); FileUtil::makeWritable(WCF_DIR.$cachedFilename); - @header('Content-Type: image/png'); + // file file extension + if ($fileExtension != $this->user->gravatarFileExtension) { + $editor = new UserEditor($this->user); + $editor->update(array( + 'gravatarFileExtension' => $fileExtension + )); + } + + @header('Content-Type: '.$mimeType); @readfile(WCF_DIR.$cachedFilename); exit; } diff --git a/wcfsetup/install/files/lib/data/user/UserProfile.class.php b/wcfsetup/install/files/lib/data/user/UserProfile.class.php index 8a65a1cd12..6328236650 100644 --- a/wcfsetup/install/files/lib/data/user/UserProfile.class.php +++ b/wcfsetup/install/files/lib/data/user/UserProfile.class.php @@ -267,7 +267,7 @@ class UserProfile extends DatabaseObjectDecorator implements IBreadcrumbProvider } } else if (MODULE_GRAVATAR && $this->enableGravatar) { - $this->avatar = new Gravatar($this->userID, $this->email); + $this->avatar = new Gravatar($this->userID, $this->email, ($this->gravatarFileExtension ?: 'png')); } } } diff --git a/wcfsetup/install/files/lib/data/user/avatar/Gravatar.class.php b/wcfsetup/install/files/lib/data/user/avatar/Gravatar.class.php index cc242cd161..34ae68462b 100644 --- a/wcfsetup/install/files/lib/data/user/avatar/Gravatar.class.php +++ b/wcfsetup/install/files/lib/data/user/avatar/Gravatar.class.php @@ -28,7 +28,7 @@ class Gravatar extends DefaultAvatar { * gravatar local cache location * @var string */ - const GRAVATAR_CACHE_LOCATION = 'images/avatars/gravatars/%s-%s.png'; + const GRAVATAR_CACHE_LOCATION = 'images/avatars/gravatars/%s-%s.%s'; /** * gravatar expire time (days) @@ -48,6 +48,12 @@ class Gravatar extends DefaultAvatar { */ public $gravatar = ''; + /** + * file extension of the gravatar image + * @var string + */ + public $fileExtension = 'png'; + /** * urls of this gravatar * @var array @@ -60,9 +66,10 @@ class Gravatar extends DefaultAvatar { * @param integer $userID * @param string $gravatar */ - public function __construct($userID, $gravatar) { + public function __construct($userID, $gravatar, $fileExtension = 'png') { $this->userID = $userID; $this->gravatar = $gravatar; + $this->fileExtension = $fileExtension; } /** @@ -85,7 +92,7 @@ class Gravatar extends DefaultAvatar { if (!isset($this->url[$size])) { // try to use cached gravatar - $cachedFilename = sprintf(self::GRAVATAR_CACHE_LOCATION, md5(mb_strtolower($this->gravatar)), $size); + $cachedFilename = sprintf(self::GRAVATAR_CACHE_LOCATION, md5(mb_strtolower($this->gravatar)), $size, $this->fileExtension); if (file_exists(WCF_DIR.$cachedFilename) && filemtime(WCF_DIR.$cachedFilename) > (TIME_NOW - (self::GRAVATAR_CACHE_EXPIRE * 86400))) { $this->url[$size] = WCF::getPath().$cachedFilename; } diff --git a/wcfsetup/install/files/lib/data/user/follow/UserFollowerList.class.php b/wcfsetup/install/files/lib/data/user/follow/UserFollowerList.class.php index 18f74b6d61..0d65a00566 100644 --- a/wcfsetup/install/files/lib/data/user/follow/UserFollowerList.class.php +++ b/wcfsetup/install/files/lib/data/user/follow/UserFollowerList.class.php @@ -34,7 +34,7 @@ class UserFollowerList extends UserFollowList { public function __construct() { parent::__construct(); - $this->sqlSelects .= "user_table.username, user_table.email, user_table.disableAvatar, user_table.enableGravatar"; + $this->sqlSelects .= "user_table.username, user_table.email, user_table.disableAvatar, user_table.enableGravatar, user_table.gravatarFileExtension"; $this->sqlSelects .= ", user_avatar.*"; $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user user_table ON (user_table.userID = user_follow.userID)"; diff --git a/wcfsetup/install/files/lib/data/user/profile/visitor/UserProfileVisitorList.class.php b/wcfsetup/install/files/lib/data/user/profile/visitor/UserProfileVisitorList.class.php index 81acbd0a92..86a78037f6 100644 --- a/wcfsetup/install/files/lib/data/user/profile/visitor/UserProfileVisitorList.class.php +++ b/wcfsetup/install/files/lib/data/user/profile/visitor/UserProfileVisitorList.class.php @@ -34,7 +34,7 @@ class UserProfileVisitorList extends DatabaseObjectList { public function __construct() { parent::__construct(); - $this->sqlSelects .= "user_table.username, user_table.email, user_table.disableAvatar, user_table.enableGravatar"; + $this->sqlSelects .= "user_table.username, user_table.email, user_table.disableAvatar, user_table.enableGravatar, user_table.gravatarFileExtension"; $this->sqlSelects .= ", user_avatar.*"; $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user user_table ON (user_table.userID = user_profile_visitor.userID)"; diff --git a/wcfsetup/setup/db/install.sql b/wcfsetup/setup/db/install.sql index f84adb707d..bcd0b80289 100644 --- a/wcfsetup/setup/db/install.sql +++ b/wcfsetup/setup/db/install.sql @@ -1102,6 +1102,7 @@ CREATE TABLE wcf1_user ( disableAvatarReason TEXT, disableAvatarExpires INT(10) NOT NULL DEFAULT 0, enableGravatar TINYINT(1) NOT NULL DEFAULT 0, + gravatarFileExtension VARCHAR(3) NOT NULL DEFAULT '', signature TEXT, signatureEnableBBCodes TINYINT(1) NOT NULL DEFAULT 1, signatureEnableHtml TINYINT(1) NOT NULL DEFAULT 0, -- 2.20.1