]> git.mxchange.org Git - friendica.git/blob - mod/photo.php
Separate Object\Photo into Model\Photo and Object\Image
[friendica.git] / mod / photo.php
1 <?php
2 /**
3  * @file mod/photo.php
4  */
5 use Friendica\App;
6 use Friendica\Database\DBM;
7 use Friendica\Object\Image;
8
9 require_once 'include/security.php';
10
11 function photo_init(App $a) {
12         global $_SERVER;
13
14         $prvcachecontrol = false;
15         $file = "";
16
17         switch ($a->argc) {
18                 case 4:
19                         $person = $a->argv[3];
20                         $customres = intval($a->argv[2]);
21                         $type = $a->argv[1];
22                         break;
23                 case 3:
24                         $person = $a->argv[2];
25                         $type = $a->argv[1];
26                         break;
27                 case 2:
28                         $photo = $a->argv[1];
29                         $file = $photo;
30                         break;
31                 case 1:
32                 default:
33                         killme();
34                         // NOTREACHED
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                 header('Etag: '.$_SERVER['HTTP_IF_NONE_MATCH']);
41                 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
42                 header("Cache-Control: max-age=31536000");
43                 if (function_exists('header_remove')) {
44                         header_remove('Last-Modified');
45                         header_remove('Expires');
46                         header_remove('Cache-Control');
47                 }
48                 exit;
49         }
50
51         $default = 'images/person-175.jpg';
52
53         if (isset($type)) {
54
55                 /**
56                  * Profile photos
57                  */
58
59                 switch ($type) {
60
61                         case 'profile':
62                         case 'custom':
63                                 $resolution = 4;
64                                 break;
65                         case 'micro':
66                                 $resolution = 6;
67                                 $default = 'images/person-48.jpg';
68                                 break;
69                         case 'avatar':
70                         default:
71                                 $resolution = 5;
72                                 $default = 'images/person-80.jpg';
73                                 break;
74                 }
75
76                 $uid = str_replace(array('.jpg', '.png', '.gif'), array('', '', ''), $person);
77
78                 foreach (Image::supportedTypes() AS $m => $e) {
79                         $uid = str_replace('.'.$e, '', $uid);
80                 }
81
82                 $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
83                         intval($resolution),
84                         intval($uid)
85                 );
86                 if (DBM::is_result($r)) {
87                         $data = $r[0]['data'];
88                         $mimetype = $r[0]['type'];
89                 }
90                 if (empty($data)) {
91                         $data = file_get_contents($default);
92                         $mimetype = 'image/jpeg';
93                 }
94         } else {
95
96                 /**
97                  * Other photos
98                  */
99
100                 $resolution = 0;
101                 $photo = str_replace(array('.jpg', '.png', '.gif'), array('', '', ''), $photo);
102
103                 foreach (Image::supportedTypes() AS $m => $e) {
104                         $photo = str_replace('.'.$e, '', $photo);
105                 }
106
107                 if (substr($photo, -2, 1) == '-') {
108                         $resolution = intval(substr($photo, -1, 1));
109                         $photo = substr($photo, 0, -2);
110                 }
111
112                 // check if the photo exists and get the owner of the photo
113                 $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
114                         dbesc($photo),
115                         intval($resolution)
116                 );
117                 if (DBM::is_result($r)) {
118
119                         $sql_extra = permissions_sql($r[0]['uid']);
120
121                         // Now we'll see if we can access the photo
122
123                         $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` <= %d $sql_extra ORDER BY scale DESC LIMIT 1",
124                                 dbesc($photo),
125                                 intval($resolution)
126                         );
127
128                         $public = (DBM::is_result($r)) && ($r[0]['allow_cid'] == '') && ($r[0]['allow_gid'] == '') && ($r[0]['deny_cid']  == '') && ($r[0]['deny_gid']  == '');
129
130                         if (DBM::is_result($r)) {
131                                 $resolution = $r[0]['scale'];
132                                 $data = $r[0]['data'];
133                                 $mimetype = $r[0]['type'];
134                         } else {
135                                 // The picure exists. We already checked with the first query.
136                                 // obviously, this is not an authorized viev!
137                                 $data = file_get_contents('images/nosign.jpg');
138                                 $mimetype = 'image/jpeg';
139                                 $prvcachecontrol = true;
140                                 $public = false;
141                         }
142                 }
143         }
144
145         if (empty($data)) {
146                 if (isset($resolution)) {
147                         switch ($resolution) {
148
149                                 case 4:
150                                         $data = file_get_contents('images/person-175.jpg');
151                                         $mimetype = 'image/jpeg';
152                                         break;
153                                 case 5:
154                                         $data = file_get_contents('images/person-80.jpg');
155                                         $mimetype = 'image/jpeg';
156                                         break;
157                                 case 6:
158                                         $data = file_get_contents('images/person-48.jpg');
159                                         $mimetype = 'image/jpeg';
160                                         break;
161                                 default:
162                                         killme();
163                                         // NOTREACHED
164                                         break;
165                         }
166                 }
167         }
168
169         // Resize only if its not a GIF and it is supported by the library
170         if (($mimetype != "image/gif") && in_array($mimetype, Image::supportedTypes())) {
171                 $Image = new Image($data, $mimetype);
172                 if ($Image->isValid()) {
173                         if (isset($customres) && $customres > 0 && $customres < 500) {
174                                 $Image->scaleToSquare($customres);
175                         }
176                         $data = $Image->asString();
177                         $mimetype = $Image->getType();
178                 }
179         }
180
181         if (function_exists('header_remove')) {
182                 header_remove('Pragma');
183                 header_remove('pragma');
184         }
185
186         header("Content-type: ".$mimetype);
187
188         if ($prvcachecontrol) {
189
190                 // it is a private photo that they have no permission to view.
191                 // tell the browser not to cache it, in case they authenticate
192                 // and subsequently have permission to see it
193
194                 header("Cache-Control: no-store, no-cache, must-revalidate");
195
196         } else {
197                 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
198                 header('Etag: "'.md5($data).'"');
199                 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
200                 header("Cache-Control: max-age=31536000");
201         }
202         echo $data;
203
204         // If the photo is public and there is an existing photo directory store the photo there
205         if ($public and ($file != "")) {
206                 // If the photo path isn't there, try to create it
207                 $basepath = $a->get_basepath();
208                 if (!is_dir($basepath."/photo")) {
209                         if (is_writable($basepath)) {
210                                 mkdir($basepath."/photo");
211                         }
212                 }
213
214                 if (is_dir($basepath."/photo")) {
215                         file_put_contents($basepath."/photo/".$file, $data);
216                 }
217         }
218
219         killme();
220         // NOTREACHED
221 }