From f5d2eef55c89b7f1a5549704705c25fd7f0c0185 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Tue, 31 Dec 2013 07:58:29 -0500
Subject: [PATCH] More CS fixes, replace global $CONFIG usage with $RCMAIL->config->get()

---
 program/include/rcmail.php |  163 +++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 135 insertions(+), 28 deletions(-)

diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index d430cd3..e85d82c 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -51,6 +51,7 @@
    */
   public $action = '';
   public $comm_path = './';
+  public $filename = '';
 
   private $address_books = array();
   private $action_map = array();
@@ -65,12 +66,13 @@
   /**
    * This implements the 'singleton' design pattern
    *
+   * @param string Environment name to run (e.g. live, dev, test)
    * @return rcmail The one and only instance
    */
-  static function get_instance()
+  static function get_instance($env = '')
   {
     if (!self::$instance || !is_a(self::$instance, 'rcmail')) {
-      self::$instance = new rcmail();
+      self::$instance = new rcmail($env);
       self::$instance->startup();  // init AFTER object was linked with self::$instance
     }
 
@@ -85,6 +87,10 @@
   protected function startup()
   {
     $this->init(self::INIT_WITH_DB | self::INIT_WITH_PLUGINS);
+
+    // set filename if not index.php
+    if (($basename = basename($_SERVER['SCRIPT_FILENAME'])) && $basename != 'index.php')
+      $this->filename = $basename;
 
     // start session
     $this->session_init();
@@ -257,6 +263,23 @@
 
 
   /**
+   * Return identifier of the address book object
+   *
+   * @param rcube_addressbook Addressbook source object
+   *
+   * @return string Source identifier
+   */
+  public function get_address_book_id($object)
+  {
+    foreach ($this->address_books as $index => $book) {
+      if ($book === $object) {
+        return $index;
+      }
+    }
+  }
+
+
+  /**
    * Return address books list
    *
    * @param boolean True if the address book needs to be writeable
@@ -323,6 +346,44 @@
     return $list;
   }
 
+  /**
+   * Getter for compose responses.
+   * These are stored in local config and user preferences.
+   *
+   * @param boolean True to sort the list alphabetically
+   * @param boolean True if only this user's responses shall be listed
+   * @return array List of the current user's stored responses
+   */
+  public function get_compose_responses($sorted = false, $user_only = false)
+  {
+    $responses = array();
+
+    if (!$user_only) {
+      foreach ($this->config->get('compose_responses_static', array()) as $response) {
+        if (empty($response['key']))
+          $response['key'] = substr(md5($response['name']), 0, 16);
+        $response['static'] = true;
+        $response['class'] = 'readonly';
+        $k = $sorted ? '0000-' . strtolower($response['name']) : $response['key'];
+        $responses[$k] = $response;
+      }
+    }
+
+    foreach ($this->config->get('compose_responses', array()) as $response) {
+      if (empty($response['key']))
+        $response['key'] = substr(md5($response['name']), 0, 16);
+      $k = $sorted ? strtolower($response['name']) : $response['key'];
+      $responses[$k] = $response;
+    }
+
+    // sort list by name
+    if ($sorted) {
+      ksort($responses, SORT_LOCALE_STRING);
+    }
+
+    return array_values($responses);
+  }
+
 
   /**
    * Init output object for GUI and add common scripts.
@@ -351,6 +412,9 @@
     $this->output->set_env('action', $this->action);
     $this->output->set_env('comm_path', $this->comm_path);
     $this->output->set_charset(RCUBE_CHARSET);
+
+    if ($this->user && $this->user->ID)
+      $this->output->set_env('user_id', $this->user->get_hash());
 
     // add some basic labels to client
     $this->output->add_label('loading', 'servererror', 'requesttimedout', 'refreshing');
@@ -458,15 +522,22 @@
         $port = $config['default_port'];
     }
 
-    /* Modify username with domain if required
-       Inspired by Marco <P0L0_notspam_binware.org>
-    */
-    // Check if we need to add domain
-    if (!empty($config['username_domain']) && strpos($username, '@') === false) {
-      if (is_array($config['username_domain']) && isset($config['username_domain'][$host]))
-        $username .= '@'.rcube_utils::parse_host($config['username_domain'][$host], $host);
-      else if (is_string($config['username_domain']))
-        $username .= '@'.rcube_utils::parse_host($config['username_domain'], $host);
+    // Check if we need to add/force domain to username
+    if (!empty($config['username_domain'])) {
+      $domain = is_array($config['username_domain']) ? $config['username_domain'][$host] : $config['username_domain'];
+
+      if ($domain = rcube_utils::parse_host((string)$domain, $host)) {
+        $pos = strpos($username, '@');
+
+        // force configured domains
+        if (!empty($config['username_domain_forced']) && $pos !== false) {
+          $username = substr($username, 0, $pos) . '@' . $domain;
+        }
+        // just add domain if not specified
+        else if ($pos === false) {
+          $username .= '@' . $domain;
+        }
+      }
     }
 
     if (!isset($config['login_lc'])) {
@@ -724,7 +795,7 @@
     $p['_task'] = $task;
     unset($p['task']);
 
-    $url = './';
+    $url = './' . $this->filename;
     $delm = '?';
     foreach (array_reverse($p) as $key => $val) {
       if ($val !== '' && $val !== null) {
@@ -909,6 +980,19 @@
 
 
     /**
+     * Set environment variables for specified config options
+     */
+    public function set_env_config($options)
+    {
+        foreach ((array) $options as $option) {
+            if ($this->config->get($option)) {
+                $this->output->set_env($option, true);
+            }
+        }
+    }
+
+
+    /**
      * Returns RFC2822 formatted current date in user's timezone
      *
      * @return string Date
@@ -931,22 +1015,32 @@
     /**
      * Write login data (name, ID, IP address) to the 'userlogins' log file.
      */
-    public function log_login()
+    public function log_login($user = null, $failed_login = false, $error_code = 0)
     {
         if (!$this->config->get('log_logins')) {
             return;
         }
 
-        $user_name = $this->get_user_name();
-        $user_id   = $this->get_user_id();
+        // failed login
+        if ($failed_login) {
+            $message = sprintf('Failed login for %s from %s in session %s (error: %d)',
+                $user, rcube_utils::remote_ip(), session_id(), $error_code);
+        }
+        // successful login
+        else {
+            $user_name = $this->get_user_name();
+            $user_id   = $this->get_user_id();
 
-        if (!$user_id) {
-            return;
+            if (!$user_id) {
+                return;
+            }
+
+            $message = sprintf('Successful login for %s (ID: %d) from %s in session %s',
+                    $user_name, $user_id, rcube_utils::remote_ip(), session_id());
         }
 
-        self::write_log('userlogins',
-            sprintf('Successful login for %s (ID: %d) from %s in session %s',
-                $user_name, $user_id, rcube_utils::remote_ip(), session_id()));
+        // log login
+        self::write_log('userlogins', $message);
     }
 
 
@@ -1201,13 +1295,22 @@
         }
         else {
             $js_mailboxlist = array();
-            $out = html::tag('ul', $attrib, $rcmail->render_folder_tree_html($a_mailboxes, $mbox_name, $js_mailboxlist, $attrib), html::$common_attrib);
+            $tree = $rcmail->render_folder_tree_html($a_mailboxes, $mbox_name, $js_mailboxlist, $attrib);
 
-            $rcmail->output->include_script('treelist.js');
-            $rcmail->output->add_gui_object('mailboxlist', $attrib['id']);
+            if ($type != 'js') {
+                $out = html::tag('ul', $attrib, $tree, html::$common_attrib);
+
+                $rcmail->output->include_script('treelist.js');
+                $rcmail->output->add_gui_object('mailboxlist', $attrib['id']);
+                $rcmail->output->set_env('unreadwrap', $attrib['unreadwrap']);
+                $rcmail->output->set_env('collapsed_folders', (string)$rcmail->config->get('collapsed_folders'));
+            }
+
             $rcmail->output->set_env('mailboxes', $js_mailboxlist);
-            $rcmail->output->set_env('unreadwrap', $attrib['unreadwrap']);
-            $rcmail->output->set_env('collapsed_folders', (string)$rcmail->config->get('collapsed_folders'));
+
+            // we can't use object keys in javascript because they are unordered
+            // we need sorted folders list for folder-selector widget
+            $rcmail->output->set_env('mailboxes_list', array_keys($js_mailboxlist));
         }
 
         return $out;
@@ -1392,8 +1495,12 @@
             $jslist[$folder['id']] = array(
                 'id'      => $folder['id'],
                 'name'    => $foldername,
-                'virtual' => $folder['virtual']
+                'virtual' => $folder['virtual'],
             );
+
+            if (!empty($folder_class)) {
+                $jslist[$folder['id']]['class'] = $folder_class;
+            }
 
             if (!empty($folder['folders'])) {
                 $out .= html::tag('ul', array('style' => ($is_collapsed ? "display:none;" : null)),
@@ -1549,7 +1656,7 @@
 
         $rcmail->output->add_script('rcmail.set_quota('.rcube_output::json_serialize($quota).');', 'docready');
 
-        return html::span($attrib, '');
+        return html::span($attrib, '&nbsp;');
     }
 
 
@@ -1776,7 +1883,7 @@
         }
 
         $this->output->set_env('max_filesize', $max_filesize);
-        $max_filesize = self::show_bytes($max_filesize);
+        $max_filesize = $this->show_bytes($max_filesize);
         $this->output->set_env('filesizeerror', $this->gettext(array(
             'name' => 'filesizeerror', 'vars' => array('size' => $max_filesize))));
 

--
Gitblit v1.9.1