3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Core\Logger;
26 use Friendica\Database\DBA;
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;
37 class Photo extends BaseModule
42 * Fetch a photo or an avatar, in optional size, check for permissions and
45 public static function rawContent(array $parameters = [])
47 $totalstamp = microtime(true);
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"]);
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");
68 $stamp = microtime(true);
69 if (!empty($parameters['customsize'])) {
70 $customsize = intval($parameters['customsize']);
71 $uid = MPhoto::stripExtension($parameters['name']);
72 $photo = self::getAvatar($uid, $parameters['type'], $customsize);
73 } elseif (!empty($parameters['type'])) {
74 $uid = MPhoto::stripExtension($parameters['name']);
75 $photo = self::getAvatar($uid, $parameters['type'], Proxy::PIXEL_SMALL);
76 } elseif (!empty($parameters['name'])) {
77 $photoid = MPhoto::stripExtension($parameters['name']);
79 if (substr($photoid, -2, 1) == "-") {
80 $scale = intval(substr($photoid, -1, 1));
81 $photoid = substr($photoid, 0, -2);
83 $photo = MPhoto::getPhoto($photoid, $scale);
84 if ($photo === false) {
85 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('The Photo with id %s is not available.', $photoid));
88 throw new \Friendica\Network\HTTPException\BadRequestException();
90 $fetch = microtime(true) - $stamp;
92 if ($photo === false) {
93 throw new \Friendica\Network\HTTPException\NotFoundException();
96 $cacheable = ($photo["allow_cid"] . $photo["allow_gid"] . $photo["deny_cid"] . $photo["deny_gid"] === "") && (isset($photo["cacheable"]) ? $photo["cacheable"] : true);
98 $stamp = microtime(true);
99 $imgdata = MPhoto::getImageDataForPhoto($photo);
100 $data = microtime(true) - $stamp;
102 if (empty($imgdata)) {
103 Logger::warning("Invalid photo with id {$photo["id"]}.");
104 throw new \Friendica\Network\HTTPException\InternalServerErrorException(DI::l10n()->t('Invalid photo with id %s.', $photo["id"]));
107 // if customsize is set and image is not a gif, resize it
108 if ($photo['type'] !== "image/gif" && $customsize > 0 && $customsize < 501) {
109 $img = new Image($imgdata, $photo['type']);
110 $img->scaleToSquare($customsize);
111 $imgdata = $img->asString();
114 if (function_exists("header_remove")) {
115 header_remove("Pragma");
116 header_remove("pragma");
119 header("Content-type: " . $photo['type']);
121 $stamp = microtime(true);
123 // it is a private photo that they have no permission to view.
124 // tell the browser not to cache it, in case they authenticate
125 // and subsequently have permission to see it
126 header("Cache-Control: no-store, no-cache, must-revalidate");
128 $md5 = $photo['hash'] ?: md5($imgdata);
129 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
130 header("Etag: \"{$md5}\"");
131 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
132 header("Cache-Control: max-age=31536000");
134 $checksum = microtime(true) - $stamp;
136 $stamp = microtime(true);
138 $output = microtime(true) - $stamp;
140 $total = microtime(true) - $totalstamp;
141 $rest = $total - ($fetch + $data + $checksum + $output);
143 if (!is_null($scale) && ($scale < 4)) {
144 Logger::info('Performance:', ['scale' => $scale, 'resource' => $photo['resource-id'],
145 'total' => number_format($total, 3), 'fetch' => number_format($fetch, 3),
146 'data' => number_format($data, 3), 'checksum' => number_format($checksum, 3),
147 'output' => number_format($output, 3), 'rest' => number_format($rest, 3)]);
153 private static function getAvatar($uid, $type="avatar", $customsize)
157 $media = DBA::selectFirst('post-media', ['preview', 'url', 'type', 'uri-id'], ['id' => $uid]);
161 $url = $media['preview'];
163 if (empty($url) && ($media['type'] == Post\Media::IMAGE)) {
164 $url = $media['url'];
171 $author = Contact::selectFirst([], ["`id` IN (SELECT `author-id` FROM `post` WHERE `uri-id` = ?)", $media['uri-id']]);
172 $url = Contact::magicLinkByContact($author, $url);
174 return MPhoto::createPhotoForExternalResource($url);
176 $media = DBA::selectFirst('post-media', ['url', 'uri-id'], ['id' => $uid, 'type' => Post\Media::IMAGE]);
181 $author = Contact::selectFirst([], ["`id` IN (SELECT `author-id` FROM `post` WHERE `uri-id` = ?)", $media['uri-id']]);
182 $url = Contact::magicLinkByContact($author, $media['url']);
184 return MPhoto::createPhotoForExternalResource($url);
186 $contact = Contact::getById($uid, ['uid', 'url', 'avatar', 'photo', 'xmpp', 'addr']);
187 if (empty($contact)) {
190 If (($contact['uid'] != 0) && empty($contact['photo']) && empty($contact['avatar'])) {
191 $contact = Contact::getByURL($contact['url'], false, ['avatar', 'photo', 'xmpp', 'addr']);
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);
202 $url = Contact::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
204 return MPhoto::createPhotoForExternalResource($url);
206 $contact = Contact::getById($uid, ['uid', 'url', 'header']);
207 if (empty($contact)) {
210 If (($contact['uid'] != 0) && empty($contact['header'])) {
211 $contact = Contact::getByURL($contact['url'], false, ['header']);
213 if (!empty($contact['header'])) {
214 $url = $contact['header'];
216 $url = DI::baseUrl() . '/images/blank.png';
218 return MPhoto::createPhotoForExternalResource($url);
231 $photo = MPhoto::selectFirst([], ["scale" => $scale, "uid" => $uid, "profile" => 1]);
233 $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]) ?: [];
238 $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
241 $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
245 $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_THUMB);
248 $parts = parse_url($default);
249 if (!empty($parts['scheme']) || !empty($parts['host'])) {
250 $photo = MPhoto::createPhotoForExternalResource($default);
252 $photo = MPhoto::createPhotoForSystemResource($default);