Add nocache param on JS/CSS URL to avoid cache problems

This commit is contained in:
Benjamin Renard 2018-05-22 18:47:08 +02:00
parent 18278bcc23
commit 35b9b9ad94
2 changed files with 39 additions and 13 deletions

View file

@ -1338,7 +1338,8 @@ class LSsession {
// JS // JS
$JSscript_txt=''; $JSscript_txt='';
foreach ($GLOBALS['defaultJSscipts'] as $script) { foreach ($GLOBALS['defaultJSscipts'] as $script) {
$JSscript_txt.="<script src='".LS_JS_DIR.$script."' type='text/javascript'></script>\n"; $nocache = LStemplate :: getNoCacheFileValue(LS_JS_DIR.$script);
$JSscript_txt.="<script src='".LS_JS_DIR.$script."?nocache=$nocache' type='text/javascript'></script>\n";
} }
foreach (self :: $JSscripts as $script) { foreach (self :: $JSscripts as $script) {
@ -1348,7 +1349,8 @@ class LSsession {
else { else {
$script['path'].='/'; $script['path'].='/';
} }
$JSscript_txt.="<script src='".$script['path'].$script['file']."' type='text/javascript'></script>\n"; $nocache = LStemplate :: getNoCacheFileValue($script['path'].$script['file']);
$JSscript_txt.="<script src='".$script['path'].$script['file']."?nocache=$nocache' type='text/javascript'></script>\n";
} }
$KAconf = LSconfig :: get('keepLSsessionActive'); $KAconf = LSconfig :: get('keepLSsessionActive');
@ -1384,7 +1386,8 @@ class LSsession {
} }
$Css_txt=''; $Css_txt='';
foreach (self :: $CssFiles as $file) { foreach (self :: $CssFiles as $file) {
$Css_txt.="<link rel='stylesheet' type='text/css' href='".$file."' />\n"; $nocache = LStemplate :: getNoCacheFileValue($file);
$Css_txt.="<link rel='stylesheet' type='text/css' href='".$file."?nocache=$nocache' />\n";
} }
LStemplate :: assign('LSsession_css',$Css_txt); LStemplate :: assign('LSsession_css',$Css_txt);

View file

@ -138,32 +138,39 @@ class LStemplate {
* @param[in] string $name The file name (eg: mail.png) * @param[in] string $name The file name (eg: mail.png)
* @param[in] string $root_dir The root directory (eg: images) * @param[in] string $root_dir The root directory (eg: images)
* @param[in] string $default_dir The default directory (eg: default) * @param[in] string $default_dir The default directory (eg: default)
* @param[in] bool $with_nocache If true, include nocache URL param (default: false)
* *
* @retval string The path of the file * @retval string The path of the file
**/ **/
public static function getFilePath($file,$root_dir,$default_dir='default') { public static function getFilePath($file, $root_dir, $default_dir=null, $with_nocache=false) {
if ($default_dir === null)
$default_dir = 'default';
foreach(self :: $directories as $dir) { foreach(self :: $directories as $dir) {
if (file_exists($root_dir.'/'.$dir.'/'.$file)) { if (file_exists($root_dir.'/'.$dir.'/'.$file)) {
return $root_dir.'/'.$dir.'/'.$file; $path = $root_dir.'/'.$dir.'/'.$file;
} }
} }
if (!$default_dir) { if (!$default_dir) {
return; return;
} }
return $root_dir.'/'.$default_dir.'/'.$file; $path = $root_dir.'/'.$default_dir.'/'.$file;
if ($with_nocache)
$path .= "?nocache=".self::getNoCacheFileValue($path);
return $path;
} }
/** /**
* Return the path of the image file to use * Return the path of the image file to use
* *
* @param[in] string $image The image name (eg: mail) * @param[in] string $image The image name (eg: mail)
* @param[in] bool $with_nocache If true, include nocache URL param (default: false)
* *
* @retval string The path of the image file * @retval string The path of the image file
**/ **/
public static function getImagePath($image) { public static function getImagePath($image, $with_nocache=false) {
$exts=array('png','gif','jpg'); $exts=array('png','gif','jpg');
foreach($exts as $ext) { foreach($exts as $ext) {
$path=self :: getFilePath("$image.$ext",self :: $config['image_dir'],False); $path = self :: getFilePath("$image.$ext", self :: $config['image_dir'], False, $with_nocache);
if ($path) return $path; if ($path) return $path;
} }
return self :: $config['image_dir']."/default/$image.png"; return self :: $config['image_dir']."/default/$image.png";
@ -173,22 +180,38 @@ class LStemplate {
* Return the path of the CSS file to use * Return the path of the CSS file to use
* *
* @param[in] string $css The CSS name (eg: main.css) * @param[in] string $css The CSS name (eg: main.css)
* @param[in] bool $with_nocache If true, include nocache URL param (default: false)
* *
* @retval string The path of the CSS file * @retval string The path of the CSS file
**/ **/
public static function getCSSPath($css) { public static function getCSSPath($css, $with_nocache=false) {
return self :: getFilePath($css,self :: $config['css_dir']); return self :: getFilePath($css, self :: $config['css_dir'], Null, $with_nocache);
} }
/** /**
* Return the path of the Smarty template file to use * Return the path of the Smarty template file to use
* *
* @param[in] string $template The template name (eg: top.tpl) * @param[in] string $template The template name (eg: top.tpl)
* @param[in] bool $with_nocache If true, include nocache URL param (default: false)
* *
* @retval string The path of the Smarty template file * @retval string The path of the Smarty template file
**/ **/
public static function getTemplatePath($template) { public static function getTemplatePath($template, $with_nocache=false) {
return self :: getFilePath($template,self :: $config['template_dir']); return self :: getFilePath($template, self :: $config['template_dir'], null, $with_nocache);
}
/**
* Return the nocache value of the specify file
*
* @param[in] string $file The file path
*
* @retval string The specified file's nocache value
**/
public static function getNoCacheFileValue($file) {
$stat = @stat($file);
if (is_array($stat) && isset($stat['mtime']))
return md5($stat['mtime']);
return md5(time());
} }
/** /**
@ -295,7 +318,7 @@ function LStemplate_smarty_img($params) {
function LStemplate_smarty_css($params) { function LStemplate_smarty_css($params) {
extract($params); extract($params);
echo LStemplate :: getCSSPath($name); echo LStemplate :: getCSSPath($name, true);
} }
function LStemplate_smarty_uniqid($params, &$smarty) { function LStemplate_smarty_uniqid($params, &$smarty) {