source: subversion/trunk/roundcubemail/program/include/rcube_template.php @ 1330

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