2 // Based upon "Privacy Image Cache" by Tobias Hößl <https://github.com/CatoTH/>
5 use Friendica\Core\Config;
6 use Friendica\Core\System;
7 use Friendica\Database\DBM;
9 define('PROXY_DEFAULT_TIME', 86400); // 1 Day
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');
17 require_once 'include/security.php';
18 require_once 'include/Photo.php';
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
26 // Question: Do we really need these three methods?
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');
35 if (function_exists('header_remove')) {
36 header_remove('Last-Modified');
37 header_remove('Expires');
38 header_remove('Cache-Control');
43 if (function_exists('header_remove')) {
44 header_remove('Pragma');
45 header_remove('pragma');
51 $basepath = $a->get_basepath();
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');
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'));
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])) {
65 } elseif (isset($a->argv[2])) {
71 if (isset($a->argv[3]) && ($a->argv[3] == 'thumb')) {
75 // thumb, small, medium and large.
76 if (substr($url, -6) == ':micro') {
79 $url = substr($url, 0, -6);
80 } elseif (substr($url, -6) == ':thumb') {
83 $url = substr($url, 0, -6);
84 } elseif (substr($url, -6) == ':small') {
86 $url = substr($url, 0, -6);
88 } elseif (substr($url, -7) == ':medium') {
90 $url = substr($url, 0, -7);
91 $sizetype = ':medium';
92 } elseif (substr($url, -6) == ':large') {
94 $url = substr($url, 0, -6);
98 $pos = strrpos($url, '=.');
100 $url = substr($url, 0, $pos + 1);
103 $url = str_replace(array('.jpg', '.jpeg', '.gif', '.png'), array('','','',''), $url);
105 $url = base64_decode(strtr($url, '-_', '+/'), true);
108 $_REQUEST['url'] = $url;
111 $direct_cache = false;
114 if (!$direct_cache) {
115 $urlhash = 'pic:' . sha1($_REQUEST['url']);
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));
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');
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();
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'];
152 $mime = 'image/jpeg';
157 if (!DBM::is_result($r)) {
158 // It shouldn't happen but it does - spaces in URL
159 $_REQUEST['url'] = str_replace(' ', '+', $_REQUEST['url']);
161 $img_str = fetch_url($_REQUEST['url'], true, $redirects, 10);
163 $tempfile = tempnam(get_temppath(), 'cache');
164 file_put_contents($tempfile, $img_str);
165 $mime = image_type_to_mime_type(exif_imagetype($tempfile));
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');
172 $cachefile = ''; // Clear the cachefile so that the dummy isn't stored
174 $img = new Photo($img_str, 'image/png');
175 if ($img->is_valid()) {
176 $img->scaleImage(10);
177 $img_str = $img->imageString();
179 } elseif ($mime != 'image/jpeg' && !$direct_cache && $cachefile == '') {
180 $image = @imagecreatefromstring($img_str);
182 if ($image === FALSE) {
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);
192 $img = new Photo($img_str, $mime);
193 if ($img->is_valid() && !$direct_cache && ($cachefile == '')) {
194 $img->store(0, 0, $urlhash, $_REQUEST['url'], '', 100);
199 $img_str_orig = $img_str;
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();
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);
218 } elseif ($cachefile != '') {
219 file_put_contents($cachefile, $img_str_orig);
222 header('Content-type: ' . $mime);
224 // Only output the cache headers when the file is 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');
238 * @brief Transform a remote URL into a local one
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.
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
248 * @return string The proxyfied URL or relative path
250 function proxy_url($url, $writemode = false, $size = '') {
253 if (substr($url, 0, strlen('http')) !== 'http') {
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);
263 if (Config::get('system', 'proxy_disabled')) {
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');
270 // Creating a sub directory to reduce the amount of files in the cache directory
271 $basepath = $a->get_basepath() . '/proxy';
273 $shortpath = hash('md5', $url);
274 $longpath = substr($shortpath, 0, 2);
276 if (is_dir($basepath) && $writemode && !is_dir($basepath . '/' . $longpath)) {
277 mkdir($basepath . '/' . $longpath);
278 chmod($basepath . '/' . $longpath, 0777);
281 $longpath .= '/' . strtr(base64_encode($url), '+/', '-_');
283 // Extract the URL extension
284 $extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
286 $extensions = array('jpg', 'jpeg', 'gif', 'png');
287 if (in_array($extension, $extensions)) {
288 $shortpath .= '.' . $extension;
289 $longpath .= '.' . $extension;
292 $proxypath = System::baseUrl() . '/proxy/' . $longpath;
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) {
302 } elseif (strlen($proxypath) > 250) {
303 return System::baseUrl() . '/proxy/' . $shortpath . '?url=' . urlencode($url);
304 } elseif ($writemode) {
307 return $proxypath . $size;
315 function proxy_is_local_image($url) {
316 if ($url[0] == '/') {
320 if (strtolower(substr($url, 0, 5)) == 'data:') {
324 // links normalised - bug #431
325 $baseurl = normalise_link(System::baseUrl());
326 $url = normalise_link($url);
327 return (substr($url, 0, strlen($baseurl)) == $baseurl);
331 * @brief Return the array of query string parameters from a URL
334 * @return array Associative array of query string parameters
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);
342 foreach ($query_list as $key_value) {
343 $key_value_list = explode('=', $key_value);
344 $arr[$key_value_list[0]] = $key_value_list[1];
347 unset($url, $query_list, $url);
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']);
358 // following line changed per bug #431
359 if (proxy_is_local_image($matches[2])) {
360 return $matches[1] . $matches[2] . $matches[3];
363 return $matches[1] . proxy_url(htmlspecialchars_decode($matches[2])) . $matches[3];
366 function proxy_parse_html($html) {
367 $html = str_replace(normalise_link(System::baseUrl()) . '/', System::baseUrl() . '/', $html);
369 return preg_replace_callback('/(<img [^>]*src *= *["\'])([^"\']+)(["\'][^>]*>)/siU', 'proxy_img_cb', $html);