Skip to content

Commit b56fc21

Browse files
committed
Abstracted user avatar fetching away from gravatar
Still uses gravatar as a default. Updated URL placeholders to follow LDAP format. Potential breaking config change: `GRAVATAR=false` replaced by `AVATAR_URL=false` Builds upon #1111
1 parent d673bf6 commit b56fc21

File tree

6 files changed

+55
-32
lines changed

6 files changed

+55
-32
lines changed

.env.example

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ GITLAB_BASE_URI=false
6060
DISCORD_APP_ID=false
6161
DISCORD_APP_SECRET=false
6262

63-
# External services such as Gravatar and Draw.IO
63+
64+
# Disable default services such as Gravatar and Draw.IO
6465
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
66+
# Use custom avatar service, Sets fetch URL
67+
# Possible placeholders: ${hash} ${size} ${email}
68+
# If set, Avatars will be fetched regardless of DISABLE_EXTERNAL_SERVICES option.
69+
# AVATAR_URL=https://seccdn.libravatar.org/avatar/${hash}?s=${size}&d=identicon
7070

7171
# LDAP Settings
7272
LDAP_SERVER=false

app/Auth/UserRepo.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ public function registerNew(array $data, $verifyEmail = false)
8585
{
8686
$user = $this->create($data, $verifyEmail);
8787
$this->attachDefaultRole($user);
88-
89-
// Get avatar from gravatar and save
90-
$this->downloadGravatarToUserAvatar($user);
88+
$this->downloadAndAssignUserAvatar($user);
9189

9290
return $user;
9391
}
@@ -238,25 +236,24 @@ public function getRestrictableRoles()
238236
}
239237

240238
/**
241-
* Get a gravatar image for a user and set it as their avatar.
242-
* Does not run if gravatar disabled in config.
239+
* Get an avatar image for a user and set it as their avatar.
240+
* Returns early if avatars disabled or not set in config.
243241
* @param User $user
244242
* @return bool
245243
*/
246-
public function downloadGravatarToUserAvatar(User $user)
244+
public function downloadAndAssignUserAvatar(User $user)
247245
{
248-
// Get avatar from gravatar and save
249-
if (!config('services.gravatar')) {
246+
if (!Images::avatarFetchEnabled()) {
250247
return false;
251248
}
252249

253250
try {
254-
$avatar = Images::saveUserGravatar($user, config('services.gravatar_url'));
251+
$avatar = Images::saveUserAvatar($user);
255252
$user->avatar()->associate($avatar);
256253
$user->save();
257254
return true;
258255
} catch (Exception $e) {
259-
\Log::error('Failed to save user gravatar image');
256+
\Log::error('Failed to save user avatar image');
260257
return false;
261258
}
262259
}

app/Console/Commands/CreateAdmin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function handle()
7676

7777
$user = $this->userRepo->create(['email' => $email, 'name' => $name, 'password' => $password]);
7878
$this->userRepo->attachSystemRole($user, 'admin');
79-
$this->userRepo->downloadGravatarToUserAvatar($user);
79+
$this->userRepo->downloadAndAssignUserAvatar($user);
8080
$user->email_confirmed = true;
8181
$user->save();
8282

app/Http/Controllers/UserController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function store(Request $request)
9292
$user->roles()->sync($roles);
9393
}
9494

95-
$this->userRepo->downloadGravatarToUserAvatar($user);
95+
$this->userRepo->downloadAndAssignUserAvatar($user);
9696

9797
return redirect('/settings/users');
9898
}

app/Uploads/ImageService.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,30 +279,51 @@ protected function destroyImagesFromPath(string $path)
279279
}
280280

281281
/**
282-
* Save a gravatar image and set a the profile image for a user.
282+
* Save an avatar image from an external service.
283283
* @param \BookStack\Auth\User $user
284-
* @param null|string $gravatarUrl
285284
* @param int $size
286-
* @return mixed
285+
* @return Image
287286
* @throws Exception
288287
*/
289-
public function saveUserGravatar(User $user, $gravatarUrl, $size = 500)
288+
public function saveUserAvatar(User $user, $size = 500)
290289
{
291-
if (!is_string($gravatarUrl) || empty($gravatarUrl)) {
292-
$gravatarUrl = 'https://www.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon';
293-
}
290+
$avatarUrl = $this->getAvatarUrl();
294291
$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);
298-
$imageName = str_replace(' ', '-', $user->name . '-gravatar.png');
299-
$image = $this->saveNewFromUrl($gravatarUrl, 'user', $imageName);
292+
293+
$replacements = [
294+
'${hash}' => md5($email),
295+
'${size}' => $size,
296+
'${email}' => urlencode($email),
297+
];
298+
299+
$userAvatarUrl = strtr($avatarUrl, $replacements);
300+
$imageName = str_replace(' ', '-', $user->name . '-avatar.png');
301+
$image = $this->saveNewFromUrl($userAvatarUrl, 'user', $imageName);
300302
$image->created_by = $user->id;
301303
$image->updated_by = $user->id;
302304
$image->save();
305+
303306
return $image;
304307
}
305308

309+
/**
310+
* Check if fetching external avatars is enabled.
311+
* @return bool
312+
*/
313+
public function avatarFetchEnabled()
314+
{
315+
$fetchUrl = $this->getAvatarUrl();
316+
return is_string($fetchUrl) && strpos($fetchUrl, 'http') === 0;
317+
}
318+
319+
/**
320+
* Get the URL to fetch avatars from.
321+
* @return string|mixed
322+
*/
323+
protected function getAvatarUrl()
324+
{
325+
return trim(config('services.avatar_url'));
326+
}
306327

307328
/**
308329
* Delete gallery and drawings that are not within HTML content of pages or page revisions.

config/services.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616

1717
// Single option to disable non-auth external services such as Gravatar and Draw.io
1818
'disable_services' => env('DISABLE_EXTERNAL_SERVICES', false),
19-
'gravatar' => env('GRAVATAR', !env('DISABLE_EXTERNAL_SERVICES', false)),
19+
20+
// Draw.io integration active
2021
'drawio' => env('DRAWIO', !env('DISABLE_EXTERNAL_SERVICES', false)),
2122

22-
'gravatar_url' => env('GRAVATAR_URL', false),
23+
// URL for fetching avatars
24+
'avatar_url' => env('AVATAR_URL',
25+
env('DISABLE_EXTERNAL_SERVICES', false) ? false : 'https://www.gravatar.com/avatar/${hash}?s=${size}&d=identicon'
26+
),
2327

28+
// Callback URL for social authentication methods
2429
'callback_url' => env('APP_URL', false),
2530

2631
'mailgun' => [

0 commit comments

Comments
 (0)