]> BookStack Code Mirror - bookstack/blob - app/Uploads/UserAvatars.php
OIDC: Added testing coverage for picture fetching
[bookstack] / app / Uploads / UserAvatars.php
1 <?php
2
3 namespace BookStack\Uploads;
4
5 use BookStack\Exceptions\HttpFetchException;
6 use BookStack\Http\HttpRequestService;
7 use BookStack\Users\Models\User;
8 use BookStack\Util\WebSafeMimeSniffer;
9 use Exception;
10 use GuzzleHttp\Psr7\Request;
11 use Illuminate\Support\Facades\Log;
12 use Illuminate\Support\Str;
13 use Psr\Http\Client\ClientExceptionInterface;
14
15 class UserAvatars
16 {
17     public function __construct(
18         protected ImageService $imageService,
19         protected HttpRequestService $http
20     ) {
21     }
22
23     /**
24      * Fetch and assign an avatar image to the given user.
25      */
26     public function fetchAndAssignToUser(User $user): void
27     {
28         if (!$this->avatarFetchEnabled()) {
29             return;
30         }
31
32         try {
33             $this->destroyAllForUser($user);
34             $avatar = $this->saveAvatarImage($user);
35             $user->avatar()->associate($avatar);
36             $user->save();
37         } catch (Exception $e) {
38             Log::error('Failed to save user avatar image', ['exception' => $e]);
39         }
40     }
41
42     /**
43      * Assign a new avatar image to the given user using the given image data.
44      */
45     public function assignToUserFromExistingData(User $user, string $imageData, string $extension): void
46     {
47         try {
48             $this->destroyAllForUser($user);
49             $avatar = $this->createAvatarImageFromData($user, $imageData, $extension);
50             $user->avatar()->associate($avatar);
51             $user->save();
52         } catch (Exception $e) {
53             Log::error('Failed to save user avatar image', ['exception' => $e]);
54         }
55     }
56
57     /**
58      * Assign a new avatar image to the given user by fetching from a remote URL.
59      */
60     public function assignToUserFromUrl(User $user, string $avatarUrl): void
61     {
62         try {
63             $this->destroyAllForUser($user);
64             $imageData = $this->getAvatarImageData($avatarUrl);
65
66             $mime = (new WebSafeMimeSniffer())->sniff($imageData);
67             [$format, $type] = explode('/', $mime, 2);
68             if ($format !== 'image' || !ImageService::isExtensionSupported($type)) {
69                 return;
70             }
71
72             $avatar = $this->createAvatarImageFromData($user, $imageData, $type);
73             $user->avatar()->associate($avatar);
74             $user->save();
75         } catch (Exception $e) {
76             Log::error('Failed to save user avatar image from URL', [
77                 'exception' => $e,
78                 'url'       => $avatarUrl,
79                 'user_id'   => $user->id,
80             ]);
81         }
82     }
83
84     /**
85      * Destroy all user avatars uploaded to the given user.
86      */
87     public function destroyAllForUser(User $user): void
88     {
89         $profileImages = Image::query()->where('type', '=', 'user')
90             ->where('uploaded_to', '=', $user->id)
91             ->get();
92
93         foreach ($profileImages as $image) {
94             $this->imageService->destroy($image);
95         }
96     }
97
98     /**
99      * Save an avatar image from an external service.
100      *
101      * @throws HttpFetchException
102      */
103     protected function saveAvatarImage(User $user, int $size = 500): Image
104     {
105         $avatarUrl = $this->getAvatarUrl();
106         $email = strtolower(trim($user->email));
107
108         $replacements = [
109             '${hash}'  => md5($email),
110             '${size}'  => $size,
111             '${email}' => urlencode($email),
112         ];
113
114         $userAvatarUrl = strtr($avatarUrl, $replacements);
115         $imageData = $this->getAvatarImageData($userAvatarUrl);
116
117         return $this->createAvatarImageFromData($user, $imageData, 'png');
118     }
119
120     /**
121      * Creates a new image instance and saves it in the system as a new user avatar image.
122      */
123     protected function createAvatarImageFromData(User $user, string $imageData, string $extension): Image
124     {
125         $imageName = Str::random(10) . '-avatar.' . $extension;
126
127         $image = $this->imageService->saveNew($imageName, $imageData, 'user', $user->id);
128         $image->created_by = $user->id;
129         $image->updated_by = $user->id;
130         $image->save();
131
132         return $image;
133     }
134
135     /**
136      * Get an image from a URL and return it as a string of image data.
137      *
138      * @throws HttpFetchException
139      */
140     protected function getAvatarImageData(string $url): string
141     {
142         try {
143             $client = $this->http->buildClient(5);
144             $response = $client->sendRequest(new Request('GET', $url));
145
146             if ($response->getStatusCode() !== 200) {
147                 throw new HttpFetchException(trans('errors.cannot_get_image_from_url', ['url' => $url]));
148             }
149
150             return (string) $response->getBody();
151         } catch (ClientExceptionInterface $exception) {
152             throw new HttpFetchException(trans('errors.cannot_get_image_from_url', ['url' => $url]), $exception->getCode(), $exception);
153         }
154     }
155
156     /**
157      * Check if fetching external avatars is enabled.
158      */
159     public function avatarFetchEnabled(): bool
160     {
161         $fetchUrl = $this->getAvatarUrl();
162
163         return str_starts_with($fetchUrl, 'http');
164     }
165
166     /**
167      * Get the URL to fetch avatars from.
168      */
169     public function getAvatarUrl(): string
170     {
171         $configOption = config('services.avatar_url');
172         if ($configOption === false) {
173             return '';
174         }
175
176         $url = trim($configOption);
177
178         if (empty($url) && !config('services.disable_services')) {
179             $url = 'https://www.gravatar.com/avatar/${hash}?s=${size}&d=identicon';
180         }
181
182         return $url;
183     }
184 }