@@ -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.
0 commit comments