]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcmail_template.inc
Imported Upstream version 0.1~rc2
[roundcube.git] / program / include / rcmail_template.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcmail_template.inc                                   |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2007, RoundCube Dev. - Switzerland                      |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Class to handle HTML page output using a skin template.             |
13  |   Extends rcube_html_page class from rcube_html.inc                   |
14  |                                                                       |
15  +-----------------------------------------------------------------------+
16  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17  +-----------------------------------------------------------------------+
18
19  $Id:  $
20
21 */
22
23
24 /**
25  * Classes and functions for HTML output
26  *
27  * @package View
28  */
29
30 require_once('include/rcube_html.inc');
31
32
33 /**
34  * Class to create HTML page output using a skin template
35  */
36 class rcmail_template extends rcube_html_page
37 {
38   var $config;
39   var $task = '';
40   var $framed = false;
41   var $ajax_call = false;
42   var $pagetitle = '';
43   var $env = array();
44   var $js_env = array();
45   var $js_commands = array();
46   var $object_handlers = array();
47
48
49   /**
50    * Constructor
51    *
52    * @param array Configuration array
53    * @param string Current task
54    */
55   function __construct(&$config, $task)
56   {
57     $this->task = $task;
58     $this->config = $config;
59     $this->ajax_call = !empty($_GET['_remote']) || !empty($_POST['_remote']);
60     
61     // add common javascripts
62     if (!$this->ajax_call)
63     {
64       $javascript = "var ".JS_OBJECT_NAME." = new rcube_webmail();";
65
66       // don't wait for page onload. Call init at the bottom of the page (delayed)
67       $javascript_foot = "if (window.call_init)\n call_init('".JS_OBJECT_NAME."');";
68
69       $this->add_script($javascript, 'head_top');
70       $this->add_script($javascript_foot, 'foot');
71       $this->scripts_path = 'program/js/';
72       $this->include_script('common.js');
73       $this->include_script('app.js');
74     }
75   }
76
77   /**
78    * PHP 4 compatibility
79    * @see rcmail_template::__construct()
80    */
81   function rcmail_template(&$config, $task)
82   {
83     $this->__construct($config, $task);
84   }
85   
86   
87   /**
88    * Set environment variable
89    *
90    * @param string Property name
91    * @param mixed Property value
92    * @param boolean True if this property should be added to client environment
93    */
94   function set_env($name, $value, $addtojs=true)
95   {
96     $this->env[$name] = $value;
97     if ($addtojs || isset($this->js_env[$name]))
98       $this->js_env[$name] = $value;
99   }
100
101
102   /**
103    * Set page title variable
104    */
105   function set_pagetitle($title)
106   {
107     $this->pagetitle = $title;
108   }
109
110
111   /**
112    * Register a template object handler
113    *
114    * @param string Object name
115    * @param string Function name to call
116    */
117   function add_handler($obj, $func)
118   {
119     $this->object_handlers[$obj] = $func;
120   }
121
122   /**
123    * Register a list of template object handlers
124    *
125    * @param array Hash array with object=>handler pairs
126    */
127   function add_handlers($arr)
128   {
129     $this->object_handlers = array_merge($this->object_handlers, $arr);
130   }
131
132   /**
133    * Register a GUI object to the client script
134    *
135    * @param string Object name
136    * @param string Object ID
137    */
138   function add_gui_object($obj, $id)
139   {
140     $this->add_script(JS_OBJECT_NAME.".gui_object('$obj', '$id');");
141   }
142
143
144   /**
145    * Call a client method
146    *
147    * @param string Method to call
148    * @param ... Additional arguments
149    */
150   function command()
151   {
152     $this->js_commands[] = func_get_args();
153   }
154
155
156   /**
157    * Invoke display_message command
158    *
159    * @param string Message to display
160    * @param string Message type [notice|confirm|error]
161    * @param array Key-value pairs to be replaced in localized text
162    */
163   function show_message($message, $type='notice', $vars=NULL)
164   {
165     $this->command(
166       'display_message',
167       rcube_label(array('name' => $message, 'vars' => $vars)),
168       $type);
169   }
170
171
172   /**
173    * Delete all stored env variables and commands
174    */
175   function reset()
176   {
177     $this->env = array();
178     $this->js_env = array();
179     $this->js_commands = array();
180     $this->object_handlers = array();    
181     parent::reset();
182   }
183
184   /**
185    * Send the request output to the client.
186    * This will either parse a skin tempalte or send an AJAX response
187    *
188    * @param string  Template name
189    * @param boolean True if script should terminate (default)
190    */
191   function send($templ=null, $exit=true)
192   {
193     if ($this->ajax_call)
194       $this->remote_response('', !$exit);
195     else if ($templ != 'iframe')
196       $this->parse($templ, false);
197     else
198     {
199       $this->framed = $templ == 'iframe' ? true : $this->framed;
200       $this->write();
201     }
202     
203     if ($exit)
204       exit;
205   }
206
207
208   /**
209    * Send an AJAX response with executable JS code
210    * 
211    * @param string  Additional JS code
212    * @param boolean True if output buffer should be flushed
213    */
214   function remote_response($add='', $flush=false)
215   {
216     static $s_header_sent = FALSE;
217
218     if (!$s_header_sent)
219     {
220       $s_header_sent = TRUE;
221       send_nocacheing_headers();
222       header('Content-Type: application/x-javascript; charset='.RCMAIL_CHARSET);
223       print '/** ajax response ['.date('d/M/Y h:i:s O')."] **/\n";
224     }
225     
226     // unset default env vars
227     unset($this->js_env['task'], $this->js_env['action'], $this->js_env['comm_path']);
228
229     // send response code
230     print rcube_charset_convert($this->get_js_commands() . $add, RCMAIL_CHARSET, $this->get_charset());
231
232     if ($flush)  // flush the output buffer
233       flush();
234   }
235   
236   
237   /**
238    * Process template and write to stdOut
239    *
240    * @param string HTML template
241    * @see rcube_html_page::write()
242    */
243   function write($template='')
244   {
245     // unlock interface after iframe load
246     if ($this->framed)
247       array_unshift($this->js_commands, array('set_busy', false));
248     
249     // write all env variables to client
250     $js = $this->framed ? "if(window.parent) {\n" : '';
251     $js .= $this->get_js_commands() . ($this->framed ? ' }' : '');
252     $this->add_script($js, 'head_top');
253
254     // call super method
255     parent::write($template, $this->config['skin_path']);
256   }
257
258
259   /**
260    * Parse a specific skin template and deliver to stdout
261    *
262    * @param string  Template name
263    * @param boolean Exit script
264    */  
265   function parse($name='main', $exit=true)
266   {
267     $skin_path = $this->config['skin_path'];
268
269     // read template file
270     $templ = '';
271     $path = "$skin_path/templates/$name.html";
272
273     if($fp = @fopen($path, 'r'))
274     {
275       $templ = fread($fp, filesize($path));
276       fclose($fp);
277     }
278     else
279     {
280       raise_error(array(
281         'code' => 501,
282         'type' => 'php',
283         'line' => __LINE__,
284         'file' => __FILE__,
285         'message' => "Error loading template for '$name'"), TRUE, TRUE);
286       return FALSE;
287     }
288
289     // parse for specialtags
290     $output = $this->parse_xml($this->parse_conditions($templ));
291
292     // add debug console
293     if ($this->config['debug_level'] & 8)
294       $this->add_footer('<div style="position:absolute;top:5px;left:5px;width:400px;padding:0.2em;background:white;opacity:0.8;z-index:9000">
295         <a href="#toggle" onclick="con=document.getElementById(\'dbgconsole\');con.style.display=(con.style.display==\'none\'?\'block\':\'none\');return false">console</a>
296         <form action="/" name="debugform"><textarea name="console" id="dbgconsole" rows="20" cols="40" wrap="off" style="display:none;width:400px;border:none;font-size:x-small"></textarea></form></div>');
297
298     $this->write(trim($this->parse_with_globals($output)), $skin_path);
299
300     if ($exit)
301       exit;
302   }
303
304
305   /**
306    * Return executable javascript code for all registered commands
307    * @access private
308    */
309   function get_js_commands()
310   {
311     $out = '';
312     if (!$this->framed && !empty($this->js_env))
313       $out .= ($this->ajax_call ? 'this' : JS_OBJECT_NAME) . '.set_env('.json_serialize($this->js_env).");\n";
314     
315     foreach ($this->js_commands as $i => $args)
316     {
317       $method = array_shift($args);
318       foreach ($args as $i => $arg)
319         $args[$i] = json_serialize($arg);
320
321       $parent = $this->framed || preg_match('/^parent\./', $method);
322       $out .= sprintf(
323         "%s.%s(%s);\n",
324         $this->ajax_call ? 'this' : ($parent ? 'parent.' : '') . JS_OBJECT_NAME,
325         preg_replace('/^parent\./', '', $method),
326         join(',', $args));
327     }
328     
329     // add command to set page title
330     if ($this->ajax_call && !empty($this->pagetitle))
331       $out .= sprintf(
332         "this.set_pagetitle('%s');\n",
333         JQ((!empty($this->config['product_name']) ? $this->config['product_name'].' :: ' : '') . $this->pagetitle)
334       );
335     
336     return $out;
337   }
338   
339   /**
340    * Make URLs starting with a slash point to skin directory
341    * @access private
342    */
343   function abs_url($str)
344   {
345     return preg_replace('/^\//', $this->config['skin_path'].'/', $str);
346   }
347
348
349
350   /*****  Template parsing methods  *****/
351   
352   /**
353    * Replace all strings ($varname)
354    * with the content of the according global variable.
355    * @access private
356    */
357   function parse_with_globals($input)
358   {
359     $GLOBALS['__comm_path'] = urlencode($GLOBALS['COMM_PATH']);
360     return preg_replace('/\$(__[a-z0-9_\-]+)/e', '$GLOBALS["\\1"]', $input);
361   }
362   
363   
364   /**
365    * Parse for conditional tags
366    * @access private
367    */
368   function parse_conditions($input)
369   {
370     if (($matches = preg_split('/<roundcube:(if|elseif|else|endif)\s+([^>]+)>/is', $input, 2, PREG_SPLIT_DELIM_CAPTURE)) && count($matches)==4)
371     {
372       if (preg_match('/^(else|endif)$/i', $matches[1]))
373         return $matches[0] . $this->parse_conditions($matches[3]);
374       else
375       {
376         $attrib = parse_attrib_string($matches[2]);
377         if (isset($attrib['condition']))
378         {
379           $condmet = $this->check_condition($attrib['condition']);
380           $submatches = preg_split('/<roundcube:(elseif|else|endif)\s+([^>]+)>/is', $matches[3], 2, PREG_SPLIT_DELIM_CAPTURE);
381
382           if ($condmet)
383             $result = $submatches[0] . ($submatches[1] != 'endif' ? preg_replace('/.*<roundcube:endif\s+[^>]+>/Uis', '', $submatches[3], 1) : $submatches[3]);
384           else
385             $result = "<roundcube:$submatches[1] $submatches[2]>" . $submatches[3];
386
387           return $matches[0] . $this->parse_conditions($result);
388         }
389         else
390         {
391           raise_error(array('code' => 500, 'type' => 'php', 'line' => __LINE__, 'file' => __FILE__,
392                             'message' => "Unable to parse conditional tag " . $matches[2]), TRUE, FALSE);
393         }
394       }
395     }
396
397     return $input;
398   }
399
400
401   /**
402    * Determines if a given condition is met
403    *
404    * @return True if condition is valid, False is not
405    * @access private
406    */
407   function check_condition($condition)
408   {
409     $condition = preg_replace(
410         array('/session:([a-z0-9_]+)/i', '/config:([a-z0-9_]+)/i', '/env:([a-z0-9_]+)/i', '/request:([a-z0-9_]+)/ie'),
411         array("\$_SESSION['\\1']", "\$this->config['\\1']", "\$this->env['\\1']", "get_input_value('\\1', RCUBE_INPUT_GPC)"),
412         $condition);
413
414     return @eval("return (".$condition.");");
415   }
416
417
418   /**
419    * Search for special tags in input and replace them
420    * with the appropriate content
421    *
422    * @param string Input string to parse
423    * @return Altered input string
424    * @access private
425    */
426   function parse_xml($input)
427   {
428     return preg_replace('/<roundcube:([-_a-z]+)\s+([^>]+)>/Uie', "\$this->xml_command('\\1', '\\2')", $input);
429   }
430
431
432   /**
433    * Convert a xml command tag into real content
434    *
435    * @param string Tag command: object,button,label, etc.
436    * @param string Attribute string
437    * @return Tag/Object content string
438    * @access private
439    */
440   function xml_command($command, $str_attrib, $add_attrib=array())
441   {
442     $command = strtolower($command);
443     $attrib = parse_attrib_string($str_attrib) + $add_attrib;
444
445     // empty output if required condition is not met
446     if (!empty($attrib['condition']) && !$this->check_condition($attrib['condition']))
447       return '';
448
449     // execute command
450     switch ($command)
451     {
452       // return a button
453       case 'button':
454         if ($attrib['command'])
455           return $this->button($attrib);
456         break;
457
458       // show a label
459       case 'label':
460         if ($attrib['name'] || $attrib['command'])
461           return Q(rcube_label($attrib + array('vars' => array('product' => $this->config['product_name']))));
462         break;
463
464       // include a file 
465       case 'include':
466         $path = realpath($this->config['skin_path'].$attrib['file']);
467         if (filesize($path))
468         {
469           if ($this->config['skin_include_php'])
470             $incl = $this->include_php($path);
471           else if ($fp = @fopen($path, 'r'))
472           {
473             $incl = fread($fp, filesize($path));
474             fclose($fp);
475           }
476           return $this->parse_xml($incl);
477         }
478         break;
479
480       // return code for a specific application object
481       case 'object':
482         $object = strtolower($attrib['name']);
483
484         // execute object handler function
485         if ($this->object_handlers[$object] && function_exists($this->object_handlers[$object]))
486           return call_user_func($this->object_handlers[$object], $attrib);
487
488         else if ($object=='productname')
489         {
490           $name = !empty($this->config['product_name']) ? $this->config['product_name'] : 'RoundCube Webmail';
491           return Q($name);
492         }
493         else if ($object=='version')
494         {
495           return (string)RCMAIL_VERSION;
496         }
497         else if ($object=='pagetitle')
498         {
499           $task = $this->task;
500           $title = !empty($this->config['product_name']) ? $this->config['product_name'].' :: ' : '';
501
502           if (!empty($this->pagetitle))
503             $title .= $this->pagetitle;
504           else if ($task == 'login')
505             $title = rcube_label(array('name' => 'welcome', 'vars' => array('product' => $this->config['product_name'])));
506           else
507             $title .= ucfirst($task);
508
509           return Q($title);
510         }
511
512         break;
513       
514       // return variable
515       case 'var':
516         $var = explode(':', $attrib['name']);
517         $name = $var[1];
518         $value = '';
519         
520         switch ($var[0])
521         {
522           case 'env':
523             $value = $this->env[$name];
524             break;
525           case 'config':
526             $value = $this->config[$name];
527             if (is_array($value) && $value[$_SESSION['imap_host']])
528               $value = $value[$_SESSION['imap_host']];
529             break;
530           case 'request':
531             $value = get_input_value($name, RCUBE_INPUT_GPC);
532             break;
533           case 'session':
534             $value = $_SESSION[$name];
535             break;
536         }
537         
538         if (is_array($value))
539           $value = join(", ", $value);
540         
541         return Q($value);
542     }
543
544     return '';
545   }
546
547
548   /**
549    * Include a specific file and return it's contents
550    *
551    * @param string File path
552    * @return string Contents of the processed file
553    */
554   function include_php($file)
555   {
556     ob_start();
557     @include($file);
558     $out = ob_get_contents();
559     ob_end_clean();
560     
561     return $out;
562   }
563
564
565   /**
566    * Create and register a button
567    *
568    * @param array Button attributes
569    * @return HTML button
570    * @access private
571    */
572   function button($attrib)
573   {
574     global $CONFIG, $OUTPUT, $BROWSER, $MAIN_TASKS;
575     static $sa_buttons = array();
576     static $s_button_count = 100;
577
578     // these commands can be called directly via url
579     $a_static_commands = array('compose', 'list');
580
581     $skin_path = $this->config['skin_path'];
582
583     if (!($attrib['command'] || $attrib['name']))
584       return '';
585
586     // try to find out the button type
587     if ($attrib['type'])
588       $attrib['type'] = strtolower($attrib['type']);
589     else
590       $attrib['type'] = ($attrib['image'] || $attrib['imagepas'] || $attrib['imageact']) ? 'image' : 'link';
591
592     $command = $attrib['command'];
593
594     // take the button from the stack
595     if($attrib['name'] && $sa_buttons[$attrib['name']])
596       $attrib = $sa_buttons[$attrib['name']];
597
598     // add button to button stack
599     else if($attrib['image'] || $attrib['imageact'] || $attrib['imagepas'] || $attrib['class'])
600     {
601       if (!$attrib['name'])
602         $attrib['name'] = $command;
603
604       if (!$attrib['image'])
605         $attrib['image'] = $attrib['imagepas'] ? $attrib['imagepas'] : $attrib['imageact'];
606
607       $sa_buttons[$attrib['name']] = $attrib;
608     }
609
610     // get saved button for this command/name
611     else if ($command && $sa_buttons[$command])
612       $attrib = $sa_buttons[$command];
613
614     //else
615     //  return '';
616
617
618     // set border to 0 because of the link arround the button
619     if ($attrib['type']=='image' && !isset($attrib['border']))
620       $attrib['border'] = 0;
621
622     if (!$attrib['id'])
623       $attrib['id'] =  sprintf('rcmbtn%d', $s_button_count++);
624
625     // get localized text for labels and titles
626     if ($attrib['title'])
627       $attrib['title'] = Q(rcube_label($attrib['title']));
628     if ($attrib['label'])
629       $attrib['label'] = Q(rcube_label($attrib['label']));
630
631     if ($attrib['alt'])
632       $attrib['alt'] = Q(rcube_label($attrib['alt']));
633
634     // set title to alt attribute for IE browsers
635     if ($BROWSER['ie'] && $attrib['title'] && !$attrib['alt'])
636     {
637       $attrib['alt'] = $attrib['title'];
638       unset($attrib['title']);
639     }
640
641     // add empty alt attribute for XHTML compatibility
642     if (!isset($attrib['alt']))
643       $attrib['alt'] = '';
644
645
646     // register button in the system
647     if ($attrib['command'])
648     {
649       $this->add_script(sprintf(
650         "%s.register_button('%s', '%s', '%s', '%s', '%s', '%s');",
651         JS_OBJECT_NAME,
652         $command,
653         $attrib['id'],
654         $attrib['type'],
655         $attrib['imageact'] ? $skin_path.$attrib['imageact'] : $attrib['classact'],
656         $attrib['imagesel'] ? $skin_path.$attrib['imagesel'] : $attrib['classsel'],
657         $attrib['imageover'] ? $skin_path.$attrib['imageover'] : '')
658       );
659
660       // make valid href to specific buttons
661       if (in_array($attrib['command'], $MAIN_TASKS))
662         $attrib['href'] = Q(rcmail_url(null, null, $attrib['command']));
663       else if (in_array($attrib['command'], $a_static_commands))
664         $attrib['href'] = Q(rcmail_url($attrib['command']));
665     }
666
667     // overwrite attributes
668     if (!$attrib['href'])
669       $attrib['href'] = '#';
670
671     if ($command)
672       $attrib['onclick'] = sprintf("return %s.command('%s','%s',this)", JS_OBJECT_NAME, $command, $attrib['prop']);
673
674     if ($command && $attrib['imageover'])
675     {
676       $attrib['onmouseover'] = sprintf("return %s.button_over('%s','%s')", JS_OBJECT_NAME, $command, $attrib['id']);
677       $attrib['onmouseout'] = sprintf("return %s.button_out('%s','%s')", JS_OBJECT_NAME, $command, $attrib['id']);
678     }
679
680     if ($command && $attrib['imagesel'])
681     {
682       $attrib['onmousedown'] = sprintf("return %s.button_sel('%s','%s')", JS_OBJECT_NAME, $command, $attrib['id']);
683       $attrib['onmouseup'] = sprintf("return %s.button_out('%s','%s')", JS_OBJECT_NAME, $command, $attrib['id']);
684     }
685
686     $out = '';
687
688     // generate image tag
689     if ($attrib['type']=='image')
690     {
691       $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'width', 'height', 'border', 'hspace', 'vspace', 'align', 'alt'));
692       $img_tag = sprintf('<img src="%%s"%s />', $attrib_str);
693       $btn_content = sprintf($img_tag, $skin_path.$attrib['image']);
694       if ($attrib['label'])
695         $btn_content .= ' '.$attrib['label'];
696
697       $link_attrib = array('href', 'onclick', 'onmouseover', 'onmouseout', 'onmousedown', 'onmouseup', 'title');
698     }
699     else if ($attrib['type']=='link')
700     {
701       $btn_content = $attrib['label'] ? $attrib['label'] : $attrib['command'];
702       $link_attrib = array('href', 'onclick', 'title', 'id', 'class', 'style');
703     }
704     else if ($attrib['type']=='input')
705     {
706       $attrib['type'] = 'button';
707
708       if ($attrib['label'])
709         $attrib['value'] = $attrib['label'];
710
711       $attrib_str = create_attrib_string($attrib, array('type', 'value', 'onclick', 'id', 'class', 'style'));
712       $out = sprintf('<input%s disabled="disabled" />', $attrib_str);
713     }
714
715     // generate html code for button
716     if ($btn_content)
717     {
718       $attrib_str = create_attrib_string($attrib, $link_attrib);
719       $out = sprintf('<a%s>%s</a>', $attrib_str, $btn_content);
720     }
721
722     return $out;
723   }
724
725 }  // end class rcmail_template
726
727
728
729 // ************** common functions delivering gui objects **************
730
731
732 /**
733  * Builder for GUI object 'message'
734  *
735  * @param array Named tag parameters
736  * @return string HTML code for the gui object
737  */
738 function rcmail_message_container($attrib)
739   {
740   global $OUTPUT;
741
742   if (!$attrib['id'])
743     $attrib['id'] = 'rcmMessageContainer';
744
745   // allow the following attributes to be added to the <table> tag
746   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
747   $out = '<div' . $attrib_str . "></div>";
748   
749   $OUTPUT->add_gui_object('message', $attrib['id']);
750   
751   return $out;
752   }
753
754
755 /**
756  * GUI object 'username'
757  * Showing IMAP username of the current session
758  *
759  * @param array Named tag parameters (currently not used)
760  * @return string HTML code for the gui object
761  */
762 function rcmail_current_username($attrib)
763   {
764   global $DB;
765   static $s_username;
766
767   // alread fetched  
768   if (!empty($s_username))
769     return $s_username;
770
771   // get e-mail address form default identity
772   $sql_result = $DB->query(
773     "SELECT email AS mailto
774      FROM ".get_table_name('identities')."
775      WHERE  user_id=?
776      AND    standard=1
777      AND    del<>1",
778     $_SESSION['user_id']);
779                                    
780   if ($DB->num_rows($sql_result))
781     {
782     $sql_arr = $DB->fetch_assoc($sql_result);
783     $s_username = $sql_arr['mailto'];
784     }
785   else if (strstr($_SESSION['username'], '@'))
786     $s_username = $_SESSION['username'];
787   else
788     $s_username = $_SESSION['username'].'@'.$_SESSION['imap_host'];
789
790   return $s_username;
791   }
792
793
794 /**
795  * GUI object 'loginform'
796  * Returns code for the webmail login form
797  *
798  * @param array Named parameters
799  * @return string HTML code for the gui object
800  */
801 function rcmail_login_form($attrib)
802   {
803   global $CONFIG, $OUTPUT, $SESS_HIDDEN_FIELD;
804   
805   $labels = array();
806   $labels['user'] = rcube_label('username');
807   $labels['pass'] = rcube_label('password');
808   $labels['host'] = rcube_label('server');
809   
810   $input_user = new textfield(array('name' => '_user', 'id' => 'rcmloginuser', 'size' => 30) + $attrib);
811   $input_pass = new passwordfield(array('name' => '_pass', 'id' => 'rcmloginpwd', 'size' => 30) + $attrib);
812   $input_action = new hiddenfield(array('name' => '_action', 'value' => 'login'));
813     
814   $fields = array();
815   $fields['user'] = $input_user->show(get_input_value('_user', RCUBE_INPUT_POST));
816   $fields['pass'] = $input_pass->show();
817   $fields['action'] = $input_action->show();
818   
819   if (is_array($CONFIG['default_host']))
820     {
821     $select_host = new select(array('name' => '_host', 'id' => 'rcmloginhost'));
822     
823     foreach ($CONFIG['default_host'] as $key => $value)
824     {
825       if (!is_array($value))
826         $select_host->add($value, (is_numeric($key) ? $value : $key));
827       else
828         {
829         unset($select_host);
830         break;
831         }
832     }
833       
834     $fields['host'] = isset($select_host) ? $select_host->show($_POST['_host']) : null;
835     }
836   else if (!strlen($CONFIG['default_host']))
837     {
838     $input_host = new textfield(array('name' => '_host', 'id' => 'rcmloginhost', 'size' => 30));
839     $fields['host'] = $input_host->show($_POST['_host']);
840     }
841
842   $form_name = strlen($attrib['form']) ? $attrib['form'] : 'form';
843   $form_start = !strlen($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
844   $form_end = !strlen($attrib['form']) ? '</form>' : '';
845   
846   if ($fields['host'])
847     $form_host = <<<EOF
848     
849 </tr><tr>
850
851 <td class="title"><label for="rcmloginhost">$labels[host]</label></td>
852 <td>$fields[host]</td>
853
854 EOF;
855
856   $OUTPUT->add_gui_object('loginform', $form_name);
857   
858   $out = <<<EOF
859 $form_start
860 $SESS_HIDDEN_FIELD
861 $fields[action]
862 <table><tr>
863
864 <td class="title"><label for="rcmloginuser">$labels[user]</label></td>
865 <td>$fields[user]</td>
866
867 </tr><tr>
868
869 <td class="title"><label for="rcmloginpwd">$labels[pass]</label></td>
870 <td>$fields[pass]</td>
871 $form_host
872 </tr></table>
873 $form_end
874 EOF;
875
876   return $out;
877   }
878
879
880 /**
881  * GUI object 'charsetselector'
882  *
883  * @param array Named parameters for the select tag
884  * @return string HTML code for the gui object
885  */
886 function rcmail_charset_selector($attrib)
887   {
888   global $OUTPUT;
889   
890   // pass the following attributes to the form class
891   $field_attrib = array('name' => '_charset');
892   foreach ($attrib as $attr => $value)
893     if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
894       $field_attrib[$attr] = $value;
895       
896   $charsets = array(
897     'US-ASCII'     => 'ASCII (English)',
898     'EUC-JP'       => 'EUC-JP (Japanese)',
899     'EUC-KR'       => 'EUC-KR (Korean)',
900     'BIG5'         => 'BIG5 (Chinese)',
901     'GB2312'       => 'GB2312 (Chinese)',
902     'ISO-2022-JP'  => 'ISO-2022-JP (Japanese)',
903     'ISO-8859-1'   => 'ISO-8859-1 (Latin-1)',
904     'ISO-8859-2'   => 'ISO-8895-2 (Central European)',
905     'ISO-8859-7'   => 'ISO-8859-7 (Greek)',
906     'ISO-8859-9'   => 'ISO-8859-9 (Turkish)',
907     'Windows-1251' => 'Windows-1251 (Cyrillic)',
908     'Windows-1252' => 'Windows-1252 (Western)',
909     'Windows-1255' => 'Windows-1255 (Hebrew)',
910     'Windows-1256' => 'Windows-1256 (Arabic)',
911     'Windows-1257' => 'Windows-1257 (Baltic)',
912     'UTF-8'        => 'UTF-8'
913     );
914
915   $select = new select($field_attrib);
916   $select->add(array_values($charsets), array_keys($charsets));
917   
918   $set = $_POST['_charset'] ? $_POST['_charset'] : $OUTPUT->get_charset();
919   return $select->show($set);
920   }
921
922
923 /**
924  * GUI object 'searchform'
925  * Returns code for search function
926  *
927  * @param array Named parameters
928  * @return string HTML code for the gui object
929  */
930 function rcmail_search_form($attrib)
931   {
932   global $OUTPUT;
933
934   // add some labels to client
935   rcube_add_label('searching');
936
937   $attrib['name'] = '_q';
938
939   if (empty($attrib['id']))
940     $attrib['id'] = 'rcmqsearchbox';
941
942   $input_q = new textfield($attrib);
943   $out = $input_q->show();
944
945   $OUTPUT->add_gui_object('qsearchbox', $attrib['id']);
946
947   // add form tag around text field
948   if (empty($attrib['form']))
949     $out = sprintf(
950       '<form name="rcmqsearchform" action="./" '.
951       'onsubmit="%s.command(\'search\');return false" style="display:inline;">%s</form>',
952       JS_OBJECT_NAME,
953       $out);
954
955   return $out;
956   } 
957
958
959 ?>