From 8799df8ccdb09114b14742e0493a8ba401688fdf Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Thu, 8 Aug 2013 14:15:30 +0200 Subject: Fix redundant SQL query on contact photo request after photo upload. The query was also invalid in case of uploading photo in contact create form. Move contact photo handling from show.inc into a separate file. --- program/steps/addressbook/photo.inc | 91 +++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 program/steps/addressbook/photo.inc (limited to 'program/steps/addressbook/photo.inc') diff --git a/program/steps/addressbook/photo.inc b/program/steps/addressbook/photo.inc new file mode 100644 index 000000000..658027de4 --- /dev/null +++ b/program/steps/addressbook/photo.inc @@ -0,0 +1,91 @@ + | + | Author: Aleksander Machniak | + +-----------------------------------------------------------------------+ +*/ + +// Get contact ID and source ID from request +$cids = rcmail_get_cids(); +$source = key($cids); +$cid = $cids ? array_shift($cids[$source]) : null; + +// 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']) { + if ($tempfile['data']) + $data = $tempfile['data']; + else if ($tempfile['path']) + $data = file_get_contents($tempfile['path']); + } +} +else { + // by email, search for contact first + if ($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; + } + } + } + + // by contact id + if (!$record && $cid) { + // Initialize addressbook source + $CONTACTS = rcmail_contact_source($source, true); + $SOURCE_ID = $source; + // read contact record + $record = $CONTACTS->get_record($cid, true); + } + + if ($record['photo']) { + $data = is_array($record['photo']) ? $record['photo'][0] : $record['photo']; + if (!preg_match('![^a-z0-9/=+-]!i', $data)) + $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/resources/blank.gif'); +exit; -- cgit v1.2.3