From a3d4d43e85917835a61b9b85b3e56c53e9d0e342 Mon Sep 17 00:00:00 2001 From: Janusz Dobrowolski Date: Thu, 25 Dec 2014 18:31:05 +0100 Subject: [PATCH 1/1] Fixed display issues when different settings were used for various app users (javascript files are now cached for every user separately). --- access/login.php | 1 + admin/db/company_db.inc | 2 +- includes/current_user.inc | 15 ++++++++++++++- includes/lang/language.php | 3 --- includes/main.inc | 23 ++++++++++++++++++++++- includes/page/header.inc | 3 ++- includes/ui/ui_view.inc | 2 +- 7 files changed, 41 insertions(+), 8 deletions(-) diff --git a/access/login.php b/access/login.php index 45294376..b2bad446 100644 --- a/access/login.php +++ b/access/login.php @@ -43,6 +43,7 @@ function defaultCompany() document.getElementById('log_msg').innerHTML='$demo_text'}, 1000*$login_delay);"; $demo_text = $blocked_msg; } + flush_dir(user_js_cache()); if (!isset($def_coy)) $def_coy = 0; $def_theme = "default"; diff --git a/admin/db/company_db.inc b/admin/db/company_db.inc index 9fd2e68c..d0f85e40 100644 --- a/admin/db/company_db.inc +++ b/admin/db/company_db.inc @@ -82,7 +82,7 @@ function set_company_pref($pref, $category, $type, $length, $value) function refresh_sys_prefs() { - flush_dir(company_path().'/js_cache'); // clear cache + flush_dir(user_js_cache()); // clear cache unset($_SESSION['SysPrefs']); get_company_prefs(); } diff --git a/includes/current_user.inc b/includes/current_user.inc index 26b288a1..eea6d1b9 100644 --- a/includes/current_user.inc +++ b/includes/current_user.inc @@ -136,6 +136,7 @@ class current_user $this->logged = true; $this->last_act = time(); $this->timeout = session_timeout(); + flush_dir(user_js_cache()); // refresh cache on login } } return $this->logged; @@ -598,6 +599,18 @@ function add_user_js_data() { add_js_source($js); } +function user_js_cache($id=null) +{ + global $path_to_root; + + if (!$id) + $id = @$_SESSION['wa_current_user']->user; + + if (!$id) + $id = 0; // before login + return $path_to_root.'/company/'.user_company().'/js_cache/'.$id; +} + //-------------------------------------------------------------------------- function session_timeout() @@ -688,7 +701,7 @@ function recalculate_cols(&$cols) function flush_dir($path, $wipe = false) { - $dir = opendir($path); + $dir = @opendir($path); if(!$dir) return; diff --git a/includes/lang/language.php b/includes/lang/language.php index c090f1fc..752c7e44 100644 --- a/includes/lang/language.php +++ b/includes/lang/language.php @@ -55,9 +55,6 @@ class language if ($lang && $changed) { - // flush cache as we can use several languages in one account - flush_dir(company_path().'/js_cache'); - $this->name = $lang['name']; $this->code = $lang['code']; $this->encoding = $lang['encoding']; diff --git a/includes/main.inc b/includes/main.inc index 8ef038ca..018fb2b0 100644 --- a/includes/main.inc +++ b/includes/main.inc @@ -69,13 +69,34 @@ function cache_js_file($fpath, $text) if(!$go_debug) $text = js_compress($text); - $file = fopen($fpath, 'w'); + $file = force_open($fpath); if (!$file) return false; if (!fwrite($file, $text)) return false; return fclose($file); } +/* + Open file for writing with creration of subfolders if needed. +*/ +function force_open($fname) +{ + $file = pathinfo($fname); + + $path = $fname[0] == '/' ? '/' : ''; + $tree = explode('/', $file['dirname']); + foreach($tree as $level) { + $path .= $level; + if (!file_exists($path)) { + if (!mkdir($path)) { + return null; + } + } + $path .= '/'; + } + return fopen($fname, 'w'); +} + function add_js_file($filename) { global $js_static; diff --git a/includes/page/header.inc b/includes/page/header.inc index 0ae62638..61c15585 100644 --- a/includes/page/header.inc +++ b/includes/page/header.inc @@ -52,7 +52,8 @@ function send_scripts() $js =''; foreach($js_static as $jsfile) { - $fpath = company_path().'/js_cache/'.$jsfile; + $cached_name = basename($jsfile); + $fpath = user_js_cache().'/'.$cached_name; // compress also static files if (!file_exists($fpath) || $go_debug) cache_js_file($fpath, file_get_contents($js_path.$jsfile)); diff --git a/includes/ui/ui_view.inc b/includes/ui/ui_view.inc index 8c8bae8d..768447bc 100644 --- a/includes/ui/ui_view.inc +++ b/includes/ui/ui_view.inc @@ -889,7 +889,7 @@ function reset_focus() function get_js_date_picker() { global $go_debug; - $fpath = company_path().'/js_cache/'.'date_picker.js'; + $fpath = user_js_cache().'/'.'date_picker.js'; if (!file_exists($fpath) || $go_debug) { -- 2.30.2