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