From 8577940e1e476167690e0af0bdc44bb38e8e660e Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 2 Sep 2014 16:31:38 +0200 Subject: [PATCH] StaticController: cache gravatar images fixes #7061 --- application/controllers/StaticController.php | 22 ++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/application/controllers/StaticController.php b/application/controllers/StaticController.php index 51b5fdb62..c90272ffc 100644 --- a/application/controllers/StaticController.php +++ b/application/controllers/StaticController.php @@ -2,10 +2,11 @@ // {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}} -use Zend_Controller_Action_Exception as ActionException; use Icinga\Web\Controller\ActionController; use Icinga\Application\Icinga; use Icinga\Logger\Logger; +use Icinga\Web\FileCache; +use Zend_Controller_Action_Exception as ActionException; /** * Delivery static content to clients @@ -30,8 +31,25 @@ class StaticController extends ActionController public function gravatarAction() { + $cache = FileCache::instance(); + $filename = md5(strtolower(trim($this->_request->getParam('email')))); + $cacheFile = 'gravatar-' . $filename; + header('Cache-Control: public'); + header('Pragma: cache'); + if ($etag = $cache->etagMatchesCachedFile($cacheFile)) { + header("HTTP/1.1 304 Not Modified"); + return; + } + header('Content-Type: image/jpg'); - $img = file_get_contents('http://www.gravatar.com/avatar/' . md5(strtolower(trim($this->_request->getParam('email')))) . '?s=200&d=mm'); + if ($cache->has($cacheFile)) { + header('ETag: "' . $cache->etagForCachedFile($cacheFile) . '"'); + $cache->send($cacheFile); + return; + } + $img = file_get_contents('http://www.gravatar.com/avatar/' . $filename . '?s=200&d=mm'); + $cache->store($cacheFile, $img); + header('ETag: "' . $cache->etagForCachedFile($cacheFile) . '"'); echo $img; }