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