From 538e64c572220bba43960a3073468c0ce3dc5f4c Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Wed, 18 Dec 2013 14:57:35 -0500
Subject: [PATCH] Fix Closure-compiler warnings, small code improvements

---
 program/include/rcmail_output_html.php |   73 ++++++++++++++++++++++++++----------
 1 files changed, 53 insertions(+), 20 deletions(-)

diff --git a/program/include/rcmail_output_html.php b/program/include/rcmail_output_html.php
index 006b89c..7ff85e0 100644
--- a/program/include/rcmail_output_html.php
+++ b/program/include/rcmail_output_html.php
@@ -45,6 +45,7 @@
     protected $footer = '';
     protected $body = '';
     protected $base_path = '';
+    protected $devel_mode = false;
 
     // deprecated names of templates used before 0.5
     protected $deprecated_templates = array(
@@ -63,6 +64,8 @@
     public function __construct($task = null, $framed = false)
     {
         parent::__construct();
+
+        $this->devel_mode = $this->config->get('devel_mode');
 
         //$this->framed = $framed;
         $this->set_env('task', $task);
@@ -310,12 +313,14 @@
      */
     public function reset($all = false)
     {
+        $framed = $this->framed;
         $env = $all ? null : array_intersect_key($this->env, array('extwin'=>1, 'framed'=>1));
 
         parent::reset();
 
         // let some env variables survive
         $this->env = $this->js_env = $env;
+        $this->framed = $framed || $this->env['framed'];
         $this->js_labels    = array();
         $this->js_commands  = array();
         $this->script_files = array();
@@ -323,6 +328,11 @@
         $this->header       = '';
         $this->footer       = '';
         $this->body         = '';
+
+        // load defaults
+        if (!$all) {
+            $this->__construct();
+        }
     }
 
     /**
@@ -651,8 +661,19 @@
         }
 
         // add file modification timestamp
-        if (preg_match('/\.(js|css)$/', $file)) {
-            if ($fs = @filemtime($file)) {
+        if (preg_match('/\.(js|css)$/', $file, $m)) {
+            $fs  = false;
+            $ext = $m[1];
+
+            // use minified file if exists (not in development mode)
+            if (!$this->devel_mode && !preg_match('/\.min\.' . $ext . '$/', $file)) {
+                $minified_file = substr($file, 0, strlen($ext) * -1) . 'min.' . $ext;
+                if ($fs = @filemtime($minified_file)) {
+                    $file = $minified_file . '?s=' . $fs;
+                }
+            }
+
+            if (!$fs && ($fs = @filemtime($file))) {
                 $file .= '?s=' . $fs;
             }
         }
@@ -924,8 +945,21 @@
                 }
                 else if ($object == 'logo') {
                     $attrib += array('alt' => $this->xml_command(array('', 'object', 'name="productname"')));
-                    if ($logo = $this->config->get('skin_logo'))
-                        $attrib['src'] = $logo;
+
+                    if ($logo = $this->config->get('skin_logo')) {
+                        if (is_array($logo)) {
+                            if ($template_logo = $logo[$this->template_name]) {
+                                $attrib['src'] = $template_logo;
+                            }
+                            elseif ($template_logo = $logo['*']) {
+                                $attrib['src'] = $template_logo;
+                            }
+                        }
+                        else {
+                            $attrib['src'] = $logo;
+                        }
+                    }
+
                     $content = html::img($attrib);
                 }
                 else if ($object == 'productname') {
@@ -951,7 +985,7 @@
                   $content = html::quote($this->get_pagetitle());
                 }
                 else if ($object == 'pagetitle') {
-                    if ($this->config->get('devel_mode') && !empty($_SESSION['username']))
+                    if ($this->devel_mode && !empty($_SESSION['username']))
                         $title = $_SESSION['username'].' :: ';
                     else if ($prod_name = $this->config->get('product_name'))
                         $title = $prod_name . ' :: ';
@@ -1042,7 +1076,7 @@
         // these commands can be called directly via url
         $a_static_commands = array('compose', 'list', 'preferences', 'folders', 'identities');
 
-        if (!($attrib['command'] || $attrib['name'])) {
+        if (!($attrib['command'] || $attrib['name'] || $attrib['href'])) {
             return '';
         }
 
@@ -1152,7 +1186,7 @@
         }
         else if ($attrib['type'] == 'link') {
             $btn_content = isset($attrib['content']) ? $attrib['content'] : ($attrib['label'] ? $attrib['label'] : $attrib['command']);
-            $link_attrib = array('href', 'onclick', 'title', 'id', 'class', 'style', 'tabindex', 'target');
+            $link_attrib = array_merge(html::$common_attrib, array('href', 'onclick', 'tabindex', 'target'));
             if ($attrib['innerclass'])
                 $btn_content = html::span($attrib['innerclass'], $btn_content);
         }
@@ -1171,7 +1205,7 @@
 
         // generate html code for button
         if ($btn_content) {
-            $attrib_str = html::attrib_string($attrib, array_merge(html::$common_attrib, $link_attrib));
+            $attrib_str = html::attrib_string($attrib, $link_attrib);
             $out = sprintf('<a%s>%s</a>', $attrib_str, $btn_content);
         }
 
@@ -1190,8 +1224,6 @@
      */
     public function include_script($file, $position='head')
     {
-        static $sa_files = array();
-
         if (!preg_match('|^https?://|i', $file) && $file[0] != '/') {
             $file = $this->scripts_path . $file;
             if ($fs = @filemtime($file)) {
@@ -1199,17 +1231,13 @@
             }
         }
 
-        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;
+        if (!in_array($file, $this->script_files[$position])) {
+            $this->script_files[$position][] = $file;
+        }
     }
 
     /**
@@ -1267,7 +1295,12 @@
      */
     public function _write($templ = '', $base_path = '')
     {
-        $output = empty($templ) ? $this->default_template : trim($templ);
+        $output = trim($templ);
+
+        if (empty($output)) {
+            $output   = $this->default_template;
+            $is_empty = true;
+        }
 
         // set default page title
         if (empty($this->pagetitle)) {
@@ -1358,8 +1391,8 @@
         }
 
         // 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>')))
+        if (!empty($this->css_files) && !$is_empty
+            && (($pos = stripos($output, '<script ')) || ($pos = stripos($output, '</head>')))
         ) {
             $css = '';
             foreach ($this->css_files as $file) {

--
Gitblit v1.9.1