Module: website Branch: master Commit: 4a98d2a130f6d220583b8fae8caae5afae10c38b URL: https://source.winehq.org/git/website.git/?a=commit;h=4a98d2a130f6d220583b8f...
Author: Jeremy Newman jnewman@codeweavers.com Date: Tue Jun 7 10:38:54 2022 -0500
last modified header support
dynamically build last modified based off on content update headers for cacheing
---
include/html.php | 105 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 40 deletions(-)
diff --git a/include/html.php b/include/html.php index ca744580..46c95f46 100644 --- a/include/html.php +++ b/include/html.php @@ -29,6 +29,7 @@ class html private $template_cache = array(); // cache for template data private $template_vars = array(); // vars to pass to EXEC script from template private $trans_table = array(); // translation table data + private $last_modified = 0; // default last modified for header
// header code in showpage() private $header_code = array('pre' => '', 'post' => ''); @@ -423,6 +424,29 @@ class html } }
+ // SET LAST MODIFIED + public function set_last_modified ($time = 0, $convert = false) + { + // set to current time + if ($time === -1) + { + debug("global", "last-modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT"); + $this->last_modified = time(); + return; + } + + // convert time from string + if ($convert) + $time = strtotime($time); + + // if time is newer, then update last modified + if ($time <= time() and $time > $this->last_modified) + { + debug("global", "last-modified: " . gmdate("D, d M Y H:i:s", $time) . " GMT"); + $this->last_modified = $time; + } + } + // HTML BR public function br ($count = 1) { @@ -981,52 +1005,53 @@ class html { global $config;
- // if no theme, use default theme from config - if (!$theme) - $theme = $config->theme; - // debug debug("template", "loading template: theme:[{$theme}] lang: [{$this->lang}] template:[{$template}]");
- // load from cache if we have already loaded before - if (isset($this->template_cache[$template])) + $file = ''; + $in = ''; + + // theme determines where template is loaded from + switch ($theme) { + // load from global + case "global": + if (file_exists("{$this->_file_root}/templates/global/{$template}.template")) + $file = "{$this->_file_root}/templates/global/{$template}.template"; + break; + + // load template from theme + default: + if (file_exists("{$this->_file_root}/templates/{$this->lang}/{$template}.template")) + $file = "{$this->_file_root}/templates/{$this->lang}/{$template}.template"; + else if (($config->lang != $this->lang) and file_exists("{$this->_file_root}/templates/{$config->lang}/{$template}.template")) + $file = "{$this->_file_root}/templates/{$config->lang}/{$template}.template"; + break; + } + + // set last mofified if newer + if (!empty($file)) + { + $this->set_last_modified(filemtime($file)); + } + + // where to load data from for template + if (!empty($this->template_cache[$template])) + { + // from cache $in = $this->template_cache[$template]; } - else + else if (!empty($file)) { - // theme determines where template is loaded from - switch ($theme) - { - // load from local template repository - case "base": - case "local": - if (file_exists("{$this->_file_root}/templates/{$this->lang}/{$template}.template")) - $in = join("",file("{$this->_file_root}/templates/{$this->lang}/{$template}.template")); - else if (($config->lang != $this->lang) and file_exists("{$this->_file_root}/templates/{$config->lang}/{$template}.template")) - $in = join("",file("{$this->_file_root}/templates/{$config->lang}/{$template}.template")); - break; - - // load from global - case "global": - if (file_exists("{$this->_file_root}/templates/global/{$template}.template")) - $in = join("",file("{$this->_file_root}/templates/global/{$template}.template")); - break; - - // load template from theme - default: - if (file_exists("{$this->_file_root}/templates/{$this->lang}/global/themes/{$theme}/{$template}.template")) - $in = join("",file("{$this->_file_root}/templates/{$this->lang}/global/themes/{$theme}/{$template}.template")); - else if (($config->lang != $this->lang) and file_exists("{$this->_file_root}/templates/{$config->lang}/global/themes/{$theme}/{$template}.template")) - $in = join("",file("{$this->_file_root}/templates/{$config->lang}/global/themes/{$theme}/{$template}.template")); - } + // from file + $in = join("",file($file)); }
// oops not found, load 404 template if (empty($in)) { - $in = ''; $this->in404 = 1; + $this->set_last_modified(-1); if (file_exists("{$this->_file_root}/templates/{$this->lang}/global/404.template")) $in .= join("",file("{$this->_file_root}/templates/{$this->lang}/global/404.template")); else if (file_exists("{$this->_file_root}/templates/{$config->lang}/global/404.template")) @@ -1036,7 +1061,8 @@ class html }
// cache this template to save on i/o - $this->template_cache[$template] = $in; + if (empty($this->template_cache[$template])) + $this->template_cache[$template] = $in;
// return the text with the vars replaced return $this->template_replace($in, $vars, $noremovetags); @@ -1579,15 +1605,14 @@ class html } }
- // HTTP HEADER (better header) + // HTTP HEADER public function http_header ($type = "text/html") { + if (empty($this->last_modified)) + $this->set_last_modified(-1); header("Content-type: ".$type."; charset=UTF-8"); - header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); - header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); - header("Cache-Control: no-store, no-cache, must-revalidate"); - header("Cache-Control: post-check=0, pre-check=0", false); - header("Pragma: no-cache"); + header("Cache-Control: public, max-age=86400"); + header("Last-Modified: " . gmdate("D, d M Y H:i:s", $this->last_modified . " GMT")); }
// REDIRECT (simple httpd header redirect)