+ global $a;
+
+ if ($a->config["system"]["db_log"] != "")
+ $stamp1 = microtime(true);
+
+ if(function_exists('header_remove')) {
+ header_remove('Pragma');
+ header_remove('pragma');
+ }
+
+ $urlhash = 'pic:' . sha1($_REQUEST['url']);
+ // Double encoded url - happens with Diaspora
+ $urlhash2 = 'pic:' . sha1(urldecode($_REQUEST['url']));
+
+ $cache = get_config('system','itemcache');
+ if (($cache != '') and is_dir($cache)) {
+ $cachefile = $cache."/".hash("md5", $_REQUEST['url']);
+ if (file_exists($cachefile)) {
+ $img_str = file_get_contents($cachefile);
+
+ $mime = image_type_to_mime_type(exif_imagetype($cachefile));
+
+ header("Content-type: $mime");
+ header("Expires: " . gmdate("D, d M Y H:i:s", time() + (3600*24)) . " GMT");
+ header("Cache-Control: max-age=" . (3600*24));
+
+ echo $img_str;
+
+ if ($a->config["system"]["db_log"] != "") {
+ $stamp2 = microtime(true);
+ $duration = round($stamp2-$stamp1, 3);
+ if ($duration > $a->config["system"]["db_loglimit"])
+ @file_put_contents($a->config["system"]["db_log"], $duration."\t".strlen($img_str)."\t".$_REQUEST['url']."\n", FILE_APPEND);
+ }
+
+ killme();
+ }
+ }
+
+ require_once("Photo.php");
+
+ $r = q("SELECT * FROM `photo` WHERE `resource-id` in ('%s', '%s') LIMIT 1", $urlhash, $urlhash2);
+ if (count($r)) {
+ $img_str = $r[0]['data'];