]> git.mxchange.org Git - friendica.git/blob - mod/photo.php
Merge https://github.com/friendica/friendica into pull
[friendica.git] / mod / photo.php
1 <?php
2
3 require_once('include/security.php');
4 require_once('include/Photo.php');
5
6 function photo_init(&$a) {
7         global $_SERVER;
8
9         $prvcachecontrol = false;
10
11         switch($a->argc) {
12                 case 4:
13                         $person = $a->argv[3];
14                         $customres = intval($a->argv[2]);
15                         $type = $a->argv[1];
16                         break;
17                 case 3:
18                         $person = $a->argv[2];
19                         $type = $a->argv[1];
20                         break;
21                 case 2:
22                         $photo = $a->argv[1];
23                         break;
24                 case 1:
25                 default:
26                         killme();
27                         // NOTREACHED
28         }
29
30         //      strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($localFileName)) {
31         if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
32                 header('HTTP/1.1 304 Not Modified');
33                 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
34                 header('Etag: '.$_SERVER['HTTP_IF_NONE_MATCH']);
35                 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
36                 header("Cache-Control: max-age=31536000");
37                 if(function_exists('header_remove')) {
38                         header_remove('Last-Modified');
39                         header_remove('Expires');
40                         header_remove('Cache-Control');
41                 }
42                 exit;
43         }
44
45
46         $default = 'images/person-175.jpg';
47
48         if(isset($type)) {
49
50
51                 /**
52                  * Profile photos
53                  */
54
55                 switch($type) {
56
57                         case 'profile':
58                         case 'custom':
59                                 $resolution = 4;
60                                 break;
61                         case 'micro':
62                                 $resolution = 6;
63                                 $default = 'images/person-48.jpg';
64                                 break;
65                         case 'avatar':
66                         default:
67                                 $resolution = 5;
68                                 $default = 'images/person-80.jpg';
69                                 break;
70                 }
71
72                 $uid = str_replace(array('.jpg','.png'),array('',''), $person);
73
74                 $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
75                         intval($resolution),
76                         intval($uid)
77                 );
78                 if(count($r)) {
79                         $data = $r[0]['data'];
80                         $mimetype = $r[0]['type'];
81                 }
82                 if(! isset($data)) {
83                         $data = file_get_contents($default);
84                         $mimetype = 'image/jpeg';
85                 }
86         }
87         else {
88
89                 /**
90                  * Other photos
91                  */
92
93                 $resolution = 0;
94                 foreach( Photo::supportedTypes() as $m=>$e){
95                         $photo = str_replace(".$e",'',$photo);
96                 }
97         
98                 if(substr($photo,-2,1) == '-') {
99                         $resolution = intval(substr($photo,-1,1));
100                         $photo = substr($photo,0,-2);
101                 }
102
103                 $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d LIMIT 1",
104                         dbesc($photo),
105                         intval($resolution)
106                 );
107                 if(count($r)) {
108                         
109                         $sql_extra = permissions_sql($r[0]['uid']);
110
111                         // Now we'll see if we can access the photo
112
113                         $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d $sql_extra LIMIT 1",
114                                 dbesc($photo),
115                                 intval($resolution)
116                         );
117
118                         if(count($r)) {
119                                 $data = $r[0]['data'];
120                                 $mimetype = $r[0]['type'];
121                         }
122                         else {
123
124                                 // Does the picture exist? It may be a remote person with no credentials,
125                                 // but who should otherwise be able to view it. Show a default image to let 
126                                 // them know permissions was denied. It may be possible to view the image 
127                                 // through an authenticated profile visit.
128                                 // There won't be many completely unauthorised people seeing this because
129                                 // they won't have the photo link, so there's a reasonable chance that the person
130                                 // might be able to obtain permission to view it.
131  
132                                 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d LIMIT 1",
133                                         dbesc($photo),
134                                         intval($resolution)
135                                 );
136                                 if(count($r)) {
137                                         $data = file_get_contents('images/nosign.jpg');
138                                         $mimetype = 'image/jpeg';
139                                         $prvcachecontrol = true;
140                                 }
141                         }
142                 }
143         }
144
145         if(! isset($data)) {
146                 if(isset($resolution)) {
147                         switch($resolution) {
148
149                                 case 4:
150                                         $data = file_get_contents('images/person-175.jpg');
151                                         $mimetype = 'image/jpeg';
152                                         break;
153                                 case 5:
154                                         $data = file_get_contents('images/person-80.jpg');
155                                         $mimetype = 'image/jpeg';
156                                         break;
157                                 case 6:
158                                         $data = file_get_contents('images/person-48.jpg');
159                                         $mimetype = 'image/jpeg';
160                                         break;
161                                 default:
162                                         killme();
163                                         // NOTREACHED
164                                         break;
165                         }
166                 }
167         }
168
169         if(isset($customres) && $customres > 0 && $customres < 500) {
170                 $ph = new Photo($data, $mimetype);
171                 if($ph->is_valid()) {
172                         $ph->scaleImageSquare($customres);
173                         $data = $ph->imageString();
174                         $mimetype = $ph->getType();
175                 }
176         }
177
178         if(function_exists('header_remove')) {
179                 header_remove('Pragma');
180                 header_remove('pragma');
181         }
182
183         header("Content-type: ".$mimetype);
184
185         if($prvcachecontrol) {
186
187                 // it is a private photo that they have no permission to view.
188                 // tell the browser not to cache it, in case they authenticate
189                 // and subsequently have permission to see it
190
191                 header("Cache-Control: no-store, no-cache, must-revalidate");
192
193         }
194         else {
195                 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
196                 header('Etag: "'.md5($data).'"');
197                 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
198                 header("Cache-Control: max-age=31536000");
199         }
200         echo $data;
201         killme();
202         // NOTREACHED
203 }