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