]> git.mxchange.org Git - friendica.git/blob - src/Module/Photo.php
Merge pull request #10446 from MrPetovan/bug/10439-addon-settings-forms
[friendica.git] / src / Module / Photo.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Logger;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Photo as MPhoto;
30 use Friendica\Model\Post;
31 use Friendica\Util\Proxy;
32 use Friendica\Object\Image;
33
34 /**
35  * Photo Module
36  */
37 class Photo extends BaseModule
38 {
39         /**
40          * Module initializer
41          *
42          * Fetch a photo or an avatar, in optional size, check for permissions and
43          * return the image
44          */
45         public static function rawContent(array $parameters = [])
46         {
47                 $totalstamp = microtime(true);
48
49                 if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
50                         header("HTTP/1.1 304 Not Modified");
51                         header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
52                         if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
53                                 header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
54                         }
55                         header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
56                         header("Cache-Control: max-age=31536000");
57                         if (function_exists("header_remove")) {
58                                 header_remove("Last-Modified");
59                                 header_remove("Expires");
60                                 header_remove("Cache-Control");
61                         }
62                         exit;
63                 }
64
65                 $customsize = 0;
66                 $square_resize = true;
67                 $photo = false;
68                 $scale = null;
69                 $stamp = microtime(true);
70                 if (!empty($parameters['customsize'])) {
71                         $customsize = intval($parameters['customsize']);
72                         $uid = MPhoto::stripExtension($parameters['name']);
73                         $photo = self::getAvatar($uid, $parameters['type'], $customsize);
74                         $square_resize = !in_array($parameters['type'], ['media', 'preview']);
75                 } elseif (!empty($parameters['type'])) {
76                         $uid = MPhoto::stripExtension($parameters['name']);
77                         $photo = self::getAvatar($uid, $parameters['type'], Proxy::PIXEL_SMALL);
78                 } elseif (!empty($parameters['name'])) {
79                         $photoid = MPhoto::stripExtension($parameters['name']);
80                         $scale = 0;
81                         if (substr($photoid, -2, 1) == "-") {
82                                 $scale = intval(substr($photoid, -1, 1));
83                                 $photoid = substr($photoid, 0, -2);
84                         }
85                         $photo = MPhoto::getPhoto($photoid, $scale);
86                         if ($photo === false) {
87                                 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('The Photo with id %s is not available.', $photoid));
88                         }
89                 } else {
90                         throw new \Friendica\Network\HTTPException\BadRequestException();
91                 }
92                 $fetch = microtime(true) - $stamp;
93
94                 if ($photo === false) {
95                         throw new \Friendica\Network\HTTPException\NotFoundException();
96                 }
97
98                 $cacheable = ($photo["allow_cid"] . $photo["allow_gid"] . $photo["deny_cid"] . $photo["deny_gid"] === "") && (isset($photo["cacheable"]) ? $photo["cacheable"] : true);
99
100                 $stamp = microtime(true);
101                 $imgdata = MPhoto::getImageDataForPhoto($photo);
102                 $data = microtime(true) - $stamp;
103
104                 if (empty($imgdata)) {
105                         Logger::warning("Invalid photo with id {$photo["id"]}.");
106                         throw new \Friendica\Network\HTTPException\InternalServerErrorException(DI::l10n()->t('Invalid photo with id %s.', $photo["id"]));
107                 }
108
109                 // if customsize is set and image is not a gif, resize it
110                 if ($photo['type'] !== "image/gif" && $customsize > 0 && $customsize <= Proxy::PIXEL_THUMB && $square_resize) {
111                         $img = new Image($imgdata, $photo['type']);
112                         $img->scaleToSquare($customsize);
113                         $imgdata = $img->asString();
114                 } elseif ($photo['type'] !== "image/gif" && $customsize > 0) {
115                         $img = new Image($imgdata, $photo['type']);
116                         $img->scaleDown($customsize);
117                         $imgdata = $img->asString();
118                 }
119
120                 if (function_exists("header_remove")) {
121                         header_remove("Pragma");
122                         header_remove("pragma");
123                 }
124
125                 header("Content-type: " . $photo['type']);
126
127                 $stamp = microtime(true);
128                 if (!$cacheable) {
129                         // it is a private photo that they have no permission to view.
130                         // tell the browser not to cache it, in case they authenticate
131                         // and subsequently have permission to see it
132                         header("Cache-Control: no-store, no-cache, must-revalidate");
133                 } else {
134                         $md5 = $photo['hash'] ?: md5($imgdata);
135                         header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
136                         header("Etag: \"{$md5}\"");
137                         header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
138                         header("Cache-Control: max-age=31536000");
139                 }
140                 $checksum = microtime(true) - $stamp;
141
142                 $stamp = microtime(true);
143                 echo $imgdata;
144                 $output = microtime(true) - $stamp;
145
146                 $total = microtime(true) - $totalstamp;
147                 $rest = $total - ($fetch + $data + $checksum + $output);
148
149                 if (!is_null($scale) && ($scale < 4)) {
150                         Logger::info('Performance:', ['scale' => $scale, 'resource' => $photo['resource-id'],
151                                 'total' => number_format($total, 3), 'fetch' => number_format($fetch, 3),
152                                 'data' => number_format($data, 3), 'checksum' => number_format($checksum, 3),
153                                 'output' => number_format($output, 3), 'rest' => number_format($rest, 3)]);
154                 }
155
156                 exit();
157         }
158
159         private static function getAvatar($uid, $type="avatar", $customsize)
160         {
161                 switch($type) {
162                         case "preview":
163                                 $media = DBA::selectFirst('post-media', ['preview', 'url', 'type', 'uri-id'], ['id' => $uid]);
164                                 if (empty($media)) {
165                                         return false;
166                                 }
167                                 $url = $media['preview'];
168
169                                 if (empty($url) && ($media['type'] == Post\Media::IMAGE)) {
170                                         $url = $media['url'];
171                                 }
172
173                                 if (empty($url)) {
174                                         return false;
175                                 }
176
177                                 return MPhoto::createPhotoForExternalResource($url, (int)local_user());
178                         case "media":
179                                 $media = DBA::selectFirst('post-media', ['url', 'uri-id'], ['id' => $uid, 'type' => Post\Media::IMAGE]);
180                                 if (empty($media)) {
181                                         return false;
182                                 }
183
184                                 return MPhoto::createPhotoForExternalResource($media['url'], (int)local_user());
185                         case "contact":
186                                 $contact = Contact::getById($uid, ['uid', 'url', 'avatar', 'photo', 'xmpp', 'addr']);
187                                 if (empty($contact)) {
188                                         return false;
189                                 }
190                                 If (($contact['uid'] != 0) && empty($contact['photo']) && empty($contact['avatar'])) {
191                                         $contact = Contact::getByURL($contact['url'], false, ['avatar', 'photo', 'xmpp', 'addr']);
192                                 }
193                                 if (!empty($contact['photo'])) {
194                                         $url = $contact['photo'];
195                                 } elseif (!empty($contact['avatar'])) {
196                                         $url = $contact['avatar'];
197                                 } elseif ($customsize <= Proxy::PIXEL_MICRO) {
198                                         $url = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
199                                 } elseif ($customsize <= Proxy::PIXEL_THUMB) {
200                                         $url = Contact::getDefaultAvatar($contact, Proxy::SIZE_THUMB);
201                                 } else {
202                                         $url = Contact::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
203                                 }
204                                 return MPhoto::createPhotoForExternalResource($url);
205                         case "header":
206                                 $contact = Contact::getById($uid, ['uid', 'url', 'header']);
207                                 if (empty($contact)) {
208                                         return false;
209                                 }
210                                 If (($contact['uid'] != 0) && empty($contact['header'])) {
211                                         $contact = Contact::getByURL($contact['url'], false, ['header']);
212                                 }
213                                 if (!empty($contact['header'])) {
214                                         $url = $contact['header'];
215                                 } else {
216                                         $url = DI::baseUrl() . '/images/blank.png';
217                                 }
218                                 return MPhoto::createPhotoForExternalResource($url);
219                         case "profile":
220                         case "custom":
221                                 $scale = 4;
222                                 break;
223                         case "micro":
224                                 $scale = 6;
225                                 break;
226                         case "avatar":
227                         default:
228                                 $scale = 5;
229                 }
230
231                 $photo = MPhoto::selectFirst([], ["scale" => $scale, "uid" => $uid, "profile" => 1]);
232                 if (empty($photo)) {
233                         $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]) ?: [];
234
235                         switch($type) {
236                                 case "profile":
237                                 case "custom":
238                                         $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
239                                         break;
240                                 case "micro":
241                                         $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
242                                         break;
243                                 case "avatar":
244                                 default:
245                                         $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_THUMB);
246                         }
247
248                         $parts = parse_url($default);
249                         if (!empty($parts['scheme']) || !empty($parts['host'])) {
250                                 $photo = MPhoto::createPhotoForExternalResource($default);
251                         } else {
252                                 $photo = MPhoto::createPhotoForSystemResource($default);
253                         }
254                 }
255                 return $photo;
256         }
257 }