Aleksander Machniak
2016-03-28 46f7b7096450939fe03c95aa81ce06ae4bfca89d
commit | author | age
4e17e6 1 <?php
T 2
a95874 3 /**
4e17e6 4  +-----------------------------------------------------------------------+
T 5  | program/steps/mail/show.inc                                           |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
f5d2ee 8  | Copyright (C) 2005-2013, The Roundcube Dev Team                       |
7fe381 9  |                                                                       |
T 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.                     |
4e17e6 13  |                                                                       |
T 14  | PURPOSE:                                                              |
15  |   Display a mail message similar as a usual mail application does     |
16  |                                                                       |
17  +-----------------------------------------------------------------------+
18  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19  +-----------------------------------------------------------------------+
20 */
21
323fa2 22 $PRINT_MODE = $RCMAIL->action == 'print' ? true : false;
4e17e6 23
e349a8 24 // Read browser capabilities and store them in session
6b2b2e 25 if ($caps = rcube_utils::get_input_value('_caps', rcube_utils::INPUT_GET)) {
f5d2ee 26     $browser_caps = array();
AM 27     foreach (explode(',', $caps) as $cap) {
28         $cap = explode('=', $cap);
29         $browser_caps[$cap[0]] = $cap[1];
30     }
31     $_SESSION['browser_caps'] = $browser_caps;
e349a8 32 }
AM 33
323fa2 34 $msg_id    = rcube_utils::get_input_value('_uid', rcube_utils::INPUT_GET);
AM 35 $uid       = preg_replace('/\.[0-9.]+$/', '', $msg_id);
a02c77 36 $mbox_name = $RCMAIL->storage->get_folder();
AM 37
4e17e6 38 // similar code as in program/steps/mail/get.inc
a02c77 39 if ($uid) {
f5d2ee 40     // set message format (need to be done before rcube_message construction)
AM 41     if (!empty($_GET['_format'])) {
42         $prefer_html = $_GET['_format'] == 'html';
43         $RCMAIL->config->set('prefer_html', $prefer_html);
44         $_SESSION['msg_formats'][$mbox_name.':'.$uid] = $prefer_html;
19cc5b 45     }
f5d2ee 46     else if (isset($_SESSION['msg_formats'][$mbox_name.':'.$uid])) {
AM 47         $RCMAIL->config->set('prefer_html', $_SESSION['msg_formats'][$mbox_name.':'.$uid]);
0ea884 48     }
4e17e6 49
323fa2 50     $MESSAGE = new rcube_message($msg_id, $mbox_name, intval($_GET['_safe']));
f5d2ee 51
AM 52     // if message not found (wrong UID)...
53     if (empty($MESSAGE->headers)) {
54         rcmail_message_error($uid);
55     }
56
57     // show images?
58     rcmail_check_safe($MESSAGE);
59
60     // set message charset as default
61     if (!empty($MESSAGE->headers->charset)) {
62         $RCMAIL->storage->set_charset($MESSAGE->headers->charset);
63     }
64
65     $OUTPUT->set_pagetitle(abbreviate_string($MESSAGE->subject, 128, '...', true));
66
67     // set message environment
323fa2 68     $OUTPUT->set_env('uid', $msg_id);
f5d2ee 69     $OUTPUT->set_env('safemode', $MESSAGE->is_safe);
46f7b7 70     $OUTPUT->set_env('message_context', $MESSAGE->context);
f5d2ee 71     $OUTPUT->set_env('sender', $MESSAGE->sender['string']);
AM 72     $OUTPUT->set_env('mailbox', $mbox_name);
d56091 73     $OUTPUT->set_env('username', $RCMAIL->get_user_name());
ce3105 74     $OUTPUT->set_env('permaurl', $RCMAIL->url(array('_action' => 'show', '_uid' => $msg_id, '_mbox' => $mbox_name)));
f5d2ee 75
AM 76     if ($MESSAGE->headers->get('list-post', false)) {
77         $OUTPUT->set_env('list_post', true);
78     }
79
80     // set environment
81     $OUTPUT->set_env('delimiter', $RCMAIL->storage->get_hierarchy_delimiter());
82
83     // set configuration
84     $RCMAIL->set_env_config(array('delete_junk', 'flag_for_deletion', 'read_when_deleted',
d47833 85         'skip_deleted', 'display_next', 'forward_attachment'));
f5d2ee 86
AM 87     // set special folders
88     foreach (array('drafts', 'trash', 'junk') as $mbox) {
89         if ($folder = $RCMAIL->config->get($mbox . '_mbox')) {
90             $OUTPUT->set_env($mbox . '_mailbox', $folder);
91         }
92     }
93
94     // mimetypes supported by the browser (default settings)
95     $mimetypes = (array)$RCMAIL->config->get('client_mimetypes');
96
97     // Remove unsupported types, which makes that attachment which cannot be
98     // displayed in a browser will be downloaded directly without displaying an overlay page
99     if (empty($_SESSION['browser_caps']['pdf']) && ($key = array_search('application/pdf', $mimetypes)) !== false) {
100         unset($mimetypes[$key]);
101     }
102     if (empty($_SESSION['browser_caps']['flash']) && ($key = array_search('application/x-shockwave-flash', $mimetypes)) !== false) {
103         unset($mimetypes[$key]);
104     }
105     if (empty($_SESSION['browser_caps']['tif']) && ($key = array_search('image/tiff', $mimetypes)) !== false) {
106         // we can convert tiff to jpeg
8968f9 107         if (!rcube_image::is_convertable('image/tiff')) {
f5d2ee 108             unset($mimetypes[$key]);
AM 109         }
323fa2 110     }
AM 111     if (!in_array('message/rfc822', $mimetypes)) {
112         $mimetypes[] = 'message/rfc822';
f5d2ee 113     }
AM 114
115     $OUTPUT->set_env('mimetypes', array_values($mimetypes));
116
117     if ($MESSAGE->has_html_part()) {
118         $prefer_html = $RCMAIL->config->get('prefer_html');
119         $OUTPUT->set_env('optional_format', $prefer_html ? 'text' : 'html');
120     }
121
122     if (!$OUTPUT->ajax_call) {
123         $OUTPUT->add_label('checkingmail', 'deletemessage', 'movemessagetotrash',
124             'movingmessage', 'deletingmessage', 'markingmessage', 'replyall', 'replylist');
125     }
126
127     // check for unset disposition notification
128     if ($MESSAGE->headers->mdn_to
46f7b7 129         && $MESSAGE->context === null
f5d2ee 130         && empty($MESSAGE->headers->flags['MDNSENT'])
AM 131         && empty($MESSAGE->headers->flags['SEEN'])
132         && ($RCMAIL->storage->check_permflag('MDNSENT') || $RCMAIL->storage->check_permflag('*'))
133         && $mbox_name != $RCMAIL->config->get('drafts_mbox')
134         && $mbox_name != $RCMAIL->config->get('sent_mbox')
135     ) {
136         $mdn_cfg = intval($RCMAIL->config->get('mdn_requests'));
137
138         if ($mdn_cfg == 1 || (($mdn_cfg == 3 || $mdn_cfg ==  4) && rcmail_contact_exists($MESSAGE->sender['mailto']))) {
139             // Send MDN
140             if (rcmail_send_mdn($MESSAGE, $smtp_error))
141                 $OUTPUT->show_message('receiptsent', 'confirmation');
142             else if ($smtp_error)
143                 $OUTPUT->show_message($smtp_error['label'], 'error', $smtp_error['vars']);
144             else
145                 $OUTPUT->show_message('errorsendingreceipt', 'error');
146         }
147         else if ($mdn_cfg != 2 && $mdn_cfg != 4) {
148             // Ask user
149             $OUTPUT->add_label('mdnrequest');
150             $OUTPUT->set_env('mdn_request', true);
151         }
152     }
153
154     if (empty($MESSAGE->headers->flags['SEEN'])
323fa2 155         && $MESSAGE->context === null
f5d2ee 156         && ($RCMAIL->action == 'show' || ($RCMAIL->action == 'preview' && intval($RCMAIL->config->get('preview_pane_mark_read')) == 0))
AM 157     ) {
d28dae 158         $RCMAIL->output->command('set_unread_message', $MESSAGE->uid, $mbox_name);
f5d2ee 159         $RCMAIL->plugins->exec_hook('message_read', array(
AM 160             'uid'     => $MESSAGE->uid,
161             'mailbox' => $mbox_name,
162             'message' => $MESSAGE,
163         ));
d28dae 164
AM 165         $set_seen_flag = true;
f5d2ee 166     }
cc97ea 167 }
f5d2ee 168
AM 169
170 $OUTPUT->add_handlers(array(
171     'messageattachments' => 'rcmail_message_attachments',
172     'mailboxname'        => 'rcmail_mailbox_name_display',
173     'messageobjects'     => 'rcmail_message_objects',
174     'contactphoto'       => 'rcmail_message_contactphoto',
175 ));
176
177
178 if ($RCMAIL->action == 'print' && $OUTPUT->template_exists('messageprint'))
179     $OUTPUT->send('messageprint', false);
180 else if ($RCMAIL->action == 'preview' && $OUTPUT->template_exists('messagepreview'))
181     $OUTPUT->send('messagepreview', false);
182 else
183     $OUTPUT->send('message', false);
184
185
186 // mark message as read
d28dae 187 if (!empty($set_seen_flag)) {
cc6c7e 188     if ($RCMAIL->storage->set_flag($MESSAGE->uid, 'SEEN', $mbox_name)) {
f5d2ee 189         if ($count = rcmail_get_unseen_count($mbox_name)) {
AM 190             rcmail_set_unseen_count($mbox_name, $count - 1);
191         }
192     }
193 }
194
1aa0c8 195 // Save preview_pane preference, if not set yet (#1490362)
AM 196 if ($RCMAIL->action == 'preview' && !$RCMAIL->config->get('preview_pane')) {
197     $RCMAIL->user->save_prefs(array('preview_pane' => true));
198 }
4e17e6 199
1aa0c8 200 exit;
4e17e6 201
T 202
203 function rcmail_message_attachments($attrib)
8fa58e 204 {
f5d2ee 205     global $PRINT_MODE, $MESSAGE, $RCMAIL;
83ba22 206
f5d2ee 207     $out = $ol = '';
AM 208     $attachments = array();
4e17e6 209
f5d2ee 210     if (sizeof($MESSAGE->attachments)) {
AM 211         foreach ($MESSAGE->attachments as $attach_prop) {
212             $filename = rcmail_attachment_name($attach_prop, true);
1881a8 213             $filesize = $RCMAIL->message_part_size($attach_prop);
203ee4 214
f5d2ee 215             if ($PRINT_MODE) {
1881a8 216                 $ol .= html::tag('li', null, rcube::Q(sprintf("%s (%s)", $filename, $filesize)));
f5d2ee 217             }
AM 218             else {
219                 if ($attrib['maxlength'] && mb_strlen($filename) > $attrib['maxlength']) {
220                     $title    = $filename;
221                     $filename = abbreviate_string($filename, $attrib['maxlength']);
222                 }
223                 else {
224                     $title = '';
225                 }
226
b2992d 227                 if ($attach_prop->size) {
1881a8 228                     $size = ' ' . html::span('attachment-size', '(' . rcube::Q($filesize) . ')');
b2992d 229                 }
TB 230
f5d2ee 231                 $mimetype = rcmail_fix_mimetype($attach_prop->mimetype);
AM 232                 $class    = rcube_utils::file2class($mimetype, $filename);
233                 $id       = 'attach' . $attach_prop->mime_id;
234                 $link     = html::a(array(
235                     'href'        => $MESSAGE->get_part_url($attach_prop->mime_id, false),
236                     'onclick'     => sprintf('return %s.command(\'load-attachment\',\'%s\',this)',
237                         rcmail_output::JS_OBJECT_NAME, $attach_prop->mime_id),
238                     'onmouseover' => $title ? '' : 'rcube_webmail.long_subject_title_ex(this, 0)',
239                     'title'       => rcube::Q($title),
b2992d 240                     ), rcube::Q($filename) . $size);
f5d2ee 241
AM 242                 $ol .= html::tag('li', array('class' => $class, 'id' => $id), $link);
243
244                 $attachments[$attach_prop->mime_id] = $mimetype;
245             }
0c2596 246         }
A 247
f5d2ee 248         $out = html::tag('ul', $attrib, $ol, html::$common_attrib);
bc2c43 249
f5d2ee 250         $RCMAIL->output->set_env('attachments', $attachments);
d56091 251         $RCMAIL->output->add_gui_object('attachments', $attrib['id']);
8fa58e 252     }
4e17e6 253
f5d2ee 254     return $out;
8fa58e 255 }
4e17e6 256
46cdbf 257 function rcmail_remote_objects_msg()
62e43d 258 {
f5d2ee 259     global $MESSAGE, $RCMAIL;
83ba22 260
f5d2ee 261     $attrib['id']    = 'remote-objects-message';
AM 262     $attrib['class'] = 'notice';
263     $attrib['style'] = 'display: none';
83ba22 264
f5d2ee 265     $msg = rcube::Q($RCMAIL->gettext('blockedimages')) . '&nbsp;';
AM 266     $msg .= html::a(array(
267             'href'    => "#loadimages",
268             'onclick' => rcmail_output::JS_OBJECT_NAME.".command('load-images')"
269         ),
270         rcube::Q($RCMAIL->gettext('showimages')));
83ba22 271
f5d2ee 272     // add link to save sender in addressbook and reload message
AM 273     if ($MESSAGE->sender['mailto'] && $RCMAIL->config->get('show_images') == 1) {
274         $msg .= ' ' . html::a(array(
275                 'href'    => "#alwaysload",
276                 'onclick' => rcmail_output::JS_OBJECT_NAME.".command('always-load')",
277                 'style'   => "white-space:nowrap"
278             ),
279             rcube::Q($RCMAIL->gettext(array('name' => 'alwaysshow', 'vars' => array('sender' => $MESSAGE->sender['mailto'])))));
280     }
83ba22 281
f5d2ee 282     $RCMAIL->output->add_gui_object('remoteobjectsmsg', $attrib['id']);
AM 283
284     return html::div($attrib, $msg);
46cdbf 285 }
A 286
287 function rcmail_message_buttons()
288 {
cd4e50 289     global $RCMAIL, $MESSAGE;
46cdbf 290
f5d2ee 291     $delim = $RCMAIL->storage->get_hierarchy_delimiter();
AM 292     $dbox  = $RCMAIL->config->get('drafts_mbox');
46cdbf 293
f5d2ee 294     // the message is not a draft
cd4e50 295     if ($MESSAGE->folder != $dbox && strpos($MESSAGE->folder, $dbox.$delim) !== 0) {
f5d2ee 296         return '';
AM 297     }
46cdbf 298
f5d2ee 299     $attrib['id']    = 'message-buttons';
AM 300     $attrib['class'] = 'notice';
46cdbf 301
f5d2ee 302     $msg = rcube::Q($RCMAIL->gettext('isdraft')) . '&nbsp;';
AM 303     $msg .= html::a(array(
304             'href'    => "#edit",
305             'onclick' => rcmail_output::JS_OBJECT_NAME.".command('edit')"
306         ),
307         rcube::Q($RCMAIL->gettext('edit')));
46cdbf 308
f5d2ee 309     return html::div($attrib, $msg);
46cdbf 310 }
A 311
312 function rcmail_message_objects($attrib)
313 {
f5d2ee 314     global $RCMAIL, $MESSAGE;
46cdbf 315
f5d2ee 316     if (!$attrib['id'])
AM 317         $attrib['id'] = 'message-objects';
46cdbf 318
f5d2ee 319     $content = array(
AM 320         rcmail_message_buttons(),
321         rcmail_remote_objects_msg(),
322     );
46cdbf 323
f5d2ee 324     $plugin = $RCMAIL->plugins->exec_hook('message_objects',
AM 325         array('content' => $content, 'message' => $MESSAGE));
46cdbf 326
f5d2ee 327     $content = implode("\n", $plugin['content']);
46cdbf 328
f5d2ee 329     return html::div($attrib, $content);
a99968 330 }
A 331
332 function rcmail_contact_exists($email)
333 {
f5d2ee 334     global $RCMAIL;
a99968 335
f5d2ee 336     if ($email) {
AM 337         // @TODO: search in all address books?
338         $CONTACTS = $RCMAIL->get_address_book(-1, true);
ae80b5 339
f5d2ee 340         if (is_object($CONTACTS)) {
8716fc 341             $existing = $CONTACTS->search('email', $email, 1, false);
f5d2ee 342             if ($existing->count) {
AM 343                 return true;
344             }
345         }
ae80b5 346     }
a99968 347
f5d2ee 348     return false;
62e43d 349 }
4e17e6 350
384948 351 function rcmail_message_contactphoto($attrib)
TB 352 {
f5d2ee 353     global $RCMAIL, $MESSAGE;
384948 354
681ba6 355     $placeholder = $attrib['placeholder'] ? $RCMAIL->output->abs_url($attrib['placeholder'], true) : null;
827159 356     $placeholder = $RCMAIL->output->asset_url($placeholder ?: 'program/resources/blank.gif');
384948 357
f5d2ee 358     if ($MESSAGE->sender) {
AM 359         $photo_img = $RCMAIL->url(array(
360             '_task'   => 'addressbook',
361             '_action' => 'photo',
362             '_email'  => $MESSAGE->sender['mailto'],
363         ));
fcb7d4 364
681ba6 365         $attrib['onerror'] = "this.src = '$placeholder'";
b46edc 366     }
f5d2ee 367     else {
681ba6 368         $photo_img = $placeholder;
f5d2ee 369     }
AM 370
7fafb4 371     return html::img(array('src' => $photo_img, 'alt' => $RCMAIL->gettext('contactphoto')) + $attrib);
c64277 372 }