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