From 840b4dbeb81603ed44c30dc77be8eff98d7667c3 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Tue, 10 Jul 2012 14:30:34 -0400
Subject: [PATCH] Simplified method of getting default addressbook. Make sure to use the same source when adding contact and checking if message is safe (sender is in addressbook). Small code improvements.

---
 program/include/rcmail.php        |   36 ++++++++++-------
 program/steps/mail/func.inc       |   29 ++++++++------
 program/steps/mail/addcontact.inc |   13 +-----
 3 files changed, 40 insertions(+), 38 deletions(-)

diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index 63ae8e2..c701702 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -171,7 +171,7 @@
   /**
    * Return instance of the internal address book class
    *
-   * @param string  Address book identifier
+   * @param string  Address book identifier (-1 for default addressbook)
    * @param boolean True if the address book needs to be writeable
    *
    * @return rcube_contacts Address book object
@@ -180,17 +180,17 @@
   {
     $contacts    = null;
     $ldap_config = (array)$this->config->get('ldap_public');
-    $abook_type  = strtolower($this->config->get('address_book_type'));
 
     // 'sql' is the alias for '0' used by autocomplete
     if ($id == 'sql')
-        $id = '0';
+      $id = '0';
+    else if ($id == -1) {
+      $id = $this->config->get('default_addressbook');
+      $default = true;
+    }
 
     // use existing instance
-    if (isset($this->address_books[$id]) && is_object($this->address_books[$id])
-      && is_a($this->address_books[$id], 'rcube_addressbook')
-      && (!$writeable || !$this->address_books[$id]->readonly)
-    ) {
+    if (isset($this->address_books[$id]) && ($this->address_books[$id] instanceof rcube_addressbook)) {
       $contacts = $this->address_books[$id];
     }
     else if ($id && $ldap_config[$id]) {
@@ -206,14 +206,16 @@
       if ($plugin['instance'] instanceof rcube_addressbook) {
         $contacts = $plugin['instance'];
       }
-      // get first source from the list
-      else if (!$id) {
-        $source = reset($this->get_address_sources($writeable));
-        if (!empty($source)) {
-          $contacts = $this->get_address_book($source['id']);
-          if ($contacts)
-            $id = $source['id'];
-        }
+    }
+
+    // Get first addressbook from the list if configured default doesn't exist
+    // This can happen when user deleted the addressbook (e.g. Kolab folder)
+    if (!$contacts && (!$id || $default)) {
+      $source = reset($this->get_address_sources($writeable));
+      if (!empty($source)) {
+        $contacts = $this->get_address_book($source['id']);
+        if ($contacts)
+          $id = $source['id'];
       }
     }
 
@@ -225,6 +227,10 @@
         true, true);
     }
 
+    if ($writeable && $contacts->readonly) {
+      return null;
+    }
+
     // set configured sort order
     if ($sort_col = $this->config->get('addressbook_sort_col'))
         $contacts->set_sort_order($sort_col);
diff --git a/program/steps/mail/addcontact.inc b/program/steps/mail/addcontact.inc
index 2dc406f..3805577 100644
--- a/program/steps/mail/addcontact.inc
+++ b/program/steps/mail/addcontact.inc
@@ -23,17 +23,8 @@
 if (!$OUTPUT->ajax_call)
   return;
 
-$abook = $RCMAIL->config->get('default_addressbook');
-
-// Get configured addressbook
-$CONTACTS = $RCMAIL->get_address_book($abook, true);
-
-// Get first writeable addressbook if the configured doesn't exist
-// This can happen when user deleted the addressbook (e.g. Kolab folder)
-if ($abook == null || !is_object($CONTACTS)) {
-  $source = reset($RCMAIL->get_address_sources(true));
-  $CONTACTS = $RCMAIL->get_address_book($source['id'], true);
-}
+// Get default addressbook
+$CONTACTS = $RCMAIL->get_address_book(-1, true);
 
 if (!empty($_POST['_address']) && is_object($CONTACTS))
 {
diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index 5a18ded..21fa3e8 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -518,21 +518,26 @@
 {
   global $RCMAIL;
 
-  $show_images = $RCMAIL->config->get('show_images');
   if (!$message->is_safe
-    && !empty($show_images)
-    && $message->has_html_part())
-  {
-    switch($show_images) {
-      case '1': // known senders only
-        $CONTACTS = new rcube_contacts($RCMAIL->db, $_SESSION['user_id']);
-        if ($CONTACTS->search('email', $message->sender['mailto'], true, false)->count) {
-          $message->set_safe(true);
+    && ($show_images = $RCMAIL->config->get('show_images'))
+    && $message->has_html_part()
+  ) {
+    switch ($show_images) {
+      case 1: // known senders only
+        // get default addressbook, like in addcontact.inc
+        $CONTACTS = $RCMAIL->get_address_book(-1, true);
+
+        if ($CONTACTS) {
+          $result = $CONTACTS->search('email', $message->sender['mailto'], 1, false);
+          if ($result->count) {
+            $message->set_safe(true);
+          }
         }
-      break;
-      case '2': // always
+        break;
+
+      case 2: // always
         $message->set_safe(true);
-      break;
+        break;
     }
   }
 }

--
Gitblit v1.9.1