Skip to content

Commit d673bf6

Browse files
committed
Merge branch 'custom-avatar-provider' of git://github.com/Vinrobot/BookStack into Vinrobot-custom-avatar-provider
2 parents 18b1015 + 5e6c039 commit d673bf6

4 files changed

Lines changed: 17 additions & 5 deletions

File tree

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ DISCORD_APP_SECRET=false
6262

6363
# External services such as Gravatar and Draw.IO
6464
DISABLE_EXTERNAL_SERVICES=false
65+
# Default GRAVATAR_URL set to Gravatar service
66+
GRAVATAR_URL=false
67+
# To use a different service to get user's avatar like libravatar
68+
# Possible placeholders: %{hash} %{size} %{email}
69+
#GRAVATAR_URL=https://seccdn.libravatar.org/avatar/%{hash}?s=%{size}&d=identicon
6570

6671
# LDAP Settings
6772
LDAP_SERVER=false

app/Auth/UserRepo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public function downloadGravatarToUserAvatar(User $user)
251251
}
252252

253253
try {
254-
$avatar = Images::saveUserGravatar($user);
254+
$avatar = Images::saveUserGravatar($user, config('services.gravatar_url'));
255255
$user->avatar()->associate($avatar);
256256
$user->save();
257257
return true;

app/Uploads/ImageService.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,22 @@ protected function destroyImagesFromPath(string $path)
281281
/**
282282
* Save a gravatar image and set a the profile image for a user.
283283
* @param \BookStack\Auth\User $user
284+
* @param null|string $gravatarUrl
284285
* @param int $size
285286
* @return mixed
286287
* @throws Exception
287288
*/
288-
public function saveUserGravatar(User $user, $size = 500)
289+
public function saveUserGravatar(User $user, $gravatarUrl, $size = 500)
289290
{
290-
$emailHash = md5(strtolower(trim($user->email)));
291-
$url = 'https://www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon';
291+
if (!is_string($gravatarUrl) || empty($gravatarUrl)) {
292+
$gravatarUrl = 'https://www.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon';
293+
}
294+
$email = strtolower(trim($user->email));
295+
$gravatarUrl = str_replace('%{hash}', md5($email), $gravatarUrl);
296+
$gravatarUrl = str_replace('%{size}', $size, $gravatarUrl);
297+
$gravatarUrl = str_replace('%{email}', urlencode($email), $gravatarUrl);
292298
$imageName = str_replace(' ', '-', $user->name . '-gravatar.png');
293-
$image = $this->saveNewFromUrl($url, 'user', $imageName);
299+
$image = $this->saveNewFromUrl($gravatarUrl, 'user', $imageName);
294300
$image->created_by = $user->id;
295301
$image->updated_by = $user->id;
296302
$image->save();

config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'gravatar' => env('GRAVATAR', !env('DISABLE_EXTERNAL_SERVICES', false)),
2020
'drawio' => env('DRAWIO', !env('DISABLE_EXTERNAL_SERVICES', false)),
2121

22+
'gravatar_url' => env('GRAVATAR_URL', false),
2223

2324
'callback_url' => env('APP_URL', false),
2425

0 commit comments

Comments
 (0)