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