Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / page / MediaPage.class.php
CommitLineData
59ab4d0f
MS
1<?php
2namespace wcf\page;
3use wcf\data\media\Media;
4use wcf\system\exception\IllegalLinkException;
306b6bdd 5use wcf\system\exception\PermissionDeniedException;
59ab4d0f
MS
6use wcf\util\FileReader;
7use wcf\util\StringUtil;
8
9/**
10 * Shows a media file.
11 *
12 * @author Matthias Schmidt
c839bd49 13 * @copyright 2001-2018 WoltLab GmbH
59ab4d0f 14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4
MW
15 * @package WoltLabSuite\Core\Page
16 * @since 3.0
59ab4d0f
MS
17 */
18class MediaPage extends AbstractPage {
19 /**
20 * etag for the media file
21 * @var string
22 */
306b6bdd 23 public $eTag;
59ab4d0f
MS
24
25 /**
26 * file reader object
27 * @var FileReader
28 */
306b6bdd 29 public $fileReader;
59ab4d0f
MS
30
31 /**
32 * requested media file
33 * @var Media
34 */
306b6bdd
MS
35 public $media;
36
59ab4d0f
MS
37 /**
38 * id of the requested media file
39 * @var integer
40 */
41 public $mediaID = 0;
42
43 /**
44 * size of the requested thumbnail
45 * @var string
46 */
47 public $thumbnail = '';
48
49 /**
00cefc6d 50 * @inheritDoc
59ab4d0f
MS
51 */
52 public $useTemplate = false;
53
54 /**
55 * list of mime types which belong to files that are displayed inline
56 * @var string[]
57 */
58 public static $inlineMimeTypes = [
59 'image/gif',
60 'image/jpeg',
61 'image/png',
62 'image/x-png',
63 'application/pdf',
64 'image/pjpeg'
65 ];
66
59ab4d0f 67 /**
00cefc6d 68 * @inheritDoc
59ab4d0f
MS
69 */
70 public function readData() {
71 parent::readData();
72
73 // get file data
74 if ($this->thumbnail) {
75 $mimeType = $this->media->{$this->thumbnail.'ThumbnailType'};
76 $filesize = $this->media->{$this->thumbnail.'ThumbnailSize'};
77 $location = $this->media->getThumbnailLocation($this->thumbnail);
78 $this->eTag = strtoupper($this->thumbnail).'_'.$this->mediaID;
79 }
80 else {
81 $mimeType = $this->media->fileType;
82 $filesize = $this->media->filesize;
83 $location = $this->media->getLocation();
84 $this->eTag = $this->mediaID;
85 }
86
87 // init file reader
88 $this->fileReader = new FileReader($location, [
89 'filename' => $this->media->filename,
90 'mimeType' => $mimeType,
91 'filesize' => $filesize,
63b9817b
MS
92 'showInline' => in_array($mimeType, self::$inlineMimeTypes),
93 'enableRangeSupport' => $this->thumbnail ? true : false,
59ab4d0f
MS
94 'lastModificationTime' => $this->media->uploadTime,
95 'expirationDate' => TIME_NOW + 31536000,
96 'maxAge' => 31536000
97 ]);
98
99 if ($this->eTag !== null) {
100 $this->fileReader->addHeader('ETag', '"'.$this->eTag.'"');
101 }
102 }
103
104 /**
00cefc6d 105 * @inheritDoc
59ab4d0f
MS
106 */
107 public function readParameters() {
108 parent::readParameters();
109
110 if (isset($_REQUEST['id'])) $this->mediaID = intval($_REQUEST['id']);
111 $this->media = new Media($this->mediaID);
112 if (!$this->media->mediaID) {
113 throw new IllegalLinkException();
114 }
c14f5026
MS
115 if (!$this->media->isAccessible()) {
116 throw new PermissionDeniedException();
117 }
59ab4d0f
MS
118
119 if (isset($_REQUEST['thumbnail'])) $this->thumbnail = StringUtil::trim($_REQUEST['thumbnail']);
120 if ($this->thumbnail && !isset(Media::getThumbnailSizes()[$this->thumbnail])) {
121 throw new IllegalLinkException();
122 }
123
306b6bdd 124 if ($this->thumbnail && !$this->media->{$this->thumbnail.'ThumbnailType'}) {
59ab4d0f
MS
125 $this->thumbnail = '';
126 }
59ab4d0f
MS
127 }
128
129 /**
00cefc6d 130 * @inheritDoc
59ab4d0f
MS
131 */
132 public function show() {
133 parent::show();
134
135 // etag caching
136 if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == '"'.$this->eTag.'"') {
137 @header('HTTP/1.1 304 Not Modified');
138 exit;
139 }
140
141 // send file to client
142 $this->fileReader->send();
143 exit;
144 }
145}