]> git.mxchange.org Git - friendica.git/blob - src/Module/Photo.php
Add new TemplateEngine->testInstall method
[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\Core\System;
27 use Friendica\DI;
28 use Friendica\Model\Photo as MPhoto;
29
30 /**
31  * Photo Module
32  */
33 class Photo extends BaseModule
34 {
35         /**
36          * Module initializer
37          *
38          * Fetch a photo or an avatar, in optional size, check for permissions and
39          * return the image
40          */
41         public static function init(array $parameters = [])
42         {
43                 $a = DI::app();
44                 // @TODO: Replace with parameter from router
45                 if ($a->argc <= 1 || $a->argc > 4) {
46                         throw new \Friendica\Network\HTTPException\BadRequestException();
47                 }
48
49                 if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
50                         header("HTTP/1.1 304 Not Modified");
51                         header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
52                         if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
53                                 header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
54                         }
55                         header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
56                         header("Cache-Control: max-age=31536000");
57                         if (function_exists("header_remove")) {
58                                 header_remove("Last-Modified");
59                                 header_remove("Expires");
60                                 header_remove("Cache-Control");
61                         }
62                         exit;
63                 }
64
65                 $customsize = 0;
66                 $photo = false;
67                 // @TODO: Replace with parameter from router
68                 switch($a->argc) {
69                         case 4:
70                                 $customsize = intval($a->argv[2]);
71                                 $uid = MPhoto::stripExtension($a->argv[3]);
72                                 $photo = self::getAvatar($uid, $a->argv[1]);
73                                 break;
74                         case 3:
75                                 $uid = MPhoto::stripExtension($a->argv[2]);
76                                 $photo = self::getAvatar($uid, $a->argv[1]);
77                                 break;
78                         case 2:
79                                 $photoid = MPhoto::stripExtension($a->argv[1]);
80                                 $scale = 0;
81                                 if (substr($photoid, -2, 1) == "-") {
82                                         $scale = intval(substr($photoid, -1, 1));
83                                         $photoid = substr($photoid, 0, -2);
84                                 }
85                                 $photo = MPhoto::getPhoto($photoid, $scale);
86                                 if ($photo === false) {
87                                         throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('The Photo with id %s is not available.', $photoid));
88                                 }
89                                 break;
90                 }
91
92                 if ($photo === false) {
93                         throw new \Friendica\Network\HTTPException\NotFoundException();
94                 }
95
96                 $cacheable = ($photo["allow_cid"] . $photo["allow_gid"] . $photo["deny_cid"] . $photo["deny_gid"] === "") && (isset($photo["cacheable"]) ? $photo["cacheable"] : true);
97
98                 $img = MPhoto::getImageForPhoto($photo);
99
100                 if (is_null($img) || !$img->isValid()) {
101                         Logger::log("Invalid photo with id {$photo["id"]}.");
102                         throw new \Friendica\Network\HTTPException\InternalServerErrorException(DI::l10n()->t('Invalid photo with id %s.', $photo["id"]));
103                 }
104
105                 // if customsize is set and image is not a gif, resize it
106                 if ($img->getType() !== "image/gif" && $customsize > 0 && $customsize < 501) {
107                         $img->scaleToSquare($customsize);
108                 }
109
110                 if (function_exists("header_remove")) {
111                         header_remove("Pragma");
112                         header_remove("pragma");
113                 }
114
115                 header("Content-type: " . $img->getType());
116
117                 if (!$cacheable) {
118                         // it is a private photo that they have no permission to view.
119                         // tell the browser not to cache it, in case they authenticate
120                         // and subsequently have permission to see it
121                         header("Cache-Control: no-store, no-cache, must-revalidate");
122                 } else {
123                         $md5 = md5($img->asString());
124                         header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
125                         header("Etag: \"{$md5}\"");
126                         header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
127                         header("Cache-Control: max-age=31536000");
128                 }
129
130                 echo $img->asString();
131
132                 exit();
133         }
134
135         private static function getAvatar($uid, $type="avatar")
136         {
137
138                 switch($type) {
139                 case "profile":
140                 case "custom":
141                         $scale = 4;
142                         $default = "images/person-300.jpg";
143                         break;
144                 case "micro":
145                         $scale = 6;
146                         $default = "images/person-48.jpg";
147                         break;
148                 case "avatar":
149                 default:
150                         $scale = 5;
151                         $default = "images/person-80.jpg";
152                 }
153
154                 $photo = MPhoto::selectFirst([], ["scale" => $scale, "uid" => $uid, "profile" => 1]);
155                 if ($photo === false) {
156                         $photo = MPhoto::createPhotoForSystemResource($default);
157                 }
158                 return $photo;
159         }
160
161 }