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