]> git.mxchange.org Git - friendica.git/blob - mod/attach.php
file attachments
[friendica.git] / mod / attach.php
1 <?php
2
3 function attach_init(&$a) {
4
5         if($a->argc != 2) {
6                 notice( t('Item not available.') . EOL);
7                 return;
8         }
9
10         $item_id = intval($a->argv[1]);
11
12         $r = q("SELECT * FROM `attach` WHERE `id` = %d LIMIT 1",
13                 intval($item_id)
14         );
15         if(! count($r)) {
16                 notice( t('Item was not found.'). EOL);
17                 return;
18         }
19
20         $owner = $r[0]['uid'];
21
22         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
23
24         if(local_user() && ($owner == $_SESSION['uid'])) {
25
26                         // Owner can always see his/her photos
27                         $sql_extra = ''; 
28
29         }
30         elseif(remote_user()) {
31
32                 // authenticated visitor - here lie dragons
33
34                 $groups = init_groups_visitor($_SESSION['visitor_id']);
35                 $gs = '<<>>'; // should be impossible to match
36                 if(count($groups)) {
37                         foreach($groups as $g)
38                                 $gs .= '|<' . intval($g) . '>';
39                 } 
40
41                 $sql_extra = sprintf(
42                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
43                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
44                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
45                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
46
47                         intval($_SESSION['visitor_id']),
48                         intval($_SESSION['visitor_id']),
49                         dbesc($gs),
50                         dbesc($gs)
51                 );
52         }
53
54         // Now we'll see if we can access the attachment
55
56         $r = q("SELECT * FROM `attach` WHERE `id` = '%d' $sql_extra LIMIT 1",
57                 dbesc($item_id)
58         );
59
60         if(count($r)) {
61                 $data = $r[0]['data'];
62         }
63         else {
64                 notice( t('Permission denied.') . EOL);
65                 return;
66         }
67
68         header('Content-type: ' . $r[0]['filetype']);
69         header('Content-disposition: attachment; filename=' . $r[0]['filename']);
70         echo $data;
71         killme();
72         // NOTREACHED
73 }