From 47124c2279382714afd8dbe4a867a867ea179199 Mon Sep 17 00:00:00 2001 From: thomascube Date: Sat, 12 Apr 2008 13:54:45 +0000 Subject: Changed codebase to PHP5 with autoloader + added some new classes from the devel-vnext branch --- program/include/rcube_html_page.php | 256 ++++++++++++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100644 program/include/rcube_html_page.php (limited to 'program/include/rcube_html_page.php') diff --git a/program/include/rcube_html_page.php b/program/include/rcube_html_page.php new file mode 100644 index 000000000..42036f63c --- /dev/null +++ b/program/include/rcube_html_page.php @@ -0,0 +1,256 @@ + | + +-----------------------------------------------------------------------+ + + $Id: $ + +*/ + +/** + * Class for HTML page creation + * + * @package HTML + */ +class rcube_html_page +{ + protected $scripts_path = ''; + protected $script_files = array(); + protected $external_scripts = array(); + protected $scripts = array(); + protected $charset = 'UTF-8'; + + protected $script_tag_file = "\n"; + protected $script_tag = "\n"; + protected $default_template = "\n\n\n"; + protected $tag_format_external_script = "\n"; + + protected $title = ''; + protected $header = ''; + protected $footer = ''; + protected $body = ''; + + + /** Constructor */ + public function __construct() {} + + /** + * Link an external script file + * + * @param string File URL + * @param string Target position [head|foot] + */ + public function include_script($file, $position='head') + { + static $sa_files = array(); + + if (in_array($file, $sa_files)) { + return; + } + if (!is_array($this->script_files[$position])) { + $this->script_files[$position] = array(); + } + $this->script_files[$position][] = $file; + } + + /** + * Add inline javascript code + * + * @param string JS code snippet + * @param string Target position [head|head_top|foot] + */ + 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); + } + } + + /** + * Add HTML code to the page header + */ + public function add_header($str) + { + $this->header .= "\n".$str; + } + + /** + * Add HTML code to the page footer + * To be added right befor + */ + public function add_footer($str) + { + $this->footer .= "\n".$str; + } + + /** + * Setter for page title + */ + public function set_title($t) + { + $this->title = $t; + } + + /** + * Setter for output charset. + * To be specified in a meta tag and sent as http-header + */ + public function set_charset($charset) + { + $this->charset = $charset; + } + + /** + * Getter for output charset + */ + public function get_charset() + { + return $this->charset; + } + + /** + * Reset all saved properties + */ + public function reset() + { + $this->script_files = array(); + $this->scripts = array(); + $this->title = ''; + $this->header = ''; + $this->footer = ''; + } + + /** + * Process template and write to stdOut + * + * @param string HTML template + * @param string Base for absolute paths + */ + public function write($templ='', $base_path='') + { + $output = empty($templ) ? $this->default_template : trim($templ); + + // set default page title + if (empty($this->title)) { + $this->title = 'RoundCube Mail'; + } + + // replace specialchars in content + $__page_title = Q($this->title, 'show', FALSE); + $__page_header = $__page_body = $__page_footer = ''; + + // include meta tag with charset + if (!empty($this->charset)) { + if (!headers_sent()) { + header('Content-Type: text/html; charset=' . $this->charset); + } + $__page_header = '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); + } + } + + $head_script = $this->scripts['head_top'] . $this->scripts['head']; + if (!empty($head_script)) { + $__page_header .= sprintf($this->script_tag, $head_script); + } + + if (!empty($this->header)) { + $__page_header .= $this->header; + } + + 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); + } + } + + if (!empty($this->scripts['foot'])) { + $__page_footer .= sprintf($this->script_tag, $this->scripts['foot']); + } + + if (!empty($this->footer)) { + $__page_footer .= $this->footer; + } + + // find page header + if ($hpos = strpos(strtolower($output), '')) { + $__page_header .= "\n"; + } + else { + if (!is_numeric($hpos)) { + $hpos = strpos(strtolower($output), '')+7; + } + + // 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, '')) || ($fpos = strrpos($output_lc, ''))) { + $output = substr($output, 0, $fpos) . "$__page_footer\n" . substr($output, $fpos); + } + else { + $output .= "\n".$__page_footer; + } + + // reset those global vars + $__page_header = $__page_footer = ''; + + // 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); + + print rcube_charset_convert($output, 'UTF-8', $this->charset); + } +} + -- cgit v1.2.3