source: subversion/trunk/roundcubemail/program/steps/mail/get.inc @ 5143

Last change on this file since 5143 was 5143, checked in by thomasb, 21 months ago

Add plugin hook message_part_get for message part downloads

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/steps/mail/get.inc                                            |
6 |                                                                       |
7 | This file is part of the Roundcube Webmail client                     |
8 | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
9 | Licensed under the GNU GPL                                            |
10 |                                                                       |
11 | PURPOSE:                                                              |
12 |   Delivering a specific part of a mail message                        |
13 |                                                                       |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16 +-----------------------------------------------------------------------+
17
18 $Id$
19
20*/
21
22
23// show loading page
24if (!empty($_GET['_preload'])) {
25  $url = str_replace('&_preload=1', '', $_SERVER['REQUEST_URI']);
26  $message = rcube_label('loadingdata');
27
28  header('Content-Type: text/html; charset=' . RCMAIL_CHARSET);
29  print "<html>\n<head>\n"
30        . '<meta http-equiv="refresh" content="0; url='.Q($url).'">' . "\n"
31        . '<meta http-equiv="content-type" content="text/html; charset='.RCMAIL_CHARSET.'">' . "\n"
32        . "</head>\n<body>\n$message\n</body>\n</html>";
33  exit;
34}
35
36ob_end_clean();
37
38// Now we need IMAP connection
39if (!$RCMAIL->imap_connect()) {
40  // Get action is often executed simultanously.
41  // Some servers have MAXPERIP or other limits.
42  // To workaround this we'll wait for some time
43  // and try again (once).
44  // Note: Random sleep interval is used to minimize concurency
45  // in getting message parts
46  if (!isset($_GET['_redirected'])) {
47    usleep(rand(10,30)*100000); // 1-3 sec.
48    header('Location: ' . $_SERVER['REQUEST_URI'] . '&_redirected=1');
49  }
50  else {
51    raise_error(array(
52      'code' => 500, 'type' => 'php',
53      'file' => __FILE__, 'line' => __LINE__,
54      'message' => 'Unable to get/display message part. IMAP connection error'),
55      true, true);
56  }
57  // Don't kill session, just quit (#1486995)
58  exit;
59}
60
61// similar code as in program/steps/mail/show.inc
62if (!empty($_GET['_uid'])) {
63  $RCMAIL->config->set('prefer_html', true);
64  $MESSAGE = new rcube_message(get_input_value('_uid', RCUBE_INPUT_GET));
65}
66
67send_nocacheing_headers();
68
69// show part page
70if (!empty($_GET['_frame'])) {
71  $OUTPUT->send('messagepart');
72  exit;
73}
74
75else if ($pid = get_input_value('_part', RCUBE_INPUT_GET)) {
76
77  if ($part = $MESSAGE->mime_parts[$pid]) {
78    $ctype_primary = strtolower($part->ctype_primary);
79    $ctype_secondary = strtolower($part->ctype_secondary);
80    $mimetype = sprintf('%s/%s', $ctype_primary, $ctype_secondary);
81
82    // allow post-processing of the message body
83    $plugin = $RCMAIL->plugins->exec_hook('message_part_get',
84      array('id' => $part->mime_id, 'mimetype' => $mimetype, 'part' => $part, 'download' => !empty($_GET['_download'])));
85
86    if ($plugin['abort'])
87      exit;
88
89    // overwrite modified vars from plugin
90    $mimetype = $plugin['mimetype'];
91    list($ctype_primary, $ctype_secondary) = explode('/', $mimetype);
92    if ($plugin['body'])
93      $part->body = $plugin['body'];
94
95    $browser = $RCMAIL->output->browser;
96
97    // send download headers
98    if ($plugin['download']) {
99      header("Content-Type: application/octet-stream");
100      if ($browser->ie)
101        header("Content-Type: application/force-download");
102    }
103    else if ($ctype_primary == 'text') {
104      header("Content-Type: text/$ctype_secondary; charset=" . ($part->charset ? $part->charset : RCMAIL_CHARSET));
105    }
106    else {
107      $mimetype = rcmail_fix_mimetype($mimetype);
108      header("Content-Type: $mimetype");
109      header("Content-Transfer-Encoding: binary");
110    }
111
112    // deliver part content
113    if ($ctype_primary == 'text' && $ctype_secondary == 'html' && empty($plugin['download'])) {
114      // get part body if not available
115      if (!$part->body)
116        $part->body = $MESSAGE->get_part_content($part->mime_id);
117
118      $OUTPUT = new rcube_html_page();
119      $OUTPUT->write(rcmail_print_body($part, array('safe' => $MESSAGE->is_safe, 'inline_html' => false)));
120    }
121    else {
122      // don't kill the connection if download takes more than 30 sec.
123      @set_time_limit(0);
124
125      $filename = $part->filename ? $part->filename : ($MESSAGE->subject ? $MESSAGE->subject : 'roundcube') . '.'.$ctype_secondary;
126      $filename = preg_replace('[\r\n]', '', $filename);
127
128      if ($browser->ie && $browser->ver < 7)
129        $filename = rawurlencode(abbreviate_string($filename, 55));
130      else if ($browser->ie)
131        $filename = rawurlencode($filename);
132      else
133        $filename = addcslashes($filename, '"');
134
135      $disposition = !empty($plugin['download']) ? 'attachment' : 'inline';
136
137      header("Content-Disposition: $disposition; filename=\"$filename\"");
138
139      // turn off output buffering and print part content
140      if ($part->body)
141        echo $part->body;
142      else if ($part->size)
143        $IMAP->get_message_part($MESSAGE->uid, $part->mime_id, $part, true);
144    }
145
146    exit;
147  }
148}
149
150// print message
151else {
152  // send correct headers for content type
153  header("Content-Type: text/html");
154
155  $cont = "<html>\n<head><title></title>\n</head>\n<body>";
156  $cont .= rcmail_message_body(array());
157  $cont .= "\n</body>\n</html>";
158
159  $OUTPUT = new rcube_html_page();
160  $OUTPUT->write($cont);
161
162  exit;
163}
164
165
166// if we arrive here, the requested part was not found
167header('HTTP/1.1 404 Not Found');
168exit;
169
170
Note: See TracBrowser for help on using the repository browser.