From 9be6dfadba85819aa0dadf7df7082331f14e88d6 Mon Sep 17 00:00:00 2001 From: rabuzarus <> Date: Mon, 23 Oct 2017 15:16:23 +0200 Subject: [PATCH] frio: use cached css if no settings (or file) have changed --- view/theme/frio/config.php | 90 +++++----- view/theme/frio/style.php | 171 +++++++++++-------- view/theme/frio/templates/theme_settings.tpl | 2 +- view/theme/frio/theme.php | 2 +- 4 files changed, 152 insertions(+), 113 deletions(-) diff --git a/view/theme/frio/config.php b/view/theme/frio/config.php index 6e5aca1ba..e3164697b 100644 --- a/view/theme/frio/config.php +++ b/view/theme/frio/config.php @@ -1,81 +1,85 @@ " . t("Note: "). "".t("Check image permissions if all users are allowed to visit the image"); - + $background_image_help = "" . t("Note"). ": ".t("Check image permissions if all users are allowed to visit the image"); $t = get_markup_template('theme_settings.tpl'); $o .= replace_macros($t, array( - '$submit' => t('Submit'), - '$baseurl' => System::baseUrl(), - '$title' => t("Theme settings"), - '$schema' => array('frio_schema', t("Select scheme"), $arr["schema"], '', $scheme_choices), - '$nav_bg' => array_key_exists("nav_bg", $disable) ? "" : array('frio_nav_bg', t('Navigation bar background color'), $arr['nav_bg']), - '$nav_icon_color' => array_key_exists("nav_icon_color", $disable) ? "" : array('frio_nav_icon_color', t('Navigation bar icon color '), $arr['nav_icon_color']), - '$link_color' => array_key_exists("link_color", $disable) ? "" : array('frio_link_color', t('Link color'), $arr['link_color'], '', $link_colors), - '$bgcolor' => array_key_exists("bgcolor", $disable) ? "" : array('frio_background_color', t('Set the background color'), $arr['bgcolor']), - '$contentbg_transp' => array_key_exists("contentbg_transp", $disable) ? "" : array('frio_contentbg_transp', t("Content background transparency"), ($arr["contentbg_transp"] ? $arr["contentbg_transp"] : 0 )), - '$background_image' => array_key_exists("background_image", $disable ) ? "" : array('frio_background_image', t('Set the background image'), $arr['background_image'], $background_image_help), - '$bg_image_options' => Image::get_options($arr), + '$submit' => t('Submit'), + '$baseurl' => System::baseUrl(), + '$title' => t("Theme settings"), + '$schema' => array('frio_schema', t("Select scheme"), $arr["schema"], '', $scheme_choices), + '$nav_bg' => array_key_exists("nav_bg", $disable) ? "" : array('frio_nav_bg', t('Navigation bar background color'), $arr['nav_bg']), + '$nav_icon_color' => array_key_exists("nav_icon_color", $disable) ? "" : array('frio_nav_icon_color', t('Navigation bar icon color '), $arr['nav_icon_color']), + '$link_color' => array_key_exists("link_color", $disable) ? "" : array('frio_link_color', t('Link color'), $arr['link_color'], '', $link_colors), + '$bgcolor' => array_key_exists("bgcolor", $disable) ? "" : array('frio_background_color', t('Set the background color'), $arr['bgcolor']), + '$contentbg_transp' => array_key_exists("contentbg_transp", $disable) ? "" : array('frio_contentbg_transp', t("Content background transparency"), ((isset($arr["contentbg_transp"]) && $arr["contentbg_transp"] != "") ? $arr["contentbg_transp"] : 100)), + '$background_image' => array_key_exists("background_image", $disable ) ? "" : array('frio_background_image', t('Set the background image'), $arr['background_image'], $background_image_help), + '$bg_image_options' => Image::get_options($arr), )); return $o; diff --git a/view/theme/frio/style.php b/view/theme/frio/style.php index cc77f8234..76cbb9777 100644 --- a/view/theme/frio/style.php +++ b/view/theme/frio/style.php @@ -1,40 +1,52 @@ install) { - // Get the UID of the channel owner +$schemecss = ""; +$schemecssfile = false; +$scheme_modified = 0; + +if (! $a->install) { + // Get the UID of the profile owner. $uid = get_theme_uid(); - if($uid) { - load_pconfig($uid,'frio'); + if ($uid) { + PConfig::load($uid, 'frio'); + + // Load the profile owners pconfig. + $schema = PConfig::get($uid, "frio", "schema"); + $nav_bg = PConfig::get($uid, "frio", "nav_bg"); + $nav_icon_color = PConfig::get($uid, "frio", "nav_icon_color"); + $link_color = PConfig::get($uid, "frio", "link_color"); + $bgcolor = PConfig::get($uid, "frio", "background_color"); + $contentbg_transp = PConfig::get($uid, "frio", "contentbg_transp"); + $background_image = PConfig::get($uid, "frio", "background_image"); + $bg_image_option = PConfig::get($uid, "frio", "bg_image_option"); + $modified = PConfig::get($uid, "frio", "css_modified"); + + // There is maybe the case that the user did never modify the theme settings. + // In this case we store the present time. + if (empty($modified)) { + PConfig::set($uid, 'frio', 'css_modified', time()); + } } - // Load the owners pconfig - $schema = get_pconfig($uid, "frio", "schema"); - $nav_bg = get_pconfig($uid, "frio", "nav_bg"); - $nav_icon_color = get_pconfig($uid, "frio", "nav_icon_color"); - $link_color = get_pconfig($uid, "frio", "link_color"); - $bgcolor = get_pconfig($uid, "frio", "background_color"); - $contentbg_transp = get_pconfig($uid, "frio", "contentbg_transp"); - $background_image = get_pconfig($uid, "frio", "background_image"); - $bg_image_option = get_pconfig($uid, "frio", "bg_image_option"); } // Now load the scheme. If a value is changed above, we'll keep the settings // If not, we'll keep those defined by the schema // Setting $schema to '' wasn't working for some reason, so we'll check it's // not --- like the mobile theme does instead. -// Allow layouts to over-ride the schema -if($_REQUEST['schema']) { +// Allow layouts to over-ride the schema. +if ($_REQUEST['schema']) { $schema = $_REQUEST['schema']; } if (($schema) && ($schema != '---')) { - // Check it exists, because this setting gets distributed to clones - if(file_exists('view/theme/frio/schema/' . $schema . '.php')) { + if (file_exists('view/theme/frio/schema/' . $schema . '.php')) { $schemefile = 'view/theme/frio/schema/' . $schema . '.php'; - require_once ($schemefile); + require_once $schemefile; } - if(file_exists('view/theme/frio/schema/' . $schema . '.css')) { - $schemecss = file_get_contents('view/theme/frio/schema/' . $schema . '.css'); + if (file_exists('view/theme/frio/schema/' . $schema . '.css')) { + $schemecssfile = 'view/theme/frio/schema/' . $schema . '.css'; } } @@ -44,57 +56,53 @@ if (($schema) && ($schema != '---')) { if (! $schema) { if(file_exists('view/theme/frio/schema/default.php')) { $schemefile = 'view/theme/frio/schema/default.php'; - require_once ($schemefile); + require_once $schemefile; } if(file_exists('view/theme/frio/schema/default.css')) { - $schemecss = file_get_contents('view/theme/frio/schema/default.css'); + $schemecssfile = 'view/theme/frio/schema/default.css'; } } //Set some defaults - we have to do this after pulling owner settings, and we have to check for each setting //individually. If we don't, we'll have problems if a user has set one, but not all options. -if(! $nav_bg) - $nav_bg = "#708fa0"; -if(! $nav_icon_color) - $nav_icon_color = "#fff"; -if(! $link_color) - $link_color = "#6fdbe8"; -if(! $bgcolor) - $bgcolor = "#ededed"; -if(! $contentbg_transp) - $contentbg_transp = 100; -if(! $background_image) - $background_image =''; +$nav_bg = (empty($nav_bg) ? "#708fa0" : $nav_bg); +$nav_icon_color = (empty($nav_icon_color) ? "#fff" : $nav_icon_color); +$link_color = (empty($link_color) ? "#6fdbe8" : $link_color); +$bgcolor = (empty($bgcolor) ? "#ededed" : $bgcolor); +$background_image = (empty($background_image) ? '' : $background_image); +$modified = (empty($modified) ? time() : $modified); -// Calculate some colors in dependance of existing colors +$contentbg_transp = ((isset($contentbg_transp) && $contentbg_transp != "") ? $contentbg_transp : 100); + +// Calculate some colors in dependance of existing colors. // Some colors are calculated to don't have too many selection -// fields in the theme settings -if(! $menu_background_hover_color) { +// fields in the theme settings. +if (! $menu_background_hover_color) { $mbhc = new Color($nav_bg); $mcolor = $mbhc->getHex(); - if($mbhc->isLight($mcolor, 75)) { + if ($mbhc->isLight($mcolor, 75)) { $menu_is = 'light'; $menu_background_hover_color = '#' . $mbhc->darken(5); } else { $menu_is = 'dark'; $menu_background_hover_color = '#' . $mbhc->lighten(5); - //$menu_background_hover_color = "#628394"; } } -if(! $nav_icon_hover_color) { +if (! $nav_icon_hover_color) { $nihc = new Color($nav_bg); - if($nihc->isLight()) + if ($nihc->isLight()) { $nav_icon_hover_color = '#' . $nihc->darken(10); - else + } else { $nav_icon_hover_color = '#' . $nihc->lighten(10); + } } -if(! $link_hover_color) { +if (! $link_hover_color) { $lhc = new Color($link_color); $lcolor = $lhc->getHex(); - if($lhc->isLight($lcolor, 75)) { + if ($lhc->isLight($lcolor, 75)) { $link_hover_color = '#' . $lhc->darken(5); } else { $link_hover_color = '#' . $lhc->lighten(5); @@ -102,7 +110,7 @@ if(! $link_hover_color) { } -// Convert $bg_image_options into css +// Convert $bg_image_options into css. switch ($bg_image_option) { case "stretch": $background_size_img = "100%"; @@ -122,32 +130,59 @@ switch ($bg_image_option) { break; } -// Convert transparency level from percentage to opacity value +// Convert transparency level from percentage to opacity value. $contentbg_transp = $contentbg_transp / 100; +$options = array ( + '$nav_bg' => $nav_bg, + '$nav_icon_color' => $nav_icon_color, + '$nav_icon_hover_color' => $nav_icon_hover_color, + '$link_color' => $link_color, + '$link_hover_color' => $link_hover_color, + '$menu_background_hover_color' => $menu_background_hover_color, + '$btn_primary_color' => $nav_icon_color, + '$btn_primary_hover_color' => $menu_background_hover_color, + '$bgcolor' => $bgcolor, + '$contentbg_transp' => $contentbg_transp, + '$background_image' => $background_image, + '$background_size_img' => $background_size_img, +); -// Apply the settings -if(file_exists('view/theme/frio/css/style.css')) { - $x = file_get_contents('view/theme/frio/css/style.css'); +$css_tpl = file_get_contents('view/theme/frio/css/style.css'); - $options = array ( - '$nav_bg' => $nav_bg, - '$nav_icon_color' => $nav_icon_color, - '$nav_icon_hover_color' => $nav_icon_hover_color, - '$link_color' => $link_color, - '$link_hover_color' => $link_hover_color, - '$menu_background_hover_color' => $menu_background_hover_color, - '$btn_primary_color' => $nav_icon_color, - '$btn_primary_hover_color' => $menu_background_hover_color, - '$bgcolor' => $bgcolor, - '$contentbg_transp' => $contentbg_transp, - '$background_image' => $background_image, - '$background_size_img' => $background_size_img, - ); - - echo str_replace(array_keys($options), array_values($options), $x); +// Get the content of the scheme css file and the time of the last file change. +if ($schemecssfile) { + $css_tpl .= file_get_contents($schemecssfile); + $scheme_modified = filemtime($schemecssfile); } -if($schemecss) { - echo $schemecss; +// We need to check which is the most recent css data. +// We will use this time later to decide if we load the cached or fresh css data. +if ($scheme_modified > $modified) { + $modified = $scheme_modified; } +// Apply the settings to the css template. +$css = str_replace(array_keys($options), array_values($options), $css_tpl); + +$modified = gmdate('r', $modified); + +$etag = md5($css); + +// Set a header for caching. +header('Cache-Control: public'); +header('ETag: "'.$etag.'"'); +header('Last-Modified: '.$modified); + +// Only send the CSS file if it was changed. +if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) { + $cached_modified = gmdate('r', strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])); + $cached_etag = str_replace(array('"', "-gzip"), array('', ''), + stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])); + + if (($cached_modified == $modified) && ($cached_etag == $etag)) { + header('HTTP/1.1 304 Not Modified'); + exit(); + } +} + +echo $css; diff --git a/view/theme/frio/templates/theme_settings.tpl b/view/theme/frio/templates/theme_settings.tpl index d4438caa8..cc9d941f0 100644 --- a/view/theme/frio/templates/theme_settings.tpl +++ b/view/theme/frio/templates/theme_settings.tpl @@ -31,7 +31,7 @@ EOT; - } +} if ($style == "") $style = get_config('frio', 'style');