]> git.mxchange.org Git - friendica.git/blob - src/Module/Photo.php
Pluggable storage backends: first steps
[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\Model\Photo as MPhoto;
11 use Friendica\Object\Image;
12 use Friendica\Util\Security;
13 use Friendica\Network\HTTPException\NotFoundException;
14 use Friendica\Network\HTTPException\BadRequestException;
15 use Friendica\Network\HTTPException\InternalServerErrorException;
16
17 /**
18  * @brief Photo Module
19  */
20 class Photo extends BaseModule
21 {
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                         throw new BadRequestException();
34                         killme();
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                         header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found" , true, 404);
77                         killme();
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                         throw new InternalServerErrorException();
87                 }
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
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                         header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
110                         header('Etag: "' . md5($img->asString()) . '"');
111                         header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
112                         header("Cache-Control: max-age=31536000");
113                 }
114
115
116
117                 echo $img->asString();
118
119
120                 killme();
121         }
122
123         private static function stripExtension($name)
124         {
125                 $name = str_replace([".jpg", ".png", ".gif"], ["", "", ""], $name);
126                 foreach (Image::supportedTypes() AS $m => $e) {
127                         $name = str_replace('.' . $e, '', $name);
128                 }
129                 return $name;
130         }
131
132         private static function getAvatar($uid, $type="avatar")
133         {
134
135                 switch($type) {
136                 case "profile":
137                 case "custom":
138                         $scale = 4;
139                         $default = "images/person-300.jpg";
140                         break;
141                 case "micro":
142                         $scale = 6;
143                         $default = "images/person-48.jpg";
144                         break;
145                 case "avatar":
146                 default:
147                         $scale = 5;
148                         $default = "images/person-80.jpg";
149                 }
150
151                 $photo = MPhoto::selectFirst([], ["scale" => $scale, "uid" => $uid, "profile" => 1]);
152                 if ($photo===false) {
153                         // todo default image info
154                 }
155                 return $photo;
156         }
157
158 }