From 5e6c039b08f3c0c90329201b4eb083e637405992 Mon Sep 17 00:00:00 2001 From: Vinrobot Date: Thu, 8 Nov 2018 09:14:11 +0100 Subject: [PATCH] Added config to change Gravatar URL --- .env.example | 5 +++++ app/Auth/UserRepo.php | 2 +- app/Uploads/ImageService.php | 14 ++++++++++---- config/services.php | 1 + 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index eda20ea26..06394f54f 100644 --- a/.env.example +++ b/.env.example @@ -61,6 +61,11 @@ DISCORD_APP_SECRET=false # External services such as Gravatar and Draw.IO DISABLE_EXTERNAL_SERVICES=false +# Default GRAVATAR_URL set to Gravatar service +GRAVATAR_URL=false +# To use a different service to get user's avatar like libravatar +# Possible placeholders: %{hash} %{size} %{email} +#GRAVATAR_URL=https://seccdn.libravatar.org/avatar/%{hash}?s=%{size}&d=identicon # LDAP Settings LDAP_SERVER=false diff --git a/app/Auth/UserRepo.php b/app/Auth/UserRepo.php index 7c88badb8..abff7c641 100644 --- a/app/Auth/UserRepo.php +++ b/app/Auth/UserRepo.php @@ -251,7 +251,7 @@ class UserRepo } try { - $avatar = Images::saveUserGravatar($user); + $avatar = Images::saveUserGravatar($user, config('services.gravatar_url')); $user->avatar()->associate($avatar); $user->save(); return true; diff --git a/app/Uploads/ImageService.php b/app/Uploads/ImageService.php index f109db600..b65a476f4 100644 --- a/app/Uploads/ImageService.php +++ b/app/Uploads/ImageService.php @@ -281,16 +281,22 @@ class ImageService extends UploadService /** * Save a gravatar image and set a the profile image for a user. * @param \BookStack\Auth\User $user + * @param null|string $gravatarUrl * @param int $size * @return mixed * @throws Exception */ - public function saveUserGravatar(User $user, $size = 500) + public function saveUserGravatar(User $user, $gravatarUrl, $size = 500) { - $emailHash = md5(strtolower(trim($user->email))); - $url = 'https://www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon'; + if (!is_string($gravatarUrl) || empty($gravatarUrl)) { + $gravatarUrl = 'https://www.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon'; + } + $email = strtolower(trim($user->email)); + $gravatarUrl = str_replace('%{hash}', md5($email), $gravatarUrl); + $gravatarUrl = str_replace('%{size}', $size, $gravatarUrl); + $gravatarUrl = str_replace('%{email}', urlencode($email), $gravatarUrl); $imageName = str_replace(' ', '-', $user->name . '-gravatar.png'); - $image = $this->saveNewFromUrl($url, 'user', $imageName); + $image = $this->saveNewFromUrl($gravatarUrl, 'user', $imageName); $image->created_by = $user->id; $image->updated_by = $user->id; $image->save(); diff --git a/config/services.php b/config/services.php index 711040386..b8f152124 100644 --- a/config/services.php +++ b/config/services.php @@ -19,6 +19,7 @@ return [ 'gravatar' => env('GRAVATAR', !env('DISABLE_EXTERNAL_SERVICES', false)), 'drawio' => env('DRAWIO', !env('DISABLE_EXTERNAL_SERVICES', false)), + 'gravatar_url' => env('GRAVATAR_URL', false), 'callback_url' => env('APP_URL', false),