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