3 use wcf\data\article\Article
;
5 use wcf\data\media\Media
;
7 use wcf\system\box\BoxHandler
;
8 use wcf\system\event\EventHandler
;
9 use wcf\system\exception\IllegalLinkException
;
10 use wcf\system\exception\PermissionDeniedException
;
11 use wcf\system\message\embedded\
object\MessageEmbeddedObjectManager
;
13 use wcf\util\FileReader
;
14 use wcf\util\StringUtil
;
19 * @author Matthias Schmidt
20 * @copyright 2001-2016 WoltLab GmbH
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 * @package WoltLabSuite\Core\Page
25 class MediaPage
extends AbstractPage
{
27 * article which uses the media file as the main article image
33 * id of the article which uses the media file as the main article image
36 public $articleID = 0;
39 * box which uses the media file as box image
45 * id of the box which uses the media file as box image
51 * etag for the media file
63 * requested media file
69 * message in which the media is embedded
75 * id of the message in which the media is embedded
78 public $messageID = 0;
81 * name of the object type of the message in which the media is embedded
84 public $messageObjectType = '';
87 * id of the requested media file
93 * size of the requested thumbnail
96 public $thumbnail = '';
101 public $useTemplate = false;
104 * list of mime types which belong to files that are displayed inline
107 public static $inlineMimeTypes = [
119 public function checkPermissions() {
120 parent
::checkPermissions();
122 if (!WCF
::getSession()->getPermission('admin.content.cms.canManageMedia')) {
123 if ($this->articleID
) {
124 $this->article
= new Article($this->articleID
);
126 if (!$this->article
->articleID ||
!$this->article
->canRead()) {
127 throw new PermissionDeniedException();
130 else if ($this->boxID
) {
131 $this->box
= BoxHandler
::getInstance()->getBox($this->boxID
);
133 if ($this->box
=== null ||
!$this->box
->isAccessible()) {
134 throw new PermissionDeniedException();
137 else if ($this->messageID
) {
138 MessageEmbeddedObjectManager
::getInstance()->loadObjects($this->messageObjectType
, [$this->messageID
]);
139 $this->message
= MessageEmbeddedObjectManager
::getInstance()->getObject($this->messageObjectType
, $this->messageID
);
140 if ($this->message
=== null ||
!($this->message
instanceof IMessage
) ||
!$this->message
->isVisible()) {
141 throw new PermissionDeniedException();
145 $parameters = ['canAccess' => false];
147 EventHandler
::getInstance()->fireAction($this, 'checkMediaAccess', $parameters);
149 if (empty($parameters['canAccess'])) {
150 throw new PermissionDeniedException();
159 public function readData() {
163 if ($this->thumbnail
) {
164 $mimeType = $this->media
->{$this->thumbnail
.'ThumbnailType'};
165 $filesize = $this->media
->{$this->thumbnail
.'ThumbnailSize'};
166 $location = $this->media
->getThumbnailLocation($this->thumbnail
);
167 $this->eTag
= strtoupper($this->thumbnail
).'_'.$this->mediaID
;
170 $mimeType = $this->media
->fileType
;
171 $filesize = $this->media
->filesize
;
172 $location = $this->media
->getLocation();
173 $this->eTag
= $this->mediaID
;
177 $this->fileReader
= new FileReader($location, [
178 'filename' => $this->media
->filename
,
179 'mimeType' => $mimeType,
180 'filesize' => $filesize,
181 'showInline' => (in_array($mimeType, self
::$inlineMimeTypes)),
182 'enableRangeSupport' => ($this->thumbnail ?
true : false),
183 'lastModificationTime' => $this->media
->uploadTime
,
184 'expirationDate' => TIME_NOW +
31536000,
188 if ($this->eTag
!== null) {
189 $this->fileReader
->addHeader('ETag', '"'.$this->eTag
.'"');
196 public function readParameters() {
197 parent
::readParameters();
199 if (isset($_REQUEST['id'])) $this->mediaID
= intval($_REQUEST['id']);
200 $this->media
= new Media($this->mediaID
);
201 if (!$this->media
->mediaID
) {
202 throw new IllegalLinkException();
205 if (isset($_REQUEST['thumbnail'])) $this->thumbnail
= StringUtil
::trim($_REQUEST['thumbnail']);
206 if ($this->thumbnail
&& !isset(Media
::getThumbnailSizes()[$this->thumbnail
])) {
207 throw new IllegalLinkException();
210 if ($this->thumbnail
&& !$this->media
->{$this->thumbnail
.'ThumbnailType'}) {
211 $this->thumbnail
= '';
214 // read context parameters
215 if (isset($_REQUEST['articleID'])) {
216 $this->articleID
= intval($_REQUEST['articleID']);
218 else if (isset($_REQUEST['boxID'])) {
219 $this->boxID
= intval($_REQUEST['boxID']);
221 else if (isset($_REQUEST['messageObjectType']) && isset($_REQUEST['messageID'])) {
222 $this->messageObjectType
= StringUtil
::trim($_REQUEST['messageObjectType']);
223 $this->messageID
= intval($_REQUEST['messageID']);
230 public function show() {
234 if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == '"'.$this->eTag
.'"') {
235 @header
('HTTP/1.1 304 Not Modified');
239 // send file to client
240 $this->fileReader
->send();