| | |
| | | +-----------------------------------------------------------------------+ |
| | | | program/include/rcube_html_page.php | |
| | | | | |
| | | | This file is part of the RoundCube PHP suite | |
| | | | Copyright (C) 2005-2008, RoundCube Dev. - Switzerland | |
| | | | Licensed under the GNU GPL | |
| | | | This file is part of the Roundcube PHP suite | |
| | | | Copyright (C) 2005-2011 The Roundcube Dev Team | |
| | | | | |
| | | | Licensed under the GNU General Public License version 3 or | |
| | | | any later version with exceptions for skins & plugins. | |
| | | | See the README file for a full license statement. | |
| | | | | |
| | | | CONTENTS: | |
| | | | Class to build XHTML page output | |
| | |
| | | | Author: Thomas Bruederli <roundcube@gmail.com> | |
| | | +-----------------------------------------------------------------------+ |
| | | |
| | | $Id: $ |
| | | $Id$ |
| | | |
| | | */ |
| | | |
| | |
| | | { |
| | | protected $scripts_path = ''; |
| | | protected $script_files = array(); |
| | | protected $external_scripts = array(); |
| | | protected $css_files = array(); |
| | | protected $scripts = array(); |
| | | protected $charset = 'UTF-8'; |
| | | |
| | | protected $script_tag_file = "<script type=\"text/javascript\" src=\"%s%s\"></script>\n"; |
| | | protected $script_tag = "<script type=\"text/javascript\">\n<!--\n%s\n\n//-->\n</script>\n"; |
| | | protected $charset = RCMAIL_CHARSET; |
| | | protected $default_template = "<html>\n<head><title></title></head>\n<body></body>\n</html>"; |
| | | protected $tag_format_external_script = "<script type=\"text/javascript\" src=\"%s\"></script>\n"; |
| | | |
| | | protected $title = ''; |
| | | protected $header = ''; |
| | | protected $footer = ''; |
| | | protected $body = ''; |
| | | protected $base_path = ''; |
| | | |
| | | |
| | | /** Constructor */ |
| | |
| | | { |
| | | static $sa_files = array(); |
| | | |
| | | if (!preg_match('|^https?://|i', $file) && $file[0] != '/') { |
| | | $file = $this->scripts_path . $file; |
| | | if ($fs = @filemtime($file)) { |
| | | $file .= '?s=' . $fs; |
| | | } |
| | | } |
| | | |
| | | if (in_array($file, $sa_files)) { |
| | | return; |
| | | } |
| | | |
| | | $sa_files[] = $file; |
| | | |
| | | if (!is_array($this->script_files[$position])) { |
| | | $this->script_files[$position] = array(); |
| | | } |
| | | |
| | | $this->script_files[$position][] = $file; |
| | | } |
| | | |
| | |
| | | public function add_script($script, $position='head') |
| | | { |
| | | if (!isset($this->scripts[$position])) { |
| | | $this->scripts[$position] = "\n".rtrim($script); |
| | | } else { |
| | | $this->scripts[$position] .= "\n".rtrim($script); |
| | | $this->scripts[$position] = "\n" . rtrim($script); |
| | | } |
| | | else { |
| | | $this->scripts[$position] .= "\n" . rtrim($script); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Link an external css file |
| | | * |
| | | * @param string File URL |
| | | */ |
| | | public function include_css($file) |
| | | { |
| | | $this->css_files[] = $file; |
| | | } |
| | | |
| | | /** |
| | | * Add HTML code to the page header |
| | | * |
| | | * @param string $str HTML code |
| | | */ |
| | | public function add_header($str) |
| | | { |
| | | $this->header .= "\n".$str; |
| | | $this->header .= "\n" . $str; |
| | | } |
| | | |
| | | /** |
| | | * Add HTML code to the page footer |
| | | * To be added right befor </body> |
| | | * |
| | | * @param string $str HTML code |
| | | */ |
| | | public function add_footer($str) |
| | | { |
| | | $this->footer .= "\n".$str; |
| | | $this->footer .= "\n" . $str; |
| | | } |
| | | |
| | | /** |
| | | * Setter for page title |
| | | * |
| | | * @param string $t Page title |
| | | */ |
| | | public function set_title($t) |
| | | { |
| | |
| | | /** |
| | | * Setter for output charset. |
| | | * To be specified in a meta tag and sent as http-header |
| | | * |
| | | * @param string $charset Charset |
| | | */ |
| | | public function set_charset($charset) |
| | | { |
| | |
| | | |
| | | /** |
| | | * Getter for output charset |
| | | * |
| | | * @return string Output charset |
| | | */ |
| | | public function get_charset() |
| | | { |
| | |
| | | public function reset() |
| | | { |
| | | $this->script_files = array(); |
| | | $this->scripts = array(); |
| | | $this->title = ''; |
| | | $this->header = ''; |
| | | $this->footer = ''; |
| | | $this->scripts = array(); |
| | | $this->title = ''; |
| | | $this->header = ''; |
| | | $this->footer = ''; |
| | | $this->body = ''; |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | // set default page title |
| | | if (empty($this->title)) { |
| | | $this->title = 'RoundCube Mail'; |
| | | $this->title = 'Roundcube Mail'; |
| | | } |
| | | |
| | | // replace specialchars in content |
| | | $__page_title = Q($this->title, 'show', FALSE); |
| | | $__page_header = $__page_body = $__page_footer = ''; |
| | | $page_title = Q($this->title, 'show', FALSE); |
| | | $page_header = ''; |
| | | $page_footer = ''; |
| | | |
| | | // include meta tag with charset |
| | | if (!empty($this->charset)) { |
| | | if (!headers_sent()) { |
| | | header('Content-Type: text/html; charset=' . $this->charset); |
| | | } |
| | | $__page_header = '<meta http-equiv="content-type"'; |
| | | $__page_header.= ' content="text/html; charset='; |
| | | $__page_header.= $this->charset . '" />'."\n"; |
| | | $page_header = '<meta http-equiv="content-type"'; |
| | | $page_header.= ' content="text/html; charset='; |
| | | $page_header.= $this->charset . '" />'."\n"; |
| | | } |
| | | |
| | | // definition of the code to be placed in the document header and footer |
| | | if (is_array($this->script_files['head'])) { |
| | | foreach ($this->script_files['head'] as $file) { |
| | | $__page_header .= sprintf($this->script_tag_file, $this->scripts_path, $file); |
| | | $page_header .= html::script($file); |
| | | } |
| | | } |
| | | |
| | | $head_script = $this->scripts['head_top'] . $this->scripts['head']; |
| | | if (!empty($head_script)) { |
| | | $__page_header .= sprintf($this->script_tag, $head_script); |
| | | $page_header .= html::script(array(), $head_script); |
| | | } |
| | | |
| | | if (!empty($this->header)) { |
| | | $__page_header .= $this->header; |
| | | $page_header .= $this->header; |
| | | } |
| | | |
| | | // put docready commands into page footer |
| | | if (!empty($this->scripts['docready'])) { |
| | | $this->add_script('$(document).ready(function(){ ' . $this->scripts['docready'] . "\n});", 'foot'); |
| | | } |
| | | |
| | | if (is_array($this->script_files['foot'])) { |
| | | foreach ($this->script_files['foot'] as $file) { |
| | | $__page_footer .= sprintf($this->script_tag_file, $this->scripts_path, $file); |
| | | $page_footer .= html::script($file); |
| | | } |
| | | } |
| | | |
| | | if (!empty($this->scripts['foot'])) { |
| | | $__page_footer .= sprintf($this->script_tag, $this->scripts['foot']); |
| | | } |
| | | |
| | | if (!empty($this->footer)) { |
| | | $__page_footer .= $this->footer; |
| | | $page_footer .= $this->footer . "\n"; |
| | | } |
| | | |
| | | if (!empty($this->scripts['foot'])) { |
| | | $page_footer .= html::script(array(), $this->scripts['foot']); |
| | | } |
| | | |
| | | // find page header |
| | | if ($hpos = strpos(strtolower($output), '</head>')) { |
| | | $__page_header .= "\n"; |
| | | if ($hpos = stripos($output, '</head>')) { |
| | | $page_header .= "\n"; |
| | | } |
| | | else { |
| | | if (!is_numeric($hpos)) { |
| | | $hpos = strpos(strtolower($output), '<body'); |
| | | $hpos = stripos($output, '<body'); |
| | | } |
| | | if (!is_numeric($hpos) && ($hpos = strpos(strtolower($output), '<html'))) { |
| | | if (!is_numeric($hpos) && ($hpos = stripos($output, '<html'))) { |
| | | while ($output[$hpos] != '>') { |
| | | $hpos++; |
| | | } |
| | | $hpos++; |
| | | } |
| | | $__page_header = "<head>\n<title>$__page_title</title>\n$__page_header\n</head>\n"; |
| | | $page_header = "<head>\n<title>$page_title</title>\n$page_header\n</head>\n"; |
| | | } |
| | | |
| | | // add page hader |
| | | if ($hpos) { |
| | | $output = substr($output,0,$hpos) . $__page_header . substr($output,$hpos,strlen($output)); |
| | | $output = substr_replace($output, $page_header, $hpos, 0); |
| | | } |
| | | else { |
| | | $output = $__page_header . $output; |
| | | $output = $page_header . $output; |
| | | } |
| | | |
| | | // find page body |
| | | if ($bpos = strpos(strtolower($output), '<body')) { |
| | | while ($output[$bpos] != '>') { |
| | | $bpos++; |
| | | // add page footer |
| | | if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) { |
| | | $output = substr_replace($output, $page_footer."\n", $fpos, 0); |
| | | } |
| | | else { |
| | | $output .= "\n".$page_footer; |
| | | } |
| | | |
| | | // add css files in head, before scripts, for speed up with parallel downloads |
| | | if (!empty($this->css_files) && |
| | | (($pos = stripos($output, '<script ')) || ($pos = stripos($output, '</head>'))) |
| | | ) { |
| | | $css = ''; |
| | | foreach ($this->css_files as $file) { |
| | | $css .= html::tag('link', array('rel' => 'stylesheet', |
| | | 'type' => 'text/css', 'href' => $file, 'nl' => true)); |
| | | } |
| | | $bpos++; |
| | | } |
| | | else { |
| | | $bpos = strpos(strtolower($output), '</head>')+7; |
| | | $output = substr_replace($output, $css, $pos, 0); |
| | | } |
| | | |
| | | // add page body |
| | | if ($bpos && $__page_body) { |
| | | $output = substr($output,0,$bpos) . "\n$__page_body\n" . substr($output,$bpos,strlen($output)); |
| | | } |
| | | |
| | | // find and add page footer |
| | | $output_lc = strtolower($output); |
| | | if (($fpos = strrpos($output_lc, '</body>')) || ($fpos = strrpos($output_lc, '</html>'))) { |
| | | $output = substr($output, 0, $fpos) . "$__page_footer\n" . substr($output, $fpos); |
| | | } |
| | | else { |
| | | $output .= "\n".$__page_footer; |
| | | } |
| | | |
| | | // reset those global vars |
| | | $__page_header = $__page_footer = ''; |
| | | $this->base_path = $base_path; |
| | | |
| | | // correct absolute paths in images and other tags |
| | | $output = preg_replace('/(src|href|background)=(["\']?)(\/[a-z0-9_\-]+)/Ui', "\\1=\\2$base_path\\3", $output); |
| | | $output = str_replace('$__skin_path', $base_path, $output); |
| | | // add timestamp to .js and .css filename |
| | | $output = preg_replace_callback( |
| | | '!(src|href|background)=(["\']?)([a-z0-9/_.-]+)(["\'\s>])!i', |
| | | array($this, 'file_callback'), $output); |
| | | |
| | | print rcube_charset_convert($output, 'UTF-8', $this->charset); |
| | | // trigger hook with final HTML content to be sent |
| | | $hook = rcmail::get_instance()->plugins->exec_hook("send_page", array('content' => $output)); |
| | | if (!$hook['abort']) { |
| | | if ($this->charset != RCMAIL_CHARSET) { |
| | | echo rcube_charset_convert($hook['content'], RCMAIL_CHARSET, $this->charset); |
| | | } |
| | | else { |
| | | echo $hook['content']; |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Callback function for preg_replace_callback in write() |
| | | * |
| | | * @return string Parsed string |
| | | */ |
| | | private function file_callback($matches) |
| | | { |
| | | $file = $matches[3]; |
| | | |
| | | // correct absolute paths |
| | | if ($file[0] == '/') { |
| | | $file = $this->base_path . $file; |
| | | } |
| | | |
| | | // add file modification timestamp |
| | | if (preg_match('/\.(js|css)$/', $file)) { |
| | | if ($fs = @filemtime($file)) { |
| | | $file .= '?s=' . $fs; |
| | | } |
| | | } |
| | | |
| | | return $matches[1] . '=' . $matches[2] . $file . $matches[4]; |
| | | } |
| | | } |
| | | |