2 // Based upon "Privacy Image Cache" by Tobias Hößl <https://github.com/CatoTH/>
4 define('PROXY_DEFAULT_TIME', 86400); // 1 Day
6 define('PROXY_SIZE_MICRO', 'micro');
7 define('PROXY_SIZE_THUMB', 'thumb');
8 define('PROXY_SIZE_SMALL', 'small');
9 define('PROXY_SIZE_MEDIUM', 'medium');
10 define('PROXY_SIZE_LARGE', 'large');
12 require_once 'include/security.php';
13 require_once 'include/Photo.php';
15 function proxy_init(App $a) {
16 // Pictures are stored in one of the following ways:
17 // 1. If a folder "proxy" exists and is writeable, then use this for caching
18 // 2. If a cache path is defined, use this
19 // 3. If everything else failed, cache into the database
21 // Question: Do we really need these three methods?
23 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
24 header('HTTP/1.1 304 Not Modified');
25 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
26 header('Etag: ' . $_SERVER['HTTP_IF_NONE_MATCH']);
27 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
28 header('Cache-Control: max-age=31536000');
30 if (function_exists('header_remove')) {
31 header_remove('Last-Modified');
32 header_remove('Expires');
33 header_remove('Cache-Control');
38 if (function_exists('header_remove')) {
39 header_remove('Pragma');
40 header_remove('pragma');
46 $basepath = $a->get_basepath();
48 // If the cache path isn't there, try to create it
49 if (!is_dir($basepath . '/proxy') AND is_writable($basepath)) {
50 mkdir($basepath . '/proxy');
53 // Checking if caching into a folder in the webroot is activated and working
54 $direct_cache = (is_dir($basepath . '/proxy') AND is_writable($basepath . '/proxy'));
56 // Look for filename in the arguments
57 if ((isset($a->argv[1]) OR isset($a->argv[2]) OR isset($a->argv[3])) AND !isset($_REQUEST['url'])) {
58 if (isset($a->argv[3])) {
60 } elseif (isset($a->argv[2])) {
66 if (isset($a->argv[3]) AND ($a->argv[3] == 'thumb')) {
70 // thumb, small, medium and large.
71 if (substr($url, -6) == ':micro') {
74 $url = substr($url, 0, -6);
75 } elseif (substr($url, -6) == ':thumb') {
78 $url = substr($url, 0, -6);
79 } elseif (substr($url, -6) == ':small') {
81 $url = substr($url, 0, -6);
83 } elseif (substr($url, -7) == ':medium') {
85 $url = substr($url, 0, -7);
86 $sizetype = ':medium';
87 } elseif (substr($url, -6) == ':large') {
89 $url = substr($url, 0, -6);
93 $pos = strrpos($url, '=.');
95 $url = substr($url, 0, $pos + 1);
98 $url = str_replace(array('.jpg', '.jpeg', '.gif', '.png'), array('','','',''), $url);
100 $url = base64_decode(strtr($url, '-_', '+/'), true);
103 $_REQUEST['url'] = $url;
106 $direct_cache = false;
109 if (!$direct_cache) {
110 $urlhash = 'pic:' . sha1($_REQUEST['url']);
112 $cachefile = get_cachefile(hash('md5', $_REQUEST['url']));
113 if ($cachefile != '' AND file_exists($cachefile)) {
114 $img_str = file_get_contents($cachefile);
115 $mime = image_type_to_mime_type(exif_imagetype($cachefile));
117 header('Content-type: ' . $mime);
118 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
119 header('Etag: "' . md5($img_str) . '"');
120 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
121 header('Cache-Control: max-age=31536000');
123 // reduce quality - if it isn't a GIF
124 if ($mime != 'image/gif') {
125 $img = new Photo($img_str, $mime);
126 if ($img->is_valid()) {
127 $img_str = $img->imageString();
140 if (!$direct_cache AND ($cachefile == '')) {
141 $r = qu("SELECT * FROM `photo` WHERE `resource-id` = '%s' LIMIT 1", $urlhash);
142 if (dbm::is_result($r)) {
143 $img_str = $r[0]['data'];
144 $mime = $r[0]['desc'];
146 $mime = 'image/jpeg';
153 if (!dbm::is_result($r)) {
154 // It shouldn't happen but it does - spaces in URL
155 $_REQUEST['url'] = str_replace(' ', '+', $_REQUEST['url']);
157 $img_str = fetch_url($_REQUEST['url'], true, $redirects, 10);
159 $tempfile = tempnam(get_temppath(), 'cache');
160 file_put_contents($tempfile, $img_str);
161 $mime = image_type_to_mime_type(exif_imagetype($tempfile));
164 // If there is an error then return a blank image
165 if ((substr($a->get_curl_code(), 0, 1) == '4') OR (!$img_str)) {
166 $img_str = file_get_contents('images/blank.png');
168 $cachefile = ''; // Clear the cachefile so that the dummy isn't stored
170 $img = new Photo($img_str, 'image/png');
171 if ($img->is_valid()) {
172 $img->scaleImage(10);
173 $img_str = $img->imageString();
175 } elseif ($mime != 'image/jpeg' AND !$direct_cache AND $cachefile == '') {
176 $image = @imagecreatefromstring($img_str);
178 if ($image === FALSE) {
182 q("INSERT INTO `photo`
183 ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, `album`, `height`, `width`, `desc`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
184 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' )",
185 0, 0, get_guid(), dbesc($urlhash),
186 dbesc(datetime_convert()),
187 dbesc(datetime_convert()),
188 dbesc(basename(dbesc($_REQUEST['url']))),
190 intval(imagesy($image)),
191 intval(imagesx($image)),
196 dbesc(''), dbesc(''), dbesc(''), dbesc('')
200 $img = new Photo($img_str, $mime);
201 if ($img->is_valid() AND !$direct_cache AND ($cachefile == '')) {
202 $img->store(0, 0, $urlhash, $_REQUEST['url'], '', 100);
207 $img_str_orig = $img_str;
209 // reduce quality - if it isn't a GIF
210 if ($mime != 'image/gif') {
211 $img = new Photo($img_str, $mime);
212 if ($img->is_valid()) {
213 $img->scaleImage($size);
214 $img_str = $img->imageString();
218 // If there is a real existing directory then put the cache file there
219 // advantage: real file access is really fast
220 // Otherwise write in cachefile
221 if ($valid AND $direct_cache) {
222 file_put_contents($basepath . '/proxy/' . proxy_url($_REQUEST['url'], true), $img_str_orig);
223 if ($sizetype != '') {
224 file_put_contents($basepath . '/proxy/' . proxy_url($_REQUEST['url'], true) . $sizetype, $img_str);
226 } elseif ($cachefile != '') {
227 file_put_contents($cachefile, $img_str_orig);
230 header('Content-type: ' . $mime);
232 // Only output the cache headers when the file is valid
234 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
235 header('Etag: "' . md5($img_str) . '"');
236 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
237 header('Cache-Control: max-age=31536000');
246 * @brief Transform a remote URL into a local one
248 * This function only performs the URL replacement on http URL and if the
249 * provided URL isn't local, "the isn't deactivated" (sic) and if the config
250 * system.proxy_disabled is set to false.
252 * @param string $url The URL to proxyfy
253 * @param bool $writemode Returns a local path the remote URL should be saved to
254 * @param string $size One of the PROXY_SIZE_* constants
256 * @return string The proxyfied URL or relative path
258 function proxy_url($url, $writemode = false, $size = '') {
261 if (substr($url, 0, strlen('http')) !== 'http') {
265 // Only continue if it isn't a local image and the isn't deactivated
266 if (proxy_is_local_image($url)) {
267 $url = str_replace(normalise_link($a->get_baseurl()) . '/', $a->get_baseurl() . '/', $url);
271 if (get_config('system', 'proxy_disabled')) {
275 // Image URL may have encoded ampersands for display which aren't desirable for proxy
276 $url = html_entity_decode($url, ENT_NOQUOTES, 'utf-8');
278 // Creating a sub directory to reduce the amount of files in the cache directory
279 $basepath = $a->get_basepath() . '/proxy';
281 $shortpath = hash('md5', $url);
282 $longpath = substr($shortpath, 0, 2);
284 if (is_dir($basepath) AND $writemode AND !is_dir($basepath . '/' . $longpath)) {
285 mkdir($basepath . '/' . $longpath);
286 chmod($basepath . '/' . $longpath, 0777);
289 $longpath .= '/' . strtr(base64_encode($url), '+/', '-_');
291 // Extract the URL extension
292 $extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
294 $extensions = array('jpg', 'jpeg', 'gif', 'png');
295 if (in_array($extension, $extensions)) {
296 $shortpath .= '.' . $extension;
297 $longpath .= '.' . $extension;
300 $proxypath = $a->get_baseurl() . '/proxy/' . $longpath;
306 // Too long files aren't supported by Apache
307 // Writemode in combination with long files shouldn't be possible
308 if ((strlen($proxypath) > 250) AND $writemode) {
310 } elseif (strlen($proxypath) > 250) {
311 return $a->get_baseurl() . '/proxy/' . $shortpath . '?url=' . urlencode($url);
312 } elseif ($writemode) {
315 return $proxypath . $size;
323 function proxy_is_local_image($url) {
324 if ($url[0] == '/') {
328 if (strtolower(substr($url, 0, 5)) == 'data:') {
332 // links normalised - bug #431
333 $baseurl = normalise_link(get_app()->get_baseurl());
334 $url = normalise_link($url);
335 return (substr($url, 0, strlen($baseurl)) == $baseurl);
339 * @brief Return the array of query string parameters from a URL
342 * @return array Associative array of query string parameters
344 function proxy_parse_query($url) {
345 $query = parse_url($url, PHP_URL_QUERY);
346 $query = html_entity_decode($query);
347 $query_list = explode('&', $query);
350 foreach ($query_list as $key_value) {
351 $key_value_list = explode('=', $key_value);
352 $arr[$key_value_list[0]] = $key_value_list[1];
355 unset($url, $query_list, $url);
359 function proxy_img_cb($matches) {
360 // if the picture seems to be from another picture cache then take the original source
361 $queryvar = proxy_parse_query($matches[2]);
362 if (($queryvar['url'] != '') AND (substr($queryvar['url'], 0, 4) == 'http')) {
363 $matches[2] = urldecode($queryvar['url']);
366 // following line changed per bug #431
367 if (proxy_is_local_image($matches[2])) {
368 return $matches[1] . $matches[2] . $matches[3];
371 return $matches[1] . proxy_url(htmlspecialchars_decode($matches[2])) . $matches[3];
374 function proxy_parse_html($html) {
376 $html = str_replace(normalise_link($a->get_baseurl()) . '/', $a->get_baseurl() . '/', $html);
378 return preg_replace_callback('/(<img [^>]*src *= *["\'])([^"\']+)(["\'][^>]*>)/siU', 'proxy_img_cb', $html);