]> git.mxchange.org Git - friendica.git/blob - src/Module/Photo.php
0b6874b7989c685a4b8f37850cb2fd4266ae23d0
[friendica.git] / src / Module / Photo.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Util\Proxy;
31
32 /**
33  * Photo Module
34  */
35 class Photo extends BaseModule
36 {
37         /**
38          * Module initializer
39          *
40          * Fetch a photo or an avatar, in optional size, check for permissions and
41          * return the image
42          */
43         public static function rawContent(array $parameters = [])
44         {
45                 $totalstamp = microtime(true);
46                 $a = DI::app();
47                 // @TODO: Replace with parameter from router
48                 if ($a->argc <= 1 || $a->argc > 4) {
49                         throw new \Friendica\Network\HTTPException\BadRequestException();
50                 }
51
52                 if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
53                         header("HTTP/1.1 304 Not Modified");
54                         header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
55                         if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
56                                 header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
57                         }
58                         header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
59                         header("Cache-Control: max-age=31536000");
60                         if (function_exists("header_remove")) {
61                                 header_remove("Last-Modified");
62                                 header_remove("Expires");
63                                 header_remove("Cache-Control");
64                         }
65                         exit;
66                 }
67
68                 $customsize = 0;
69                 $photo = false;
70                 $scale = null;
71                 // @TODO: Replace with parameter from router
72                 $stamp = microtime(true);
73                 switch($a->argc) {
74                         case 4:
75                                 $customsize = intval($a->argv[2]);
76                                 $uid = MPhoto::stripExtension($a->argv[3]);
77                                 $photo = self::getAvatar($uid, $a->argv[1]);
78                                 break;
79                         case 3:
80                                 $uid = MPhoto::stripExtension($a->argv[2]);
81                                 $photo = self::getAvatar($uid, $a->argv[1]);
82                                 break;
83                         case 2:
84                                 $photoid = MPhoto::stripExtension($a->argv[1]);
85                                 $scale = 0;
86                                 if (substr($photoid, -2, 1) == "-") {
87                                         $scale = intval(substr($photoid, -1, 1));
88                                         $photoid = substr($photoid, 0, -2);
89                                 }
90                                 $photo = MPhoto::getPhoto($photoid, $scale);
91                                 if ($photo === false) {
92                                         throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('The Photo with id %s is not available.', $photoid));
93                                 }
94                                 break;
95                 }
96                 $fetch = microtime(true) - $stamp;
97
98                 if ($photo === false) {
99                         throw new \Friendica\Network\HTTPException\NotFoundException();
100                 }
101
102                 $cacheable = ($photo["allow_cid"] . $photo["allow_gid"] . $photo["deny_cid"] . $photo["deny_gid"] === "") && (isset($photo["cacheable"]) ? $photo["cacheable"] : true);
103
104                 $stamp = microtime(true);
105                 $img = MPhoto::getImageForPhoto($photo);
106                 $data = microtime(true) - $stamp;
107
108                 if (is_null($img) || !$img->isValid()) {
109                         Logger::warning("Invalid photo with id {$photo["id"]}.");
110                         throw new \Friendica\Network\HTTPException\InternalServerErrorException(DI::l10n()->t('Invalid photo with id %s.', $photo["id"]));
111                 }
112
113                 // if customsize is set and image is not a gif, resize it
114                 if ($img->getType() !== "image/gif" && $customsize > 0 && $customsize < 501) {
115                         $img->scaleToSquare($customsize);
116                 }
117
118                 if (function_exists("header_remove")) {
119                         header_remove("Pragma");
120                         header_remove("pragma");
121                 }
122
123                 header("Content-type: " . $img->getType());
124
125                 $stamp = microtime(true);
126                 if (!$cacheable) {
127                         // it is a private photo that they have no permission to view.
128                         // tell the browser not to cache it, in case they authenticate
129                         // and subsequently have permission to see it
130                         header("Cache-Control: no-store, no-cache, must-revalidate");
131                 } else {
132                         $md5 = $photo['hash'] ?: md5($img->asString());
133                         header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
134                         header("Etag: \"{$md5}\"");
135                         header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
136                         header("Cache-Control: max-age=31536000");
137                 }
138                 $checksum = microtime(true) - $stamp;
139
140                 $stamp = microtime(true);
141                 echo $img->asString();
142                 $output = microtime(true) - $stamp;
143
144                 $total = microtime(true) - $totalstamp;
145                 $rest = $total - ($fetch + $data + $checksum + $output);
146
147                 if (!is_null($scale) && ($scale < 4)) {
148                         Logger::info('Performance:', ['scale' => $scale, 'resource' => $photo['resource-id'],
149                                 'total' => number_format($total, 3), 'fetch' => number_format($fetch, 3),
150                                 'data' => number_format($data, 3), 'checksum' => number_format($checksum, 3),
151                                 'output' => number_format($output, 3), 'rest' => number_format($rest, 3)]);
152                 }
153
154                 exit();
155         }
156
157         private static function getAvatar($uid, $type="avatar")
158         {
159                 switch($type) {
160                         case "profile":
161                         case "custom":
162                                 $scale = 4;
163                                 break;
164                         case "micro":
165                                 $scale = 6;
166                                 break;
167                         case "avatar":
168                         default:
169                                 $scale = 5;
170                 }
171
172                 $photo = MPhoto::selectFirst([], ["scale" => $scale, "uid" => $uid, "profile" => 1]);
173                 if (empty($photo)) {
174                         $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]) ?: [];
175
176                         switch($type) {
177                                 case "profile":
178                                 case "custom":
179                                         $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
180                                         break;
181                                 case "micro":
182                                         $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
183                                         break;
184                                 case "avatar":
185                                 default:
186                                         $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_THUMB);
187                         }
188         
189                         $photo = MPhoto::createPhotoForSystemResource($default);
190                 }
191                 return $photo;
192         }
193 }