forked from elkarte/Elkarte
/
Attachment.php
810 lines (701 loc) · 23.7 KB
/
Attachment.php
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
<?php
/**
* Attachment display.
*
* @package ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
*
* This file contains code covered by:
* copyright: 2011 Simple Machines (http://www.simplemachines.org)
*
* @version 2.0 dev
*
*/
namespace ElkArte\Controller;
use ElkArte\AbstractController;
use ElkArte\Action;
use ElkArte\Attachments\AttachmentsDirectory;
use ElkArte\Attachments\TemporaryAttachmentChunk;
use ElkArte\Attachments\TemporaryAttachmentProcess;
use ElkArte\Attachments\TemporaryAttachmentsList;
use ElkArte\Errors\AttachmentErrorContext;
use ElkArte\Exceptions\Exception;
use ElkArte\Graphics\Image;
use ElkArte\Graphics\TextImage;
use ElkArte\Helper\FileFunctions;
use ElkArte\Http\Headers;
use ElkArte\Languages\Txt;
use ElkArte\Themes\ThemeLoader;
use ElkArte\User;
/**
* Everything to do with attachment handling / processing
*
* What it does:
*
* - Handles the downloading of an attachment or avatar
* - Handles the uploading of attachments via Ajax
* - Increments the download count where applicable
*
*/
class Attachment extends AbstractController
{
/**
* {@inheritDoc}
*/
public function needTheme($action = '')
{
global $modSettings, $maintenance;
// If guests are not allowed to browse and the user is a guest... kick him!
if (empty($modSettings['allow_guestAccess']) && $this->user->is_guest)
{
return true;
}
// If not in maintenance or allowed to use the forum in maintenance
if (empty($maintenance) || allowedTo('admin_forum'))
{
$sa = $this->_req->getQuery('sa', 'trim', '');
// We will need to respond with Json
return $sa === 'ulattach' || $sa === 'rmattach' || $sa === 'ulasync';
}
// ... politely kick them out
return true;
}
/**
* {@inheritDoc}
*/
public function trackStats($action = '')
{
return false;
}
/**
* The default action is to download an attachment.
* This allows ?action=attachment to be forwarded to action_dlattach()
*/
public function action_index()
{
// add a subaction array to act accordingly
$subActions = array(
'dlattach' => array($this, 'action_dlattach'),
'tmpattach' => array($this, 'action_tmpattach'),
'ulattach' => array($this, 'action_ulattach'),
'ulasync' => array($this, 'action_ulasync'),
'rmattach' => array($this, 'action_rmattach'),
);
// Setup the action handler
$action = new Action('attachments');
$subAction = $action->initialize($subActions, 'dlattach');
// Call the action
$action->dispatch($subAction);
}
/**
* Method to upload attachments as fragments via ajax
*
* - Currently called by drag drop attachment functionality
* - Passed the form data with session vars
* - Responds back with errors or file data
*
* @return bool Returns false if there was an error, otherwise true.
*/
public function action_ulasync()
{
global $context;
// Going to send back Json
setJsonTemplate();
// Final request, rebuild the file and do standard upload checks
if ($this->_req->comparePost('async', 'complete', 'trim'))
{
$this->combineChunksAndProcess();
return true;
}
Txt::load('Errors');
// Process the chunk
$chunk = new TemporaryAttachmentChunk();
$resp_data = $chunk->action_async();
// If we have a PHP related upload error, set the error context
if ($resp_data['result'] !== true)
{
$attach_errors = AttachmentErrorContext::context();
$attach_errors->activate();
if ($attach_errors->hasErrors())
{
$errors = $attach_errors->prepareErrors();
foreach ($errors as $error)
{
$resp_data[] = $error;
}
$context['json_data'] = array('result' => false, 'data' => $resp_data);
return false;
}
}
// Set up the template details
$context['json_data'] = $resp_data;
return true;
}
/**
* Combines the temporary attachment chunks into a single file
*
* This method combines the temporary attachment chunks into a single file and performs the final
* processing request for the combined chunks. If the response data indicates that the result is
* successful, the method passes the file off to the action_ulattach method as if it were a single file.
* Otherwise, the response data is assigned to the $context['json_data'] variable.
*
* @return void
*/
private function combineChunksAndProcess()
{
global $context;
// Final chuck processing request
$chunk = new TemporaryAttachmentChunk();
$resp_data = $chunk->action_combineChunks();
if ($resp_data['result'] === true)
{
// Pass this off to action_ulattach just like it was a single file, set strict as false as we already have the
// combined chunks in the attachment directory and we don't need to verify it was a php upload any longer
$this->action_ulattach(false);
}
else
{
$context['json_data'] = $resp_data;
}
}
/**
* Method to upload attachments via ajax
*
* - Currently called by drag drop attachment functionality
* - Passed the form data with session vars
* - Responds back with errors or file data
*
* @param bool $strict True if attachment processing should use move_uploaded_file, rename otherwise. Default is true.
*
* @return bool|void False if the session is invalid or an error occurred, void otherwise.
*/
public function action_ulattach($strict = true)
{
global $context, $modSettings, $txt;
$resp_data = array();
Txt::load('Errors');
$context['attachments']['can']['post'] = !empty($modSettings['attachmentEnable']) && (int) $modSettings['attachmentEnable'] === 1 && (allowedTo('post_attachment') || ($modSettings['postmod_active'] && allowedTo('post_unapproved_attachments')));
// Set up the template details
setJsonTemplate();
// Make sure the session is still valid
if (checkSession('post', '', false) !== '')
{
$context['json_data'] = array('result' => false, 'data' => $txt['session_timeout_file_upload']);
return false;
}
// We should have files, otherwise why are we here?
if (isset($_FILES['attachment']))
{
Txt::load('Post');
$attach_errors = AttachmentErrorContext::context();
$attach_errors->activate();
if ($context['attachments']['can']['post'] && empty($this->_req->post->from_qr))
{
$processAttachments = new TemporaryAttachmentProcess();
$processAttachments->strict = $strict;
$processAttachments->processAttachments($this->_req->getPost('msg', 'intval', 0));
}
// Any mistakes?
if ($attach_errors->hasErrors())
{
$errors = $attach_errors->prepareErrors();
// Bad news for you, the attachments did not process, lets tell them why
foreach ($errors as $error)
{
$resp_data[] = $error;
}
$context['json_data'] = array('result' => false, 'data' => $resp_data);
}
// No errors, lets get the details of what we have for our response back to the upload dialog
else
{
$tmp_attachments = new TemporaryAttachmentsList();
foreach ($tmp_attachments->toArray() as $val)
{
// We need to grab the name anyhow
if (!empty($val['tmp_name']))
{
$resp_data = array(
'name' => $val['name'],
'attachid' => $val['public_attachid'],
'size' => $val['size'],
'resized' => !empty($val['resized']),
);
}
}
$context['json_data'] = ['result' => true, 'data' => $resp_data];
}
}
// Could not find the files you claimed to have sent
else
{
$context['json_data'] = ['result' => false, 'data' => $txt['no_files_uploaded']];
}
}
/**
* Function to remove temporary attachments which were newly added via ajax calls
* or to remove previous saved ones from an existing post
*
* What it does:
*
* - Currently called by drag drop attachment functionality
* - Requires file name and file path
* - Responds back with success or error
*/
public function action_rmattach()
{
global $context, $txt;
// Prepare the template so we can respond with json
setJsonTemplate();
// Make sure the session is valid
if (checkSession('post', '', false) !== '')
{
Txt::load('Errors');
$context['json_data'] = array('result' => false, 'data' => $txt['session_timeout']);
return false;
}
// We need a filename and path, or we are not going any further
if (isset($this->_req->post->attachid))
{
$result = false;
$tmp_attachments = new TemporaryAttachmentsList();
if ($tmp_attachments->hasAttachments())
{
$attachId = $tmp_attachments->getIdFromPublic($this->_req->post->attachid);
try
{
$tmp_attachments->removeById($attachId);
$context['json_data'] = array('result' => true);
$result = true;
}
catch (\Exception $e)
{
$result = $e->getMessage();
}
}
// Not a temporary attachment, but a previously uploaded one?
if ($result !== true)
{
require_once(SUBSDIR . '/ManageAttachments.subs.php');
$attachId = $this->_req->getPost('attachid', 'intval');
if (canRemoveAttachment($attachId, User::$info->id))
{
$result_tmp = removeAttachments(array('id_attach' => $attachId), '', true);
if (!empty($result_tmp))
{
$context['json_data'] = array('result' => true);
$result = true;
}
else
{
$result = $result_tmp;
}
}
}
if ($result !== true)
{
Txt::load('Errors');
$context['json_data'] = ['result' => false, 'data' => $txt[empty($result) ? 'attachment_not_found' : $result]];
}
}
else
{
Txt::load('Errors');
$context['json_data'] = ['result' => false, 'data' => $txt['attachment_not_found']];
}
}
/**
* Downloads an attachment or avatar, and increments the download count.
*
* What it does:
*
* - It requires the view_attachments permission. (not for avatars!)
* - It disables the session parser, and clears any previous output.
* - It is accessed via the query string ?action=dlattach.
* - Views to attachments and avatars do not increase hits and are not logged
* in the "Who's Online" log.
*
* @throws Exception
*/
public function action_dlattach()
{
global $modSettings, $context, $topic, $board, $settings;
// Some defaults that we need.
$context['no_last_modified'] = true;
$filename = null;
// Make sure some attachment was requested!
if (!isset($this->_req->query->attach))
{
if (!isset($this->_req->query->id))
{
// Give them the old can't find it image
$this->action_text_to_image('attachment_not_found');
}
if ($this->_req->query->id === 'ila')
{
// Give them the old can't touch this
$this->action_text_to_image(($this->user->is_guest ? 'not_applicable' : 'awaiting_approval'), 90, 90, true);
}
}
// We need to do some work on attachments and avatars.
require_once(SUBSDIR . '/Attachments.subs.php');
// Temporary attachment, special case...
if (isset($this->_req->query->attach) && strpos($this->_req->query->attach, 'post_tmp_' . $this->user->id . '_') !== false)
{
// Return via tmpattach, back presumably to the post form
$this->action_tmpattach();
}
$id_attach = $this->_req->getQuery('attach', 'intval', $this->_req->getQuery('id', 'intval', 0));
// This is just a regular attachment... Avatars are no longer a dlattach option
if (empty($topic) && !empty($id_attach))
{
$id_board = 0;
$id_topic = 0;
$attachPos = getAttachmentPosition($id_attach);
if ($attachPos !== false)
{
[$id_board, $id_topic] = array_values($attachPos);
}
}
else
{
$id_board = $board;
$id_topic = $topic;
}
isAllowedTo('view_attachments', $id_board);
if ($this->_req->getQuery('thumb') === null)
{
$attachment = getAttachmentFromTopic($id_attach, $id_topic);
}
else
{
$this->_req->query->image = true;
$attachment = getAttachmentThumbFromTopic($id_attach, $id_topic);
// No file name, no thumbnail, no image.
if (empty($attachment['filename']))
{
$full_attach = getAttachmentFromTopic($id_attach, $id_topic);
$attachment['filename'] = empty($full_attach['filename']) ? '' : $full_attach['filename'];
$attachment['id_attach'] = 0;
$attachment['attachment_type'] = 0;
$attachment['approved'] = $full_attach['approved'] ?? 0;
$attachment['id_member'] = $full_attach['id_member'];
// If it is a known extension, show a mimetype extension image
$check = returnMimeThumb(empty($full_attach['fileext']) ? 'default' : $full_attach['fileext']);
if ($check !== false)
{
$attachment['fileext'] = 'png';
$attachment['mime_type'] = 'image/png';
$filename = $check;
}
else
{
$attachmentsDir = new AttachmentsDirectory($modSettings, database());
$filename = $attachmentsDir->getCurrent() . '/' . $attachment['filename'];
}
if (strpos(getMimeType($filename), 'image') !== 0)
{
$attachment['fileext'] = 'png';
$attachment['mime_type'] = 'image/png';
$filename = $settings['theme_dir'] . '/images/mime_images/default.png';
}
}
}
if (empty($attachment))
{
// Exit via action_text_to_image
$this->action_text_to_image('attachment_not_found');
}
$id_folder = $attachment['id_folder'] ?? '';
$real_filename = $attachment['filename'] ?? '';
$file_hash = $attachment['file_hash'] ?? '';
$file_ext = $attachment['fileext'] ?? '';
$id_attach = $attachment['id_attach'] ?? -1;
$attachment_type = $attachment['attachment_type'] ?? -1;
$mime_type = $attachment['mime_type'] ?? '';
$is_approved = $attachment['approved'] ?? '';
$id_member = $attachment['id_member'] ?? '';
// If it isn't yet approved, do they have permission to view it?
if (!$is_approved && ($id_member === 0 || $this->user->id !== $id_member) && ($attachment_type === 0 || $attachment_type === 3))
{
isAllowedTo('approve_posts', $id_board ?? $board);
}
// Update the download counter (unless it's a thumbnail).
if (!empty($id_attach && $attachment_type != 3))
{
increaseDownloadCounter($id_attach);
}
if ($filename === null)
{
$filename = getAttachmentFilename($real_filename, $id_attach, $id_folder, false, $file_hash);
}
$eTag = '"' . substr($id_attach . $real_filename . @filemtime($filename), 0, 64) . '"';
$disposition = isset($this->_req->query->image) ? 'inline' : 'attachment';
$do_cache = !(!isset($this->_req->query->image) && getValidMimeImageType($file_ext) !== '');
// Make sure the mime type warrants an inline display.
if (isset($this->_req->query->image) && !empty($mime_type) && strpos($mime_type, 'image/') !== 0)
{
unset($this->_req->query->image);
$mime_type = '';
}
// Does this have a mime type?
elseif (empty($mime_type) || (!isset($this->_req->query->image) && getValidMimeImageType($file_ext) !== ''))
{
$mime_type = '';
if (isset($this->_req->query->image))
{
unset($this->_req->query->image);
}
}
$this->prepare_headers($filename, $eTag, $mime_type, $disposition, $real_filename, $do_cache);
$this->send_file($filename, $mime_type);
obExit(false);
}
/**
* Generates a language image based on text for display, outputs that image and exits
*
* @param null|string $text if null will use default attachment not found string
* @param int $width If set, defines the width of the image, text font size will be scaled to fit
* @param int $height If set, defines the height of the image
* @param bool $split If true will break text strings so all words are separated by newlines
* @throws Exception
*/
public function action_text_to_image($text = null, $width = 200, $height = 75, $split = false)
{
global $txt;
new ThemeLoader();
Txt::load('Errors');
$text = $text === null ? $txt['attachment_not_found'] : $txt[$text] ?? $text;
$text = $split ? str_replace(' ', "\n", $text) : $text;
try
{
$img = new TextImage($text);
$img = $img->generate($width, $height);
}
catch (\Exception)
{
throw new Exception('no_access', false);
}
$this->prepare_headers('no_image', 'no_image', 'image/png', 'inline', 'no_image.png', true, false);
Headers::instance()->sendHeaders();
echo $img;
obExit(false);
}
/**
* If the mime type benefits from compression e.g. text/xyz and gzencode is
* available and the user agent accepts gzip, then return true, else false
*
* @param string $mime_type
* @return bool if we should compress the file
*/
public function useCompression($mime_type)
{
global $modSettings;
// Not compressible, or not supported / requested by client
if (!preg_match('~^(?:text/|application/(?:json|xml|rss\+xml)$)~i', $mime_type)
|| (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) || strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false))
{
return false;
}
// Support is available on the serve
return !(!function_exists('gzencode') && !empty($modSettings['enableCompressedOutput']));
}
/**
* Takes care of sending out the most common headers.
*
* @param string $filename Full path+file name of the file in the filesystem
* @param string $eTag ETag cache validator
* @param string $mime_type The mime-type of the file
* @param string $disposition The value of the Content-Disposition header
* @param string $real_filename The original name of the file
* @param bool $do_cache Send a max-age header or not
* @param bool $check_filename When false, any check on $filename is skipped
*/
public function prepare_headers($filename, $eTag, $mime_type, $disposition, $real_filename, $do_cache, $check_filename = true)
{
global $txt;
$headers = Headers::instance();
$protocol = detectServer()->getProtocol();
// No point in a nicer message, because this is supposed to be an attachment anyway...
if ($check_filename && !FileFunctions::instance()->fileExists($filename))
{
Txt::load('Errors');
$headers
->removeHeader('all')
->headerSpecial($protocol . ' 404 Not Found')
->sendHeaders();
// We need to die like this *before* we send any anti-caching headers as below.
die('404 - ' . $txt['attachment_not_found']);
}
// If it hasn't been modified since the last time this attachment was retrieved, there's no need to display it again.
if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))
{
[$modified_since] = explode(';', $this->_req->server->HTTP_IF_MODIFIED_SINCE);
if (!$check_filename || strtotime($modified_since) >= filemtime($filename))
{
@ob_end_clean();
// Answer the question - no, it hasn't been modified ;).
$headers
->removeHeader('all')
->headerSpecial($protocol . ' 304 Not Modified')
->sendHeaders();
exit;
}
}
// Check whether the ETag was sent back, and cache based on that...
if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && strpos($_SERVER['HTTP_IF_NONE_MATCH'], $eTag) !== false)
{
@ob_end_clean();
$headers
->removeHeader('all')
->headerSpecial($protocol . ' 304 Not Modified')
->sendHeaders();
exit;
}
// Send the attachment headers.
$headers
->header('Expires', gmdate('D, d M Y H:i:s', time() + 525600 * 60) . ' GMT')
->header('Last-Modified', gmdate('D, d M Y H:i:s', $check_filename ? filemtime($filename) : time() - 525600 * 60) . ' GMT')
->header('Accept-Ranges', 'bytes')
->header('Connection', 'close')
->header('ETag', $eTag);
// Different browsers like different standards...
$headers->setAttachmentFileParams($mime_type, $real_filename, $disposition);
// If this has an "image extension" - but isn't actually an image - then ensure it isn't cached cause of silly IE.
if ($do_cache)
{
$headers
->header('Cache-Control', 'max-age=' . (525600 * 60) . ', private');
}
else
{
$headers
->header('Pragma', 'no-cache')
->header('Cache-Control', 'no-cache');
}
// Try to buy some time...
detectServer()->setTimeLimit(600);
}
/**
* Sends the requested file to the user. If the file is compressible e.g.
* has a mine type of text/??? may compress the file prior to sending.
*
* @param string $filename
* @param string $mime_type
*/
public function send_file($filename, $mime_type)
{
$headers = Headers::instance();
$body = file_get_contents($filename);
$length = FileFunctions::instance()->fileSize($filename);
$use_compression = $this->useCompression($mime_type);
// If we can/should compress this file
if ($use_compression && strlen($body) > 250)
{
$body = gzencode($body, 2);
$length = strlen($body);
$headers
->header('Content-Encoding', 'gzip')
->header('Vary', 'Accept-Encoding');
}
if (!empty($length))
{
$headers->header('Content-Length', $length);
}
// Forcibly end any output buffering going on.
while (ob_get_level() > 0)
{
@ob_end_clean();
}
// Someone is getting a present
$headers->send();
echo $body;
}
/**
* "Simplified", cough, version of action_dlattach to send out thumbnails while creating
* or editing a message.
*/
public function action_tmpattach()
{
global $modSettings, $topic;
// Make sure some attachment was requested!
if (!isset($this->_req->query->attach))
{
$this->action_text_to_image('attachment_not_found');
}
// We will need some help
require_once(SUBSDIR . '/Attachments.subs.php');
$tmp_attachments = new TemporaryAttachmentsList();
$attachmentsDir = new AttachmentsDirectory($modSettings, database());
try
{
if (empty($topic) || (string) (int) $this->_req->query->attach !== (string) $this->_req->query->attach)
{
$attach_data = $tmp_attachments->getTempAttachById($this->_req->query->attach, $attachmentsDir, User::$info->id);
$file_ext = pathinfo($attach_data['name'], PATHINFO_EXTENSION);
$filename = $attach_data['tmp_name'];
$id_attach = $attach_data['attachid'];
$real_filename = $attach_data['name'];
$mime_type = $attach_data['type'];
}
else
{
$id_attach = $this->_req->getQuery('attach', 'intval', -1);
isAllowedTo('view_attachments');
$attachment = getAttachmentFromTopic($id_attach, $topic);
if (empty($attachment))
{
// Exit via action_text_to_image
$this->action_text_to_image('attachment_not_found');
}
// Save some typing
$id_folder = $attachment['id_folder'];
$real_filename = $attachment['filename'];
$file_hash = $attachment['file_hash'];
$file_ext = $attachment['fileext'];
$id_attach = $attachment['id_attach'];
$attachment_type = (int) $attachment['attachment_type'];
$mime_type = $attachment['mime_type'];
$is_approved = $attachment['approved'];
$id_member = (int) $attachment['id_member'];
// If it isn't yet approved, do they have permission to view it?
if (!$is_approved && ($id_member === 0 || $this->user->id !== $id_member)
&& ($attachment_type === 0 || $attachment_type === 3))
{
isAllowedTo('approve_posts');
}
$filename = getAttachmentFilename($real_filename, $id_attach, $id_folder, false, $file_hash);
}
}
catch (\Exception $exception)
{
throw new Exception($exception->getMessage(), false);
}
$resize = true;
// Return mime type ala mimetype extension
if (strpos(getMimeType($filename), 'image') !== 0)
{
$checkMime = returnMimeThumb($file_ext);
$mime_type = 'image/png';
$resize = false;
$filename = $checkMime;
}
$eTag = '"' . substr($id_attach . $real_filename . filemtime($filename), 0, 64) . '"';
$do_cache = !(!isset($this->_req->query->image) && getValidMimeImageType($file_ext) !== '');
$this->prepare_headers($filename, $eTag, $mime_type, 'inline', $real_filename, $do_cache);
// do not resize for ;image
if ($resize && !isset($this->_req->query->ila, $this->_req->query->image))
{
// Create a thumbnail image
$image = new Image($filename);
$filename .= '_thumb';
$max_width = $this->_req->isSet('thumb') && !empty($modSettings['attachmentThumbWidth']) ? $modSettings['attachmentThumbWidth'] : 300;
$max_height = $this->_req->isSet('thumb') && !empty($modSettings['attachmentThumbHeight']) ? $modSettings['attachmentThumbHeight'] : 300;
$image->createThumbnail($max_width, $max_height, $filename, null, false);
}
// With the headers complete, send the file data
$this->send_file($filename, $mime_type);
obExit(false);
}
}