Replace calls to htmlspecialchars() with sm_encode_html_special_chars().
[squirrelmail.git] / class / template / Template.class.php
index 284be85bab402ab32f674bbf02d904770e4aec52..b12da29dbe06769272c57e74fed79dfe5fc09c7b 100644 (file)
@@ -8,7 +8,7 @@
   * class with any custom functionality needed to interface a target
   * templating engine with SquirrelMail.
   *
-  * @copyright © 2003-2007 The SquirrelMail Project Team
+  * @copyright 2003-2012 The SquirrelMail Project Team
   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
   * @version $Id$
   * @package squirrelmail
@@ -69,6 +69,11 @@ class Template
       */
     var $template_engine = '';
 
+    /**
+      * The content type for this template set
+      */
+    var $content_type = '';
+
     /**
       * The fall-back template ID
       *
@@ -191,6 +196,13 @@ class Template
             = Template::calculate_template_file_directory($this->fallback_template_set_id);
 
 
+        // determine content type, defaulting to text/html
+        //
+        $this->content_type = Template::get_template_config($this->template_set_id,
+                                                            'content_type',
+                                                            'text/html');
+
+
         // determine template engine
         // FIXME: assuming PHP template engine may not necessarily be a good thing
         //
@@ -201,7 +213,7 @@ class Template
 
         // get template file cache
         //
-        $this->template_file_cache = Template::cache_template_file_hierarchy();
+        $this->template_file_cache = Template::cache_template_file_hierarchy($template_set_id);
 
     }
 
@@ -307,6 +319,42 @@ class Template
 
     }
 
+    /**
+      * Determine what the RPC template set is.
+      *
+      * NOTE that if the default setting cannot be found in the
+      * main SquirrelMail configuration settings that the value
+      * of $default is returned.
+      *
+      * @param string $default The template set ID to use if
+      *                        the default setting cannot be
+      *                        found in SM config (optional;
+      *                        defaults to "default_rpc").
+      *
+      * @return string The ID of the RPC template set.
+      *
+      * @static
+      *
+      */
+    function get_rpc_template_set($default='default_rpc') {
+
+// FIXME: do we want to place any restrictions on the ID such as
+//        making sure no slashes included?
+
+        // values are in main SM config file
+        //
+        global $rpc_templateset;
+        $rpc_templateset = (!isset($rpc_templateset)
+                         ? $default : $rpc_templateset);
+
+        // FIXME: note that it is possible for this to
+        // point to an invalid (nonexistent) template set
+        // and that error will not be caught here
+        //
+        return $rpc_templateset;
+
+    }
+
     /**
       * Allow template set to override plugin configuration by either
       * adding or removing plugins.
@@ -491,6 +539,18 @@ class Template
 
     }
 
+    /**
+      * Return the content-type for this template set.
+      *
+      * @return string The content-type.
+      *
+      */
+    function get_content_type() {
+
+        return $this->content_type;
+
+    }
+
     /**
       * Get template set config setting
       *
@@ -621,6 +681,14 @@ class Template
       * constructed and stored in session before being returned
       * to the caller.
       *
+      * @param string  $template_set_id  The template set for which
+      *                                  the cache should be built.
+      *                                  This function will save more
+      *                                  than one set's files, so it
+      *                                  may be called multiple times
+      *                                  with different values for this
+      *                                  argument.  When regenerating,
+      *                                  all set caches are dumped.
       * @param boolean $regenerate_cache When TRUE, the file hierarchy
       *                                  is reloaded and stored fresh
       *                                  (optional; default FALSE).
@@ -631,12 +699,12 @@ class Template
       *                                  empty - no additional files).
       *
       * @return array Template file hierarchy array, whose keys
-      *               are all the template file names (with path
-      *               information relative to the template set's
-      *               base directory, e.g., "css/style.css")
-      *               found in all parent template sets including
-      *               the ultimate fall-back template set.
-      *               Array values are sub-arrays with the
+      *               are all the template file names for the given
+      *               template set ID (with path information relative
+      *               to the template set's base directory, e.g.,
+      *               "css/style.css") found in all parent template
+      *               sets including the ultimate fall-back template
+      *               set.  Array values are sub-arrays with the
       *               following key-value pairs:
       *
       *                 PATH    --  file path, relative to SM_PATH
@@ -646,7 +714,8 @@ class Template
       * @static
       *
       */
-    function cache_template_file_hierarchy($regenerate_cache=FALSE,
+    function cache_template_file_hierarchy($template_set_id,
+                                           $regenerate_cache=FALSE,
                                            $additional_files=array()) {
 
         sqGetGlobalVar('template_file_hierarchy', $template_file_hierarchy,
@@ -655,51 +724,39 @@ class Template
 
         if ($regenerate_cache) unset($template_file_hierarchy);
 
-        if (!empty($template_file_hierarchy)) {
+        if (!empty($template_file_hierarchy[$template_set_id])) {
 
             // have to add additional files if given before returning
             //
             if (!empty($additional_files)) {
-                $template_file_hierarchy = array_merge($template_file_hierarchy,
-                                                       $additional_files);
+                $template_file_hierarchy[$template_set_id]
+                    = array_merge($template_file_hierarchy[$template_set_id],
+                                  $additional_files);
+
                 sqsession_register($template_file_hierarchy,
                                    'template_file_hierarchy');
             }
 
-            return $template_file_hierarchy;
+            return $template_file_hierarchy[$template_set_id];
         }
 
 
         // nothing in cache apparently, so go build it now
         //
-        // FIXME: not sure if there is any possibility that
-        //        this could be called when $sTemplateID has
-        //        yet to be defined... throw error for now,
-        //        but if the error occurs, it's a coding error
-        //        rather than a configuration error
-        //
-        global $sTemplateID;
-        if (empty($sTemplateID)) {
+        $template_file_hierarchy[$template_set_id] = Template::catalog_template_files($template_set_id);
 
-            trigger_error('Template set ID unknown', E_USER_ERROR);
-
-        } else {
-
-            $template_file_hierarchy = Template::catalog_template_files($sTemplateID);
-
-            // additional files, if any
-            //
-            if (!empty($additional_files)) {
-                $template_file_hierarchy = array_merge($template_file_hierarchy,
-                                                       $additional_files);
-            }
-
-            sqsession_register($template_file_hierarchy,
-                               'template_file_hierarchy');
+        // additional files, if any
+        //
+        if (!empty($additional_files)) {
+            $template_file_hierarchy[$template_set_id]
+                = array_merge($template_file_hierarchy[$template_set_id],
+                              $additional_files);
+        }
 
-            return $template_file_hierarchy;
+        sqsession_register($template_file_hierarchy,
+                           'template_file_hierarchy');
 
-        }
+        return $template_file_hierarchy[$template_set_id];
 
     }
 
@@ -861,7 +918,9 @@ class Template
                                                                           )
                               );
             $this->template_file_cache
-                = $this->cache_template_file_hierarchy(FALSE, $file_list);
+                = $this->cache_template_file_hierarchy($this->template_set_id,
+                                                       FALSE,
+                                                       $file_list);
             return TRUE;
         }
 
@@ -1330,16 +1389,23 @@ FIXME: We could make the incoming array more complex so it can
       *
       * @param mixed $headers A list of (or a single) header
       *                       text to be sent.
+      * @param boolean $replace Whether or not to replace header(s)
+      *                         previously sent header(s) of the
+      *                         same type (this parameter may be
+      *                         ignored in some implementations
+      *                         of this class if the target interface
+      *                         does not support this functionality)
+      *                         (OPTIONAL; default = TRUE, always replace).
       *
       */
-    function header($headers)
+    function header($headers, $replace=TRUE)
     {
 
         if (!is_array($headers)) $headers = array($headers);
 
         foreach ($headers as $header) {
             $this->assign('header', $header);
-            header($this->fetch('header.tpl'));
+            header($this->fetch('header.tpl'), $replace);
         }
 
     }
@@ -1414,15 +1480,15 @@ FIXME: We could make the incoming array more complex so it can
 
         if (empty($template)) {
 
-            trigger_error('The template "' . htmlspecialchars($file)
+            trigger_error('The template "' . sm_encode_html_special_chars($file)
                           . '" could not be fetched!', E_USER_ERROR);
 
         } else {
 
             $aPluginOutput = array();
+            $temp = array(&$aPluginOutput, &$this);
             $aPluginOutput = concat_hook_function('template_construct_' . $file,
-                                                  $temp=array(&$aPluginOutput, &$this),
-                                                  TRUE);
+                                                  $temp, TRUE);
             $this->assign('plugin_output', $aPluginOutput);
 
             //$output = $this->apply_template($template);