alecpl
2012-01-06 e86a21bd83a0ae6cadfe9c919582951f306d3b64
program/include/rcube_html_page.php
@@ -5,7 +5,7 @@
 | program/include/rcube_html_page.php                                   |
 |                                                                       |
 | This file is part of the Roundcube PHP suite                          |
 | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
 | Copyright (C) 2005-2011 The Roundcube Dev Team                       |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | CONTENTS:                                                             |
@@ -31,10 +31,6 @@
    protected $css_files = array();
    protected $scripts = array();
    protected $charset = RCMAIL_CHARSET;
    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 $link_css_file = "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" />\n";
    protected $default_template = "<html>\n<head><title></title></head>\n<body></body>\n</html>";
    protected $title = '';
@@ -56,9 +52,13 @@
    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 (!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;
@@ -69,6 +69,7 @@
        if (!is_array($this->script_files[$position])) {
            $this->script_files[$position] = array();
        }
        $this->script_files[$position][] = $file;
    }
@@ -81,9 +82,10 @@
    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);
        }
    }
@@ -104,7 +106,7 @@
     */
    public function add_header($str)
    {
        $this->header .= "\n".$str;
        $this->header .= "\n" . $str;
    }
    /**
@@ -115,7 +117,7 @@
     */
    public function add_footer($str)
    {
        $this->footer .= "\n".$str;
        $this->footer .= "\n" . $str;
    }
    /**
@@ -195,31 +197,36 @@
        // 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, $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;
        }
        // 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, $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 . "\n";
        }
        if (!empty($this->footer)) {
            $page_footer .= $this->footer;
        if (!empty($this->scripts['foot'])) {
            $page_footer .= html::script(array(), $this->scripts['foot']);
        }
        // find page header
@@ -241,7 +248,7 @@
        // 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;
@@ -249,7 +256,7 @@
        // add page footer
        if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) {
            $output = substr($output, 0, $fpos) . "$page_footer\n" . substr($output, $fpos);
            $output = substr_replace($output, $page_footer."\n", $fpos, 0);
        }
        else {
            $output .= "\n".$page_footer;
@@ -261,9 +268,10 @@
        ) {
            $css = '';
            foreach ($this->css_files as $file) {
                $css .= sprintf($this->link_css_file, $file);
                $css .= html::tag('link', array('rel' => 'stylesheet',
                    'type' => 'text/css', 'href' => $file, 'nl' => true));
            }
            $output = substr($output, 0, $pos) . $css . substr($output, $pos);
            $output = substr_replace($output, $css, $pos, 0);
        }
        $this->base_path = $base_path;
@@ -278,13 +286,15 @@
        // 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)
            if ($this->charset != RCMAIL_CHARSET) {
                echo rcube_charset_convert($hook['content'], RCMAIL_CHARSET, $this->charset);
            else
            }
            else {
                echo $hook['content'];
            }
        }
    }
    /**
     * Callback function for preg_replace_callback in write()
     *
@@ -295,14 +305,17 @@
       $file = $matches[3];
        // correct absolute paths
       if ($file[0] == '/')
       if ($file[0] == '/') {
           $file = $this->base_path . $file;
        }
        // add file modification timestamp
       if (preg_match('/\.(js|css)$/', $file))
           $file .= '?s=' . @filemtime($file);
       if (preg_match('/\.(js|css)$/', $file)) {
            if ($fs = @filemtime($file)) {
                $file .= '?s=' . $fs;
            }
        }
       return sprintf("%s=%s%s%s", $matches[1], $matches[2], $file, $matches[4]);
       return $matches[1] . '=' . $matches[2] . $file . $matches[4];
    }
}