]> git.mxchange.org Git - friendica.git/blob - src/Module/Attach.php
Merge pull request #8135 from annando/brief
[friendica.git] / src / Module / Attach.php
1 <?php
2 /**
3  * @file src/Module/Attach.php
4  */
5
6
7 namespace Friendica\Module;
8
9 use Friendica\BaseModule;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Logger;
12 use Friendica\Core\System;
13 use Friendica\DI;
14 use Friendica\Model\Attach as MAttach;
15
16 /**
17  * Attach Module
18  */
19 class Attach extends BaseModule
20 {
21         /**
22          * Return to user an attached file given the id
23          */
24         public static function rawContent(array $parameters = [])
25         {
26                 $a = DI::app();
27                 if ($a->argc != 2) {
28                         throw new \Friendica\Network\HTTPException\BadRequestException();
29                 }
30
31                 // @TODO: Replace with parameter from router
32                 $item_id = intval($a->argv[1]);
33                 
34                 // Check for existence
35                 $item = MAttach::exists(['id' => $item_id]);
36                 if ($item === false) {
37                         throw new \Friendica\Network\HTTPException\NotFoundException(L10n::t('Item was not found.'));
38                 }
39
40                 // Now we'll fetch the item, if we have enough permisson
41                 $item = MAttach::getByIdWithPermission($item_id);
42                 if ($item === false) {
43                         throw new \Friendica\Network\HTTPException\ForbiddenException(L10n::t('Permission denied.'));
44                 }
45
46                 $data = MAttach::getData($item);
47                 if (is_null($data)) {
48                         Logger::log('NULL data for attachment with id ' . $item['id']);
49                         throw new \Friendica\Network\HTTPException\NotFoundException(L10n::t('Item was not found.'));
50                 }
51
52                 // Use quotes around the filename to prevent a "multiple Content-Disposition"
53                 // error in Chrome for filenames with commas in them
54                 header('Content-type: ' . $item['filetype']);
55                 header('Content-length: ' . $item['filesize']);
56                 if (isset($_GET['attachment']) && $_GET['attachment'] === '0') {
57                         header('Content-disposition: filename="' . $item['filename'] . '"');
58                 } else {
59                         header('Content-disposition: attachment; filename="' . $item['filename'] . '"');
60                 }
61
62                 echo $data;
63                 exit();
64                 // NOTREACHED
65         }
66 }