]> git.mxchange.org Git - friendica.git/blob - mod/photo.php
more post branch bug fixes
[friendica.git] / mod / photo.php
1 <?php
2
3 function photo_init(&$a) {
4
5         switch($a->argc) {
6                 case 3:
7                         $person = $a->argv[2];
8                         $type = $a->argv[1];
9                         break;
10                 case 2:
11                         $photo = $a->argv[1];
12                         break;
13                 case 1:
14                 default:
15                         killme();
16                         return; // NOTREACHED
17         }
18
19         if(x($type)) {
20                 switch($type) {
21
22                         case 'profile':
23                                 $resolution = 4;
24                                 break;
25                         case 'avatar':
26                         default:
27                                 $resolution = 5;
28                                 break;
29                 }
30
31                 $uid = str_replace('.jpg', '', $person);
32
33                 $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
34                         intval($resolution),
35                         intval($uid)
36                 );
37                 if(count($r)) {
38                         $data = $r[0]['data'];
39                 }
40                 if(x($data) === false) {
41                         $data = file_get_contents(($resolution == 5) 
42                                 ? 'images/default-profile-sm.jpg' 
43                                 : 'images/default-profile.jpg');
44                 }
45         }
46         else {
47
48                 $resolution = 0;
49                 $photo = str_replace('.jpg','',$photo);
50         
51                 if(substr($photo,-2,1) == '-') {
52                         $resolution = intval(substr($photo,-1,1));
53                         $photo = substr($photo,0,-2);
54                 }
55
56                 $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d LIMIT 1",
57                         dbesc($photo),
58                         intval($resolution)
59                 );
60                 if(count($r)) {
61                         
62                         $owner = $r[0]['uid'];
63
64                         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
65
66                         if(local_user() && ($owner == $_SESSION['uid'])) {
67
68                                 // Owner can always see his/her photos
69                                 $sql_extra = ''; 
70
71                         }
72                         elseif(remote_user()) {
73
74                                 // authenticated visitor - here lie dragons
75
76                                 $groups = init_groups_visitor($_SESSION['visitor_id']);
77                                 $gs = '<<>>'; // should be impossible to match
78                                 if(count($groups)) {
79                                         foreach($groups as $g)
80                                                 $gs .= '|<' . intval($g) . '>';
81                                 } 
82
83                                 $sql_extra = sprintf(
84                                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
85                                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
86                                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
87                                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
88
89                                         intval($_SESSION['visitor_id']),
90                                         intval($_SESSION['visitor_id']),
91                                         dbesc($gs),
92                                         dbesc($gs)
93                                 );
94                         }
95
96                         // Now we'll see if we can access the photo
97
98                         $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d $sql_extra LIMIT 1",
99                                 dbesc($photo),
100                                 intval($resolution)
101                         );
102
103                         if(count($r)) {
104                                 $data = $r[0]['data'];
105                         }
106                 }
107         }
108
109         if(x($data) === false) {
110                 killme();
111                 return; // NOTREACHED
112         }
113
114         header("Content-type: image/jpeg");
115         header('Expires: ' . datetime_convert('UTC','UTC', 'now + 30 minutes', 'D, d M Y H:i:s' . ' GMT'));
116         header('Expires: ' . datetime_convert('UTC','UTC', 'now + 30 minutes', 'D, d M Y H:i:s' . ' GMT'));
117         header("Cache-Control: max-age=3600, must-revalidate");
118         echo $data;
119         killme();
120         return; //NOTREACHED
121 }