Fixed wrong gravatar file extensions
authorMarcel Werk <burntime@woltlab.com>
Sat, 6 Dec 2014 19:23:24 +0000 (20:23 +0100)
committerMarcel Werk <burntime@woltlab.com>
Sat, 6 Dec 2014 19:23:24 +0000 (20:23 +0100)
com.woltlab.wcf/update_2.1.0_alpha_1.sql
wcfsetup/install/files/lib/action/GravatarDownloadAction.class.php
wcfsetup/install/files/lib/data/user/UserProfile.class.php
wcfsetup/install/files/lib/data/user/avatar/Gravatar.class.php
wcfsetup/install/files/lib/data/user/follow/UserFollowerList.class.php
wcfsetup/install/files/lib/data/user/profile/visitor/UserProfileVisitorList.class.php
wcfsetup/setup/db/install.sql

index 410eca00b8777f91fb1acb24bd35860f67562a2c..36ef05db94b5b5531bcb02b5ae62cec5d99fee47 100644 (file)
@@ -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;
 
index 00aa0fde87ad0abac1f2584368d90e3627131152..dde6d5e66d697e69ea29cb7eac3020fdbb8ab907 100644 (file)
@@ -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;
                        }
index 8a65a1cd12a5c679461845ca49e7eafda6dd57fd..6328236650e1ae1d0ebefb46ae33f36dfe522970 100644 (file)
@@ -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'));
                                        }
                                }
                        }
index cc242cd161e066847489db1728643406957ffc84..34ae68462b97dacb75c039f4e02ce3c03c5c078f 100644 (file)
@@ -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<string>
@@ -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;
                        }
index 18f74b6d61721d602999a75fae0d7278cd796950..0d65a005665fc68f9c6efa033195fe7ac4dc4d4b 100644 (file)
@@ -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)";
index 81acbd0a92c5b3be7312c89bbae145b609f6c802..86a78037f6bd7b39005bdff168b6d2b1a094d7e7 100644 (file)
@@ -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)";
index f84adb707dbfed9c1a7697e76ad72a05962b9b22..bcd0b802898a598404891f5e78c57cecf5fd17ec 100644 (file)
@@ -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,