| | |
| | | <?php |
| | | |
| | | /* |
| | | +-----------------------------------------------------------------------+ |
| | | | program/include/rcube_html_page.php | |
| | | | | |
| | | | This file is part of the RoundCube PHP suite | |
| | | | Copyright (C) 2005-2008, RoundCube Dev. - Switzerland | |
| | | | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland | |
| | | | Licensed under the GNU GPL | |
| | | | | |
| | | | CONTENTS: | |
| | |
| | | | Author: Thomas Bruederli <roundcube@gmail.com> | |
| | | +-----------------------------------------------------------------------+ |
| | | |
| | | $Id: $ |
| | | $Id$ |
| | | |
| | | */ |
| | | |
| | |
| | | * Class for HTML page creation |
| | | * |
| | | * @package HTML |
| | | * |
| | | * @author Thomas Bruederli <roundcube@gmail.com> |
| | | * @license http://gnu.org GPL |
| | | * @todo See about improving performance (__get, __set, sprintf) |
| | | */ |
| | | class rcube_html_page |
| | | { |
| | | protected $_store = array(); |
| | | protected $scripts_path = ''; |
| | | protected $script_files = array(); |
| | | protected $scripts = array(); |
| | | protected $charset = RCMAIL_CHARSET; |
| | | |
| | | /** |
| | | * Constructor |
| | | * |
| | | * @return rcube_html_page |
| | | * @uses self::reset() |
| | | */ |
| | | public function __construct() |
| | | { |
| | | $this->reset(); |
| | | } |
| | | protected $script_tag_file = "<script type=\"text/javascript\" src=\"%s\"></script>\n"; |
| | | protected $script_tag = "<script type=\"text/javascript\">\n/* <![CDATA[ */\n%s\n/* ]]> */\n</script>"; |
| | | protected $default_template = "<html>\n<head><title></title></head>\n<body></body>\n</html>"; |
| | | |
| | | protected $title = ''; |
| | | protected $header = ''; |
| | | protected $footer = ''; |
| | | protected $body = ''; |
| | | protected $base_path = ''; |
| | | |
| | | |
| | | /** Constructor */ |
| | | public function __construct() {} |
| | | |
| | | /** |
| | | * Link an external script file |
| | |
| | | public function include_script($file, $position='head') |
| | | { |
| | | static $sa_files = array(); |
| | | |
| | | if (!preg_match('|^https?://|i', $file) && $file[0] != '/') |
| | | $file = $this->scripts_path . $file . (($fs = @filemtime($this->scripts_path . $file)) ? '?s='.$fs : ''); |
| | | |
| | | if (in_array($file, $sa_files)) { |
| | | return; |
| | |
| | | |
| | | /** |
| | | * Reset all saved properties |
| | | * |
| | | * @return void |
| | | * @see self::__construct |
| | | * @uses self::$_store |
| | | */ |
| | | public function reset() |
| | | { |
| | | $this->scripts_path = ''; |
| | | $this->script_files = array(); |
| | | $this->external_scripts = array(); |
| | | $this->scripts = array(); |
| | | $this->charset = 'UTF-8'; |
| | | |
| | | // templates |
| | | $this->script_tag_file = "<script type=\"text/javascript\" src=\"%s%s\"></script>\n"; |
| | | $this->script_tag = "<script type=\"text/javascript\">\n<!--\n%s\n\n//-->\n</script>\n"; |
| | | $this->default_template = "<html>\n<head><title></title></head>\n<body></body>\n</html>"; |
| | | $this->tag_format_external_script = "<script type=\"text/javascript\" src=\"%s\"></script>\n"; |
| | | |
| | | // page stuff |
| | | $this->title = ''; |
| | | $this->header = ''; |
| | | $this->footer = ''; |
| | |
| | | // 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 .= sprintf($this->script_tag_file, $file); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | 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 .= sprintf($this->script_tag_file, $file); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | // find page header |
| | | if ($hpos = strpos(strtolower($output), '</head>')) { |
| | | 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++; |
| | | } |
| | |
| | | } |
| | | |
| | | // find page body |
| | | if ($bpos = strpos(strtolower($output), '<body')) { |
| | | if ($bpos = stripos($output, '<body')) { |
| | | while ($output[$bpos] != '>') { |
| | | $bpos++; |
| | | } |
| | | $bpos++; |
| | | } |
| | | else { |
| | | $bpos = strpos(strtolower($output), '</head>')+7; |
| | | $bpos = stripos($output, '</head>')+7; |
| | | } |
| | | |
| | | // add page body |
| | |
| | | } |
| | | |
| | | // find and add page footer |
| | | $output_lc = strtolower($output); |
| | | if (($fpos = strrpos($output_lc, '</body>')) || ($fpos = strrpos($output_lc, '</html>'))) { |
| | | if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) { |
| | | $output = substr($output, 0, $fpos) . "$__page_footer\n" . substr($output, $fpos); |
| | | } |
| | | else { |
| | |
| | | // 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); |
| | | // add timestamp to .js and .css filename |
| | | $output = preg_replace_callback('!(src|href|background)=(["\']?)([a-z0-9/_.-]+)(["\'\s>])!i', |
| | | array($this, 'file_callback'), $output); |
| | | $output = str_replace('$__skin_path', $base_path, $output); |
| | | |
| | | echo rcube_charset_convert($output, 'UTF-8', $this->charset); |
| | | if ($this->charset != RCMAIL_CHARSET) |
| | | echo rcube_charset_convert($output, RCMAIL_CHARSET, $this->charset); |
| | | else |
| | | echo $output; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * __get |
| | | * |
| | | * @param string $var A variable name. |
| | | * |
| | | * @return mixed |
| | | * @uses self::$_store |
| | | * Callback function for preg_replace_callback in write() |
| | | */ |
| | | public function __get($var) |
| | | private function file_callback($matches) |
| | | { |
| | | return $this->_store[$var]; |
| | | } |
| | | $file = $matches[3]; |
| | | |
| | | /** |
| | | * __set |
| | | * |
| | | * @param string $var A variable name. |
| | | * @param mixed $value The value of the variable. |
| | | * |
| | | * @return mixed |
| | | * @uses self::$_store |
| | | */ |
| | | public function __set($var, $value) |
| | | { |
| | | return $this->_store[$var] = $value; |
| | | // correct absolute paths |
| | | if ($file[0] == '/') |
| | | $file = $this->base_path . $file; |
| | | |
| | | // add file modification timestamp |
| | | if (preg_match('/\.(js|css)$/', $file)) |
| | | $file .= '?s=' . @filemtime($file); |
| | | |
| | | return sprintf("%s=%s%s%s", $matches[1], $matches[2], $file, $matches[4]); |
| | | } |
| | | } |
| | | |