Aleksander Machniak
2013-01-27 a188a9a305b25c89b18c71e29e348d9adcd29719
commit | author | age
47124c 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
60226a 5  | program/include/rcmail_output_html.php                                |
47124c 6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
0c2596 8  | Copyright (C) 2006-2012, The Roundcube Dev Team                       |
7fe381 9  |                                                                       |
T 10  | Licensed under the GNU General Public License version 3 or            |
11  | any later version with exceptions for skins & plugins.                |
12  | See the README file for a full license statement.                     |
47124c 13  |                                                                       |
T 14  | PURPOSE:                                                              |
15  |   Class to handle HTML page output using a skin template.             |
16  |                                                                       |
17  +-----------------------------------------------------------------------+
18  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19  +-----------------------------------------------------------------------+
041c93 20 */
47124c 21
T 22
23 /**
24  * Class to create HTML page output using a skin template
25  *
60226a 26  * @package    Core
9ab346 27  * @subpackage View
47124c 28  */
60226a 29 class rcmail_output_html extends rcmail_output
47124c 30 {
c8a21d 31     public $type = 'html';
0c2596 32
A 33     protected $message = null;
34     protected $js_env = array();
35     protected $js_labels = array();
36     protected $js_commands = array();
8fa22e 37     protected $skin_paths = array();
0c2596 38     protected $template_name;
A 39     protected $scripts_path = '';
40     protected $script_files = array();
41     protected $css_files = array();
42     protected $scripts = array();
43     protected $default_template = "<html>\n<head><title></title></head>\n<body></body>\n</html>";
44     protected $header = '';
45     protected $footer = '';
46     protected $body = '';
47     protected $base_path = '';
47124c 48
7fcb56 49     // deprecated names of templates used before 0.5
0c2596 50     protected $deprecated_templates = array(
A 51         'contact'      => 'showcontact',
52         'contactadd'   => 'addcontact',
53         'contactedit'  => 'editcontact',
7fcb56 54         'identityedit' => 'editidentity',
T 55         'messageprint' => 'printmessage',
56     );
57
47124c 58     /**
T 59      * Constructor
60      *
197601 61      * @todo   Replace $this->config with the real rcube_config object
47124c 62      */
0c2596 63     public function __construct($task = null, $framed = false)
47124c 64     {
T 65         parent::__construct();
66
197601 67         //$this->framed = $framed;
83a763 68         $this->set_env('task', $task);
0c2596 69         $this->set_env('x_frame_options', $this->config->get('x_frame_options', 'sameorigin'));
47124c 70
ae7027 71         // add cookie info
AM 72         $this->set_env('cookie_domain', ini_get('session.cookie_domain'));
73         $this->set_env('cookie_path', ini_get('session.cookie_path'));
74         $this->set_env('cookie_secure', ini_get('session.cookie_secure'));
75
423065 76         // load the correct skin (in case user-defined)
740875 77         $skin = $this->config->get('skin');
AM 78         $this->set_skin($skin);
79         $this->set_env('skin', $skin);
e58df3 80
271efe 81         if (!empty($_REQUEST['_extwin']))
TB 82           $this->set_env('extwin', 1);
83
47124c 84         // add common javascripts
60226a 85         $this->add_script('var '.self::JS_OBJECT_NAME.' = new rcube_webmail();', 'head_top');
47124c 86
T 87         // don't wait for page onload. Call init at the bottom of the page (delayed)
60226a 88         $this->add_script(self::JS_OBJECT_NAME.'.init();', 'docready');
47124c 89
T 90         $this->scripts_path = 'program/js/';
79275b 91         $this->include_script('jquery.min.js');
47124c 92         $this->include_script('common.js');
T 93         $this->include_script('app.js');
94
95         // register common UI objects
96         $this->add_handlers(array(
97             'loginform'       => array($this, 'login_form'),
8e211a 98             'preloader'       => array($this, 'preloader'),
47124c 99             'username'        => array($this, 'current_username'),
T 100             'message'         => array($this, 'message_container'),
101             'charsetselector' => array($this, 'charset_selector'),
1a0f60 102             'aboutcontent'    => array($this, 'about_content'),
47124c 103         ));
T 104     }
105
0c2596 106
47124c 107     /**
T 108      * Set environment variable
109      *
110      * @param string Property name
111      * @param mixed Property value
112      * @param boolean True if this property should be added to client environment
113      */
114     public function set_env($name, $value, $addtojs = true)
115     {
116         $this->env[$name] = $value;
117         if ($addtojs || isset($this->js_env[$name])) {
118             $this->js_env[$name] = $value;
119         }
120     }
121
f645ce 122
T 123     /**
124      * Getter for the current page title
125      *
126      * @return string The page title
127      */
0c2596 128     protected function get_pagetitle()
f645ce 129     {
T 130         if (!empty($this->pagetitle)) {
131             $title = $this->pagetitle;
132         }
133         else if ($this->env['task'] == 'login') {
0c2596 134             $title = $this->app->gettext(array(
A 135                 'name' => 'welcome',
136                 'vars' => array('product' => $this->config->get('product_name')
137             )));
f645ce 138         }
T 139         else {
140             $title = ucfirst($this->env['task']);
141         }
ad334a 142
f645ce 143         return $title;
T 144     }
0c2596 145
f645ce 146
e58df3 147     /**
A 148      * Set skin
149      */
150     public function set_skin($skin)
151     {
62c791 152         $valid = false;
ad334a 153
62c791 154         if (!empty($skin) && is_dir('skins/'.$skin) && is_readable('skins/'.$skin)) {
423065 155             $skin_path = 'skins/'.$skin;
62c791 156             $valid = true;
T 157         }
158         else {
0c2596 159             $skin_path = $this->config->get('skin_path');
A 160             if (!$skin_path) {
aff970 161                 $skin_path = 'skins/' . rcube_config::DEFAULT_SKIN;
0c2596 162             }
62c791 163             $valid = !$skin;
T 164         }
423065 165
0c2596 166         $this->config->set('skin_path', $skin_path);
39bb93 167         $this->base_path = $skin_path;
ad334a 168
8fa22e 169         // register skin path(s)
TB 170         $this->skin_paths = array();
171         $this->load_skin($skin_path);
172
62c791 173         return $valid;
8fa22e 174     }
TB 175
176     /**
177      * Helper method to recursively read skin meta files and register search paths
178      */
179     private function load_skin($skin_path)
180     {
181         $this->skin_paths[] = $skin_path;
182
183         // read meta file and check for dependecies
184         $meta = @json_decode(@file_get_contents($skin_path.'/meta.json'), true);
185         if ($meta['extends'] && is_dir('skins/' . $meta['extends'])) {
186             $this->load_skin('skins/' . $meta['extends']);
187         }
e58df3 188     }
A 189
fc7b5b 190
T 191     /**
e58df3 192      * Check if a specific template exists
A 193      *
194      * @param string Template name
195      * @return boolean True if template exists
196      */
197     public function template_exists($name)
198     {
8fa22e 199         $found = false;
TB 200         foreach ($this->skin_paths as $skin_path) {
201             $filename = $skin_path . '/templates/' . $name . '.html';
202             $found = (is_file($filename) && is_readable($filename)) || ($this->deprecated_templates[$name] && $this->template_exists($this->deprecated_templates[$name]));
203             if ($found)
204                 break;
205         }
206         return $found;
e58df3 207     }
47124c 208
T 209
210     /**
28de39 211      * Find the given file in the current skin path stack
TB 212      *
213      * @param string File name/path to resolve (starting with /)
214      * @param string Reference to the base path of the matching skin
215      * @param string Additional path to search in
216      * @return mixed Relative path to the requested file or False if not found
217      */
a188a9 218     public function get_skin_file($file, &$skin_path = null, $add_path = null)
28de39 219     {
TB 220         $skin_paths = $this->skin_paths;
221         if ($add_path)
222             array_unshift($skin_paths, $add_path);
223
224         foreach ($skin_paths as $skin_path) {
225             $path = realpath($skin_path . $file);
226             if (is_file($path)) {
227                 return $skin_path . $file;
228             }
229         }
230
231         return false;
232     }
233
234
235     /**
47124c 236      * Register a GUI object to the client script
T 237      *
238      * @param  string Object name
239      * @param  string Object ID
240      * @return void
241      */
242     public function add_gui_object($obj, $id)
243     {
60226a 244         $this->add_script(self::JS_OBJECT_NAME.".gui_object('$obj', '$id');");
47124c 245     }
0c2596 246
47124c 247
T 248     /**
249      * Call a client method
250      *
251      * @param string Method to call
252      * @param ... Additional arguments
253      */
254     public function command()
255     {
0e99d3 256         $cmd = func_get_args();
f66383 257         if (strpos($cmd[0], 'plugin.') !== false)
T 258           $this->js_commands[] = array('triggerEvent', $cmd[0], $cmd[1]);
259         else
0e99d3 260           $this->js_commands[] = $cmd;
47124c 261     }
T 262
0c2596 263
47124c 264     /**
T 265      * Add a localized label to the client environment
266      */
267     public function add_label()
268     {
cc97ea 269         $args = func_get_args();
T 270         if (count($args) == 1 && is_array($args[0]))
271           $args = $args[0];
ad334a 272
cc97ea 273         foreach ($args as $name) {
0c2596 274             $this->js_labels[$name] = $this->app->gettext($name);
47124c 275         }
T 276     }
0c2596 277
47124c 278
T 279     /**
280      * Invoke display_message command
281      *
7f5a84 282      * @param string  $message  Message to display
A 283      * @param string  $type     Message type [notice|confirm|error]
284      * @param array   $vars     Key-value pairs to be replaced in localized text
285      * @param boolean $override Override last set message
286      * @param int     $timeout  Message display time in seconds
47124c 287      * @uses self::command()
T 288      */
7f5a84 289     public function show_message($message, $type='notice', $vars=null, $override=true, $timeout=0)
47124c 290     {
69f18a 291         if ($override || !$this->message) {
0c2596 292             if ($this->app->text_exists($message)) {
8dd172 293                 if (!empty($vars))
A 294                     $vars = array_map('Q', $vars);
0c2596 295                 $msgtext = $this->app->gettext(array('name' => $message, 'vars' => $vars));
8dd172 296             }
A 297             else
298                 $msgtext = $message;
299
69f18a 300             $this->message = $message;
7f5a84 301             $this->command('display_message', $msgtext, $type, $timeout * 1000);
69f18a 302         }
47124c 303     }
T 304
0c2596 305
47124c 306     /**
T 307      * Delete all stored env variables and commands
308      */
c719f3 309     public function reset()
47124c 310     {
0c2596 311         parent::reset();
47124c 312         $this->js_env = array();
4dcd43 313         $this->js_labels = array();
47124c 314         $this->js_commands = array();
0c2596 315         $this->script_files = array();
A 316         $this->scripts      = array();
317         $this->header       = '';
318         $this->footer       = '';
319         $this->body         = '';
47124c 320     }
0c2596 321
47124c 322
T 323     /**
c719f3 324      * Redirect to a certain url
T 325      *
0c2596 326      * @param mixed $p     Either a string with the action or url parameters as key-value pairs
A 327      * @param int   $delay Delay in seconds
c719f3 328      */
0c2596 329     public function redirect($p = array(), $delay = 1)
c719f3 330     {
271efe 331         if ($this->env['extwin'])
TB 332             $p['extwin'] = 1;
c719f3 333         $location = $this->app->url($p);
T 334         header('Location: ' . $location);
335         exit;
336     }
0c2596 337
c719f3 338
T 339     /**
47124c 340      * Send the request output to the client.
T 341      * This will either parse a skin tempalte or send an AJAX response
342      *
343      * @param string  Template name
344      * @param boolean True if script should terminate (default)
345      */
346     public function send($templ = null, $exit = true)
347     {
348         if ($templ != 'iframe') {
a366a3 349             // prevent from endless loops
f78dab 350             if ($exit != 'recur' && $this->app->plugins->is_processing('render_page')) {
0c2596 351                 rcube::raise_error(array('code' => 505, 'type' => 'php',
030db5 352                   'file' => __FILE__, 'line' => __LINE__,
T 353                   'message' => 'Recursion alert: ignoring output->send()'), true, false);
a366a3 354                 return;
T 355             }
47124c 356             $this->parse($templ, false);
T 357         }
358         else {
359             $this->framed = $templ == 'iframe' ? true : $this->framed;
360             $this->write();
361         }
362
c6514e 363         // set output asap
T 364         ob_flush();
365         flush();
ad334a 366
c6514e 367         if ($exit) {
47124c 368             exit;
T 369         }
370     }
371
0c2596 372
47124c 373     /**
T 374      * Process template and write to stdOut
375      *
0c2596 376      * @param string $template HTML template content
47124c 377      */
T 378     public function write($template = '')
379     {
380         // unlock interface after iframe load
5bfa44 381         $unlock = preg_replace('/[^a-z0-9]/i', '', $_REQUEST['_unlock']);
47124c 382         if ($this->framed) {
ad334a 383             array_unshift($this->js_commands, array('set_busy', false, null, $unlock));
A 384         }
385         else if ($unlock) {
386             array_unshift($this->js_commands, array('hide_message', $unlock));
47124c 387         }
ef27a6 388
49dac9 389         if (!empty($this->script_files))
T 390           $this->set_env('request_token', $this->app->get_request_token());
ef27a6 391
47124c 392         // write all env variables to client
T 393         $js = $this->framed ? "if(window.parent) {\n" : '';
394         $js .= $this->get_js_commands() . ($this->framed ? ' }' : '');
395         $this->add_script($js, 'head_top');
ad334a 396
c170bf 397         // send clickjacking protection headers
T 398         $iframe = $this->framed || !empty($_REQUEST['_framed']);
399         if (!headers_sent() && ($xframe = $this->app->config->get('x_frame_options', 'sameorigin')))
400             header('X-Frame-Options: ' . ($iframe && $xframe == 'deny' ? 'sameorigin' : $xframe));
47124c 401
T 402         // call super method
0c2596 403         $this->_write($template, $this->config->get('skin_path'));
47124c 404     }
0c2596 405
47124c 406
T 407     /**
c739c7 408      * Parse a specific skin template and deliver to stdout (or return)
47124c 409      *
T 410      * @param  string  Template name
411      * @param  boolean Exit script
c739c7 412      * @param  boolean Don't write to stdout, return parsed content instead
A 413      *
47124c 414      * @link   http://php.net/manual/en/function.exit.php
T 415      */
c739c7 416     function parse($name = 'main', $exit = true, $write = true)
47124c 417     {
ca18a9 418         $plugin    = false;
A 419         $realname  = $name;
8fa22e 420         $this->template_name = $realname;
8e99ff 421
8fa22e 422         $temp = explode('.', $name, 2);
e7008c 423         if (count($temp) > 1) {
ca18a9 424             $plugin    = $temp[0];
A 425             $name      = $temp[1];
0c2596 426             $skin_dir  = $plugin . '/skins/' . $this->config->get('skin');
ca18a9 427
8fa22e 428             // apply skin search escalation list to plugin directory
TB 429             $plugin_skin_paths = array();
430             foreach ($this->skin_paths as $skin_path) {
28de39 431                 $plugin_skin_paths[] = $this->app->plugins->url . $plugin . '/' . $skin_path;
8fa22e 432             }
TB 433
434             // add fallback to default skin
435             if (is_dir($this->app->plugins->dir . $plugin . '/skins/default')) {
20d50d 436                 $skin_dir = $plugin . '/skins/default';
28de39 437                 $plugin_skin_paths[] = $this->app->plugins->url . $skin_dir;
8fa22e 438             }
TB 439
440             // add plugin skin paths to search list
441             $this->skin_paths = array_merge($plugin_skin_paths, $this->skin_paths);
442         }
443
444         // find skin template
445         $path = false;
446         foreach ($this->skin_paths as $skin_path) {
447             $path = "$skin_path/templates/$name.html";
448
449             // fallback to deprecated template names
450             if (!is_readable($path) && $this->deprecated_templates[$realname]) {
451                 $path = "$skin_path/templates/" . $this->deprecated_templates[$realname] . ".html";
bc66f7 452
TB 453                 if (is_readable($path)) {
454                     rcube::raise_error(array(
455                         'code' => 502, 'type' => 'php',
456                         'file' => __FILE__, 'line' => __LINE__,
457                         'message' => "Using deprecated template '" . $this->deprecated_templates[$realname]
458                             . "' in $skin_path/templates. Please rename to '$realname'"),
459                         true, false);
460                 }
8fa22e 461             }
TB 462
463             if (is_readable($path)) {
464                 $this->config->set('skin_path', $skin_path);
3806f1 465                 $this->base_path = preg_replace('!plugins/\w+/!', '', $skin_path);  // set base_path to core skin directory (not plugin's skin)
0938f6 466                 $skin_dir = preg_replace('!^plugins/!', '', $skin_path);
8fa22e 467                 break;
TB 468             }
469             else {
470                 $path = false;
20d50d 471             }
e7008c 472         }
ad334a 473
ff73e0 474         // read template file
8fa22e 475         if (!$path || ($templ = @file_get_contents($path)) === false) {
0c2596 476             rcube::raise_error(array(
47124c 477                 'code' => 501,
T 478                 'type' => 'php',
479                 'line' => __LINE__,
480                 'file' => __FILE__,
ca18a9 481                 'message' => 'Error loading template for '.$realname
397cf7 482                 ), true, $write);
47124c 483             return false;
T 484         }
ad334a 485
66f68e 486         // replace all path references to plugins/... with the configured plugins dir
T 487         // and /this/ to the current plugin skin directory
e7008c 488         if ($plugin) {
20d50d 489             $templ = preg_replace(array('/\bplugins\//', '/(["\']?)\/this\//'), array($this->app->plugins->url, '\\1'.$this->app->plugins->url.$skin_dir.'/'), $templ);
e7008c 490         }
47124c 491
T 492         // parse for specialtags
493         $output = $this->parse_conditions($templ);
494         $output = $this->parse_xml($output);
ad334a 495
742d61 496         // trigger generic hook where plugins can put additional content to the page
ca18a9 497         $hook = $this->app->plugins->exec_hook("render_page", array('template' => $realname, 'content' => $output));
47124c 498
e4a4ca 499         // save some memory
A 500         $output = $hook['content'];
501         unset($hook['content']);
502
5172ac 503         // make sure all <form> tags have a valid request token
T 504         $output = preg_replace_callback('/<form\s+([^>]+)>/Ui', array($this, 'alter_form_tag'), $output);
505         $this->footer = preg_replace_callback('/<form\s+([^>]+)>/Ui', array($this, 'alter_form_tag'), $this->footer);
506
c739c7 507         if ($write) {
A 508             // add debug console
0c2596 509             if ($realname != 'error' && ($this->config->get('debug_level') & 8)) {
909a3a 510                 $this->add_footer('<div id="console" style="position:absolute;top:5px;left:5px;width:405px;padding:2px;background:white;z-index:9000;display:none">
c739c7 511                     <a href="#toggle" onclick="con=$(\'#dbgconsole\');con[con.is(\':visible\')?\'hide\':\'show\']();return false">console</a>
f23073 512                     <textarea name="console" id="dbgconsole" rows="20" cols="40" style="display:none;width:400px;border:none;font-size:10px" spellcheck="false"></textarea></div>'
c739c7 513                 );
909a3a 514                 $this->add_script(
A 515                     "if (!window.console || !window.console.log) {\n".
516                     "  window.console = new rcube_console();\n".
517                     "  $('#console').show();\n".
518                     "}", 'foot');
c739c7 519             }
A 520             $this->write(trim($output));
521         }
522         else {
523             return $output;
47124c 524         }
ad334a 525
47124c 526         if ($exit) {
T 527             exit;
528         }
529     }
530
0c2596 531
47124c 532     /**
T 533      * Return executable javascript code for all registered commands
534      *
535      * @return string $out
536      */
0c2596 537     protected function get_js_commands()
47124c 538     {
T 539         $out = '';
540         if (!$this->framed && !empty($this->js_env)) {
60226a 541             $out .= self::JS_OBJECT_NAME . '.set_env('.self::json_serialize($this->js_env).");\n";
47124c 542         }
4dcd43 543         if (!empty($this->js_labels)) {
T 544             $this->command('add_label', $this->js_labels);
545         }
47124c 546         foreach ($this->js_commands as $i => $args) {
T 547             $method = array_shift($args);
548             foreach ($args as $i => $arg) {
0c2596 549                 $args[$i] = self::json_serialize($arg);
47124c 550             }
T 551             $parent = $this->framed || preg_match('/^parent\./', $method);
552             $out .= sprintf(
553                 "%s.%s(%s);\n",
60226a 554                 ($parent ? 'if(window.parent && parent.'.self::JS_OBJECT_NAME.') parent.' : '') . self::JS_OBJECT_NAME,
cc97ea 555                 preg_replace('/^parent\./', '', $method),
T 556                 implode(',', $args)
47124c 557             );
T 558         }
ad334a 559
47124c 560         return $out;
T 561     }
562
0c2596 563
47124c 564     /**
T 565      * Make URLs starting with a slash point to skin directory
566      *
567      * @param  string Input string
28de39 568      * @param  boolean True if URL should be resolved using the current skin path stack
47124c 569      * @return string
T 570      */
28de39 571     public function abs_url($str, $search_path = false)
47124c 572     {
28de39 573         if ($str[0] == '/') {
TB 574             if ($search_path && ($file_url = $this->get_skin_file($str, $skin_path)))
575                 return $file_url;
576
8fa22e 577             return $this->base_path . $str;
28de39 578         }
2aa2b3 579         else
A 580             return $str;
0c2596 581     }
A 582
583
584     /**
585      * Show error page and terminate script execution
586      *
587      * @param int    $code     Error code
588      * @param string $message  Error message
589      */
590     public function raise_error($code, $message)
591     {
592         global $__page_content, $ERROR_CODE, $ERROR_MESSAGE;
593
594         $ERROR_CODE    = $code;
595         $ERROR_MESSAGE = $message;
596
9be2f4 597         include RCUBE_INSTALL_PATH . 'program/steps/utils/error.inc';
0c2596 598         exit;
47124c 599     }
T 600
601
602     /*****  Template parsing methods  *****/
603
604     /**
605      * Replace all strings ($varname)
606      * with the content of the according global variable.
607      */
0c2596 608     protected function parse_with_globals($input)
47124c 609     {
0c2596 610         $GLOBALS['__version']   = html::quote(RCMAIL_VERSION);
A 611         $GLOBALS['__comm_path'] = html::quote($this->app->comm_path);
8fa22e 612         $GLOBALS['__skin_path'] = html::quote($this->base_path);
0c2596 613
5740c0 614         return preg_replace_callback('/\$(__[a-z0-9_\-]+)/',
0c2596 615             array($this, 'globals_callback'), $input);
5740c0 616     }
0c2596 617
5740c0 618
A 619     /**
620      * Callback funtion for preg_replace_callback() in parse_with_globals()
621      */
0c2596 622     protected function globals_callback($matches)
5740c0 623     {
A 624         return $GLOBALS[$matches[1]];
8fa22e 625     }
TB 626
627
628     /**
629      * Correct absolute paths in images and other tags
630      * add timestamp to .js and .css filename
631      */
632     protected function fix_paths($output)
633     {
634         return preg_replace_callback(
635             '!(src|href|background)=(["\']?)([a-z0-9/_.-]+)(["\'\s>])!i',
636             array($this, 'file_callback'), $output);
637     }
638
639
640     /**
641      * Callback function for preg_replace_callback in write()
642      *
643      * @return string Parsed string
644      */
645     protected function file_callback($matches)
646     {
647         $file = $matches[3];
0938f6 648         $file[0] = preg_replace('!^/this/!', '/', $file[0]);
8fa22e 649
TB 650         // correct absolute paths
651         if ($file[0] == '/') {
652             $file = $this->base_path . $file;
653         }
654
655         // add file modification timestamp
656         if (preg_match('/\.(js|css)$/', $file)) {
657             if ($fs = @filemtime($file)) {
658                 $file .= '?s=' . $fs;
659             }
660         }
661
662         return $matches[1] . '=' . $matches[2] . $file . $matches[4];
47124c 663     }
0c2596 664
47124c 665
T 666     /**
667      * Public wrapper to dipp into template parsing.
668      *
669      * @param  string $input
670      * @return string
0c2596 671      * @uses   rcube_output_html::parse_xml()
47124c 672      * @since  0.1-rc1
T 673      */
674     public function just_parse($input)
675     {
b01d84 676         $input = $this->parse_conditions($input);
AM 677         $input = $this->parse_xml($input);
678
679         return $input;
47124c 680     }
T 681
0c2596 682
47124c 683     /**
T 684      * Parse for conditional tags
685      *
686      * @param  string $input
687      * @return string
688      */
0c2596 689     protected function parse_conditions($input)
47124c 690     {
84581e 691         $matches = preg_split('/<roundcube:(if|elseif|else|endif)\s+([^>]+)>\n?/is', $input, 2, PREG_SPLIT_DELIM_CAPTURE);
47124c 692         if ($matches && count($matches) == 4) {
T 693             if (preg_match('/^(else|endif)$/i', $matches[1])) {
694                 return $matches[0] . $this->parse_conditions($matches[3]);
695             }
0c2596 696             $attrib = html::parse_attrib_string($matches[2]);
47124c 697             if (isset($attrib['condition'])) {
T 698                 $condmet = $this->check_condition($attrib['condition']);
84581e 699                 $submatches = preg_split('/<roundcube:(elseif|else|endif)\s+([^>]+)>\n?/is', $matches[3], 2, PREG_SPLIT_DELIM_CAPTURE);
47124c 700                 if ($condmet) {
T 701                     $result = $submatches[0];
84581e 702                     $result.= ($submatches[1] != 'endif' ? preg_replace('/.*<roundcube:endif\s+[^>]+>\n?/Uis', '', $submatches[3], 1) : $submatches[3]);
47124c 703                 }
T 704                 else {
705                     $result = "<roundcube:$submatches[1] $submatches[2]>" . $submatches[3];
706                 }
707                 return $matches[0] . $this->parse_conditions($result);
708             }
0c2596 709             rcube::raise_error(array(
47124c 710                 'code' => 500,
T 711                 'type' => 'php',
712                 'line' => __LINE__,
713                 'file' => __FILE__,
714                 'message' => "Unable to parse conditional tag " . $matches[2]
715             ), true, false);
716         }
717         return $input;
718     }
719
720
721     /**
722      * Determines if a given condition is met
723      *
724      * @todo   Get rid off eval() once I understand what this does.
725      * @todo   Extend this to allow real conditions, not just "set"
726      * @param  string Condition statement
8e1d4a 727      * @return boolean True if condition is met, False if not
47124c 728      */
0c2596 729     protected function check_condition($condition)
47124c 730     {
549933 731         return eval("return (".$this->parse_expression($condition).");");
T 732     }
ad334a 733
A 734
549933 735     /**
2eb794 736      * Inserts hidden field with CSRF-prevention-token into POST forms
549933 737      */
0c2596 738     protected function alter_form_tag($matches)
549933 739     {
0c2596 740         $out    = $matches[0];
A 741         $attrib = html::parse_attrib_string($matches[1]);
ad334a 742
549933 743         if (strtolower($attrib['method']) == 'post') {
T 744             $hidden = new html_hiddenfield(array('name' => '_token', 'value' => $this->app->get_request_token()));
745             $out .= "\n" . $hidden->show();
746         }
ad334a 747
549933 748         return $out;
8e1d4a 749     }
A 750
751
752     /**
753      * Parses expression and replaces variables
754      *
755      * @param  string Expression statement
030db5 756      * @return string Expression value
8e1d4a 757      */
0c2596 758     protected function parse_expression($expression)
8e1d4a 759     {
A 760         return preg_replace(
47124c 761             array(
T 762                 '/session:([a-z0-9_]+)/i',
f645ce 763                 '/config:([a-z0-9_]+)(:([a-z0-9_]+))?/i',
40353f 764                 '/env:([a-z0-9_]+)/i',
A 765                 '/request:([a-z0-9_]+)/i',
766                 '/cookie:([a-z0-9_]+)/i',
8e99ff 767                 '/browser:([a-z0-9_]+)/i',
A 768                 '/template:name/i',
47124c 769             ),
T 770             array(
771                 "\$_SESSION['\\1']",
f707fe 772                 "\$this->app->config->get('\\1',rcube_utils::get_boolean('\\3'))",
47124c 773                 "\$this->env['\\1']",
1aceb9 774                 "rcube_utils::get_input_value('\\1', rcube_utils::INPUT_GPC)",
a17fe6 775                 "\$_COOKIE['\\1']",
8e99ff 776                 "\$this->browser->{'\\1'}",
A 777                 $this->template_name,
47124c 778             ),
8e1d4a 779             $expression);
47124c 780     }
T 781
782
783     /**
784      * Search for special tags in input and replace them
785      * with the appropriate content
786      *
787      * @param  string Input string to parse
788      * @return string Altered input string
06655a 789      * @todo   Use DOM-parser to traverse template HTML
47124c 790      * @todo   Maybe a cache.
T 791      */
0c2596 792     protected function parse_xml($input)
47124c 793     {
6af593 794         return preg_replace_callback('/<roundcube:([-_a-z]+)\s+((?:[^>]|\\\\>)+)(?<!\\\\)>/Ui', array($this, 'xml_command'), $input);
6f488b 795     }
A 796
797
798     /**
cc97ea 799      * Callback function for parsing an xml command tag
T 800      * and turn it into real html content
47124c 801      *
cc97ea 802      * @param  array Matches array of preg_replace_callback
47124c 803      * @return string Tag/Object content
T 804      */
0c2596 805     protected function xml_command($matches)
47124c 806     {
cc97ea 807         $command = strtolower($matches[1]);
0c2596 808         $attrib  = html::parse_attrib_string($matches[2]);
47124c 809
T 810         // empty output if required condition is not met
811         if (!empty($attrib['condition']) && !$this->check_condition($attrib['condition'])) {
812             return '';
813         }
814
815         // execute command
816         switch ($command) {
817             // return a button
818             case 'button':
875a48 819                 if ($attrib['name'] || $attrib['command']) {
47124c 820                     return $this->button($attrib);
T 821                 }
822                 break;
823
b7d33e 824             // frame
AM 825             case 'frame':
826                 return $this->frame($attrib);
827                 break;
828
47124c 829             // show a label
T 830             case 'label':
8fa22e 831                 if ($attrib['expression'])
TB 832                     $attrib['name'] = eval("return " . $this->parse_expression($attrib['expression']) .";");
833
47124c 834                 if ($attrib['name'] || $attrib['command']) {
0d80fa 835                     // @FIXME: 'noshow' is useless, remove?
AM 836                     if ($attrib['noshow']) {
837                         return '';
838                     }
839
0c2596 840                     $vars = $attrib + array('product' => $this->config->get('product_name'));
c87806 841                     unset($vars['name'], $vars['command']);
0d80fa 842
AM 843                     $label   = $this->app->gettext($attrib + array('vars' => $vars));
f707fe 844                     $quoting = !empty($attrib['quoting']) ? strtolower($attrib['quoting']) : (rcube_utils::get_boolean((string)$attrib['html']) ? 'no' : '');
0d80fa 845
fa8f6e 846                     switch ($quoting) {
TB 847                         case 'no':
0d80fa 848                         case 'raw':
AM 849                             break;
fa8f6e 850                         case 'javascript':
0d80fa 851                         case 'js':
10da75 852                             $label = rcube::JQ($label);
0d80fa 853                             break;
AM 854                         default:
855                             $label = html::quote($label);
856                             break;
fa8f6e 857                     }
0d80fa 858
AM 859                     return $label;
47124c 860                 }
T 861                 break;
862
863             // include a file
864             case 'include':
8fa22e 865                 $old_base_path = $this->base_path;
19b0d4 866                 if ($path = $this->get_skin_file($attrib['file'], $skin_path, $attrib['skinpath'])) {
2a0d3f 867                     $this->base_path = preg_replace('!plugins/\w+/!', '', $skin_path);  // set base_path to core skin directory (not plugin's skin)
28de39 868                     $path = realpath($path);
8fa22e 869                 }
0c2596 870
ff73e0 871                 if (is_readable($path)) {
0c2596 872                     if ($this->config->get('skin_include_php')) {
47124c 873                         $incl = $this->include_php($path);
T 874                     }
ff73e0 875                     else {
cc97ea 876                       $incl = file_get_contents($path);
T 877                     }
b4f7c6 878                     $incl = $this->parse_conditions($incl);
8fa22e 879                     $incl = $this->parse_xml($incl);
TB 880                     $incl = $this->fix_paths($incl);
881                     $this->base_path = $old_base_path;
882                     return $incl;
47124c 883                 }
T 884                 break;
885
886             case 'plugin.include':
cc97ea 887                 $hook = $this->app->plugins->exec_hook("template_plugin_include", $attrib);
T 888                 return $hook['content'];
ad334a 889
cc97ea 890             // define a container block
T 891             case 'container':
892                 if ($attrib['name'] && $attrib['id']) {
893                     $this->command('gui_container', $attrib['name'], $attrib['id']);
894                     // let plugins insert some content here
895                     $hook = $this->app->plugins->exec_hook("template_container", $attrib);
896                     return $hook['content'];
47124c 897                 }
T 898                 break;
899
900             // return code for a specific application object
901             case 'object':
902                 $object = strtolower($attrib['name']);
cc97ea 903                 $content = '';
47124c 904
T 905                 // we are calling a class/method
906                 if (($handler = $this->object_handlers[$object]) && is_array($handler)) {
907                     if ((is_object($handler[0]) && method_exists($handler[0], $handler[1])) ||
908                     (is_string($handler[0]) && class_exists($handler[0])))
cc97ea 909                     $content = call_user_func($handler, $attrib);
47124c 910                 }
cc97ea 911                 // execute object handler function
47124c 912                 else if (function_exists($handler)) {
cc97ea 913                     $content = call_user_func($handler, $attrib);
47124c 914                 }
f23073 915                 else if ($object == 'doctype') {
T 916                     $content = html::doctype($attrib['value']);
917                 }
ae39c4 918                 else if ($object == 'logo') {
T 919                     $attrib += array('alt' => $this->xml_command(array('', 'object', 'name="productname"')));
0c2596 920                     if ($logo = $this->config->get('skin_logo'))
A 921                         $attrib['src'] = $logo;
ae39c4 922                     $content = html::img($attrib);
T 923                 }
cc97ea 924                 else if ($object == 'productname') {
0c2596 925                     $name = $this->config->get('product_name', 'Roundcube Webmail');
A 926                     $content = html::quote($name);
47124c 927                 }
cc97ea 928                 else if ($object == 'version') {
c8fb2b 929                     $ver = (string)RCMAIL_VERSION;
9be2f4 930                     if (is_file(RCUBE_INSTALL_PATH . '.svn/entries')) {
c8fb2b 931                         if (preg_match('/Revision:\s(\d+)/', @shell_exec('svn info'), $regs))
T 932                           $ver .= ' [SVN r'.$regs[1].']';
933                     }
9be2f4 934                     else if (is_file(RCUBE_INSTALL_PATH . '.git/index')) {
914c3e 935                         if (preg_match('/Date:\s+([^\n]+)/', @shell_exec('git log -1'), $regs)) {
AM 936                             if ($date = date('Ymd.Hi', strtotime($regs[1]))) {
937                                 $ver .= ' [GIT '.$date.']';
938                             }
939                         }
940                     }
0c2596 941                     $content = html::quote($ver);
47124c 942                 }
cc97ea 943                 else if ($object == 'steptitle') {
0c2596 944                   $content = html::quote($this->get_pagetitle());
f645ce 945                 }
cc97ea 946                 else if ($object == 'pagetitle') {
0c2596 947                     if ($this->config->get('devel_mode') && !empty($_SESSION['username']))
A 948                         $title = $_SESSION['username'].' :: ';
949                     else if ($prod_name = $this->config->get('product_name'))
950                         $title = $prod_name . ' :: ';
159763 951                     else
0c2596 952                         $title = '';
f645ce 953                     $title .= $this->get_pagetitle();
0c2596 954                     $content = html::quote($title);
47124c 955                 }
ad334a 956
cc97ea 957                 // exec plugin hooks for this template object
T 958                 $hook = $this->app->plugins->exec_hook("template_object_$object", $attrib + array('content' => $content));
959                 return $hook['content'];
8e1d4a 960
A 961             // return code for a specified eval expression
962             case 'exp':
cc97ea 963                 $value = $this->parse_expression($attrib['expression']);
0c2596 964                 return eval("return html::quote($value);");
ad334a 965
47124c 966             // return variable
T 967             case 'var':
968                 $var = explode(':', $attrib['name']);
969                 $name = $var[1];
970                 $value = '';
971
972                 switch ($var[0]) {
973                     case 'env':
974                         $value = $this->env[$name];
975                         break;
976                     case 'config':
0c2596 977                         $value = $this->config->get($name);
c321a9 978                         if (is_array($value) && $value[$_SESSION['storage_host']]) {
T 979                             $value = $value[$_SESSION['storage_host']];
47124c 980                         }
T 981                         break;
982                     case 'request':
1aceb9 983                         $value = rcube_utils::get_input_value($name, rcube_utils::INPUT_GPC);
47124c 984                         break;
T 985                     case 'session':
986                         $value = $_SESSION[$name];
987                         break;
d4273b 988                     case 'cookie':
A 989                         $value = htmlspecialchars($_COOKIE[$name]);
990                         break;
a17fe6 991                     case 'browser':
A 992                         $value = $this->browser->{$name};
993                         break;
47124c 994                 }
T 995
996                 if (is_array($value)) {
997                     $value = implode(', ', $value);
998                 }
999
0c2596 1000                 return html::quote($value);
47124c 1001                 break;
T 1002         }
1003         return '';
1004     }
0c2596 1005
47124c 1006
T 1007     /**
1008      * Include a specific file and return it's contents
1009      *
1010      * @param string File path
1011      * @return string Contents of the processed file
1012      */
0c2596 1013     protected function include_php($file)
47124c 1014     {
T 1015         ob_start();
1016         include $file;
1017         $out = ob_get_contents();
1018         ob_end_clean();
1019
1020         return $out;
1021     }
0c2596 1022
47124c 1023
T 1024     /**
1025      * Create and register a button
1026      *
1027      * @param  array Named button attributes
1028      * @return string HTML button
1029      * @todo   Remove all inline JS calls and use jQuery instead.
1030      * @todo   Remove all sprintf()'s - they are pretty, but also slow.
1031      */
ed132e 1032     public function button($attrib)
47124c 1033     {
T 1034         static $s_button_count = 100;
1035
1036         // these commands can be called directly via url
cc97ea 1037         $a_static_commands = array('compose', 'list', 'preferences', 'folders', 'identities');
47124c 1038
T 1039         if (!($attrib['command'] || $attrib['name'])) {
1040             return '';
1041         }
ae0c82 1042
47124c 1043         // try to find out the button type
T 1044         if ($attrib['type']) {
1045             $attrib['type'] = strtolower($attrib['type']);
1046         }
1047         else {
1048             $attrib['type'] = ($attrib['image'] || $attrib['imagepas'] || $attrib['imageact']) ? 'image' : 'link';
1049         }
e9b5a6 1050
47124c 1051         $command = $attrib['command'];
T 1052
e9b5a6 1053         if ($attrib['task'])
T 1054           $command = $attrib['task'] . '.' . $command;
ad334a 1055
af3cf8 1056         if (!$attrib['image']) {
T 1057             $attrib['image'] = $attrib['imagepas'] ? $attrib['imagepas'] : $attrib['imageact'];
1058         }
47124c 1059
T 1060         if (!$attrib['id']) {
1061             $attrib['id'] =  sprintf('rcmbtn%d', $s_button_count++);
1062         }
1063         // get localized text for labels and titles
1064         if ($attrib['title']) {
0c2596 1065             $attrib['title'] = html::quote($this->app->gettext($attrib['title'], $attrib['domain']));
47124c 1066         }
T 1067         if ($attrib['label']) {
0c2596 1068             $attrib['label'] = html::quote($this->app->gettext($attrib['label'], $attrib['domain']));
47124c 1069         }
T 1070         if ($attrib['alt']) {
0c2596 1071             $attrib['alt'] = html::quote($this->app->gettext($attrib['alt'], $attrib['domain']));
47124c 1072         }
9b2ccd 1073
47124c 1074         // set title to alt attribute for IE browsers
c9e9fe 1075         if ($this->browser->ie && !$attrib['title'] && $attrib['alt']) {
A 1076             $attrib['title'] = $attrib['alt'];
47124c 1077         }
T 1078
1079         // add empty alt attribute for XHTML compatibility
1080         if (!isset($attrib['alt'])) {
1081             $attrib['alt'] = '';
1082         }
1083
1084         // register button in the system
1085         if ($attrib['command']) {
1086             $this->add_script(sprintf(
1087                 "%s.register_button('%s', '%s', '%s', '%s', '%s', '%s');",
60226a 1088                 self::JS_OBJECT_NAME,
47124c 1089                 $command,
T 1090                 $attrib['id'],
1091                 $attrib['type'],
ae0c82 1092                 $attrib['imageact'] ? $this->abs_url($attrib['imageact']) : $attrib['classact'],
A 1093                 $attrib['imagesel'] ? $this->abs_url($attrib['imagesel']) : $attrib['classsel'],
1094                 $attrib['imageover'] ? $this->abs_url($attrib['imageover']) : ''
47124c 1095             ));
T 1096
1097             // make valid href to specific buttons
197601 1098             if (in_array($attrib['command'], rcmail::$main_tasks)) {
0c2596 1099                 $attrib['href']    = $this->app->url(array('task' => $attrib['command']));
60226a 1100                 $attrib['onclick'] = sprintf("return %s.command('switch-task','%s',this,event)", self::JS_OBJECT_NAME, $attrib['command']);
47124c 1101             }
e9b5a6 1102             else if ($attrib['task'] && in_array($attrib['task'], rcmail::$main_tasks)) {
0c2596 1103                 $attrib['href'] = $this->app->url(array('action' => $attrib['command'], 'task' => $attrib['task']));
e9b5a6 1104             }
47124c 1105             else if (in_array($attrib['command'], $a_static_commands)) {
0c2596 1106                 $attrib['href'] = $this->app->url(array('action' => $attrib['command']));
a25d39 1107             }
271efe 1108             else if (($attrib['command'] == 'permaurl' || $attrib['command'] == 'extwin') && !empty($this->env['permaurl'])) {
29f977 1109               $attrib['href'] = $this->env['permaurl'];
T 1110             }
47124c 1111         }
T 1112
1113         // overwrite attributes
1114         if (!$attrib['href']) {
1115             $attrib['href'] = '#';
1116         }
e9b5a6 1117         if ($attrib['task']) {
T 1118             if ($attrib['classact'])
1119                 $attrib['class'] = $attrib['classact'];
1120         }
1121         else if ($command && !$attrib['onclick']) {
47124c 1122             $attrib['onclick'] = sprintf(
c28161 1123                 "return %s.command('%s','%s',this,event)",
60226a 1124                 self::JS_OBJECT_NAME,
47124c 1125                 $command,
T 1126                 $attrib['prop']
1127             );
1128         }
1129
1130         $out = '';
1131
1132         // generate image tag
0c2596 1133         if ($attrib['type'] == 'image') {
47124c 1134             $attrib_str = html::attrib_string(
T 1135                 $attrib,
1136                 array(
c9e9fe 1137                     'style', 'class', 'id', 'width', 'height', 'border', 'hspace',
A 1138                     'vspace', 'align', 'alt', 'tabindex', 'title'
47124c 1139                 )
T 1140             );
ae0c82 1141             $btn_content = sprintf('<img src="%s"%s />', $this->abs_url($attrib['image']), $attrib_str);
47124c 1142             if ($attrib['label']) {
T 1143                 $btn_content .= ' '.$attrib['label'];
1144             }
c9e9fe 1145             $link_attrib = array('href', 'onclick', 'onmouseover', 'onmouseout', 'onmousedown', 'onmouseup', 'target');
47124c 1146         }
0c2596 1147         else if ($attrib['type'] == 'link') {
356a67 1148             $btn_content = isset($attrib['content']) ? $attrib['content'] : ($attrib['label'] ? $attrib['label'] : $attrib['command']);
203ee4 1149             $link_attrib = array('href', 'onclick', 'title', 'id', 'class', 'style', 'tabindex', 'target');
5587b3 1150             if ($attrib['innerclass'])
T 1151                 $btn_content = html::span($attrib['innerclass'], $btn_content);
47124c 1152         }
0c2596 1153         else if ($attrib['type'] == 'input') {
47124c 1154             $attrib['type'] = 'button';
T 1155
1156             if ($attrib['label']) {
1157                 $attrib['value'] = $attrib['label'];
1158             }
f94e44 1159             if ($attrib['command']) {
T 1160               $attrib['disabled'] = 'disabled';
1161             }
47124c 1162
ce6433 1163             $out = html::tag('input', $attrib, null, array('type', 'value', 'onclick', 'id', 'class', 'style', 'tabindex', 'disabled'));
47124c 1164         }
T 1165
1166         // generate html code for button
1167         if ($btn_content) {
1168             $attrib_str = html::attrib_string($attrib, $link_attrib);
1169             $out = sprintf('<a%s>%s</a>', $attrib_str, $btn_content);
1170         }
1171
1172         return $out;
0c2596 1173     }
A 1174
1175
1176     /**
1177      * Link an external script file
1178      *
1179      * @param string File URL
1180      * @param string Target position [head|foot]
1181      */
1182     public function include_script($file, $position='head')
1183     {
1184         static $sa_files = array();
1185
1186         if (!preg_match('|^https?://|i', $file) && $file[0] != '/') {
1187             $file = $this->scripts_path . $file;
1188             if ($fs = @filemtime($file)) {
1189                 $file .= '?s=' . $fs;
1190             }
1191         }
1192
1193         if (in_array($file, $sa_files)) {
1194             return;
1195         }
1196
1197         $sa_files[] = $file;
1198
1199         if (!is_array($this->script_files[$position])) {
1200             $this->script_files[$position] = array();
1201         }
1202
1203         $this->script_files[$position][] = $file;
1204     }
1205
1206
1207     /**
1208      * Add inline javascript code
1209      *
1210      * @param string JS code snippet
1211      * @param string Target position [head|head_top|foot]
1212      */
1213     public function add_script($script, $position='head')
1214     {
1215         if (!isset($this->scripts[$position])) {
1216             $this->scripts[$position] = "\n" . rtrim($script);
1217         }
1218         else {
1219             $this->scripts[$position] .= "\n" . rtrim($script);
1220         }
1221     }
1222
1223
1224     /**
1225      * Link an external css file
1226      *
1227      * @param string File URL
1228      */
1229     public function include_css($file)
1230     {
1231         $this->css_files[] = $file;
1232     }
1233
1234
1235     /**
1236      * Add HTML code to the page header
1237      *
1238      * @param string $str HTML code
1239      */
1240     public function add_header($str)
1241     {
1242         $this->header .= "\n" . $str;
1243     }
1244
1245
1246     /**
1247      * Add HTML code to the page footer
1248      * To be added right befor </body>
1249      *
1250      * @param string $str HTML code
1251      */
1252     public function add_footer($str)
1253     {
1254         $this->footer .= "\n" . $str;
1255     }
1256
1257
1258     /**
1259      * Process template and write to stdOut
1260      *
1261      * @param string HTML template
1262      * @param string Base for absolute paths
1263      */
1264     public function _write($templ = '', $base_path = '')
1265     {
1266         $output = empty($templ) ? $this->default_template : trim($templ);
1267
1268         // set default page title
1269         if (empty($this->pagetitle)) {
1270             $this->pagetitle = 'Roundcube Mail';
1271         }
1272
1273         // replace specialchars in content
1274         $page_title  = html::quote($this->pagetitle);
1275         $page_header = '';
1276         $page_footer = '';
1277
1278         // include meta tag with charset
1279         if (!empty($this->charset)) {
1280             if (!headers_sent()) {
1281                 header('Content-Type: text/html; charset=' . $this->charset);
1282             }
1283             $page_header = '<meta http-equiv="content-type"';
1284             $page_header.= ' content="text/html; charset=';
1285             $page_header.= $this->charset . '" />'."\n";
1286         }
1287
1288         // definition of the code to be placed in the document header and footer
1289         if (is_array($this->script_files['head'])) {
1290             foreach ($this->script_files['head'] as $file) {
1291                 $page_header .= html::script($file);
1292             }
1293         }
1294
1295         $head_script = $this->scripts['head_top'] . $this->scripts['head'];
1296         if (!empty($head_script)) {
1297             $page_header .= html::script(array(), $head_script);
1298         }
1299
1300         if (!empty($this->header)) {
1301             $page_header .= $this->header;
1302         }
1303
1304         // put docready commands into page footer
1305         if (!empty($this->scripts['docready'])) {
1306             $this->add_script('$(document).ready(function(){ ' . $this->scripts['docready'] . "\n});", 'foot');
1307         }
1308
1309         if (is_array($this->script_files['foot'])) {
1310             foreach ($this->script_files['foot'] as $file) {
1311                 $page_footer .= html::script($file);
1312             }
1313         }
1314
1315         if (!empty($this->footer)) {
1316             $page_footer .= $this->footer . "\n";
1317         }
1318
1319         if (!empty($this->scripts['foot'])) {
1320             $page_footer .= html::script(array(), $this->scripts['foot']);
1321         }
1322
1323         // find page header
1324         if ($hpos = stripos($output, '</head>')) {
1325             $page_header .= "\n";
1326         }
1327         else {
1328             if (!is_numeric($hpos)) {
1329                 $hpos = stripos($output, '<body');
1330             }
1331             if (!is_numeric($hpos) && ($hpos = stripos($output, '<html'))) {
1332                 while ($output[$hpos] != '>') {
1333                     $hpos++;
1334                 }
1335                 $hpos++;
1336             }
1337             $page_header = "<head>\n<title>$page_title</title>\n$page_header\n</head>\n";
1338         }
1339
1340         // add page hader
1341         if ($hpos) {
1342             $output = substr_replace($output, $page_header, $hpos, 0);
1343         }
1344         else {
1345             $output = $page_header . $output;
1346         }
1347
1348         // add page footer
1349         if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) {
1350             $output = substr_replace($output, $page_footer."\n", $fpos, 0);
1351         }
1352         else {
1353             $output .= "\n".$page_footer;
1354         }
1355
1356         // add css files in head, before scripts, for speed up with parallel downloads
1357         if (!empty($this->css_files) && 
1358             (($pos = stripos($output, '<script ')) || ($pos = stripos($output, '</head>')))
1359         ) {
1360             $css = '';
1361             foreach ($this->css_files as $file) {
1362                 $css .= html::tag('link', array('rel' => 'stylesheet',
1363                     'type' => 'text/css', 'href' => $file, 'nl' => true));
1364             }
1365             $output = substr_replace($output, $css, $pos, 0);
1366         }
1367
2a0d3f 1368         $output = $this->parse_with_globals($this->fix_paths($output));
TB 1369
0c2596 1370         // trigger hook with final HTML content to be sent
be98df 1371         $hook = $this->app->plugins->exec_hook("send_page", array('content' => $output));
0c2596 1372         if (!$hook['abort']) {
a92beb 1373             if ($this->charset != RCUBE_CHARSET) {
AM 1374                 echo rcube_charset::convert($hook['content'], RCUBE_CHARSET, $this->charset);
0c2596 1375             }
A 1376             else {
1377                 echo $hook['content'];
1378             }
1379         }
47124c 1380     }
T 1381
1382
b7d33e 1383     /**
AM 1384      * Returns iframe object, registers some related env variables
1385      *
1386      * @param array $attrib HTML attributes
28de39 1387      * @param boolean $is_contentframe Register this iframe as the 'contentframe' gui object
b7d33e 1388      * @return string IFRAME element
AM 1389      */
28de39 1390     public function frame($attrib, $is_contentframe = false)
b7d33e 1391     {
28de39 1392         static $idcount = 0;
TB 1393
b7d33e 1394         if (!$attrib['id']) {
28de39 1395             $attrib['id'] = 'rcmframe' . ++$idcount;
b7d33e 1396         }
AM 1397
28de39 1398         $attrib['name'] = $attrib['id'];
TB 1399         $attrib['src'] = $attrib['src'] ? $this->abs_url($attrib['src'], true) : 'program/resources/blank.gif';
b7d33e 1400
28de39 1401         // register as 'contentframe' object
e0f7b9 1402         if ($is_contentframe || $attrib['contentframe']) {
AM 1403             $this->set_env('contentframe', $attrib['contentframe'] ? $attrib['contentframe'] : $attrib['name']);
28de39 1404             $this->set_env('blankpage', $attrib['src']);
TB 1405         }
b7d33e 1406
AM 1407         return html::iframe($attrib);
1408     }
1409
1410
47124c 1411     /*  ************* common functions delivering gui objects **************  */
T 1412
1413
1414     /**
197601 1415      * Create a form tag with the necessary hidden fields
T 1416      *
1417      * @param array Named tag parameters
1418      * @return string HTML code for the form
1419      */
1420     public function form_tag($attrib, $content = null)
1421     {
57f0c8 1422       if ($this->framed || !empty($_REQUEST['_framed'])) {
197601 1423         $hiddenfield = new html_hiddenfield(array('name' => '_framed', 'value' => '1'));
T 1424         $hidden = $hiddenfield->show();
1425       }
271efe 1426       if ($this->env['extwin']) {
TB 1427         $hiddenfield = new html_hiddenfield(array('name' => '_extwin', 'value' => '1'));
1428         $hidden = $hiddenfield->show();
1429       }
ad334a 1430
197601 1431       if (!$content)
T 1432         $attrib['noclose'] = true;
ad334a 1433
197601 1434       return html::tag('form',
T 1435         $attrib + array('action' => "./", 'method' => "get"),
57f0c8 1436         $hidden . $content,
T 1437         array('id','class','style','name','method','action','enctype','onsubmit'));
1438     }
ad334a 1439
A 1440
57f0c8 1441     /**
T 1442      * Build a form tag with a unique request token
1443      *
1444      * @param array Named tag parameters including 'action' and 'task' values which will be put into hidden fields
1445      * @param string Form content
1446      * @return string HTML code for the form
1447      */
747797 1448     public function request_form($attrib, $content = '')
57f0c8 1449     {
T 1450         $hidden = new html_hiddenfield();
1451         if ($attrib['task']) {
1452             $hidden->add(array('name' => '_task', 'value' => $attrib['task']));
1453         }
1454         if ($attrib['action']) {
1455             $hidden->add(array('name' => '_action', 'value' => $attrib['action']));
1456         }
ad334a 1457
57f0c8 1458         unset($attrib['task'], $attrib['request']);
T 1459         $attrib['action'] = './';
ad334a 1460
57f0c8 1461         // we already have a <form> tag
0501b6 1462         if ($attrib['form']) {
T 1463             if ($this->framed || !empty($_REQUEST['_framed']))
1464                 $hidden->add(array('name' => '_framed', 'value' => '1'));
57f0c8 1465             return $hidden->show() . $content;
0501b6 1466         }
57f0c8 1467         else
T 1468             return $this->form_tag($attrib, $hidden->show() . $content);
197601 1469     }
T 1470
1471
1472     /**
47124c 1473      * GUI object 'username'
T 1474      * Showing IMAP username of the current session
1475      *
1476      * @param array Named tag parameters (currently not used)
1477      * @return string HTML code for the gui object
1478      */
197601 1479     public function current_username($attrib)
47124c 1480     {
T 1481         static $username;
1482
1483         // alread fetched
1484         if (!empty($username)) {
1485             return $username;
1486         }
1487
e99991 1488         // Current username is an e-mail address
A 1489         if (strpos($_SESSION['username'], '@')) {
1490             $username = $_SESSION['username'];
1491         }
929a50 1492         // get e-mail address from default identity
e99991 1493         else if ($sql_arr = $this->app->user->get_identity()) {
7e9cec 1494             $username = $sql_arr['email'];
47124c 1495         }
T 1496         else {
7e9cec 1497             $username = $this->app->user->get_username();
47124c 1498         }
T 1499
1aceb9 1500         return rcube_utils::idn_to_utf8($username);
47124c 1501     }
T 1502
1503
1504     /**
1505      * GUI object 'loginform'
1506      * Returns code for the webmail login form
1507      *
1508      * @param array Named parameters
1509      * @return string HTML code for the gui object
1510      */
0c2596 1511     protected function login_form($attrib)
47124c 1512     {
0c2596 1513         $default_host = $this->config->get('default_host');
A 1514         $autocomplete = (int) $this->config->get('login_autocomplete');
47124c 1515
T 1516         $_SESSION['temp'] = true;
ad334a 1517
cc97ea 1518         // save original url
1aceb9 1519         $url = rcube_utils::get_input_value('_url', rcube_utils::INPUT_POST);
3a2b27 1520         if (empty($url) && !preg_match('/_(task|action)=logout/', $_SERVER['QUERY_STRING']))
cc97ea 1521             $url = $_SERVER['QUERY_STRING'];
47124c 1522
b8dc3e 1523         // Disable autocapitalization on iPad/iPhone (#1488609)
AM 1524         $attrib['autocapitalize'] = 'off';
1525
1cca4f 1526         // set atocomplete attribute
A 1527         $user_attrib = $autocomplete > 0 ? array() : array('autocomplete' => 'off');
1528         $host_attrib = $autocomplete > 0 ? array() : array('autocomplete' => 'off');
1529         $pass_attrib = $autocomplete > 1 ? array() : array('autocomplete' => 'off');
1530
c3be8e 1531         $input_task   = new html_hiddenfield(array('name' => '_task', 'value' => 'login'));
47124c 1532         $input_action = new html_hiddenfield(array('name' => '_action', 'value' => 'login'));
c8ae24 1533         $input_tzone  = new html_hiddenfield(array('name' => '_timezone', 'id' => 'rcmlogintz', 'value' => '_default_'));
cc97ea 1534         $input_url    = new html_hiddenfield(array('name' => '_url', 'id' => 'rcmloginurl', 'value' => $url));
1cca4f 1535         $input_user   = new html_inputfield(array('name' => '_user', 'id' => 'rcmloginuser')
A 1536             + $attrib + $user_attrib);
1537         $input_pass   = new html_passwordfield(array('name' => '_pass', 'id' => 'rcmloginpwd')
1538             + $attrib + $pass_attrib);
47124c 1539         $input_host   = null;
T 1540
f3e101 1541         if (is_array($default_host) && count($default_host) > 1) {
47124c 1542             $input_host = new html_select(array('name' => '_host', 'id' => 'rcmloginhost'));
T 1543
1544             foreach ($default_host as $key => $value) {
1545                 if (!is_array($value)) {
1546                     $input_host->add($value, (is_numeric($key) ? $value : $key));
1547                 }
1548                 else {
1549                     $input_host = null;
1550                     break;
1551                 }
1552             }
f3e101 1553         }
01f1af 1554         else if (is_array($default_host) && ($host = key($default_host)) !== null) {
f3e101 1555             $hide_host = true;
A 1556             $input_host = new html_hiddenfield(array(
01f1af 1557                 'name' => '_host', 'id' => 'rcmloginhost', 'value' => is_numeric($host) ? $default_host[$host] : $host) + $attrib);
47124c 1558         }
e3e597 1559         else if (empty($default_host)) {
1cca4f 1560             $input_host = new html_inputfield(array('name' => '_host', 'id' => 'rcmloginhost')
A 1561                 + $attrib + $host_attrib);
47124c 1562         }
T 1563
1564         $form_name  = !empty($attrib['form']) ? $attrib['form'] : 'form';
1565         $this->add_gui_object('loginform', $form_name);
1566
1567         // create HTML table with two cols
1568         $table = new html_table(array('cols' => 2));
1569
0c2596 1570         $table->add('title', html::label('rcmloginuser', html::quote($this->app->gettext('username'))));
1aceb9 1571         $table->add('input', $input_user->show(rcube_utils::get_input_value('_user', rcube_utils::INPUT_GPC)));
47124c 1572
0c2596 1573         $table->add('title', html::label('rcmloginpwd', html::quote($this->app->gettext('password'))));
497013 1574         $table->add('input', $input_pass->show());
47124c 1575
T 1576         // add host selection row
f3e101 1577         if (is_object($input_host) && !$hide_host) {
0c2596 1578             $table->add('title', html::label('rcmloginhost', html::quote($this->app->gettext('server'))));
1aceb9 1579             $table->add('input', $input_host->show(rcube_utils::get_input_value('_host', rcube_utils::INPUT_GPC)));
47124c 1580         }
T 1581
c3be8e 1582         $out  = $input_task->show();
T 1583         $out .= $input_action->show();
c8ae24 1584         $out .= $input_tzone->show();
cc97ea 1585         $out .= $input_url->show();
47124c 1586         $out .= $table->show();
ad334a 1587
f3e101 1588         if ($hide_host) {
A 1589             $out .= $input_host->show();
1590         }
47124c 1591
T 1592         // surround html output with a form tag
1593         if (empty($attrib['form'])) {
f3e101 1594             $out = $this->form_tag(array('name' => $form_name, 'method' => 'post'), $out);
47124c 1595         }
T 1596
086b15 1597         // include script for timezone detection
TB 1598         $this->include_script('jstz.min.js');
1599
47124c 1600         return $out;
T 1601     }
1602
1603
1604     /**
8e211a 1605      * GUI object 'preloader'
A 1606      * Loads javascript code for images preloading
1607      *
1608      * @param array Named parameters
1609      * @return void
1610      */
0c2596 1611     protected function preloader($attrib)
8e211a 1612     {
A 1613         $images = preg_split('/[\s\t\n,]+/', $attrib['images'], -1, PREG_SPLIT_NO_EMPTY);
1614         $images = array_map(array($this, 'abs_url'), $images);
1615
1616         if (empty($images) || $this->app->task == 'logout')
1617             return;
1618
0c2596 1619         $this->add_script('var images = ' . self::json_serialize($images) .';
8e211a 1620             for (var i=0; i<images.length; i++) {
A 1621                 img = new Image();
1622                 img.src = images[i];
044d66 1623             }', 'docready');
8e211a 1624     }
A 1625
1626
1627     /**
47124c 1628      * GUI object 'searchform'
T 1629      * Returns code for search function
1630      *
1631      * @param array Named parameters
1632      * @return string HTML code for the gui object
1633      */
0c2596 1634     protected function search_form($attrib)
47124c 1635     {
T 1636         // add some labels to client
1637         $this->add_label('searching');
1638
1639         $attrib['name'] = '_q';
1640
1641         if (empty($attrib['id'])) {
1642             $attrib['id'] = 'rcmqsearchbox';
1643         }
a3f149 1644         if ($attrib['type'] == 'search' && !$this->browser->khtml) {
2eb794 1645             unset($attrib['type'], $attrib['results']);
a3f149 1646         }
ad334a 1647
47124c 1648         $input_q = new html_inputfield($attrib);
T 1649         $out = $input_q->show();
1650
1651         $this->add_gui_object('qsearchbox', $attrib['id']);
1652
1653         // add form tag around text field
1654         if (empty($attrib['form'])) {
197601 1655             $out = $this->form_tag(array(
T 1656                 'name' => "rcmqsearchform",
60226a 1657                 'onsubmit' => self::JS_OBJECT_NAME . ".command('search'); return false",
197601 1658                 'style' => "display:inline"),
2eb794 1659                 $out);
47124c 1660         }
T 1661
1662         return $out;
1663     }
1664
1665
1666     /**
1667      * Builder for GUI object 'message'
1668      *
1669      * @param array Named tag parameters
1670      * @return string HTML code for the gui object
1671      */
0c2596 1672     protected function message_container($attrib)
47124c 1673     {
T 1674         if (isset($attrib['id']) === false) {
1675             $attrib['id'] = 'rcmMessageContainer';
1676         }
1677
1678         $this->add_gui_object('message', $attrib['id']);
0c2596 1679
A 1680         return html::div($attrib, '');
47124c 1681     }
T 1682
1683
1684     /**
1685      * GUI object 'charsetselector'
1686      *
1687      * @param array Named parameters for the select tag
1688      * @return string HTML code for the gui object
1689      */
0c2596 1690     public function charset_selector($attrib)
47124c 1691     {
T 1692         // pass the following attributes to the form class
1693         $field_attrib = array('name' => '_charset');
1694         foreach ($attrib as $attr => $value) {
e55ab0 1695             if (in_array($attr, array('id', 'name', 'class', 'style', 'size', 'tabindex'))) {
47124c 1696                 $field_attrib[$attr] = $value;
T 1697             }
1698         }
e55ab0 1699
47124c 1700         $charsets = array(
0c2596 1701             'UTF-8'        => 'UTF-8 ('.$this->app->gettext('unicode').')',
A 1702             'US-ASCII'     => 'ASCII ('.$this->app->gettext('english').')',
1703             'ISO-8859-1'   => 'ISO-8859-1 ('.$this->app->gettext('westerneuropean').')',
1704             'ISO-8859-2'   => 'ISO-8859-2 ('.$this->app->gettext('easterneuropean').')',
1705             'ISO-8859-4'   => 'ISO-8859-4 ('.$this->app->gettext('baltic').')',
1706             'ISO-8859-5'   => 'ISO-8859-5 ('.$this->app->gettext('cyrillic').')',
1707             'ISO-8859-6'   => 'ISO-8859-6 ('.$this->app->gettext('arabic').')',
1708             'ISO-8859-7'   => 'ISO-8859-7 ('.$this->app->gettext('greek').')',
1709             'ISO-8859-8'   => 'ISO-8859-8 ('.$this->app->gettext('hebrew').')',
1710             'ISO-8859-9'   => 'ISO-8859-9 ('.$this->app->gettext('turkish').')',
1711             'ISO-8859-10'   => 'ISO-8859-10 ('.$this->app->gettext('nordic').')',
1712             'ISO-8859-11'   => 'ISO-8859-11 ('.$this->app->gettext('thai').')',
1713             'ISO-8859-13'   => 'ISO-8859-13 ('.$this->app->gettext('baltic').')',
1714             'ISO-8859-14'   => 'ISO-8859-14 ('.$this->app->gettext('celtic').')',
1715             'ISO-8859-15'   => 'ISO-8859-15 ('.$this->app->gettext('westerneuropean').')',
1716             'ISO-8859-16'   => 'ISO-8859-16 ('.$this->app->gettext('southeasterneuropean').')',
1717             'WINDOWS-1250' => 'Windows-1250 ('.$this->app->gettext('easterneuropean').')',
1718             'WINDOWS-1251' => 'Windows-1251 ('.$this->app->gettext('cyrillic').')',
1719             'WINDOWS-1252' => 'Windows-1252 ('.$this->app->gettext('westerneuropean').')',
1720             'WINDOWS-1253' => 'Windows-1253 ('.$this->app->gettext('greek').')',
1721             'WINDOWS-1254' => 'Windows-1254 ('.$this->app->gettext('turkish').')',
1722             'WINDOWS-1255' => 'Windows-1255 ('.$this->app->gettext('hebrew').')',
1723             'WINDOWS-1256' => 'Windows-1256 ('.$this->app->gettext('arabic').')',
1724             'WINDOWS-1257' => 'Windows-1257 ('.$this->app->gettext('baltic').')',
1725             'WINDOWS-1258' => 'Windows-1258 ('.$this->app->gettext('vietnamese').')',
1726             'ISO-2022-JP'  => 'ISO-2022-JP ('.$this->app->gettext('japanese').')',
1727             'ISO-2022-KR'  => 'ISO-2022-KR ('.$this->app->gettext('korean').')',
1728             'ISO-2022-CN'  => 'ISO-2022-CN ('.$this->app->gettext('chinese').')',
1729             'EUC-JP'       => 'EUC-JP ('.$this->app->gettext('japanese').')',
1730             'EUC-KR'       => 'EUC-KR ('.$this->app->gettext('korean').')',
1731             'EUC-CN'       => 'EUC-CN ('.$this->app->gettext('chinese').')',
1732             'BIG5'         => 'BIG5 ('.$this->app->gettext('chinese').')',
1733             'GB2312'       => 'GB2312 ('.$this->app->gettext('chinese').')',
e55ab0 1734         );
47124c 1735
089e53 1736         if (!empty($_POST['_charset'])) {
AM 1737             $set = $_POST['_charset'];
1738         }
1739         else if (!empty($attrib['selected'])) {
1740             $set = $attrib['selected'];
1741         }
1742         else {
1743             $set = $this->get_charset();
1744         }
47124c 1745
089e53 1746         $set = strtoupper($set);
AM 1747         if (!isset($charsets[$set])) {
1748             $charsets[$set] = $set;
1749         }
e55ab0 1750
A 1751         $select = new html_select($field_attrib);
1752         $select->add(array_values($charsets), array_keys($charsets));
1753
1754         return $select->show($set);
47124c 1755     }
T 1756
1a0f60 1757     /**
T 1758      * Include content from config/about.<LANG>.html if available
1759      */
0c2596 1760     protected function about_content($attrib)
1a0f60 1761     {
T 1762         $content = '';
1763         $filenames = array(
1764             'about.' . $_SESSION['language'] . '.html',
1765             'about.' . substr($_SESSION['language'], 0, 2) . '.html',
1766             'about.html',
1767         );
1768         foreach ($filenames as $file) {
592668 1769             $fn = RCUBE_CONFIG_DIR . $file;
1a0f60 1770             if (is_readable($fn)) {
T 1771                 $content = file_get_contents($fn);
1772                 $content = $this->parse_conditions($content);
1773                 $content = $this->parse_xml($content);
1774                 break;
1775             }
1776         }
1777
1778         return $content;
1779     }
1780
0c2596 1781 }