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\Util\Proxy;
31 use Friendica\Object\Image;
36 class Photo extends BaseModule
41 * Fetch a photo or an avatar, in optional size, check for permissions and
44 public static function rawContent(array $parameters = [])
46 $totalstamp = microtime(true);
48 if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
49 header("HTTP/1.1 304 Not Modified");
50 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
51 if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
52 header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
54 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
55 header("Cache-Control: max-age=31536000");
56 if (function_exists("header_remove")) {
57 header_remove("Last-Modified");
58 header_remove("Expires");
59 header_remove("Cache-Control");
67 $stamp = microtime(true);
68 if (!empty($parameters['customsize'])) {
69 $customsize = intval($parameters['customsize']);
70 $uid = MPhoto::stripExtension($parameters['name']);
71 $photo = self::getAvatar($uid, $parameters['type']);
72 } elseif (!empty($parameters['type'])) {
73 $uid = MPhoto::stripExtension($parameters['name']);
74 $photo = self::getAvatar($uid, $parameters['type']);
75 } elseif (!empty($parameters['name'])) {
76 $photoid = MPhoto::stripExtension($parameters['name']);
78 if (substr($photoid, -2, 1) == "-") {
79 $scale = intval(substr($photoid, -1, 1));
80 $photoid = substr($photoid, 0, -2);
82 $photo = MPhoto::getPhoto($photoid, $scale);
83 if ($photo === false) {
84 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('The Photo with id %s is not available.', $photoid));
87 throw new \Friendica\Network\HTTPException\BadRequestException();
89 $fetch = microtime(true) - $stamp;
91 if ($photo === false) {
92 throw new \Friendica\Network\HTTPException\NotFoundException();
95 $cacheable = ($photo["allow_cid"] . $photo["allow_gid"] . $photo["deny_cid"] . $photo["deny_gid"] === "") && (isset($photo["cacheable"]) ? $photo["cacheable"] : true);
97 $stamp = microtime(true);
98 $imgdata = MPhoto::getImageDataForPhoto($photo);
99 $data = microtime(true) - $stamp;
101 if (empty($imgdata)) {
102 Logger::warning("Invalid photo with id {$photo["id"]}.");
103 throw new \Friendica\Network\HTTPException\InternalServerErrorException(DI::l10n()->t('Invalid photo with id %s.', $photo["id"]));
106 // if customsize is set and image is not a gif, resize it
107 if ($photo['type'] !== "image/gif" && $customsize > 0 && $customsize < 501) {
108 $img = new Image($imgdata, $photo['type']);
109 $img->scaleToSquare($customsize);
110 $imgdata = $img->asString();
113 if (function_exists("header_remove")) {
114 header_remove("Pragma");
115 header_remove("pragma");
118 header("Content-type: " . $photo['type']);
120 $stamp = microtime(true);
122 // it is a private photo that they have no permission to view.
123 // tell the browser not to cache it, in case they authenticate
124 // and subsequently have permission to see it
125 header("Cache-Control: no-store, no-cache, must-revalidate");
127 $md5 = $photo['hash'] ?: md5($imgdata);
128 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
129 header("Etag: \"{$md5}\"");
130 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
131 header("Cache-Control: max-age=31536000");
133 $checksum = microtime(true) - $stamp;
135 $stamp = microtime(true);
137 $output = microtime(true) - $stamp;
139 $total = microtime(true) - $totalstamp;
140 $rest = $total - ($fetch + $data + $checksum + $output);
142 if (!is_null($scale) && ($scale < 4)) {
143 Logger::info('Performance:', ['scale' => $scale, 'resource' => $photo['resource-id'],
144 'total' => number_format($total, 3), 'fetch' => number_format($fetch, 3),
145 'data' => number_format($data, 3), 'checksum' => number_format($checksum, 3),
146 'output' => number_format($output, 3), 'rest' => number_format($rest, 3)]);
152 private static function getAvatar($uid, $type="avatar")
156 $contact = Contact::getById($uid, ['uid', 'url', 'avatar', 'photo']);
157 if (empty($contact)) {
160 If (($contact['uid'] != 0) && empty($contact['photo']) && empty($contact['avatar'])) {
161 $contact = Contact::getByURL($contact['url'], false, ['avatar', 'photo']);
163 if (!empty($contact['photo'])) {
164 $url = $contact['photo'];
165 } elseif (!empty($contact['avatar'])) {
166 $url = $contact['avatar'];
168 $url = DI::baseUrl() . Contact::DEFAULT_AVATAR_PHOTO;
170 return MPhoto::createPhotoForExternalResource($url);
172 $contact = Contact::getById($uid, ['uid', 'url', 'header']);
173 if (empty($contact)) {
176 If (($contact['uid'] != 0) && empty($contact['header'])) {
177 $contact = Contact::getByURL($contact['url'], false, ['header']);
179 if (!empty($contact['header'])) {
180 $url = $contact['header'];
182 $url = DI::baseUrl() . '/images/blank.png';
184 return MPhoto::createPhotoForExternalResource($url);
197 $photo = MPhoto::selectFirst([], ["scale" => $scale, "uid" => $uid, "profile" => 1]);
199 $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]) ?: [];
204 $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
207 $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
211 $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_THUMB);
214 $parts = parse_url($default);
215 if (!empty($parts['scheme']) || !empty($parts['host'])) {
216 $photo = MPhoto::createPhotoForExternalResource($default);
218 $photo = MPhoto::createPhotoForSystemResource($default);