-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathUserAvatarFileProcessor.class.php
More file actions
264 lines (221 loc) · 7.4 KB
/
UserAvatarFileProcessor.class.php
File metadata and controls
264 lines (221 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace wcf\system\file\processor;
use wcf\data\file\File;
use wcf\data\user\UserEditor;
use wcf\data\user\UserProfile;
use wcf\system\cache\runtime\UserProfileRuntimeCache;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\UserInputException;
use wcf\command\user\SetAvatar;
use wcf\system\WCF;
use wcf\util\FileUtil;
/**
* @author Olaf Braun
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*/
final class UserAvatarFileProcessor extends AbstractFileProcessor
{
private const SESSION_VARIABLE = 'wcf_user_avatar_processor_%d';
public const AVATAR_SIZE = 128;
/**
* Size of HiDPI version
*/
public const AVATAR_SIZE_2X = 256;
#[\Override]
public function getObjectTypeName(): string
{
return 'com.woltlab.wcf.user.avatar';
}
#[\Override]
public function getAllowedFileExtensions(array $context): array
{
return [
"png",
"jpg",
"jpeg",
"gif",
"webp",
];
}
#[\Override]
public function canAdopt(File $file, array $context): bool
{
$userFromContext = $this->getUser($context);
$userFromCoreFile = $this->getUserByFile($file);
if ($userFromCoreFile === null) {
return true;
}
if ($userFromContext->userID === $userFromCoreFile->userID) {
return true;
}
return false;
}
#[\Override]
public function adopt(File $file, array $context): void
{
$user = $this->getUser($context);
if ($user === null) {
return;
}
// Save the `fileID` in the session variable so that the current user can delete the old avatar
if ($user->avatarFileID !== null) {
WCF::getSession()->register(\sprintf(self::SESSION_VARIABLE, $user->avatarFileID), TIME_NOW);
WCF::getSession()->update();
}
(new SetAvatar($user->getDecoratedObject(), $file))();
}
#[\Override]
public function acceptUpload(string $filename, int $fileSize, array $context): FileProcessorPreflightResult
{
$user = $this->getUser($context);
if ($user === null) {
return FileProcessorPreflightResult::InvalidContext;
}
if (!$user->canEditAvatar()) {
return FileProcessorPreflightResult::InsufficientPermissions;
}
if ($fileSize > $this->getMaximumSize($context)) {
return FileProcessorPreflightResult::FileSizeTooLarge;
}
if (!FileUtil::endsWithAllowedExtension($filename, $this->getAllowedFileExtensions($context))) {
return FileProcessorPreflightResult::FileExtensionNotPermitted;
}
return FileProcessorPreflightResult::Passed;
}
#[\Override]
public function validateUpload(File $file): void
{
$imageData = @\getimagesize($file->getPathname());
if ($imageData === false) {
throw new UserInputException('file', 'noImage');
}
if ($imageData[0] !== $imageData[1]) {
throw new UserInputException('file', 'notSquare');
}
if (
$imageData[0] != UserAvatarFileProcessor::AVATAR_SIZE
&& $imageData[0] != UserAvatarFileProcessor::AVATAR_SIZE_2X
) {
throw new UserInputException('file', 'wrongSize');
}
}
#[\Override]
public function canDelete(File $file): bool
{
$user = $this->getUserByFile($file);
if ($user === null) {
return WCF::getSession()->getVar(
\sprintf(self::SESSION_VARIABLE, $file->fileID)
) !== null;
}
return $user->canEditAvatar();
}
#[\Override]
public function canDownload(File $file): bool
{
$user = $this->getUserByFile($file);
if ($user === null) {
return false;
}
return $user->canSeeAvatar();
}
#[\Override]
public function getThumbnailFormats(): array
{
return [];
}
#[\Override]
public function delete(array $fileIDs, array $thumbnailIDs): void
{
\array_map(
static fn(int $fileID) => WCF::getSession()->unregister(
\sprintf(self::SESSION_VARIABLE, $fileID)
),
$fileIDs
);
$conditionBuilder = new PreparedStatementConditionBuilder();
$conditionBuilder->add('avatarFileID IN (?)', [$fileIDs]);
$sql = "UPDATE wcf1_user
SET avatarFileID = ?,
avatarPathname = ?
" . $conditionBuilder;
$statement = WCF::getDB()->prepare($sql);
$statement->execute([
null,
null,
...$conditionBuilder->getParameters()
]);
}
#[\Override]
public function getMaximumSize(array $context): int
{
/**
* Reject the file if it is larger than 750 kB after resizing. A worst-case
* completely-random 128x128 PNG is around 35 kB and JPEG is around 50 kB.
*
* Animated GIFs can be much larger depending on the length of animation,
* 750 kB seems to be a reasonable upper bound for anything that can be
* considered reasonable with regard to "distraction" and mobile data
* volume.
*/
return 750_000;
}
#[\Override]
public function getImageCropperConfiguration(): ImageCropperConfiguration
{
return ImageCropperConfiguration::forExact(
new ImageCropSize(UserAvatarFileProcessor::AVATAR_SIZE, UserAvatarFileProcessor::AVATAR_SIZE),
new ImageCropSize(UserAvatarFileProcessor::AVATAR_SIZE_2X, UserAvatarFileProcessor::AVATAR_SIZE_2X)
);
}
#[\Override]
public function replacedWithWebpVariant(File $file): void
{
$user = $this->getUserByFile($file);
if ($user === null) {
return;
}
$filename = $file->getSourceFilenameWebp() ?? $file->getSourceFilename();
$pathname = $file->getRelativePath() . $filename;
if ($user->avatarPathname === $pathname) {
return;
}
// The relative path to the avatar is stored in a denormalized form in
// the user table. This path may be outdated if either the avatar is
// later converted to WebP or during the upload.
(new UserEditor($user->getDecoratedObject()))->update([
'avatarPathname' => $pathname,
]);
}
/**
* @param array<string, mixed> $context
*/
private function getUser(array $context): ?UserProfile
{
$userID = $context['objectID'] ?? null;
if ($userID === null) {
return null;
}
return UserProfileRuntimeCache::getInstance()->getObject($userID);
}
private function getUserByFile(File $file): ?UserProfile
{
$sql = "SELECT userID
FROM wcf1_user
WHERE avatarFileID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$file->fileID]);
$userID = $statement->fetchSingleColumn();
if ($userID === false) {
return null;
}
return UserProfileRuntimeCache::getInstance()->getObject($userID);
}
#[\Override]
public function isSingleFile(): bool
{
return true;
}
}