]> git.mxchange.org Git - friendica.git/blob - mod/photo.php
72b7e8e95a00610724ff59655ee54dceb10e298d
[friendica.git] / mod / photo.php
1 <?php
2
3 use Friendica\App;
4
5 require_once('include/security.php');
6 require_once('include/Photo.php');
7
8 function photo_init(App $a) {
9         global $_SERVER;
10
11         $prvcachecontrol = false;
12         $file = "";
13
14         switch ($a->argc) {
15                 case 4:
16                         $person = $a->argv[3];
17                         $customres = intval($a->argv[2]);
18                         $type = $a->argv[1];
19                         break;
20                 case 3:
21                         $person = $a->argv[2];
22                         $type = $a->argv[1];
23                         break;
24                 case 2:
25                         $photo = $a->argv[1];
26                         $file = $photo;
27                         break;
28                 case 1:
29                 default:
30                         killme();
31                         // NOTREACHED
32         }
33
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                  * Profile photos
54                  */
55
56                 switch ($type) {
57
58                         case 'profile':
59                         case 'custom':
60                                 $resolution = 4;
61                                 break;
62                         case 'micro':
63                                 $resolution = 6;
64                                 $default = 'images/person-48.jpg';
65                                 break;
66                         case 'avatar':
67                         default:
68                                 $resolution = 5;
69                                 $default = 'images/person-80.jpg';
70                                 break;
71                 }
72
73                 $uid = str_replace(array('.jpg', '.png', '.gif'), array('', '', ''), $person);
74
75                 foreach (Photo::supportedTypes() AS $m => $e) {
76                         $uid = str_replace('.'.$e, '', $uid);
77                 }
78
79                 $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
80                         intval($resolution),
81                         intval($uid)
82                 );
83                 if (dbm::is_result($r)) {
84                         $data = $r[0]['data'];
85                         $mimetype = $r[0]['type'];
86                 }
87                 if (empty($data)) {
88                         $data = file_get_contents($default);
89                         $mimetype = 'image/jpeg';
90                 }
91         } else {
92
93                 /**
94                  * Other photos
95                  */
96
97                 $resolution = 0;
98                 $photo = str_replace(array('.jpg', '.png', '.gif'), array('', '', ''), $photo);
99
100                 foreach (Photo::supportedTypes() AS $m => $e) {
101                         $photo = str_replace('.'.$e, '', $photo);
102                 }
103
104                 if (substr($photo, -2, 1) == '-') {
105                         $resolution = intval(substr($photo, -1, 1));
106                         $photo = substr($photo, 0, -2);
107                 }
108
109                 // check if the photo exists and get the owner of the photo
110                 $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
111                         dbesc($photo),
112                         intval($resolution)
113                 );
114                 if (dbm::is_result($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 ORDER BY scale DESC LIMIT 1",
121                                 dbesc($photo),
122                                 intval($resolution)
123                         );
124
125                         $public = (dbm::is_result($r)) && ($r[0]['allow_cid'] == '') && ($r[0]['allow_gid'] == '') && ($r[0]['deny_cid']  == '') && ($r[0]['deny_gid']  == '');
126
127                         if (dbm::is_result($r)) {
128                                 $resolution = $r[0]['scale'];
129                                 $data = $r[0]['data'];
130                                 $mimetype = $r[0]['type'];
131                         } else {
132                                 // The picure exists. We already checked with the first query.
133                                 // obviously, this is not an authorized viev!
134                                 $data = file_get_contents('images/nosign.jpg');
135                                 $mimetype = 'image/jpeg';
136                                 $prvcachecontrol = true;
137                                 $public = false;
138                         }
139                 }
140         }
141
142         if (empty($data)) {
143                 if (isset($resolution)) {
144                         switch ($resolution) {
145
146                                 case 4:
147                                         $data = file_get_contents('images/person-175.jpg');
148                                         $mimetype = 'image/jpeg';
149                                         break;
150                                 case 5:
151                                         $data = file_get_contents('images/person-80.jpg');
152                                         $mimetype = 'image/jpeg';
153                                         break;
154                                 case 6:
155                                         $data = file_get_contents('images/person-48.jpg');
156                                         $mimetype = 'image/jpeg';
157                                         break;
158                                 default:
159                                         killme();
160                                         // NOTREACHED
161                                         break;
162                         }
163                 }
164         }
165
166         // Resize only if its not a GIF and it is supported by the library
167         if (($mimetype != "image/gif") && in_array($mimetype, Photo::supportedTypes())) {
168                 $ph = new Photo($data, $mimetype);
169                 if ($ph->is_valid()) {
170                         if (isset($customres) && $customres > 0 && $customres < 500) {
171                                 $ph->scaleImageSquare($customres);
172                         }
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         } else {
194                 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
195                 header('Etag: "'.md5($data).'"');
196                 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
197                 header("Cache-Control: max-age=31536000");
198         }
199         echo $data;
200
201         // If the photo is public and there is an existing photo directory store the photo there
202         if ($public and ($file != "")) {
203                 // If the photo path isn't there, try to create it
204                 $basepath = $a->get_basepath();
205                 if (!is_dir($basepath."/photo")) {
206                         if (is_writable($basepath)) {
207                                 mkdir($basepath."/photo");
208                         }
209                 }
210
211                 if (is_dir($basepath."/photo")) {
212                         file_put_contents($basepath."/photo/".$file, $data);
213                 }
214         }
215
216         killme();
217         // NOTREACHED
218 }