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