4 * @brief Based upon "Privacy Image Cache" by Tobias Hößl <https://github.com/CatoTH/>
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;
14 define('PROXY_DEFAULT_TIME', 86400); // 1 Day
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');
22 require_once 'include/security.php';
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
30 // Question: Do we really need these three methods?
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');
39 if (function_exists('header_remove')) {
40 header_remove('Last-Modified');
41 header_remove('Expires');
42 header_remove('Cache-Control');
47 if (function_exists('header_remove')) {
48 header_remove('Pragma');
49 header_remove('pragma');
55 $basepath = $a->get_basepath();
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');
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'));
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])) {
69 } elseif (isset($a->argv[2])) {
75 if (isset($a->argv[3]) && ($a->argv[3] == 'thumb')) {
79 // thumb, small, medium and large.
80 if (substr($url, -6) == ':micro') {
83 $url = substr($url, 0, -6);
84 } elseif (substr($url, -6) == ':thumb') {
87 $url = substr($url, 0, -6);
88 } elseif (substr($url, -6) == ':small') {
90 $url = substr($url, 0, -6);
92 } elseif (substr($url, -7) == ':medium') {
94 $url = substr($url, 0, -7);
95 $sizetype = ':medium';
96 } elseif (substr($url, -6) == ':large') {
98 $url = substr($url, 0, -6);
102 $pos = strrpos($url, '=.');
104 $url = substr($url, 0, $pos + 1);
107 $url = str_replace(['.jpg', '.jpeg', '.gif', '.png'], ['','','',''], $url);
109 $url = base64_decode(strtr($url, '-_', '+/'), true);
112 $_REQUEST['url'] = $url;
115 $direct_cache = false;
118 if (!$direct_cache) {
119 $urlhash = 'pic:' . sha1($_REQUEST['url']);
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));
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');
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();
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'];
155 $mime = 'image/jpeg';
160 if (!DBM::is_result($photo)) {
161 // It shouldn't happen but it does - spaces in URL
162 $_REQUEST['url'] = str_replace(' ', '+', $_REQUEST['url']);
164 $img_str = fetch_url($_REQUEST['url'], true, $redirects, 10);
166 $tempfile = tempnam(get_temppath(), 'cache');
167 file_put_contents($tempfile, $img_str);
168 $mime = image_type_to_mime_type(exif_imagetype($tempfile));
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');
175 $cachefile = ''; // Clear the cachefile so that the dummy isn't stored
177 $Image = new Image($img_str, 'image/png');
178 if ($Image->isValid()) {
179 $Image->scaleDown(10);
180 $img_str = $Image->asString();
182 } elseif ($mime != 'image/jpeg' && !$direct_cache && $cachefile == '') {
183 $image = @imagecreatefromstring($img_str);
185 if ($image === FALSE) {
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);
195 $Image = new Image($img_str, $mime);
196 if ($Image->isValid() && !$direct_cache && ($cachefile == '')) {
197 Photo::store($Image, 0, 0, $urlhash, $_REQUEST['url'], '', 100);
202 $img_str_orig = $img_str;
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();
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);
221 } elseif ($cachefile != '') {
222 file_put_contents($cachefile, $img_str_orig);
225 header('Content-type: ' . $mime);
227 // Only output the cache headers when the file is 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');
241 * @brief Transform a remote URL into a local one
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.
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
251 * @return string The proxyfied URL or relative path
253 function proxy_url($url, $writemode = false, $size = '') {
256 if (substr($url, 0, strlen('http')) !== 'http') {
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);
266 if (Config::get('system', 'proxy_disabled')) {
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');
273 // Creating a sub directory to reduce the amount of files in the cache directory
274 $basepath = $a->get_basepath() . '/proxy';
276 $shortpath = hash('md5', $url);
277 $longpath = substr($shortpath, 0, 2);
279 if (is_dir($basepath) && $writemode && !is_dir($basepath . '/' . $longpath)) {
280 mkdir($basepath . '/' . $longpath);
281 chmod($basepath . '/' . $longpath, 0777);
284 $longpath .= '/' . strtr(base64_encode($url), '+/', '-_');
286 // Extract the URL extension
287 $extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
289 $extensions = ['jpg', 'jpeg', 'gif', 'png'];
290 if (in_array($extension, $extensions)) {
291 $shortpath .= '.' . $extension;
292 $longpath .= '.' . $extension;
295 $proxypath = System::baseUrl() . '/proxy/' . $longpath;
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) {
305 } elseif (strlen($proxypath) > 250) {
306 return System::baseUrl() . '/proxy/' . $shortpath . '?url=' . urlencode($url);
307 } elseif ($writemode) {
310 return $proxypath . $size;
318 function proxy_is_local_image($url) {
319 if ($url[0] == '/') {
323 if (strtolower(substr($url, 0, 5)) == 'data:') {
327 // links normalised - bug #431
328 $baseurl = normalise_link(System::baseUrl());
329 $url = normalise_link($url);
330 return (substr($url, 0, strlen($baseurl)) == $baseurl);
334 * @brief Return the array of query string parameters from a URL
337 * @return array Associative array of query string parameters
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);
345 foreach ($query_list as $key_value) {
346 $key_value_list = explode('=', $key_value);
347 $arr[$key_value_list[0]] = $key_value_list[1];
350 unset($url, $query_list, $url);
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']);
361 // following line changed per bug #431
362 if (proxy_is_local_image($matches[2])) {
363 return $matches[1] . $matches[2] . $matches[3];
366 return $matches[1] . proxy_url(htmlspecialchars_decode($matches[2])) . $matches[3];
369 function proxy_parse_html($html) {
370 $html = str_replace(normalise_link(System::baseUrl()) . '/', System::baseUrl() . '/', $html);
372 return preg_replace_callback('/(<img [^>]*src *= *["\'])([^"\']+)(["\'][^>]*>)/siU', 'proxy_img_cb', $html);