}
/**
- * Save a gravatar image and set a the profile image for a user.
+ * Save an avatar image from an external service.
* @param \BookStack\Auth\User $user
* @param int $size
- * @return mixed
+ * @return Image
* @throws Exception
*/
- public function saveUserGravatar(User $user, $size = 500)
+ public function saveUserAvatar(User $user, $size = 500)
{
- $emailHash = md5(strtolower(trim($user->email)));
- $url = 'https://www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon';
- $imageName = str_replace(' ', '-', $user->name . '-gravatar.png');
- $image = $this->saveNewFromUrl($url, 'user', $imageName);
+ $avatarUrl = $this->getAvatarUrl();
+ $email = strtolower(trim($user->email));
+
+ $replacements = [
+ '${hash}' => md5($email),
+ '${size}' => $size,
+ '${email}' => urlencode($email),
+ ];
+
+ $userAvatarUrl = strtr($avatarUrl, $replacements);
+ $imageName = str_replace(' ', '-', $user->name . '-avatar.png');
+ $image = $this->saveNewFromUrl($userAvatarUrl, 'user', $imageName);
$image->created_by = $user->id;
$image->updated_by = $user->id;
$image->save();
+
return $image;
}
+ /**
+ * Check if fetching external avatars is enabled.
+ * @return bool
+ */
+ public function avatarFetchEnabled()
+ {
+ $fetchUrl = $this->getAvatarUrl();
+ return is_string($fetchUrl) && strpos($fetchUrl, 'http') === 0;
+ }
+
+ /**
+ * Get the URL to fetch avatars from.
+ * @return string|mixed
+ */
+ protected function getAvatarUrl()
+ {
+ return trim(config('services.avatar_url'));
+ }
/**
* Delete gallery and drawings that are not within HTML content of pages or page revisions.