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