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