]> git.mxchange.org Git - friendica.git/blob - src/Module/Photo.php
Inverted condition
[friendica.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
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"]);
53                         }
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");
60                         }
61                         exit;
62                 }
63
64                 $customsize = 0;
65                 $photo = false;
66                 $scale = null;
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']);
77                         $scale = 0;
78                         if (substr($photoid, -2, 1) == "-") {
79                                 $scale = intval(substr($photoid, -1, 1));
80                                 $photoid = substr($photoid, 0, -2);
81                         }
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));
85                         }
86                 } else {
87                         throw new \Friendica\Network\HTTPException\BadRequestException();
88                 }
89                 $fetch = microtime(true) - $stamp;
90
91                 if ($photo === false) {
92                         throw new \Friendica\Network\HTTPException\NotFoundException();
93                 }
94
95                 $cacheable = ($photo["allow_cid"] . $photo["allow_gid"] . $photo["deny_cid"] . $photo["deny_gid"] === "") && (isset($photo["cacheable"]) ? $photo["cacheable"] : true);
96
97                 $stamp = microtime(true);
98                 $imgdata = MPhoto::getImageDataForPhoto($photo);
99                 $data = microtime(true) - $stamp;
100
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"]));
104                 }
105
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();
111                 }
112
113                 if (function_exists("header_remove")) {
114                         header_remove("Pragma");
115                         header_remove("pragma");
116                 }
117
118                 header("Content-type: " . $photo['type']);
119
120                 $stamp = microtime(true);
121                 if (!$cacheable) {
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");
126                 } else {
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");
132                 }
133                 $checksum = microtime(true) - $stamp;
134
135                 $stamp = microtime(true);
136                 echo $imgdata;
137                 $output = microtime(true) - $stamp;
138
139                 $total = microtime(true) - $totalstamp;
140                 $rest = $total - ($fetch + $data + $checksum + $output);
141
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)]);
147                 }
148
149                 exit();
150         }
151
152         private static function getAvatar($uid, $type="avatar")
153         {
154                 switch($type) {
155                         case "contact":
156                                 $contact = Contact::getById($uid, ['uid', 'url', 'avatar', 'photo']);
157                                 if (empty($contact)) {
158                                         return false;
159                                 }
160                                 If (($contact['uid'] != 0) && empty($contact['photo']) && empty($contact['avatar'])) {
161                                         $contact = Contact::getByURL($contact['url'], false, ['avatar', 'photo']);
162                                 }
163                                 if (!empty($contact['photo'])) {
164                                         $url = $contact['photo'];
165                                 } elseif (!empty($contact['avatar'])) {
166                                         $url = $contact['avatar'];
167                                 } else {
168                                         $url = DI::baseUrl() . Contact::DEFAULT_AVATAR_PHOTO;
169                                 }
170                                 return MPhoto::createPhotoForExternalResource($url);
171                         case "header":
172                                 $contact = Contact::getById($uid, ['uid', 'url', 'header']);
173                                 if (empty($contact)) {
174                                         return false;
175                                 }
176                                 If (($contact['uid'] != 0) && empty($contact['header'])) {
177                                         $contact = Contact::getByURL($contact['url'], false, ['header']);
178                                 }
179                                 if (!empty($contact['header'])) {
180                                         $url = $contact['header'];
181                                 } else {
182                                         $url = DI::baseUrl() . '/images/blank.png';
183                                 }
184                                 return MPhoto::createPhotoForExternalResource($url);
185                         case "profile":
186                         case "custom":
187                                 $scale = 4;
188                                 break;
189                         case "micro":
190                                 $scale = 6;
191                                 break;
192                         case "avatar":
193                         default:
194                                 $scale = 5;
195                 }
196
197                 $photo = MPhoto::selectFirst([], ["scale" => $scale, "uid" => $uid, "profile" => 1]);
198                 if (empty($photo)) {
199                         $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]) ?: [];
200
201                         switch($type) {
202                                 case "profile":
203                                 case "custom":
204                                         $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
205                                         break;
206                                 case "micro":
207                                         $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
208                                         break;
209                                 case "avatar":
210                                 default:
211                                         $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_THUMB);
212                         }
213         
214                         $parts = parse_url($default);
215                         if (!empty($parts['scheme']) || !empty($parts['host'])) {
216                                 $photo = MPhoto::createPhotoForExternalResource($default);
217                         } else {
218                                 $photo = MPhoto::createPhotoForSystemResource($default);
219                         }
220                 }
221                 return $photo;
222         }
223 }