]> git.mxchange.org Git - friendica.git/blob - src/Module/Photo.php
Merge branch 'develop' into task/mod_hostxrd
[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                 // @TODO: Replace with parameter from router
29                 if ($a->argc <= 1 || $a->argc > 4) {
30                         System::httpExit(400);
31                 }
32
33                 if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
34                         header("HTTP/1.1 304 Not Modified");
35                         header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
36                         if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
37                                 header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
38                         }
39                         header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
40                         header("Cache-Control: max-age=31536000");
41                         if (function_exists("header_remove")) {
42                                 header_remove("Last-Modified");
43                                 header_remove("Expires");
44                                 header_remove("Cache-Control");
45                         }
46                         exit;
47                 }
48
49                 $customsize = 0;
50                 $photo = false;
51                 // @TODO: Replace with parameter from router
52                 switch($a->argc) {
53                         case 4:
54                                 $customsize = intval($a->argv[2]);
55                                 $uid = self::stripExtension($a->argv[3]);
56                                 $photo = self::getAvatar($uid, $a->argv[1]);
57                                 break;
58                         case 3:
59                                 $uid = self::stripExtension($a->argv[2]);
60                                 $photo = self::getAvatar($uid, $a->argv[1]);
61                                 break;
62                         case 2:
63                                 $photoid = self::stripExtension($a->argv[1]);
64                                 $scale = 0;
65                                 if (substr($photoid, -2, 1) == "-") {
66                                         $scale = intval(substr($photoid, -1, 1));
67                                         $photoid = substr($photoid, 0, -2);
68                                 }
69                                 $photo = MPhoto::getPhoto($photoid, $scale);
70                                 if ($photo === false) {
71                                         $photo = MPhoto::createPhotoForSystemResource("images/nosign.jpg");
72                                 }
73                                 break;
74                 }
75
76                 if ($photo === false) {
77                         // not using System::httpExit() because we don't want html here.
78                         header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found" , true, 404);
79                         exit();
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                         System::httpExit(500, ["description" => "Invalid photo with id {$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 stripExtension($name)
122         {
123                 $name = str_replace([".jpg", ".png", ".gif"], ["", "", ""], $name);
124                 foreach (Image::supportedTypes() as $m => $e) {
125                         $name = str_replace("." . $e, "", $name);
126                 }
127                 return $name;
128         }
129
130         private static function getAvatar($uid, $type="avatar")
131         {
132
133                 switch($type) {
134                 case "profile":
135                 case "custom":
136                         $scale = 4;
137                         $default = "images/person-300.jpg";
138                         break;
139                 case "micro":
140                         $scale = 6;
141                         $default = "images/person-48.jpg";
142                         break;
143                 case "avatar":
144                 default:
145                         $scale = 5;
146                         $default = "images/person-80.jpg";
147                 }
148
149                 $photo = MPhoto::selectFirst([], ["scale" => $scale, "uid" => $uid, "profile" => 1]);
150                 if ($photo === false) {
151                         $photo = MPhoto::createPhotoForSystemResource($default);
152                 }
153                 return $photo;
154         }
155
156 }