-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathAttachment.class.php
More file actions
499 lines (446 loc) · 15.7 KB
/
Attachment.class.php
File metadata and controls
499 lines (446 loc) · 15.7 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<?php
namespace wcf\data\attachment;
use wcf\data\DatabaseObject;
use wcf\data\file\File;
use wcf\data\file\thumbnail\FileThumbnailList;
use wcf\data\ILinkableObject;
use wcf\data\IThumbnailFile;
use wcf\data\object\type\ObjectTypeCache;
use wcf\system\request\IRouteController;
use wcf\system\request\LinkHandler;
use wcf\system\WCF;
use wcf\util\FileUtil;
/**
* Represents an attachment.
*
* @author Marcel Werk
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @property-read int $attachmentID unique id of the attachment
* @property-read int $objectTypeID id of the `com.woltlab.wcf.attachment.objectType` object type
* @property-read int|null $objectID id of the attachment container object the attachment belongs to
* @property-read int|null $userID id of the user who uploaded the attachment or `null` if the user does not exist anymore or if the attachment has been uploaded by a guest
* @property-read string $tmpHash temporary hash used to identify uploaded attachments but not associated with an object yet or empty if the attachment has been associated with an object
* @property-read string $filename name of the physical attachment file
* @property-read int $filesize size of the physical attachment file
* @property-read string $fileType type of the physical attachment file
* @property-read string $fileHash hash of the physical attachment file
* @property-read int $isImage is `1` if the attachment is an image, otherwise `0`
* @property-read int $width width of the attachment if `$isImage` is `1`, otherwise `0`
* @property-read int $height height of the attachment if `$isImage` is `1`, otherwise `0`
* @property-read string $tinyThumbnailType type of the tiny thumbnail file for the attachment if `$isImage` is `1`, otherwise empty
* @property-read int $tinyThumbnailSize size of the tiny thumbnail file for the attachment if `$isImage` is `1`, otherwise `0`
* @property-read int $tinyThumbnailWidth width of the tiny thumbnail file for the attachment if `$isImage` is `1`, otherwise `0`
* @property-read int $tinyThumbnailHeight height of the tiny thumbnail file for the attachment if `$isImage` is `1`, otherwise `0`
* @property-read string $thumbnailType type of the thumbnail file for the attachment if `$isImage` is `1`, otherwise empty
* @property-read int $thumbnailSize size of the thumbnail file for the attachment if `$isImage` is `1`, otherwise `0`
* @property-read int $downloads number of times the attachment has been downloaded
* @property-read int $lastDownloadTime timestamp at which the attachment has been downloaded the last time
* @property-read int $thumbnailWidth width of the thumbnail file for the attachment if `$isImage` is `1`, otherwise `0`
* @property-read int $thumbnailHeight height of the thumbnail file for the attachment if `$isImage` is `1`, otherwise `0`
* @property-read int $uploadTime timestamp at which the attachment has been uploaded
* @property-read int $showOrder position of the attachment in relation to the other attachment to the same message
* @property-read int|null $fileID
* @property-read int|null $thumbnailID
* @property-read int|null $tinyThumbnailID
*/
class Attachment extends DatabaseObject implements ILinkableObject, IRouteController, IThumbnailFile
{
/**
* indicates if the attachment is embedded
* @var bool
*/
protected $embedded = false;
/**
* user permissions for attachment access
* @var bool[]
*/
protected $permissions = [];
protected File $file;
/**
* @inheritDoc
*/
public function getLink(): string
{
$file = $this->getFile();
if ($file !== null) {
return $file->getLink();
}
// Do not use `LinkHandler::getControllerLink()` or `forceFrontend` as attachment
// links can be opened in the frontend and in the ACP.
return LinkHandler::getInstance()->getLink('Attachment', [
'object' => $this,
]);
}
public function getFullSizeImageSource(): string
{
return $this->getFile()?->getFullSizeImageSource() ?: $this->getLink();
}
/**
* Returns true if a user has the permission to download this attachment.
*
* @return bool
*/
public function canDownload()
{
return $this->getPermission('canDownload');
}
/**
* Returns true if a user has the permission to view the preview of this
* attachment.
*
* @return bool
*/
public function canViewPreview()
{
return $this->getPermission('canViewPreview');
}
/**
* Returns true if a user has the permission to delete the preview of this
* attachment.
*
* @return bool
*/
public function canDelete()
{
return $this->getPermission('canDelete');
}
/**
* Checks permissions.
*
* @param string $permission
* @return bool
*/
protected function getPermission($permission)
{
if (!isset($this->permissions[$permission])) {
$this->permissions[$permission] = true;
if ($this->tmpHash) {
if ($this->userID && $this->userID != WCF::getUser()->userID) {
$this->permissions[$permission] = false;
}
} else {
$objectType = ObjectTypeCache::getInstance()->getObjectType($this->objectTypeID);
$processor = $objectType->getProcessor();
if ($processor !== null) {
$this->permissions[$permission] = \call_user_func([$processor, $permission], $this->objectID);
}
}
}
return $this->permissions[$permission];
}
/**
* Sets the permissions for attachment access.
*
* @param bool[] $permissions
*/
public function setPermissions(array $permissions)
{
$this->permissions = $permissions;
}
/**
* @inheritDoc
* @deprecated 6.1 This will no longer be required once the attachments have been migrated.
*/
public function getLocation()
{
return $this->getLocationHelper(self::getStorage() . \substr(
$this->fileHash,
0,
2
) . '/' . $this->attachmentID . '-' . $this->fileHash);
}
/**
* Returns the physical location of the tiny thumbnail.
*
* @return string
* @deprecated 6.1 This will no longer be required once the attachments have been migrated.
*/
public function getTinyThumbnailLocation()
{
return $this->getThumbnailLocation('tiny');
}
/**
* @inheritDoc
* @deprecated 6.1 This will no longer be required once the attachments have been migrated.
*/
public function getThumbnailLocation($size = '')
{
if ($size == 'tiny') {
$location = self::getStorage() . \substr(
$this->fileHash,
0,
2
) . '/' . $this->attachmentID . '-tiny-' . $this->fileHash;
} else {
$location = self::getStorage() . \substr(
$this->fileHash,
0,
2
) . '/' . $this->attachmentID . '-thumbnail-' . $this->fileHash;
}
return $this->getLocationHelper($location);
}
/**
* Migrates the storage location of this attachment to
* include the `.bin` suffix.
*
* @since 5.2
* @deprecated 6.1 This will no longer be required once the attachments have been migrated.
*/
public function migrateStorage()
{
foreach ([
$this->getLocation(),
$this->getThumbnailLocation(),
$this->getThumbnailLocation('tiny'),
] as $location) {
if (!\str_ends_with($location, '.bin')) {
\rename($location, $location . '.bin');
}
}
}
/**
* Returns the appropriate location with or without extension.
*
* Files are suffixed with `.bin` since 5.2, but they are recognized
* without the extension for backward compatibility.
*
* @param string $location
* @return string
* @since 5.2
* @deprecated 6.1 This will no longer be required once the attachments have been migrated.
*/
final protected function getLocationHelper($location)
{
if (\is_readable($location . '.bin')) {
return $location . '.bin';
} elseif (\is_readable($location)) {
return $location;
}
// Assume that the attachment has not been uploaded yet.
return $location . '.bin';
}
/**
* @inheritDoc
*/
public function getThumbnailLink($size = '')
{
$file = $this->getFile();
if ($file === null) {
return '';
}
if ($size === '') {
return $file->getLink();
}
$thumbnail = $file->getThumbnail($size !== 'tiny' ? '' : $size);
if ($thumbnail === null) {
return '';
}
return $thumbnail->getLink();
}
/**
* @inheritDoc
*/
public function getTitle(): string
{
return $this->filename;
}
/**
* Marks this attachment as embedded.
*/
public function markAsEmbedded()
{
$this->embedded = true;
}
/**
* Returns true if this attachment is embedded.
*
* @return bool
*/
public function isEmbedded()
{
return $this->embedded;
}
/**
* Returns true if this attachment should be shown as an image.
*
* @return bool
*/
public function showAsImage()
{
if ($this->isImage) {
if (!$this->hasThumbnail() && ($this->width > \ATTACHMENT_THUMBNAIL_WIDTH || $this->height > \ATTACHMENT_THUMBNAIL_HEIGHT)) {
// Some images have no thumbnail because this is not really an
// image or it is unrecognized. However, there are images that
// do not have a thumbnail because one of its dimensions is
// less then the thumbnails minimum of that side.
$shouldHaveThumbnail = true;
if ($this->width > \ATTACHMENT_THUMBNAIL_WIDTH && $this->height < \ATTACHMENT_THUMBNAIL_HEIGHT) {
$shouldHaveThumbnail = false;
} else if ($this->height > ATTACHMENT_THUMBNAIL_HEIGHT && $this->width < \ATTACHMENT_THUMBNAIL_WIDTH) {
$shouldHaveThumbnail = false;
}
if ($shouldHaveThumbnail) {
return false;
}
}
if ($this->canDownload()) {
return true;
}
if ($this->canViewPreview() && $this->hasThumbnail()) {
return true;
}
}
return false;
}
/**
* Returns true if this attachment has a thumbnail.
*
* @return bool
*/
public function hasThumbnail()
{
return $this->thumbnailType ? true : false;
}
/**
* Returns true if this attachment should be shown as a file.
*
* @return bool
*/
public function showAsFile()
{
return !$this->showAsImage();
}
/**
* Returns icon name for this attachment.
*
* @return string
*/
public function getIconName()
{
if ($iconName = FileUtil::getIconNameByFilename($this->filename)) {
return 'file-' . $iconName;
}
return 'paperclip';
}
public function getFile(): ?File
{
// This method is called within `__get()`, therefore we must dereference
// the data array directly to avoid recursion.
$fileID = $this->data['fileID'] ?? null;
if (!$fileID) {
return null;
}
if (!isset($this->file)) {
$this->file = new File($fileID);
$thumbnailList = new FileThumbnailList();
$thumbnailList->getConditionBuilder()->add("fileID = ?", [$this->file->fileID]);
$thumbnailList->readObjects();
foreach ($thumbnailList as $thumbnail) {
$this->file->addThumbnail($thumbnail);
}
}
return $this->file;
}
public function setFile(File $file): void
{
if ($this->fileID === $file->fileID) {
$this->file = $file;
}
}
#[\Override]
public function __get($name)
{
if ($name === 'downloads' || $name === 'lastDownloadTime') {
// Static files are no longer served through PHP but the web server
// instead, therefore we can no longer report any meaningful numbers.
//
// For existing files the stored value is suppressed because it is
// not going to be increased ever, possibly creating a false
// impression when the historic stored value is being reported.
if ($this->getFile()?->isStaticFile()) {
return 0;
}
}
if (\in_array($name, [
'filename',
'filesize',
'fileType',
'isImage',
'height',
'width',
'thumbnailType',
'thumbnailWidth',
'thumbnailHeight',
'tinyThumbnailType',
'tinyThumbnailWidth',
'tinyThumbnailHeight',
])) {
$file = $this->getFile();
if ($file === null) {
return parent::__get($name);
}
return match ($name) {
'filename' => $file->filename,
'filesize' => $file->fileSize,
'fileType' => $file->mimeType,
'isImage' => $file->isImage() ? 1 : 0,
'height' => $file->height ?: 0,
'width' => $file->width ?: 0,
'thumbnailType' => $file->getThumbnail('')?->getMimeType() ?: '',
'thumbnailWidth' => $file->getThumbnail('')?->width ?: 0,
'thumbnailHeight' => $file->getThumbnail('')?->height ?: 0,
'tinyThumbnailType' => $file->getThumbnail('tiny')?->getMimeType() ?: '',
'tinyThumbnailWidth' => $file->getThumbnail('tiny')?->width ?: 0,
'tinyThumbnailHeight' => $file->getThumbnail('tiny')?->height ?: 0,
};
}
return parent::__get($name);
}
public function toHtmlElement(): ?string
{
return $this->getFile()?->toHtmlElement([
'attachmentID' => $this->attachmentID,
'showOrder' => $this->showOrder,
]);
}
public static function findByFileID(int $fileID): ?Attachment
{
$sql = "SELECT *
FROM wcf1_attachment
WHERE fileID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$fileID]);
return $statement->fetchObject(Attachment::class);
}
/**
* Returns the storage path.
*
* @deprecated 6.1 This method is no longer in use.
*/
public static function getStorage(): string
{
return WCF_DIR . 'attachments/';
}
/**
* @inheritDoc
*/
public static function getThumbnailSizes()
{
return [
'tiny' => [
'height' => 144,
'retainDimensions' => false,
'width' => 144,
],
// standard thumbnail size
'' => [
'height' => ATTACHMENT_THUMBNAIL_HEIGHT,
'retainDimensions' => ATTACHMENT_RETAIN_DIMENSIONS,
'width' => ATTACHMENT_THUMBNAIL_WIDTH,
],
];
}
}