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