Make style.php only load font prefs.
[squirrelmail.git] / src / style.php
index 7d7976bd0b07c8d817bbd3ea3370ab5660a4a897..2a6ec0144a4f235331c9653f6002ac3cd6dcbb1f 100644 (file)
@@ -1,13 +1,18 @@
 <?php
+
 /**
  * Style sheet script
  *
- * Script processes GET arguments and generates CSS output from stylesheet.tpl.
+ * Script processes GET arguments and generates CSS output from stylesheet.tpl,
+ * which is defined in each template set.
+ *
  * Used GET arguments:
  * <ul>
  *   <li>themeid - string, sets theme file from themes/*.php
+ *   <li>templateid - string, sets template set ID
  *   <li>fontset - string, sets selected set of fonts from $fontsets array.
  *   <li>fontsize - integer, sets selected font size
+ *   <li>dir - string, sets text direction variables. Possible values 'rtl' or 'ltr'
  * </ul>
  * @copyright &copy; 2005-2006 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @package squirrelmail
  */
 
-/** @ignore */
-define('SM_PATH','../');
-
-/* SquirrelMail required files. */
-require_once(SM_PATH . 'functions/global.php');
-require_once(SM_PATH . 'functions/strings.php');
-require_once(SM_PATH . 'config/config.php');
-require_once(SM_PATH . 'include/load_prefs.php');
-
-/* temp setting containing list of font styles. Should go to config.php */
-$fontsets=array();
-$fontsets['sans']['NAME']='Sans';
-$fontsets['sans']['STYLE']='helvetica,arial,sans-serif';
-$fontsets['serif']['NAME']='Serif';
-$fontsets['serif']['STYLE']='serif';
-$fontsets['comicsans']['NAME']='Comic Sans';
-$fontsets['comicsans']['STYLE']='comic sans ms,sans-serif';
-$fontsets['vera']['NAME']='Vera';
-$fontsets['vera']['STYLE']='bitstream vera sans,verdana,sans-serif';
-$fontsets['tahoma']['NAME']='Tahoma';
-$fontsets['tahoma']['STYLE']='tahoma,sans-serif';
-
-/* template init */
-/** start block copy from right_main.php */
-include_once(SM_PATH . 'class/template/template.class.php');
-
-$oTemplate = new Template($sTplDir);
-/** end block copy */
+/**
+ * Set the location in order to skip unneeded validation and other includes
+ * in the SquirrelMail initialisation file.
+ */
+$sInitLocation = 'style';
+
+/**
+ * Include the SquirrelMail initialization file.
+ */
+require('../include/init.php');
+
+/* safety check for older config.php */
+if (!isset($fontsets) || !is_array($fontsets)) {
+    $fontsets=array();
+}
+
 
+/**
+ * The collowing code should no longer be neccesary, but it will remain for the
+ * time being, just in case.
+ * 
+ * TODO: Remove if no longer needed.
+ **/
 /* set default colors in case color theme is not full */
 $color = array();
 $color[0]   = '#dcdcdc'; // (light gray)     TitleBar
@@ -64,31 +63,6 @@ $color[14]  = '#ff0000'; // (red)            Color for quoted text -- >> 2 or mo
 $color[15]  = '#002266'; // (dark blue)      Unselectable folders
 $color[16]  = '#ff9933'; // (orange)         Highlight color
 
-/** get theme from GET */
-if (sqgetGlobalVar('themeid',$themeid,SQ_GET) &&
-    file_exists(SM_PATH . 'themes/'.basename($themeid,'.php').'.php')) {
-    include_once(SM_PATH . 'themes/'.basename($themeid,'.php').'.php');
-} elseif (file_exists($theme[$theme_default]['PATH'])) {
-    include_once($theme[$theme_default]['PATH']);
-}
-
-/**
- * get alignment variable from language settings...
- * MOVE THIS to a central init section !!!!
- */
-if (!sqgetGlobalVar('align',$align,SQ_SESSION)) {
-    $dir = ( isset( $languages[$squirrelmail_language]['DIR']) ) ? $languages[$squirrelmail_language]['DIR'] : 'ltr';
-    if ( $dir == 'ltr' ) {
-        $align = array('left' => 'left', 'right' => 'right');
-    } else {
-        $align = array('left' => 'right', 'right' => 'left');
-    }
-    sqsession_register($align, 'align');
-}
-
-/**/
-$oTemplate->assign('color', $color);
-
 /**
  * set color constants in order to use simple names instead of color array
  * 0 - SQM_TEXT_DISABLED, SQM_TITLE_BACKGROUND, SQM_BUTTON_BACKGROUND_DISABLED,
@@ -157,23 +131,85 @@ define('SQM_ERROR_TEXT',$color[2]);
 define('SQM_ALIGN_LEFT', $align['left']);
 define('SQM_ALIGN_RIGHT', $align['right']);
 
+// END TODO
+
 if (sqgetGlobalVar('fontset',$fontset,SQ_GET) &&
     isset($fontsets[$fontset])) {
-    $fontfamily=$fontsets[$fontset]['STYLE'];
+    $fontfamily=$fontsets[$fontset];
 } else {
     $fontfamily='';
 }
-$oTemplate->assign('fontfamily', $fontfamily);
 
 if (! sqgetGlobalVar('fontsize',$fontsize,SQ_GET)) {
     $fontsize = 0;
 } else {
     $fontsize = (int) $fontsize;
 }
-$oTemplate->assign('fontsize', $fontsize);
 
 header('Content-Type: text/css');
+/**
+ * GOTCHA #1: When sending the headers for caching, we must send Expires,
+ *            Last-Modified, Pragma, and Cache-Control headers.  If we don't PHP 
+ *            will makeup values that will break the cacheing.
+ * 
+ * GOTCHA #2: If the current template does not contain a template named
+ *            stylesheet.tpl, this cacheing will break because filemtime() won't
+ *            work.  This is a problem e.g. with the default_advanced template
+ *            that inherits CSS properties from the default template but
+ *            doesn't contain stylesheet.tpl itself.
+IDEA: So ask the Template class object to return the mtime or better yet, the full file path (at least from SM_PATH) by using $oTemplate->get_template_file_path(stylesheet.tpl) but this is still a problem if the default template also does not have such a file (in which case, we fall back to SM's css/deafult css file (so in that case, go get that file's mtime!)
+ *            Possibly naive suggestion - template can define its own default 
+ *            template name
+ * 
+ * GOTCHA #3: If the user changes user prefs for things like font size then
+ *            the mtime should be updated to the time of that change, and not
+ *            that of the stylesheet.tpl file.  IDEA: can this be a value kept
+ *            in user prefs (always compare to actual file mtime before sending
+ *            to the browser)
+ *
+ * Comment re gotcha #3: If we only define basic font prefs here, we really
+ * only need to refresh the cache if one of the font prefs changes.
+ * Possibly some type of "force nocache flag could be set if a font pref is
+ * changed?
+ * 
+ * TODO: Fix this. :)
+ * */
+
+if ( $lastmod = @filemtime(SM_PATH . $oTemplate->get_template_file_directory() 
+                         . 'css/stylesheet.tpl') ) {
+    $gmlastmod = gmdate('D, d M Y H:i:s', $lastmod) . ' GMT';
+    $expires = gmdate('D, d M Y H:i:s', strtotime('+1 week')) . ' GMT';
+    header('Last-Modified: ' . $gmlastmod);
+    header('Expires: '. $expires);
+    header('Pragma: ');
+    header('Cache-Control: public, must-revalidate');
+}
 
-$oTemplate->display('stylesheet.tpl');
+/**
+ * Additional styles are now handled by adding stylesheets to $sTplDir/css/,
+ * so here, we simply define some basic styles based on user prefs.
+ */
+?>
+/* older css template */
+body, td, th, dd, dt, h1, h2, h3, h4, h5, h6, p, ol, ul, li {
+<?php
+if($fontfamily) echo '  font-family: '.$fontfamily.";\n";
+?>
+}
+body, small {
+<?php
+if($fontsize) echo '  font-size: '.($fontsize-2)."pt;\n";
+?>
+}
+td, th {
+<?php
+if($fontsize) echo '  font-size: '.$fontsize."pt;\n";
+?>
+}
+textarea, pre {
+font-family: monospace;
+<?php
+if($fontsize) echo '  font-size: '.($fontsize-1)."pt;\n";
+?>
+}
 
-?>
\ No newline at end of file