]> git.donarmstrong.com Git - roundcube.git/blobdiff - program/include/rcube_html_page.php
Imported Upstream version 0.7
[roundcube.git] / program / include / rcube_html_page.php
index a3cec3f6c0619ae49648e9f784996307e0390f1c..77b7ff2d86db642ceab0a37b901419ce6e311c54 100644 (file)
@@ -5,7 +5,7 @@
  | program/include/rcube_html_page.php                                   |
  |                                                                       |
  | This file is part of the Roundcube PHP suite                          |
- | Copyright (C) 2005-2009, Roundcube Dev. - Switzerland                 |
+ | Copyright (C) 2005-2011 The Roundcube Dev Team                       |
  | Licensed under the GNU GPL                                            |
  |                                                                       |
  | CONTENTS:                                                             |
@@ -15,7 +15,7 @@
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  +-----------------------------------------------------------------------+
 
- $Id: rcube_html_page.php 4015 2010-09-29 16:47:05Z alec $
+ $Id: rcube_html_page.php 5135 2011-08-26 09:22:53Z alec $
 
 */
 
@@ -28,11 +28,13 @@ class rcube_html_page
 {
     protected $scripts_path = '';
     protected $script_files = array();
+    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 $script_tag  =  "<script type=\"text/javascript\">\n/* <![CDATA[ */\n%s\n/* ]]> */\n</script>\n";
+    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 = '';
@@ -61,6 +63,9 @@ class rcube_html_page
         if (in_array($file, $sa_files)) {
             return;
         }
+
+        $sa_files[] = $file;
+
         if (!is_array($this->script_files[$position])) {
             $this->script_files[$position] = array();
         }
@@ -82,6 +87,16 @@ class rcube_html_page
         }
     }
 
+    /**
+     * 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
      *
@@ -163,52 +178,58 @@ class rcube_html_page
         }
 
         // 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, $file);
+                $page_header .= sprintf($this->script_tag_file, $file);
             }
         }
 
         $head_script = $this->scripts['head_top'] . $this->scripts['head'];
         if (!empty($head_script)) {
-            $__page_header .= sprintf($this->script_tag, $head_script);
+            $page_header .= sprintf($this->script_tag, $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, $file);
+                $page_footer .= sprintf($this->script_tag_file, $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 .= sprintf($this->script_tag, $this->scripts['foot']);
         }
 
         // find page header
         if ($hpos = stripos($output, '</head>')) {
-            $__page_header .= "\n";
+            $page_header .= "\n";
         }
         else {
             if (!is_numeric($hpos)) {
@@ -220,57 +241,55 @@ class rcube_html_page
                 }
                 $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 = stripos($output, '<body')) {
-            while ($output[$bpos] != '>') {
-                $bpos++;
-            }
-            $bpos++;
+        // add page footer
+        if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) {
+            $output = substr_replace($output, $page_footer."\n", $fpos, 0);
         }
         else {
-            $bpos = stripos($output, '</head>')+7;
-        }
-
-        // add page body
-        if ($bpos && $__page_body) {
-            $output = substr($output,0,$bpos) . "\n$__page_body\n" . substr($output,$bpos,strlen($output));
+            $output .= "\n".$page_footer;
         }
 
-        // find and add page footer
-        if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) {
-            $output = substr($output, 0, $fpos) . "$__page_footer\n" . substr($output, $fpos);
-        }
-        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 .= sprintf($this->link_css_file, $file);
+            }
+            $output = substr_replace($output, $css, $pos, 0);
         }
 
-        // reset those global vars
-        $__page_header = $__page_footer = '';
+        $this->base_path = $base_path;
 
-           $this->base_path = $base_path;
         // correct absolute paths in images and other tags
-           // add timestamp to .js and .css filename
-        $output = preg_replace_callback('!(src|href|background)=(["\']?)([a-z0-9/_.-]+)(["\'\s>])!i',
-           array($this, 'file_callback'), $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);
 
-        if ($this->charset != RCMAIL_CHARSET)
-               echo rcube_charset_convert($output, RCMAIL_CHARSET, $this->charset);
-           else
-               echo $output;
+        // 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()
      *