]> git.mxchange.org Git - friendica.git/blob - mod/proxy.php
Class file relocations
[friendica.git] / mod / proxy.php
1 <?php
2 // Based upon "Privacy Image Cache" by Tobias Hößl <https://github.com/CatoTH/>
3
4 use Friendica\App;
5 use Friendica\Core\Config;
6 use Friendica\Core\System;
7 use Friendica\Database\DBM;
8
9 define('PROXY_DEFAULT_TIME', 86400); // 1 Day
10
11 define('PROXY_SIZE_MICRO', 'micro');
12 define('PROXY_SIZE_THUMB', 'thumb');
13 define('PROXY_SIZE_SMALL', 'small');
14 define('PROXY_SIZE_MEDIUM', 'medium');
15 define('PROXY_SIZE_LARGE', 'large');
16
17 require_once 'include/security.php';
18 require_once 'include/Photo.php';
19
20 function proxy_init(App $a) {
21         // Pictures are stored in one of the following ways:
22         // 1. If a folder "proxy" exists and is writeable, then use this for caching
23         // 2. If a cache path is defined, use this
24         // 3. If everything else failed, cache into the database
25         //
26         // Question: Do we really need these three methods?
27
28         if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
29                 header('HTTP/1.1 304 Not Modified');
30                 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
31                 header('Etag: ' . $_SERVER['HTTP_IF_NONE_MATCH']);
32                 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
33                 header('Cache-Control: max-age=31536000');
34
35                 if (function_exists('header_remove')) {
36                         header_remove('Last-Modified');
37                         header_remove('Expires');
38                         header_remove('Cache-Control');
39                 }
40                 exit;
41         }
42
43         if (function_exists('header_remove')) {
44                 header_remove('Pragma');
45                 header_remove('pragma');
46         }
47
48         $thumb = false;
49         $size = 1024;
50         $sizetype = '';
51         $basepath = $a->get_basepath();
52
53         // If the cache path isn't there, try to create it
54         if (!is_dir($basepath . '/proxy') && is_writable($basepath)) {
55                 mkdir($basepath . '/proxy');
56         }
57
58         // Checking if caching into a folder in the webroot is activated and working
59         $direct_cache = (is_dir($basepath . '/proxy') && is_writable($basepath . '/proxy'));
60
61         // Look for filename in the arguments
62         if ((isset($a->argv[1]) || isset($a->argv[2]) || isset($a->argv[3])) && !isset($_REQUEST['url'])) {
63                 if (isset($a->argv[3])) {
64                         $url = $a->argv[3];
65                 } elseif (isset($a->argv[2])) {
66                         $url = $a->argv[2];
67                 } else {
68                         $url = $a->argv[1];
69                 }
70
71                 if (isset($a->argv[3]) && ($a->argv[3] == 'thumb')) {
72                         $size = 200;
73                 }
74
75                 // thumb, small, medium and large.
76                 if (substr($url, -6) == ':micro') {
77                         $size = 48;
78                         $sizetype = ':micro';
79                         $url = substr($url, 0, -6);
80                 } elseif (substr($url, -6) == ':thumb') {
81                         $size = 80;
82                         $sizetype = ':thumb';
83                         $url = substr($url, 0, -6);
84                 } elseif (substr($url, -6) == ':small') {
85                         $size = 175;
86                         $url = substr($url, 0, -6);
87                         $sizetype = ':small';
88                 } elseif (substr($url, -7) == ':medium') {
89                         $size = 600;
90                         $url = substr($url, 0, -7);
91                         $sizetype = ':medium';
92                 } elseif (substr($url, -6) == ':large') {
93                         $size = 1024;
94                         $url = substr($url, 0, -6);
95                         $sizetype = ':large';
96                 }
97
98                 $pos = strrpos($url, '=.');
99                 if ($pos) {
100                         $url = substr($url, 0, $pos + 1);
101                 }
102
103                 $url = str_replace(array('.jpg', '.jpeg', '.gif', '.png'), array('','','',''), $url);
104
105                 $url = base64_decode(strtr($url, '-_', '+/'), true);
106
107                 if ($url) {
108                         $_REQUEST['url'] = $url;
109                 }
110         } else {
111                 $direct_cache = false;
112         }
113
114         if (!$direct_cache) {
115                 $urlhash = 'pic:' . sha1($_REQUEST['url']);
116
117                 $cachefile = get_cachefile(hash('md5', $_REQUEST['url']));
118                 if ($cachefile != '' && file_exists($cachefile)) {
119                         $img_str = file_get_contents($cachefile);
120                         $mime = image_type_to_mime_type(exif_imagetype($cachefile));
121
122                         header('Content-type: ' . $mime);
123                         header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
124                         header('Etag: "' . md5($img_str) . '"');
125                         header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
126                         header('Cache-Control: max-age=31536000');
127
128                         // reduce quality - if it isn't a GIF
129                         if ($mime != 'image/gif') {
130                                 $img = new Photo($img_str, $mime);
131                                 if ($img->is_valid()) {
132                                         $img_str = $img->imageString();
133                                 }
134                         }
135
136                         echo $img_str;
137                         killme();
138                 }
139         } else {
140                 $cachefile = '';
141         }
142
143         $valid = true;
144         $r = array();
145
146         if (!$direct_cache && ($cachefile == '')) {
147                 $r = dba::select('photo', array('data', 'desc'), array('resource-id' => $urlhash), array('limit' => 1));
148                 if (DBM::is_result($r)) {
149                         $img_str = $r['data'];
150                         $mime = $r['desc'];
151                         if ($mime == '') {
152                                 $mime = 'image/jpeg';
153                         }
154                 }
155         }
156
157         if (!DBM::is_result($r)) {
158                 // It shouldn't happen but it does - spaces in URL
159                 $_REQUEST['url'] = str_replace(' ', '+', $_REQUEST['url']);
160                 $redirects = 0;
161                 $img_str = fetch_url($_REQUEST['url'], true, $redirects, 10);
162
163                 $tempfile = tempnam(get_temppath(), 'cache');
164                 file_put_contents($tempfile, $img_str);
165                 $mime = image_type_to_mime_type(exif_imagetype($tempfile));
166                 unlink($tempfile);
167
168                 // If there is an error then return a blank image
169                 if ((substr($a->get_curl_code(), 0, 1) == '4') || (!$img_str)) {
170                         $img_str = file_get_contents('images/blank.png');
171                         $mime = 'image/png';
172                         $cachefile = ''; // Clear the cachefile so that the dummy isn't stored
173                         $valid = false;
174                         $img = new Photo($img_str, 'image/png');
175                         if ($img->is_valid()) {
176                                 $img->scaleImage(10);
177                                 $img_str = $img->imageString();
178                         }
179                 } elseif ($mime != 'image/jpeg' && !$direct_cache && $cachefile == '') {
180                         $image = @imagecreatefromstring($img_str);
181
182                         if ($image === FALSE) {
183                                 die();
184                         }
185
186                         $fields = array('uid' => 0, 'contact-id' => 0, 'guid' => get_guid(), 'resource-id' => $urlhash, 'created' => datetime_convert(), 'edited' => datetime_convert(),
187                                 'filename' => basename($_REQUEST['url']), 'type' => '', 'album' => '', 'height' => imagesy($image), 'width' => imagesx($image),
188                                 'datasize' => 0, 'data' => $img_str, 'scale' => 100, 'profile' => 0,
189                                 'allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '', 'desc' => $mime);
190                         dba::insert('photo', $fields);
191                 } else {
192                         $img = new Photo($img_str, $mime);
193                         if ($img->is_valid() && !$direct_cache && ($cachefile == '')) {
194                                 $img->store(0, 0, $urlhash, $_REQUEST['url'], '', 100);
195                         }
196                 }
197         }
198
199         $img_str_orig = $img_str;
200
201         // reduce quality - if it isn't a GIF
202         if ($mime != 'image/gif') {
203                 $img = new Photo($img_str, $mime);
204                 if ($img->is_valid()) {
205                         $img->scaleImage($size);
206                         $img_str = $img->imageString();
207                 }
208         }
209
210         // If there is a real existing directory then put the cache file there
211         // advantage: real file access is really fast
212         // Otherwise write in cachefile
213         if ($valid && $direct_cache) {
214                 file_put_contents($basepath . '/proxy/' . proxy_url($_REQUEST['url'], true), $img_str_orig);
215                 if ($sizetype != '') {
216                         file_put_contents($basepath . '/proxy/' . proxy_url($_REQUEST['url'], true) . $sizetype, $img_str);
217                 }
218         } elseif ($cachefile != '') {
219                 file_put_contents($cachefile, $img_str_orig);
220         }
221
222         header('Content-type: ' . $mime);
223
224         // Only output the cache headers when the file is valid
225         if ($valid) {
226                 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
227                 header('Etag: "' . md5($img_str) . '"');
228                 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
229                 header('Cache-Control: max-age=31536000');
230         }
231
232         echo $img_str;
233
234         killme();
235 }
236
237 /**
238  * @brief Transform a remote URL into a local one
239  *
240  * This function only performs the URL replacement on http URL and if the
241  * provided URL isn't local, "the isn't deactivated" (sic) and if the config
242  * system.proxy_disabled is set to false.
243  *
244  * @param string $url       The URL to proxyfy
245  * @param bool   $writemode Returns a local path the remote URL should be saved to
246  * @param string $size      One of the PROXY_SIZE_* constants
247  *
248  * @return string The proxyfied URL or relative path
249  */
250 function proxy_url($url, $writemode = false, $size = '') {
251         $a = get_app();
252
253         if (substr($url, 0, strlen('http')) !== 'http') {
254                 return $url;
255         }
256
257         // Only continue if it isn't a local image and the isn't deactivated
258         if (proxy_is_local_image($url)) {
259                 $url = str_replace(normalise_link(System::baseUrl()) . '/', System::baseUrl() . '/', $url);
260                 return $url;
261         }
262
263         if (Config::get('system', 'proxy_disabled')) {
264                 return $url;
265         }
266
267         // Image URL may have encoded ampersands for display which aren't desirable for proxy
268         $url = html_entity_decode($url, ENT_NOQUOTES, 'utf-8');
269
270         // Creating a sub directory to reduce the amount of files in the cache directory
271         $basepath = $a->get_basepath() . '/proxy';
272
273         $shortpath = hash('md5', $url);
274         $longpath = substr($shortpath, 0, 2);
275
276         if (is_dir($basepath) && $writemode && !is_dir($basepath . '/' . $longpath)) {
277                 mkdir($basepath . '/' . $longpath);
278                 chmod($basepath . '/' . $longpath, 0777);
279         }
280
281         $longpath .= '/' . strtr(base64_encode($url), '+/', '-_');
282
283         // Extract the URL extension
284         $extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
285
286         $extensions = array('jpg', 'jpeg', 'gif', 'png');
287         if (in_array($extension, $extensions)) {
288                 $shortpath .= '.' . $extension;
289                 $longpath .= '.' . $extension;
290         }
291
292         $proxypath = System::baseUrl() . '/proxy/' . $longpath;
293
294         if ($size != '') {
295                 $size = ':' . $size;
296         }
297
298         // Too long files aren't supported by Apache
299         // Writemode in combination with long files shouldn't be possible
300         if ((strlen($proxypath) > 250) && $writemode) {
301                 return $shortpath;
302         } elseif (strlen($proxypath) > 250) {
303                 return System::baseUrl() . '/proxy/' . $shortpath . '?url=' . urlencode($url);
304         } elseif ($writemode) {
305                 return $longpath;
306         } else {
307                 return $proxypath . $size;
308         }
309 }
310
311 /**
312  * @param $url string
313  * @return boolean
314  */
315 function proxy_is_local_image($url) {
316         if ($url[0] == '/') {
317                 return true;
318         }
319
320         if (strtolower(substr($url, 0, 5)) == 'data:') {
321                 return true;
322         }
323
324         // links normalised - bug #431
325         $baseurl = normalise_link(System::baseUrl());
326         $url = normalise_link($url);
327         return (substr($url, 0, strlen($baseurl)) == $baseurl);
328 }
329
330 /**
331  * @brief Return the array of query string parameters from a URL
332  *
333  * @param string $url
334  * @return array Associative array of query string parameters
335  */
336 function proxy_parse_query($url) {
337         $query = parse_url($url, PHP_URL_QUERY);
338         $query = html_entity_decode($query);
339         $query_list = explode('&', $query);
340         $arr = array();
341
342         foreach ($query_list as $key_value) {
343                 $key_value_list = explode('=', $key_value);
344                 $arr[$key_value_list[0]] = $key_value_list[1];
345         }
346
347         unset($url, $query_list, $url);
348         return $arr;
349 }
350
351 function proxy_img_cb($matches) {
352         // if the picture seems to be from another picture cache then take the original source
353         $queryvar = proxy_parse_query($matches[2]);
354         if (($queryvar['url'] != '') && (substr($queryvar['url'], 0, 4) == 'http')) {
355                 $matches[2] = urldecode($queryvar['url']);
356         }
357
358         // following line changed per bug #431
359         if (proxy_is_local_image($matches[2])) {
360                 return $matches[1] . $matches[2] . $matches[3];
361         }
362
363         return $matches[1] . proxy_url(htmlspecialchars_decode($matches[2])) . $matches[3];
364 }
365
366 function proxy_parse_html($html) {
367         $html = str_replace(normalise_link(System::baseUrl()) . '/', System::baseUrl() . '/', $html);
368
369         return preg_replace_callback('/(<img [^>]*src *= *["\'])([^"\']+)(["\'][^>]*>)/siU', 'proxy_img_cb', $html);
370 }