]> git.donarmstrong.com Git - roundcube.git/blobdiff - program/include/rcmail_template.inc
Imported Upstream version 0.1~rc2
[roundcube.git] / program / include / rcmail_template.inc
index 3609bec1b64b6da76b34d3be7b02d42a5ea9cfe9..15752f8bc4ea04b8d64e671de57af686491c8e42 100644 (file)
@@ -10,7 +10,7 @@
  |                                                                       |
  | PURPOSE:                                                              |
  |   Class to handle HTML page output using a skin template.             |
- |   Extends rcube_html_page class from rcube_shared.inc                 |
+ |   Extends rcube_html_page class from rcube_html.inc                   |
  |                                                                       |
  +-----------------------------------------------------------------------+
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
 
 */
 
-require_once('include/rcube_shared.inc');
 
+/**
+ * Classes and functions for HTML output
+ *
+ * @package View
+ */
 
+require_once('include/rcube_html.inc');
+
+
+/**
+ * Class to create HTML page output using a skin template
+ */
 class rcmail_template extends rcube_html_page
 {
   var $config;
@@ -36,11 +46,14 @@ class rcmail_template extends rcube_html_page
   var $object_handlers = array();
 
 
-  // PHP 5 constructor
+  /**
+   * Constructor
+   *
+   * @param array Configuration array
+   * @param string Current task
+   */
   function __construct(&$config, $task)
   {
-    parent::__construct();
-    
     $this->task = $task;
     $this->config = $config;
     $this->ajax_call = !empty($_GET['_remote']) || !empty($_POST['_remote']);
@@ -61,7 +74,10 @@ class rcmail_template extends rcube_html_page
     }
   }
 
-  // PHP 4 compatibility
+  /**
+   * PHP 4 compatibility
+   * @see rcmail_template::__construct()
+   */
   function rcmail_template(&$config, $task)
   {
     $this->__construct($config, $task);
@@ -70,6 +86,10 @@ class rcmail_template extends rcube_html_page
   
   /**
    * Set environment variable
+   *
+   * @param string Property name
+   * @param mixed Property value
+   * @param boolean True if this property should be added to client environment
    */
   function set_env($name, $value, $addtojs=true)
   {
@@ -135,6 +155,10 @@ class rcmail_template extends rcube_html_page
 
   /**
    * Invoke display_message command
+   *
+   * @param string Message to display
+   * @param string Message type [notice|confirm|error]
+   * @param array Key-value pairs to be replaced in localized text
    */
   function show_message($message, $type='notice', $vars=NULL)
   {
@@ -211,10 +235,17 @@ class rcmail_template extends rcube_html_page
   
   
   /**
-   * @override
+   * Process template and write to stdOut
+   *
+   * @param string HTML template
+   * @see rcube_html_page::write()
    */
   function write($template='')
   {
+    // unlock interface after iframe load
+    if ($this->framed)
+      array_unshift($this->js_commands, array('set_busy', false));
+    
     // write all env variables to client
     $js = $this->framed ? "if(window.parent) {\n" : '';
     $js .= $this->get_js_commands() . ($this->framed ? ' }' : '');
@@ -273,12 +304,12 @@ class rcmail_template extends rcube_html_page
 
   /**
    * Return executable javascript code for all registered commands
-   * @private
+   * @access private
    */
   function get_js_commands()
   {
     $out = '';
-    if (!$this->framed)
+    if (!$this->framed && !empty($this->js_env))
       $out .= ($this->ajax_call ? 'this' : JS_OBJECT_NAME) . '.set_env('.json_serialize($this->js_env).");\n";
     
     foreach ($this->js_commands as $i => $args)
@@ -295,11 +326,19 @@ class rcmail_template extends rcube_html_page
         join(',', $args));
     }
     
+    // add command to set page title
+    if ($this->ajax_call && !empty($this->pagetitle))
+      $out .= sprintf(
+        "this.set_pagetitle('%s');\n",
+        JQ((!empty($this->config['product_name']) ? $this->config['product_name'].' :: ' : '') . $this->pagetitle)
+      );
+    
     return $out;
   }
   
   /**
    * Make URLs starting with a slash point to skin directory
+   * @access private
    */
   function abs_url($str)
   {
@@ -311,18 +350,20 @@ class rcmail_template extends rcube_html_page
   /*****  Template parsing methods  *****/
   
   /**
-   * Replace all strings ($varname) with the content
-   * of the according global variable.
+   * Replace all strings ($varname)
+   * with the content of the according global variable.
+   * @access private
    */
   function parse_with_globals($input)
   {
-    $GLOBALS['__comm_path'] = $GLOBALS['COMM_PATH'];
+    $GLOBALS['__comm_path'] = urlencode($GLOBALS['COMM_PATH']);
     return preg_replace('/\$(__[a-z0-9_\-]+)/e', '$GLOBALS["\\1"]', $input);
   }
   
   
   /**
    * Parse for conditional tags
+   * @access private
    */
   function parse_conditions($input)
   {
@@ -361,6 +402,7 @@ class rcmail_template extends rcube_html_page
    * Determines if a given condition is met
    *
    * @return True if condition is valid, False is not
+   * @access private
    */
   function check_condition($condition)
   {
@@ -379,6 +421,7 @@ class rcmail_template extends rcube_html_page
    *
    * @param string Input string to parse
    * @return Altered input string
+   * @access private
    */
   function parse_xml($input)
   {
@@ -392,9 +435,10 @@ class rcmail_template extends rcube_html_page
    * @param string Tag command: object,button,label, etc.
    * @param string Attribute string
    * @return Tag/Object content string
+   * @access private
    */
   function xml_command($command, $str_attrib, $add_attrib=array())
-    {
+  {
     $command = strtolower($command);
     $attrib = parse_attrib_string($str_attrib) + $add_attrib;
 
@@ -420,10 +464,15 @@ class rcmail_template extends rcube_html_page
       // include a file 
       case 'include':
         $path = realpath($this->config['skin_path'].$attrib['file']);
-        if ($fp = @fopen($path, 'r'))
+        if (filesize($path))
         {
-          $incl = fread($fp, filesize($path));
-          fclose($fp);        
+          if ($this->config['skin_include_php'])
+            $incl = $this->include_php($path);
+          else if ($fp = @fopen($path, 'r'))
+          {
+            $incl = fread($fp, filesize($path));
+            fclose($fp);
+          }
           return $this->parse_xml($incl);
         }
         break;
@@ -437,16 +486,16 @@ class rcmail_template extends rcube_html_page
           return call_user_func($this->object_handlers[$object], $attrib);
 
         else if ($object=='productname')
-          {
+        {
           $name = !empty($this->config['product_name']) ? $this->config['product_name'] : 'RoundCube Webmail';
           return Q($name);
-          }
+        }
         else if ($object=='version')
-          {
+        {
           return (string)RCMAIL_VERSION;
-          }
+        }
         else if ($object=='pagetitle')
-          {
+        {
           $task = $this->task;
           $title = !empty($this->config['product_name']) ? $this->config['product_name'].' :: ' : '';
 
@@ -458,13 +507,59 @@ class rcmail_template extends rcube_html_page
             $title .= ucfirst($task);
 
           return Q($title);
-          }
+        }
 
         break;
-      }
+      
+      // return variable
+      case 'var':
+        $var = explode(':', $attrib['name']);
+        $name = $var[1];
+        $value = '';
+        
+        switch ($var[0])
+        {
+          case 'env':
+            $value = $this->env[$name];
+            break;
+          case 'config':
+            $value = $this->config[$name];
+            if (is_array($value) && $value[$_SESSION['imap_host']])
+              $value = $value[$_SESSION['imap_host']];
+            break;
+          case 'request':
+            $value = get_input_value($name, RCUBE_INPUT_GPC);
+            break;
+          case 'session':
+            $value = $_SESSION[$name];
+            break;
+        }
+        
+        if (is_array($value))
+          $value = join(", ", $value);
+        
+        return Q($value);
+    }
 
     return '';
-    }
+  }
+
+
+  /**
+   * Include a specific file and return it's contents
+   *
+   * @param string File path
+   * @return string Contents of the processed file
+   */
+  function include_php($file)
+  {
+    ob_start();
+    @include($file);
+    $out = ob_get_contents();
+    ob_end_clean();
+    
+    return $out;
+  }
 
 
   /**
@@ -472,9 +567,10 @@ class rcmail_template extends rcube_html_page
    *
    * @param array Button attributes
    * @return HTML button
+   * @access private
    */
   function button($attrib)
-    {
+  {
     global $CONFIG, $OUTPUT, $BROWSER, $MAIN_TASKS;
     static $sa_buttons = array();
     static $s_button_count = 100;
@@ -613,7 +709,7 @@ class rcmail_template extends rcube_html_page
         $attrib['value'] = $attrib['label'];
 
       $attrib_str = create_attrib_string($attrib, array('type', 'value', 'onclick', 'id', 'class', 'style'));
-      $out = sprintf('<input%s disabled />', $attrib_str);
+      $out = sprintf('<input%s disabled="disabled" />', $attrib_str);
     }
 
     // generate html code for button
@@ -626,6 +722,238 @@ class rcmail_template extends rcube_html_page
     return $out;
   }
 
-}
+}  // end class rcmail_template
+
+
+
+// ************** common functions delivering gui objects **************
+
+
+/**
+ * Builder for GUI object 'message'
+ *
+ * @param array Named tag parameters
+ * @return string HTML code for the gui object
+ */
+function rcmail_message_container($attrib)
+  {
+  global $OUTPUT;
+
+  if (!$attrib['id'])
+    $attrib['id'] = 'rcmMessageContainer';
+
+  // allow the following attributes to be added to the <table> tag
+  $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
+  $out = '<div' . $attrib_str . "></div>";
+  
+  $OUTPUT->add_gui_object('message', $attrib['id']);
+  
+  return $out;
+  }
+
+
+/**
+ * GUI object 'username'
+ * Showing IMAP username of the current session
+ *
+ * @param array Named tag parameters (currently not used)
+ * @return string HTML code for the gui object
+ */
+function rcmail_current_username($attrib)
+  {
+  global $DB;
+  static $s_username;
+
+  // alread fetched  
+  if (!empty($s_username))
+    return $s_username;
+
+  // get e-mail address form default identity
+  $sql_result = $DB->query(
+    "SELECT email AS mailto
+     FROM ".get_table_name('identities')."
+     WHERE  user_id=?
+     AND    standard=1
+     AND    del<>1",
+    $_SESSION['user_id']);
+                                   
+  if ($DB->num_rows($sql_result))
+    {
+    $sql_arr = $DB->fetch_assoc($sql_result);
+    $s_username = $sql_arr['mailto'];
+    }
+  else if (strstr($_SESSION['username'], '@'))
+    $s_username = $_SESSION['username'];
+  else
+    $s_username = $_SESSION['username'].'@'.$_SESSION['imap_host'];
+
+  return $s_username;
+  }
+
+
+/**
+ * GUI object 'loginform'
+ * Returns code for the webmail login form
+ *
+ * @param array Named parameters
+ * @return string HTML code for the gui object
+ */
+function rcmail_login_form($attrib)
+  {
+  global $CONFIG, $OUTPUT, $SESS_HIDDEN_FIELD;
+  
+  $labels = array();
+  $labels['user'] = rcube_label('username');
+  $labels['pass'] = rcube_label('password');
+  $labels['host'] = rcube_label('server');
+  
+  $input_user = new textfield(array('name' => '_user', 'id' => 'rcmloginuser', 'size' => 30) + $attrib);
+  $input_pass = new passwordfield(array('name' => '_pass', 'id' => 'rcmloginpwd', 'size' => 30) + $attrib);
+  $input_action = new hiddenfield(array('name' => '_action', 'value' => 'login'));
+    
+  $fields = array();
+  $fields['user'] = $input_user->show(get_input_value('_user', RCUBE_INPUT_POST));
+  $fields['pass'] = $input_pass->show();
+  $fields['action'] = $input_action->show();
+  
+  if (is_array($CONFIG['default_host']))
+    {
+    $select_host = new select(array('name' => '_host', 'id' => 'rcmloginhost'));
+    
+    foreach ($CONFIG['default_host'] as $key => $value)
+    {
+      if (!is_array($value))
+        $select_host->add($value, (is_numeric($key) ? $value : $key));
+      else
+        {
+        unset($select_host);
+        break;
+        }
+    }
+      
+    $fields['host'] = isset($select_host) ? $select_host->show($_POST['_host']) : null;
+    }
+  else if (!strlen($CONFIG['default_host']))
+    {
+    $input_host = new textfield(array('name' => '_host', 'id' => 'rcmloginhost', 'size' => 30));
+    $fields['host'] = $input_host->show($_POST['_host']);
+    }
+
+  $form_name = strlen($attrib['form']) ? $attrib['form'] : 'form';
+  $form_start = !strlen($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
+  $form_end = !strlen($attrib['form']) ? '</form>' : '';
+  
+  if ($fields['host'])
+    $form_host = <<<EOF
+    
+</tr><tr>
+
+<td class="title"><label for="rcmloginhost">$labels[host]</label></td>
+<td>$fields[host]</td>
+
+EOF;
+
+  $OUTPUT->add_gui_object('loginform', $form_name);
+  
+  $out = <<<EOF
+$form_start
+$SESS_HIDDEN_FIELD
+$fields[action]
+<table><tr>
+
+<td class="title"><label for="rcmloginuser">$labels[user]</label></td>
+<td>$fields[user]</td>
+
+</tr><tr>
+
+<td class="title"><label for="rcmloginpwd">$labels[pass]</label></td>
+<td>$fields[pass]</td>
+$form_host
+</tr></table>
+$form_end
+EOF;
+
+  return $out;
+  }
+
+
+/**
+ * GUI object 'charsetselector'
+ *
+ * @param array Named parameters for the select tag
+ * @return string HTML code for the gui object
+ */
+function rcmail_charset_selector($attrib)
+  {
+  global $OUTPUT;
+  
+  // pass the following attributes to the form class
+  $field_attrib = array('name' => '_charset');
+  foreach ($attrib as $attr => $value)
+    if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
+      $field_attrib[$attr] = $value;
+      
+  $charsets = array(
+    'US-ASCII'     => 'ASCII (English)',
+    'EUC-JP'       => 'EUC-JP (Japanese)',
+    'EUC-KR'       => 'EUC-KR (Korean)',
+    'BIG5'         => 'BIG5 (Chinese)',
+    'GB2312'       => 'GB2312 (Chinese)',
+    'ISO-2022-JP'  => 'ISO-2022-JP (Japanese)',
+    'ISO-8859-1'   => 'ISO-8859-1 (Latin-1)',
+    'ISO-8859-2'   => 'ISO-8895-2 (Central European)',
+    'ISO-8859-7'   => 'ISO-8859-7 (Greek)',
+    'ISO-8859-9'   => 'ISO-8859-9 (Turkish)',
+    'Windows-1251' => 'Windows-1251 (Cyrillic)',
+    'Windows-1252' => 'Windows-1252 (Western)',
+    'Windows-1255' => 'Windows-1255 (Hebrew)',
+    'Windows-1256' => 'Windows-1256 (Arabic)',
+    'Windows-1257' => 'Windows-1257 (Baltic)',
+    'UTF-8'        => 'UTF-8'
+    );
+
+  $select = new select($field_attrib);
+  $select->add(array_values($charsets), array_keys($charsets));
+  
+  $set = $_POST['_charset'] ? $_POST['_charset'] : $OUTPUT->get_charset();
+  return $select->show($set);
+  }
+
+
+/**
+ * GUI object 'searchform'
+ * Returns code for search function
+ *
+ * @param array Named parameters
+ * @return string HTML code for the gui object
+ */
+function rcmail_search_form($attrib)
+  {
+  global $OUTPUT;
+
+  // add some labels to client
+  rcube_add_label('searching');
+
+  $attrib['name'] = '_q';
+
+  if (empty($attrib['id']))
+    $attrib['id'] = 'rcmqsearchbox';
+
+  $input_q = new textfield($attrib);
+  $out = $input_q->show();
+
+  $OUTPUT->add_gui_object('qsearchbox', $attrib['id']);
+
+  // add form tag around text field
+  if (empty($attrib['form']))
+    $out = sprintf(
+      '<form name="rcmqsearchform" action="./" '.
+      'onsubmit="%s.command(\'search\');return false" style="display:inline;">%s</form>',
+      JS_OBJECT_NAME,
+      $out);
+
+  return $out;
+  } 
+
 
-?>
\ No newline at end of file
+?>