]> git.mxchange.org Git - friendica.git/blob - mod/attach.php
Catch HTTPExceptions in App::runFrontend()
[friendica.git] / mod / attach.php
1 <?php
2 /**
3  * @file mod/attach.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\L10n;
8 use Friendica\Database\DBA;
9 use Friendica\Util\Security;
10
11 function attach_init(App $a)
12 {
13         if ($a->argc != 2) {
14                 notice(L10n::t('Item not available.') . EOL);
15                 return;
16         }
17
18         $item_id = intval($a->argv[1]);
19
20         // Check for existence, which will also provide us the owner uid
21
22         $r = DBA::selectFirst('attach', [], ['id' => $item_id]);
23         if (!DBA::isResult($r)) {
24                 notice(L10n::t('Item was not found.'). EOL);
25                 return;
26         }
27
28         $sql_extra = Security::getPermissionsSQLByUserId($r['uid']);
29
30         // Now we'll see if we can access the attachment
31
32         $r = q("SELECT * FROM `attach` WHERE `id` = '%d' $sql_extra LIMIT 1",
33                 DBA::escape($item_id)
34         );
35
36         if (!DBA::isResult($r)) {
37                 notice(L10n::t('Permission denied.') . EOL);
38                 return;
39         }
40
41         // Use quotes around the filename to prevent a "multiple Content-Disposition"
42         // error in Chrome for filenames with commas in them
43         header('Content-type: ' . $r[0]['filetype']);
44         header('Content-length: ' . $r[0]['filesize']);
45         if (isset($_GET['attachment']) && $_GET['attachment'] === '0') {
46                 header('Content-disposition: filename="' . $r[0]['filename'] . '"');
47         } else {
48                 header('Content-disposition: attachment; filename="' . $r[0]['filename'] . '"');
49         }
50
51         echo $r[0]['data'];
52         killme();
53         // NOTREACHED
54 }