3 * @copyright Copyright (C) 2020, Friendica
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;
35 class Photo extends BaseModule
40 * Fetch a photo or an avatar, in optional size, check for permissions and
43 public static function init(array $parameters = [])
46 // @TODO: Replace with parameter from router
47 if ($a->argc <= 1 || $a->argc > 4) {
48 throw new \Friendica\Network\HTTPException\BadRequestException();
51 if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
52 header("HTTP/1.1 304 Not Modified");
53 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
54 if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
55 header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
57 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
58 header("Cache-Control: max-age=31536000");
59 if (function_exists("header_remove")) {
60 header_remove("Last-Modified");
61 header_remove("Expires");
62 header_remove("Cache-Control");
69 // @TODO: Replace with parameter from router
72 $customsize = intval($a->argv[2]);
73 $uid = MPhoto::stripExtension($a->argv[3]);
74 $photo = self::getAvatar($uid, $a->argv[1]);
77 $uid = MPhoto::stripExtension($a->argv[2]);
78 $photo = self::getAvatar($uid, $a->argv[1]);
81 $photoid = MPhoto::stripExtension($a->argv[1]);
83 if (substr($photoid, -2, 1) == "-") {
84 $scale = intval(substr($photoid, -1, 1));
85 $photoid = substr($photoid, 0, -2);
87 $photo = MPhoto::getPhoto($photoid, $scale);
88 if ($photo === false) {
89 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('The Photo with id %s is not available.', $photoid));
94 if ($photo === false) {
95 throw new \Friendica\Network\HTTPException\NotFoundException();
98 $cacheable = ($photo["allow_cid"] . $photo["allow_gid"] . $photo["deny_cid"] . $photo["deny_gid"] === "") && (isset($photo["cacheable"]) ? $photo["cacheable"] : true);
100 $img = MPhoto::getImageForPhoto($photo);
102 if (is_null($img) || !$img->isValid()) {
103 Logger::log("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 ($img->getType() !== "image/gif" && $customsize > 0 && $customsize < 501) {
109 $img->scaleToSquare($customsize);
112 if (function_exists("header_remove")) {
113 header_remove("Pragma");
114 header_remove("pragma");
117 header("Content-type: " . $img->getType());
120 // it is a private photo that they have no permission to view.
121 // tell the browser not to cache it, in case they authenticate
122 // and subsequently have permission to see it
123 header("Cache-Control: no-store, no-cache, must-revalidate");
125 $md5 = md5($img->asString());
126 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
127 header("Etag: \"{$md5}\"");
128 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
129 header("Cache-Control: max-age=31536000");
132 echo $img->asString();
137 private static function getAvatar($uid, $type="avatar")
152 $photo = MPhoto::selectFirst([], ["scale" => $scale, "uid" => $uid, "profile" => 1]);
154 $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]) ?: [];
159 $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
162 $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
166 $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_THUMB);
169 $photo = MPhoto::createPhotoForSystemResource($default);