Aleksander Machniak
2015-05-07 6ccd4c54bcc4cb77365defabe8bbe7d10b2620d4
commit | author | age
8799df 1 <?php
AM 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/photo.inc                                   |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2013, The Roundcube Dev Team                       |
9  |                                                                       |
10  | Licensed under the GNU General Public License version 3 or            |
11  | any later version with exceptions for skins & plugins.                |
12  | See the README file for a full license statement.                     |
13  |                                                                       |
14  | PURPOSE:                                                              |
15  |   Show contact photo                                                  |
16  |                                                                       |
17  +-----------------------------------------------------------------------+
18  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19  | Author: Aleksander Machniak <alec@alec.pl>                            |
20  +-----------------------------------------------------------------------+
21 */
22
23 // Get contact ID and source ID from request
24 $cids   = rcmail_get_cids();
25 $source = key($cids);
26 $cid    = $cids ? array_shift($cids[$source]) : null;
27
28 // read the referenced file
6b2b2e 29 if (($file_id = rcube_utils::get_input_value('_photo', rcube_utils::INPUT_GPC)) && ($tempfile = $_SESSION['contacts']['files'][$file_id])) {
8799df 30     $tempfile = $RCMAIL->plugins->exec_hook('attachment_display', $tempfile);
AM 31     if ($tempfile['status']) {
32         if ($tempfile['data'])
33             $data = $tempfile['data'];
34         else if ($tempfile['path'])
35             $data = file_get_contents($tempfile['path']);
36     }
37 }
38 else {
39     // by email, search for contact first
6b2b2e 40     if ($email = rcube_utils::get_input_value('_email', rcube_utils::INPUT_GPC)) {
8799df 41         foreach ($RCMAIL->get_address_sources() as $s) {
AM 42             $abook = $RCMAIL->get_address_book($s['id']);
43             $result = $abook->search(array('email'), $email, 1, true, true, 'photo');
44             while ($result && ($record = $result->iterate())) {
45                 if ($record['photo'])
46                     break 2;
47             }
48         }
49     }
50
51     // by contact id
52     if (!$record && $cid) {
53         // Initialize addressbook source
54         $CONTACTS  = rcmail_contact_source($source, true);
55         $SOURCE_ID = $source;
56         // read contact record
57         $record = $CONTACTS->get_record($cid, true);
58     }
59
60     if ($record['photo']) {
61         $data = is_array($record['photo']) ? $record['photo'][0] : $record['photo'];
62         if (!preg_match('![^a-z0-9/=+-]!i', $data))
63             $data = base64_decode($data, true);
64     }
65 }
66
67 // let plugins do fancy things with contact photos
68 $plugin = $RCMAIL->plugins->exec_hook('contact_photo',
69     array('record' => $record, 'email' => $email, 'data' => $data));
70
71 // redirect to url provided by a plugin
72 if ($plugin['url']) {
73     $RCMAIL->output->redirect($plugin['url']);
74 }
1fac78 75
AM 76 $data = $plugin['data'];
77
78 // detect if photo data is an URL
79 if (strlen($data) < 1024 && filter_var($data, FILTER_VALIDATE_URL)) {
80     $RCMAIL->output->redirect($data);
8799df 81 }
AM 82
83 // cache for one day if requested by email
84 if (!$cid && $email) {
85     $RCMAIL->output->future_expire_header(86400);
86 }
87
6b2b2e 88 header('Content-Type: ' . rcube_mime::image_content_type($data));
8799df 89 echo $data ? $data : file_get_contents('program/resources/blank.gif');
AM 90 exit;