thomascube
2011-08-18 fbe54043cf598b19a753dc2b21a7ed558d23fd15
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/get.inc                                            |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
f5e7b3 8  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 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
8fa58e 24 if (!empty($_GET['_preload'])) {
4e17e6 25   $url = str_replace('&_preload=1', '', $_SERVER['REQUEST_URI']);
T 26   $message = rcube_label('loadingdata');
27
3e8b11 28   header('Content-Type: text/html; charset=' . RCMAIL_CHARSET);
A 29   print "<html>\n<head>\n"
35f40f 30         . '<meta http-equiv="refresh" content="0; url='.Q($url).'">' . "\n"
3e8b11 31         . '<meta http-equiv="content-type" content="text/html; charset='.RCMAIL_CHARSET.'">' . "\n"
A 32         . "</head>\n<body>\n$message\n</body>\n</html>";
4e17e6 33   exit;
8fa58e 34 }
4e17e6 35
d51c93 36 ob_end_clean();
4e17e6 37
431234 38 // Now we need IMAP connection
A 39 if (!$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).
b8d96f 44   // Note: Random sleep interval is used to minimize concurency
A 45   // in getting message parts
431234 46   if (!isset($_GET['_redirected'])) {
b8d96f 47     usleep(rand(10,30)*100000); // 1-3 sec.
431234 48     header('Location: ' . $_SERVER['REQUEST_URI'] . '&_redirected=1');
A 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
4e17e6 61 // similar code as in program/steps/mail/show.inc
8fa58e 62 if (!empty($_GET['_uid'])) {
T 63   $RCMAIL->config->set('prefer_html', true);
64   $MESSAGE = new rcube_message(get_input_value('_uid', RCUBE_INPUT_GET));
65 }
4e17e6 66
ebc619 67 send_nocacheing_headers();
4e17e6 68
T 69 // show part page
8fa58e 70 if (!empty($_GET['_frame'])) {
47124c 71   $OUTPUT->send('messagepart');
4e17e6 72   exit;
8fa58e 73 }
4e17e6 74
8fa58e 75 else if ($pid = get_input_value('_part', RCUBE_INPUT_GET)) {
d311d8 76
8fa58e 77   if ($part = $MESSAGE->mime_parts[$pid]) {
4e17e6 78     $ctype_primary = strtolower($part->ctype_primary);
T 79     $ctype_secondary = strtolower($part->ctype_secondary);
80     $mimetype = sprintf('%s/%s', $ctype_primary, $ctype_secondary);
9bb1fc 81
799359 82     $browser = $RCMAIL->output->browser;
4e17e6 83
5a6ad2 84     // send download headers
8fa58e 85     if ($_GET['_download']) {
4e17e6 86       header("Content-Type: application/octet-stream");
0ced2b 87       if ($browser->ie)
T 88         header("Content-Type: application/force-download");
8fa58e 89     }
0dbac3 90     else if ($ctype_primary == 'text') {
0ced2b 91       header("Content-Type: text/$ctype_secondary; charset=" . ($part->charset ? $part->charset : RCMAIL_CHARSET));
0dbac3 92     }
T 93     else {
e0bd70 94       $mimetype = rcmail_fix_mimetype($mimetype);
4e17e6 95       header("Content-Type: $mimetype");
0dbac3 96       header("Content-Transfer-Encoding: binary");
8fa58e 97     }
4b0f65 98
4e17e6 99     // deliver part content
1097a3 100     if ($ctype_primary == 'text' && $ctype_secondary == 'html' && empty($_GET['_download'])) {
8d4bcd 101       // get part body if not available
T 102       if (!$part->body)
8fa58e 103         $part->body = $MESSAGE->get_part_content($part->mime_id);
8d4bcd 104
f7bfec 105       $OUTPUT = new rcube_html_page();
21e724 106       $OUTPUT->write(rcmail_print_body($part, array('safe' => $MESSAGE->is_safe, 'inline_html' => false)));
8fa58e 107     }
T 108     else {
0ced2b 109       // don't kill the connection if download takes more than 30 sec.
6d479a 110       @set_time_limit(0);
9bb1fc 111
0ced2b 112       $filename = $part->filename ? $part->filename : ($MESSAGE->subject ? $MESSAGE->subject : 'roundcube') . '.'.$ctype_secondary;
9bb1fc 113       $filename = preg_replace('[\r\n]', '', $filename);
A 114
e943e1 115       if ($browser->ie && $browser->ver < 7)
A 116         $filename = rawurlencode(abbreviate_string($filename, 55));
117       else if ($browser->ie)
118         $filename = rawurlencode($filename);
119       else
120         $filename = addcslashes($filename, '"');
9bb1fc 121
0ced2b 122       $disposition = !empty($_GET['_download']) ? 'attachment' : 'inline';
9bb1fc 123
0ced2b 124       header("Content-Disposition: $disposition; filename=\"$filename\"");
9bb1fc 125
8d4bcd 126       // turn off output buffering and print part content
21b160 127       if ($part->body)
T 128         echo $part->body;
3b7e00 129       else if ($part->size)
21b160 130         $IMAP->get_message_part($MESSAGE->uid, $part->mime_id, $part, true);
8fa58e 131     }
8d4bcd 132
4e17e6 133     exit;
T 134   }
8fa58e 135 }
4e17e6 136
T 137 // print message
8fa58e 138 else {
4e17e6 139   // send correct headers for content type
T 140   header("Content-Type: text/html");
141
8d4bcd 142   $cont = "<html>\n<head><title></title>\n</head>\n<body>";
T 143   $cont .= rcmail_message_body(array());
144   $cont .= "\n</body>\n</html>";
4e17e6 145
T 146   $OUTPUT = new rcube_html_page();
147   $OUTPUT->write($cont);
148
149   exit;
8fa58e 150 }
4e17e6 151
T 152
153 // if we arrive here, the requested part was not found
154 header('HTTP/1.1 404 Not Found');
155 exit;
156
b25dfd 157