]> git.mxchange.org Git - friendica.git/blob - src/Module/Photo.php
13d9253767b5048f2e8016004e98bab02236786c
[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         /**
25          * @brief Module initializer
26          *
27          * Fetch a photo or an avatar, in optional size, check for permissions and
28          * return the image
29          */
30         public static function init()
31         {
32                 $a = self::getApp();
33                 if ($a->argc <= 1 || $a->argc > 4) {
34                         System::httpExit(400, "Bad Request");
35                 }
36
37                 if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
38                         header("HTTP/1.1 304 Not Modified");
39                         header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
40                         if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
41                                 header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
42                         }
43                         header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
44                         header("Cache-Control: max-age=31536000");
45                         if (function_exists("header_remove")) {
46                                 header_remove("Last-Modified");
47                                 header_remove("Expires");
48                                 header_remove("Cache-Control");
49                         }
50                         exit;
51                 }
52
53                 $customsize = 0;
54                 switch($a->argc) {
55                 case 4:
56                         $customsize = intval($a->argv[2]);
57                         $uid = self::stripExtension($a->argv[3]);
58                         $photo = self::getAvatar($uid, $a->argv[1]);
59                         break;
60                 case 3:
61                         $uid = self::stripExtension($a->argv[2]);
62                         $photo = self::getAvatar($uid, $a->argv[1]);
63                         break;
64                 case 2:
65                         $photoid = self::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                         break;
73                 }
74
75                 if ($photo===false) {
76                         // not using System::httpExit() because we don't want html here.
77                         header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found" , true, 404);
78                         killme();
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                         System::httpExit(500, "Internal Server Error");
88                 }
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
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
118                 echo $img->asString();
119
120
121                 killme();
122         }
123
124         private static function stripExtension($name)
125         {
126                 $name = str_replace([".jpg", ".png", ".gif"], ["", "", ""], $name);
127                 foreach (Image::supportedTypes() as $m => $e) {
128                         $name = str_replace("." . $e, "", $name);
129                 }
130                 return $name;
131         }
132
133         private static function getAvatar($uid, $type="avatar")
134         {
135
136                 switch($type) {
137                 case "profile":
138                 case "custom":
139                         $scale = 4;
140                         $default = "images/person-300.jpg";
141                         break;
142                 case "micro":
143                         $scale = 6;
144                         $default = "images/person-48.jpg";
145                         break;
146                 case "avatar":
147                 default:
148                         $scale = 5;
149                         $default = "images/person-80.jpg";
150                 }
151
152                 $photo = MPhoto::selectFirst([], ["scale" => $scale, "uid" => $uid, "profile" => 1]);
153                 if ($photo===false) {
154                         $photo = MPhoto::createPhotoForSystemResource($default);
155                 }
156                 return $photo;
157         }
158
159 }