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