From 384948886c4a1bedb17318af63c944c33ae0b3b6 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Fri, 13 Jul 2012 14:16:52 -0400
Subject: [PATCH] Prepare to show contact photo of email sender in mail views

---
 program/steps/addressbook/show.inc |   32 +++++++++++++++++++++++++++++++-
 1 files changed, 31 insertions(+), 1 deletions(-)

diff --git a/program/steps/addressbook/show.inc b/program/steps/addressbook/show.inc
index e5ff0d7..d7f6f8f 100644
--- a/program/steps/addressbook/show.inc
+++ b/program/steps/addressbook/show.inc
@@ -22,7 +22,7 @@
 // Get contact ID and source ID from request
 $cids   = rcmail_get_cids();
 $source = key($cids);
-$cid    = array_shift($cids[$source]);
+$cid    = $cids ? array_shift($cids[$source]) : null;
 
 // Initialize addressbook source
 $CONTACTS  = rcmail_contact_source($source, true);
@@ -39,6 +39,19 @@
 
 // return raw photo of the given contact
 if ($RCMAIL->action == 'photo') {
+    // search for contact first
+    if (!$record && ($email = get_input_value('_email', RCUBE_INPUT_GPC))) {
+        foreach ($RCMAIL->get_address_sources() as $s) {
+            $abook = $RCMAIL->get_address_book($s['id']);
+            $result = $abook->search(array('email'), $email, 1, true, true, 'photo');
+            while ($result && ($record = $result->iterate())) {
+                if ($record['photo'])
+                    break 2;
+            }
+        }
+    }
+
+    // read the referenced file
     if (($file_id = get_input_value('_photo', RCUBE_INPUT_GPC)) && ($tempfile = $_SESSION['contacts']['files'][$file_id])) {
         $tempfile = $RCMAIL->plugins->exec_hook('attachment_display', $tempfile);
         if ($tempfile['status']) {
@@ -54,6 +67,23 @@
             $data = base64_decode($data, true);
     }
 
+    // let plugins do fancy things with contact photos
+    $plugin = $RCMAIL->plugins->exec_hook('contact_photo', array('record' => $record, 'email' => $email, 'data' => $data));
+
+    // redirect to url provided by a plugin
+    if ($plugin['url'])
+        $RCMAIL->output->redirect($plugin['url']);
+    else
+        $data = $plugin['data'];
+
+    // deliver alt image
+    if (!$data && ($alt_img = get_input_value('_alt', RCUBE_INPUT_GPC)) && is_file($alt_img))
+        $data = file_get_contents($alt_img);
+
+    // cache for one day if requested by email
+    if (!$cid && $email)
+        $RCMAIL->output->future_expire_header(86400);
+
     header('Content-Type: ' . rc_image_content_type($data));
     echo $data ? $data : file_get_contents('program/blank.gif');
     exit;

--
Gitblit v1.9.1