]> git.mxchange.org Git - friendica.git/blob - mod/photo.php
typo last checkin
[friendica.git] / mod / photo.php
1 <?php
2
3 require_once('include/security.php');
4
5 function photo_init(&$a) {
6
7         switch($a->argc) {
8                 case 4:
9                         $person = $a->argv[3];
10                         $customres = intval($a->argv[2]);
11                         $type = $a->argv[1];
12                         break;
13                 case 3:
14                         $person = $a->argv[2];
15                         $type = $a->argv[1];
16                         break;
17                 case 2:
18                         $photo = $a->argv[1];
19                         break;
20                 case 1:
21                 default:
22                         killme();
23                         // NOTREACHED
24         }
25
26         $default = 'images/default-profile.jpg';
27
28         if(isset($type)) {
29
30                 /**
31                  * Profile photos
32                  */
33
34                 switch($type) {
35
36                         case 'profile':
37                         case 'custom':
38                                 $resolution = 4;
39                                 break;
40                         case 'micro':
41                                 $resolution = 6;
42                                 $default = 'images/default-profile-mm.jpg';
43                                 break;
44                         case 'avatar':
45                         default:
46                                 $resolution = 5;
47                                 $default = 'images/default-profile-sm.jpg';
48                                 break;
49                 }
50
51                 $uid = str_replace('.jpg', '', $person);
52
53                 $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
54                         intval($resolution),
55                         intval($uid)
56                 );
57                 if(count($r)) {
58                         $data = $r[0]['data'];
59                 }
60                 if(! isset($data)) {
61                         $data = file_get_contents($default);
62                 }
63         }
64         else {
65
66                 /**
67                  * Other photos
68                  */
69
70                 $resolution = 0;
71                 $photo = str_replace('.jpg','',$photo);
72         
73                 if(substr($photo,-2,1) == '-') {
74                         $resolution = intval(substr($photo,-1,1));
75                         $photo = substr($photo,0,-2);
76                 }
77
78                 $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d LIMIT 1",
79                         dbesc($photo),
80                         intval($resolution)
81                 );
82                 if(count($r)) {
83                         
84                         $sql_extra = permissions_sql($r[0]['uid']);
85
86                         // Now we'll see if we can access the photo
87
88                         $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d $sql_extra LIMIT 1",
89                                 dbesc($photo),
90                                 intval($resolution)
91                         );
92
93                         if(count($r)) {
94                                 $data = $r[0]['data'];
95                         }
96                         else {
97
98                                 // Does the picture exist? It may be a remote person with no credentials,
99                                 // but who should otherwise be able to view it. Show a default image to let 
100                                 // them know permissions was denied. It may be possible to view the image 
101                                 // through an authenticated profile visit.
102                                 // There won't be many completely unauthorised people seeing this because
103                                 // they won't have the photo link, so there's a reasonable chance that the person
104                                 // might be able to obtain permission to view it.
105  
106                                 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d LIMIT 1",
107                                         dbesc($photo),
108                                         intval($resolution)
109                                 );
110                                 if(count($r)) {
111                                         $data = file_get_contents('images/nosign.jpg');
112                                 }
113                         }
114                 }
115         }
116
117         if(! isset($data)) {
118                 killme();
119                 // NOTREACHED
120         }
121
122         if(intval($customres) && $customres > 0 && $customres < 500) {
123                 require_once('include/Photo.php');
124                 $ph = new Photo($data);
125                 if($ph->is_valid()) {
126                         $ph->scaleImageSquare($customres);
127                         $data = $ph->imageString();
128                 }
129         }
130
131         header("Content-type: image/jpeg");
132         header("Expires: " . gmdate("D, d M Y H:i:s", time() + (3600*24)) . " GMT");
133         header("Cache-Control: max-age=" . (3600*24));
134         echo $data;
135         killme();
136         // NOTREACHED
137 }