]> git.mxchange.org Git - friendica.git/blob - mod/photo.php
new api for notifications
[friendica.git] / mod / photo.php
1 <?php
2
3 require_once('include/security.php');
4 require_once('include/Photo.php');
5
6 if(! function_exists('photo_init')) {
7 function photo_init(&$a) {
8         global $_SERVER;
9
10         $prvcachecontrol = false;
11         $file = "";
12
13         switch($a->argc) {
14                 case 4:
15                         $person = $a->argv[3];
16                         $customres = intval($a->argv[2]);
17                         $type = $a->argv[1];
18                         break;
19                 case 3:
20                         $person = $a->argv[2];
21                         $type = $a->argv[1];
22                         break;
23                 case 2:
24                         $photo = $a->argv[1];
25                         $file = $photo;
26                         break;
27                 case 1:
28                 default:
29                         killme();
30                         // NOTREACHED
31         }
32
33         //      strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($localFileName)) {
34         if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
35                 header('HTTP/1.1 304 Not Modified');
36                 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
37                 header('Etag: '.$_SERVER['HTTP_IF_NONE_MATCH']);
38                 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
39                 header("Cache-Control: max-age=31536000");
40                 if(function_exists('header_remove')) {
41                         header_remove('Last-Modified');
42                         header_remove('Expires');
43                         header_remove('Cache-Control');
44                 }
45                 exit;
46         }
47
48         $default = 'images/person-175.jpg';
49
50         if(isset($type)) {
51
52
53                 /**
54                  * Profile photos
55                  */
56
57                 switch($type) {
58
59                         case 'profile':
60                         case 'custom':
61                                 $resolution = 4;
62                                 break;
63                         case 'micro':
64                                 $resolution = 6;
65                                 $default = 'images/person-48.jpg';
66                                 break;
67                         case 'avatar':
68                         default:
69                                 $resolution = 5;
70                                 $default = 'images/person-80.jpg';
71                                 break;
72                 }
73
74                 $uid = str_replace(array('.jpg','.png'),array('',''), $person);
75
76                 $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
77                         intval($resolution),
78                         intval($uid)
79                 );
80                 if(count($r)) {
81                         $data = $r[0]['data'];
82                         $mimetype = $r[0]['type'];
83                 }
84                 if(! isset($data)) {
85                         $data = file_get_contents($default);
86                         $mimetype = 'image/jpeg';
87                 }
88         }
89         else {
90
91                 /**
92                  * Other photos
93                  */
94
95                 $resolution = 0;
96                 foreach( Photo::supportedTypes() as $m=>$e){
97                         $photo = str_replace(".$e",'',$photo);
98                 }
99
100                 if(substr($photo,-2,1) == '-') {
101                         $resolution = intval(substr($photo,-1,1));
102                         $photo = substr($photo,0,-2);
103                 }
104
105         // check if the photo exists and get the owner of the photo
106                 $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
107                         dbesc($photo),
108                         intval($resolution)
109                 );
110                 if(count($r)) {
111
112                         $sql_extra = permissions_sql($r[0]['uid']);
113
114                         // Now we'll see if we can access the photo
115
116                         $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` <= %d $sql_extra ORDER BY scale DESC LIMIT 1",
117                                 dbesc($photo),
118                                 intval($resolution)
119                         );
120
121                         $public = ($r[0]['allow_cid'] == '') AND ($r[0]['allow_gid'] == '') AND ($r[0]['deny_cid']  == '') AND ($r[0]['deny_gid']  == '');
122
123                         if(count($r)) {
124                                 $resolution = $r[0]['scale'];
125                                 $data = $r[0]['data'];
126                                 $mimetype = $r[0]['type'];
127                         } else {
128                                 // The picure exists. We already checked with the first query.
129                                 // obviously, this is not an authorized viev!
130                                 $data = file_get_contents('images/nosign.jpg');
131                                 $mimetype = 'image/jpeg';
132                                 $prvcachecontrol = true;
133                                 $public = false;
134                         }
135                 }
136         }
137
138         if(! isset($data)) {
139                 if(isset($resolution)) {
140                         switch($resolution) {
141
142                                 case 4:
143                                         $data = file_get_contents('images/person-175.jpg');
144                                         $mimetype = 'image/jpeg';
145                                         break;
146                                 case 5:
147                                         $data = file_get_contents('images/person-80.jpg');
148                                         $mimetype = 'image/jpeg';
149                                         break;
150                                 case 6:
151                                         $data = file_get_contents('images/person-48.jpg');
152                                         $mimetype = 'image/jpeg';
153                                         break;
154                                 default:
155                                         killme();
156                                         // NOTREACHED
157                                         break;
158                         }
159                 }
160         }
161
162         // Resize only if its not a GIF
163         if ($mime != "image/gif") {
164                 $ph = new Photo($data, $mimetype);
165                 if($ph->is_valid()) {
166                         if(isset($customres) && $customres > 0 && $customres < 500) {
167                                 $ph->scaleImageSquare($customres);
168                         }
169                         $data = $ph->imageString();
170                         $mimetype = $ph->getType();
171                 }
172         }
173
174         if(function_exists('header_remove')) {
175                 header_remove('Pragma');
176                 header_remove('pragma');
177         }
178
179         header("Content-type: ".$mimetype);
180
181         if($prvcachecontrol) {
182
183                 // it is a private photo that they have no permission to view.
184                 // tell the browser not to cache it, in case they authenticate
185                 // and subsequently have permission to see it
186
187                 header("Cache-Control: no-store, no-cache, must-revalidate");
188
189         }
190         else {
191                 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
192                 header('Etag: "'.md5($data).'"');
193                 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
194                 header("Cache-Control: max-age=31536000");
195         }
196         echo $data;
197
198         // If the photo is public and there is an existing photo directory store the photo there
199         if ($public and ($file != "")) {
200                 // If the photo path isn't there, try to create it
201                 $basepath = $a->get_basepath();
202                 if (!is_dir($basepath."/photo"))
203                         if (is_writable($basepath))
204                                 mkdir($basepath."/photo");
205
206                 if (is_dir($basepath."/photo"))
207                         file_put_contents($basepath."/photo/".$file, $data);
208         }
209
210         killme();
211         // NOTREACHED
212 }
213 }