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