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