/** * Serves an icon * Terminates the script and sends headers on error * @return void */ public function serve() { if (headers_sent()) { return; } if (!$this->url || !$this->handle || !$this->dir_guid || !$this->hmac) { header("HTTP/1.1 400 Bad request"); exit; } $etag = md5($this->url . $this->handle . $this->ts); if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) == "\"{$etag}\"") { header("HTTP/1.1 304 Not Modified"); exit; } $this->openDbLink(); $values = $this->getDatalistValue(array('dataroot', '__site_secret__')); $this->closeDbLink(); if (empty($values)) { header("HTTP/1.1 404 Not Found"); exit; } $data_root = $values['dataroot']; $key = $values['__site_secret__']; $hmac = hash_hmac('sha256', $this->url . $this->handle, $key); if ($this->hmac != $hmac) { header("HTTP/1.1 403 Forbidden"); exit; } $locator = new EntityDirLocator($this->dir_guid); $filename = $data_root . $locator->getPath() . 'scraper_cache/thumbs/' . md5($this->url) . '.' . $this->handle . '.jpg'; error_log($this->url); error_log($filename); if (!file_exists($filename)) { header("HTTP/1.1 404 Not Found"); exit; } $filesize = filesize($filename); header("Content-type: image/jpeg"); header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', strtotime("+6 months")), true); header("Pragma: public"); header("Cache-Control: public"); header("Content-Length: {$filesize}"); header("ETag: \"{$etag}\""); readfile($filename); exit; }
list($guid, $type, $filename) = explode('/', trim($_GET['__uri'], '/')); $guid = (int) $guid; if (!$guid) { $response = new Response('', Response::HTTP_NOT_FOUND); $response->send(); } $last_cache = empty($_GET['lastcache']) ? 0 : (int) $_GET['lastcache']; // icontime // If is the same ETag, content didn't changed. $etag = $last_cache . $guid; if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) == "\"{$etag}\"") { $response = new Response('', Response::HTTP_NOT_MODIFIED); $response->send(); } // @todo: validate hmac $hmac = $_GET['hmac']; $data_root = Application::getDataPath(); $locator = new EntityDirLocator($guid); $full_path = "{$data_root}{$locator->getPath()}media/{$type}/{$filename}"; if (!file_exists($full_path)) { header("HTTP/1.1 404 Not Found"); exit; } $contents = file_get_contents($full_path); $response = new Response($contents); $d = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, basename($full_path)); $response->headers->set('Content-Disposition', $d); $response->headers->set('Content-Type', 'image/jpeg'); $response->headers->set('Expires', gmdate('D, d M Y H:i:s \\G\\M\\T', strtotime("+6 months"))); $response->headers->set('ETag', $etag); $response->send();