diff --git a/public_html/.htaccess b/public_html/.htaccess index 7e5f4514..0be5b1f1 100644 --- a/public_html/.htaccess +++ b/public_html/.htaccess @@ -1,7 +1,9 @@ RewriteEngine On -# Always rewrite tmp file access +# Always rewrite tmp, css & libs file access RewriteRule ^(tmp/.*)$ index.php?REQUESTED_URL=$1 [L,QSA] +RewriteRule ^(css/.*)$ index.php?REQUESTED_URL=$1 [L,QSA] +RewriteRule ^(libs/.*)$ index.php?REQUESTED_URL=$1 [L,QSA] # If the request is not for a valid file RewriteCond %{REQUEST_FILENAME} !-f diff --git a/public_html/includes/class/class.LSformElement_date.php b/public_html/includes/class/class.LSformElement_date.php index 8bf6bc78..9357b955 100644 --- a/public_html/includes/class/class.LSformElement_date.php +++ b/public_html/includes/class/class.LSformElement_date.php @@ -166,11 +166,11 @@ class LSformElement_date extends LSformElement { $codeLang = str_replace('_','-',preg_replace('/\..*$/','',LSsession :: getLang())); - LSsession :: addJSscript('Picker.js',LS_LIB_DIR.'arian-mootools-datepicker/'); - LSsession :: addJSscript('Picker.Attach.js',LS_LIB_DIR.'arian-mootools-datepicker/'); - LSsession :: addJSscript('Picker.Date.js',LS_LIB_DIR.'arian-mootools-datepicker/'); - LSsession :: addJSscript('Locale.'.$codeLang.'.DatePicker.js',LS_LIB_DIR.'arian-mootools-datepicker/'); - LSsession :: addCssFile('datepicker_'.$params['style'].'.css',LS_LIB_DIR.'arian-mootools-datepicker/datepicker_'.$params['style'].'/'); + LSsession :: addLibJSscript('arian-mootools-datepicker/Picker.js'); + LSsession :: addLibJSscript('arian-mootools-datepicker/Picker.Attach.js'); + LSsession :: addLibJSscript('arian-mootools-datepicker/Picker.Date.js'); + LSsession :: addLibJSscript('arian-mootools-datepicker/Locale.'.$codeLang.'.DatePicker.js'); + LSsession :: addLibCssFile('arian-mootools-datepicker/datepicker_'.$params['style'].'/datepicker_'.$params['style'].'.css'); LSsession :: addJSscript('LSformElement_date_field.js'); LSsession :: addJSscript('LSformElement_date.js'); diff --git a/public_html/includes/class/class.LSsession.php b/public_html/includes/class/class.LSsession.php index df55acd2..a5b8dcb2 100644 --- a/public_html/includes/class/class.LSsession.php +++ b/public_html/includes/class/class.LSsession.php @@ -76,12 +76,18 @@ class LSsession { // Les fichiers JS à charger dans la page private static $JSscripts = array(); + // Libs JS files to load on page + private static $LibsJSscripts = array(); + // Les paramètres JS à communiquer dans la page private static $_JSconfigParams = array(); // Les fichiers CSS à charger dans la page private static $CssFiles = array(); + // Libs CSS files to load on page + private static $LibsCssFiles = array(); + // L'objet de l'utilisateur connecté private static $LSuserObject = NULL; @@ -192,6 +198,8 @@ class LSsession { 'template_dir' => LS_ROOT_DIR . '/'. LS_TEMPLATES_DIR, 'image_dir' => LS_IMAGES_DIR, 'css_dir' => LS_CSS_DIR, + 'js_dir' => LS_JS_DIR, + 'libs_dir' => LS_LIB_DIR, 'compile_dir' => LS_TMP_DIR_PATH, 'debug' => LSdebug, 'debug_smarty' => (isset($_REQUEST) && isset($_REQUEST['LStemplate_debug'])), @@ -1400,21 +1408,33 @@ class LSsession { self :: $template = $template; } - /** - * Ajoute un script JS au chargement de la page - * - * Remarque : les scripts doivents être dans le dossier LS_JS_DIR. - * - * @param[in] $script Le nom du fichier de script à charger. - * - * @retval void - */ - public static function addJSscript($file,$path=NULL) { - $script=array( - 'file' => $file, - 'path' => $path - ); - self :: $JSscripts[$path.$file]=$script; + /** + * Add a JS script to load on page + * + * @param[in] $file string The JS filename + * @param[in] $path string|null The sub-directory path that contain this file. + * Keep for retro-compatibility : you could just + * prefix the file name. + * + * @retval void + */ + public static function addJSscript($file, $path=NULL) { + if ($path) + $file = $path.$file; + if (!in_array($file, self :: $JSscripts)) + self :: $JSscripts[] = $file; + } + + /** + * Add a library JS file to load on page + * + * @param[in] $file string The JS filename + * + * @retval void + */ + public static function addLibJSscript($file) { + if (!in_array($file, self :: $LibsJSscripts)) + self :: $LibsJSscripts[] = $file; } /** @@ -1430,20 +1450,32 @@ class LSsession { } /** - * Ajoute une feuille de style au chargement de la page + * Add a CSS file to load on page * - * @param[in] $script Le nom du fichier css à charger. + * @param[in] $file string The CSS filename + * @param[in] $path string|null The sub-directory path that contain this file. + * Keep for retro-compatibility : you could just + * prefix the file name. * * @retval void */ - public static function addCssFile($file,$path=NULL) { - if ($path) { + public static function addCssFile($file, $path=NULL) { + if ($path) $file = $path.$file; - } - else { - $file = LStemplate :: getCSSPath($file); - } - self :: $CssFiles[$file]=$file; + if (!in_array($file, self :: $CssFiles)) + self :: $CssFiles[] = $file; + } + + /** + * Add a library CSS file to load on page + * + * @param[in] $file string The CSS filename + * + * @retval void + */ + public static function addLibCssFile($file) { + if (!in_array($file, self :: $LibsCssFiles)) + self :: $LibsCssFiles[] = $file; } /** @@ -1454,24 +1486,6 @@ class LSsession { * @retval void */ public static function displayTemplate() { - // JS - $JSscript_txt=''; - foreach ($GLOBALS['defaultJSscipts'] as $script) { - $nocache = LStemplate :: getNoCacheFileValue(LS_JS_DIR.$script); - $JSscript_txt.="\n"; - } - - foreach (self :: $JSscripts as $script) { - if (!$script['path']) { - $script['path']=LS_JS_DIR; - } - else { - $script['path'].='/'; - } - $nocache = LStemplate :: getNoCacheFileValue($script['path'].$script['file']); - $JSscript_txt.="\n"; - } - $KAconf = LSconfig :: get('keepLSsessionActive'); if ( ( @@ -1487,28 +1501,28 @@ class LSsession { LStemplate :: assign('LSjsConfig',base64_encode(json_encode(self :: $_JSconfigParams))); - if (LSdebug) { - $JSscript_txt.="\n"; - } - else { - $JSscript_txt.="\n"; - } + // JS files + $JSscripts = array(); + if (isset($GLOBALS['defaultJSscipts']) && is_array($GLOBALS['defaultJSscipts'])) + foreach ($GLOBALS['defaultJSscipts'] as $script) + if (!in_array($script, $JSscripts)) + $JSscripts[] = $script; - LStemplate :: assign('LSsession_js',$JSscript_txt); + foreach (self :: $JSscripts as $script) + if (!in_array($script, $JSscripts)) + $JSscripts[] = $script; + LStemplate :: assign('JSscripts', $JSscripts); + LStemplate :: assign('LibsJSscripts', self :: $LibsJSscripts); + LStemplate :: assign('LSdebug', boolval(LSdebug)); - // Css + // CSS files self :: addCssFile("LSdefault.css"); - if (isset($GLOBALS['defaultCSSfiles']) && is_array($GLOBALS['defaultCSSfiles'])) { - foreach ($GLOBALS['defaultCSSfiles'] as $file) { - self :: addCssFile($file); - } - } - $Css_txt=''; - foreach (self :: $CssFiles as $file) { - $nocache = LStemplate :: getNoCacheFileValue($file); - $Css_txt.="\n"; - } - LStemplate :: assign('LSsession_css',$Css_txt); + if (isset($GLOBALS['defaultCSSfiles']) && is_array($GLOBALS['defaultCSSfiles'])) + foreach ($GLOBALS['defaultCSSfiles'] as $file) + if (!in_array($script, self :: $CssFiles)) + self :: addCssFile($file); + LStemplate :: assign('CssFiles', self :: $CssFiles); + LStemplate :: assign('LibsCssFiles', self :: $LibsCssFiles); // Access LStemplate :: assign('LSaccess', self :: getLSaccess()); diff --git a/public_html/includes/class/class.LStemplate.php b/public_html/includes/class/class.LStemplate.php index 7bfd699a..e22768ed 100644 --- a/public_html/includes/class/class.LStemplate.php +++ b/public_html/includes/class/class.LStemplate.php @@ -48,6 +48,8 @@ class LStemplate { 'template_dir' => 'templates', 'image_dir' => 'images', 'css_dir' => 'css', + 'js_dir' => 'includes/js', + 'libs_dir' => 'includes/libs', 'compile_dir' => 'tmp', 'debug' => False, 'debug_smarty' => False @@ -60,10 +62,7 @@ class LStemplate { private static $_smarty_version = NULL; // Array of directories - private static $directories = array( - 'local', - LS_THEME - ); + private static $directories = array('local', LS_THEME, './'); // Registered events private static $_events = array(); @@ -177,11 +176,23 @@ class LStemplate { $default_dir = self :: getDefaultDir(); $path = false; foreach(self :: $directories as $dir) { - $path = $root_dir.'/'.$dir.'/'.$file; - if (file_exists($path)) { + $dir_path = realpath($root_dir.'/'.$dir); + if ($dir_path === false) + // Directory not found or not accessible + continue; + $file_path = realpath($dir_path.'/'.$file); + if ($file_path === false) + // File not found or not accessible + continue; + // Checks that the file is in the actual folder location + $pos = strpos($file_path, $dir_path); + if (!is_int($pos) || $pos != 0) { + LSlog :: error("LStemplate :: getFilePath($file, $root_dir, $default_dir, $with_nocache) : File '$file_path' is not in root directory '$dir_path' (".varDump($pos).")."); + } + elseif (file_exists($file_path)) { + $path = $file_path; break; } - $path = false; } if (!$path) { if (!$default_dir) @@ -222,6 +233,30 @@ class LStemplate { return self :: getFilePath($css, self :: $config['css_dir'], Null, $with_nocache); } + /** + * Return the path of the JS file to use + * + * @param[in] string $js The JS name (eg: LSdefaults.js) + * @param[in] bool $with_nocache If true, include nocache URL param (default: false) + * + * @retval string The path of the CSS file + **/ + public static function getJSPath($js, $with_nocache=false) { + return self :: getFilePath($js, self :: $config['js_dir'], Null, $with_nocache); + } + + /** + * Return the path of the libary file to use + * + * @param[in] string $file_path The lib file path (eg: arian-mootools-datepicker/Picker.js) + * @param[in] bool $with_nocache If true, include nocache URL param (default: false) + * + * @retval string The path of the Lib file + **/ + public static function getLibFilePath($file_path, $with_nocache=false) { + return self :: getFilePath($file_path, self :: $config['libs_dir'], Null, $with_nocache); + } + /** * Return the path of the Smarty template file to use * @@ -429,8 +464,7 @@ function LStemplate_smarty_img($params) { } function LStemplate_smarty_css($params) { - extract($params); - echo LStemplate :: getCSSPath($name, true); + echo "css/".$params['name']; } function LStemplate_smarty_uniqid($params, &$smarty) { diff --git a/public_html/includes/functions.php b/public_html/includes/functions.php index cf9e43f9..f5d69be4 100644 --- a/public_html/includes/functions.php +++ b/public_html/includes/functions.php @@ -665,14 +665,15 @@ function LSdebugDefined() { * Dump file content * * @param[in] $file_path string The file path to dump + * @param[in] $mime_type string|null The MIME type return as Content-type (optional, default: auto-detected) * @param[in] $max_age integer The cache max_age value, as return in Cache-Control HTTP header * (optional, default: 3600) * * @retval void **/ -function dumpFile($file_path, $max_age=3600) { +function dumpFile($file_path, $mime_type=null, $max_age=3600) { if (is_file($file_path)) { - header('Content-Type: '.mime_content_type($file_path)); + header('Content-Type: '.(is_null($mime_type)?mime_content_type($file_path):$mime_type)); $last_modified_time = filemtime($file_path); $etag = md5_file($file_path); header("Cache-Control: max-age=$max_age, must-revalidate"); diff --git a/public_html/includes/routes.php b/public_html/includes/routes.php index 548d2720..3063446c 100644 --- a/public_html/includes/routes.php +++ b/public_html/includes/routes.php @@ -234,20 +234,59 @@ function handle_old_global_search_php($request) { LSurl :: add_handler('#^global_search\.php#', 'handle_old_global_search_php'); /* - * Handle image request + * Handle static file request * * @param[in] $request LSurlRequest The request * * @retval void **/ -function handle_image($request) { - $img_path = LStemplate :: getImagePath($request -> image); - if (is_file($img_path)) { - dumpFile($img_path); +function handle_static_file($request) { + switch ($request -> type) { + case 'image': + $path = LStemplate :: getImagePath($request -> file); + $mime_type = null; + break; + case 'css': + $path = LStemplate :: getCSSPath($request -> file); + $mime_type = 'text/css'; + break; + case 'js': + $path = LStemplate :: getJSPath($request -> file); + $mime_type = 'text/javascript'; + break; + } + if ($path && is_file($path)) { + dumpFile($path, $mime_type); } LSurl :: error_404($request); } -LSurl :: add_handler('#^image/(?P[^/]+)$#', 'handle_image', false); +LSurl :: add_handler('#^(?Pimage|css|js)/(?P[^/]+)$#', 'handle_static_file', false); + +/* + * Handle libs file request + * + * @param[in] $request LSurlRequest The request + * + * @retval void + **/ +function handle_libs_file($request) { + $path = LStemplate :: getLibFilePath($request -> file); + if ($path && is_file($path)) { + switch (strtolower(substr($path, -4))) { + case '.css': + $mime_type = 'text/css'; + break; + case '.js': + $mime_type = 'text/javascript'; + break; + default: + $mime_type = null; + } + dumpFile($path, $mime_type); + } + LSurl :: error_404($request); +} +LSurl :: add_handler('#^libs/(?P.+)$#', 'handle_libs_file', false); /* * Handle tmp file request diff --git a/public_html/templates/default/LSsession_css.tpl b/public_html/templates/default/LSsession_css.tpl new file mode 100644 index 00000000..a8db2663 --- /dev/null +++ b/public_html/templates/default/LSsession_css.tpl @@ -0,0 +1,13 @@ +{if isset($CssFiles) && is_array($CssFiles)} + +{foreach $CssFiles as $file} + +{/foreach} +{/if} + +{if isset($LibsCssFiles) && is_array($LibsCssFiles)} + +{foreach $LibsCssFiles as $file} + +{/foreach} +{/if} diff --git a/public_html/templates/default/LSsession_js.tpl b/public_html/templates/default/LSsession_js.tpl new file mode 100644 index 00000000..c682230f --- /dev/null +++ b/public_html/templates/default/LSsession_js.tpl @@ -0,0 +1,16 @@ +{if isset($JSscripts) && is_array($JSscripts)} + +{foreach $JSscripts as $file} + +{/foreach} +{/if} + + + + +{if isset($LibsJSscripts) && is_array($LibsJSscripts)} + +{foreach $LibsJSscripts as $file} + +{/foreach} +{/if} diff --git a/public_html/templates/default/blank.tpl b/public_html/templates/default/blank.tpl index e570b4a7..d1592814 100644 --- a/public_html/templates/default/blank.tpl +++ b/public_html/templates/default/blank.tpl @@ -8,13 +8,13 @@ - {$LSsession_css} + {include file='ls:LSsession_css.tpl'} {include file='ls:LSdefault.tpl'} -{$LSsession_js} +{include file='ls:LSsession_js.tpl'} diff --git a/public_html/templates/default/bottom.tpl b/public_html/templates/default/bottom.tpl index 560692e7..cc0b8c4e 100644 --- a/public_html/templates/default/bottom.tpl +++ b/public_html/templates/default/bottom.tpl @@ -1,6 +1,6 @@ -{$LSsession_js} +{include file='ls:LSsession_js.tpl'} diff --git a/public_html/templates/default/error_404.tpl b/public_html/templates/default/error_404.tpl index bafedfd6..06887ec7 100644 --- a/public_html/templates/default/error_404.tpl +++ b/public_html/templates/default/error_404.tpl @@ -8,17 +8,16 @@ - {$LSsession_css} + {include file='ls:LSsession_css.tpl'} {include file='ls:LSdefault.tpl'} -{$LSsession_js} -

{tr msg="The requested page was not found."}

+{include file='ls:LSsession_js.tpl'} diff --git a/public_html/templates/default/fatal_error.tpl b/public_html/templates/default/fatal_error.tpl index f9430847..f429bbcc 100644 --- a/public_html/templates/default/fatal_error.tpl +++ b/public_html/templates/default/fatal_error.tpl @@ -8,14 +8,12 @@ - {$LSsession_css} + {include file='ls:LSsession_css.tpl'} {include file='ls:LSdefault.tpl'} -{$LSsession_js} -

{tr msg="A fatal error occured. If problem persist, please contact support."}

@@ -26,5 +24,6 @@ {/if}
+{include file='ls:LSsession_js.tpl'} diff --git a/public_html/templates/default/login.tpl b/public_html/templates/default/login.tpl index 436f6e63..5c6729dd 100644 --- a/public_html/templates/default/login.tpl +++ b/public_html/templates/default/login.tpl @@ -7,8 +7,7 @@ - {$LSsession_css} - {$LSsession_js} + {include file='ls:LSsession_css.tpl'} @@ -43,5 +42,6 @@ {$lang_label} : {$LSlang|escape:"htmlall"} {$loginform_label_recoverPassword|escape:"htmlall"} +{include file='ls:LSsession_js.tpl'} diff --git a/public_html/templates/default/recoverpassword.tpl b/public_html/templates/default/recoverpassword.tpl index 68f293ac..a3705f09 100644 --- a/public_html/templates/default/recoverpassword.tpl +++ b/public_html/templates/default/recoverpassword.tpl @@ -6,8 +6,7 @@ LdapSaisie{if $pagetitle != ''} - {$pagetitle|escape:"htmlall"}{/if} - {$LSsession_css} - {$LSsession_js} + {include file='ls:LSsession_css.tpl'} @@ -34,5 +33,6 @@ {$lang_label|escape:"htmlall"} : {$LSlang|escape:"htmlall"} {$recoverpasswordform_label_back|escape:"htmlall"} +{include file='ls:LSsession_js.tpl'} diff --git a/public_html/templates/default/top.tpl b/public_html/templates/default/top.tpl index 71f539bc..c6c1d954 100644 --- a/public_html/templates/default/top.tpl +++ b/public_html/templates/default/top.tpl @@ -8,7 +8,7 @@ - {$LSsession_css} + {include file='ls:LSsession_css.tpl'}