From 88d169ce957b314b8bdff2697d3d65995fc63491 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Tue, 29 Oct 2013 03:40:18 +0000 Subject: [PATCH] Make cacheable more portable Move the cacheable code to the Http class and allow the client configuration to be cached in the browser --- include/ajax.config.php | 5 ++++- include/class.config.php | 5 +++++ include/class.file.php | 15 ++------------- include/class.http.php | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/include/ajax.config.php b/include/ajax.config.php index bf4174fe8..938625ff1 100644 --- a/include/ajax.config.php +++ b/include/ajax.config.php @@ -42,7 +42,10 @@ class ConfigAjaxAPI extends AjaxController { 'html_thread' => (bool) $cfg->isHtmlThreadEnabled(), ); - return $this->json_encode($config); + $config = $this->json_encode($config); + Http::cacheable(md5($config), $cfg->lastModified()); + + return $config; } } ?> diff --git a/include/class.config.php b/include/class.config.php index acd72ca30..e692adb2f 100644 --- a/include/class.config.php +++ b/include/class.config.php @@ -174,6 +174,11 @@ class OsticketConfig extends Config { return true; } + function lastModified() { + return max(array_map(array('parent', 'lastModified'), + array_keys($this->config))); + } + function isHelpDeskOffline() { return !$this->isOnline(); } diff --git a/include/class.file.php b/include/class.file.php index 8acd7e429..d3f8393b6 100644 --- a/include/class.file.php +++ b/include/class.file.php @@ -136,19 +136,8 @@ class AttachmentFile { return true; } - function makeCacheable($ttl=3600) { - // Thanks, http://stackoverflow.com/a/1583753/1025836 - $last_modified = Misc::db2gmtime($this->lastModified()); - header("Last-Modified: ".date('D, d M y H:i:s', $last_modified)." GMT", false); - header('ETag: "'.$this->getHash().'"'); - header("Cache-Control: private, max-age=$ttl"); - header('Expires: ' . gmdate(DATE_RFC822, time() + $ttl)." GMT"); - header('Pragma: private'); - if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified || - @trim($_SERVER['HTTP_IF_NONE_MATCH']) == $this->getHash()) { - header("HTTP/1.1 304 Not Modified"); - exit(); - } + function makeCacheable($ttl=86400) { + Http::cacheable($this->getHash(), $this->lastModified(), $ttl); } function display($scale=false) { diff --git a/include/class.http.php b/include/class.http.php index e8d6788cf..3aea32b6e 100644 --- a/include/class.http.php +++ b/include/class.http.php @@ -53,6 +53,21 @@ class Http { exit; } + function cacheable($etag, $modified, $ttl=3600) { + // Thanks, http://stackoverflow.com/a/1583753/1025836 + $last_modified = Misc::db2gmtime($modified); + header("Last-Modified: ".date('D, d M y H:i:s', $last_modified)." GMT", false); + header('ETag: "'.$etag.'"'); + header("Cache-Control: private, max-age=$ttl"); + header('Expires: ' . gmdate(DATE_RFC822, time() + $ttl)." GMT"); + header('Pragma: private'); + if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified || + @trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { + header("HTTP/1.1 304 Not Modified"); + exit(); + } + } + function download($filename, $type, $data=null) { header('Pragma: public'); header('Expires: 0'); -- GitLab