| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * download.php |
| 5 | * |
| 6 | * Handles attachment downloads to the users computer. |
| 7 | * Also allows displaying of attachments when possible. |
| 8 | * |
| 9 | * @copyright © 1999-2006 The SquirrelMail Project Team |
| 10 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 11 | * @version $Id$ |
| 12 | * @package squirrelmail |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Include the SquirrelMail initialization file. |
| 17 | */ |
| 18 | require('../include/init.php'); |
| 19 | |
| 20 | /* SquirrelMail required files. */ |
| 21 | require(SM_PATH . 'functions/imap_general.php'); |
| 22 | require(SM_PATH . 'functions/mailbox_display.php'); |
| 23 | require(SM_PATH . 'functions/mime.php'); |
| 24 | |
| 25 | header('Pragma: '); |
| 26 | header('Cache-Control: cache'); |
| 27 | |
| 28 | /* globals */ |
| 29 | sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION); |
| 30 | sqgetGlobalVar('messages', $messages, SQ_SESSION); |
| 31 | sqgetGlobalVar('mailbox', $mailbox, SQ_GET); |
| 32 | sqgetGlobalVar('ent_id', $ent_id, SQ_GET); |
| 33 | sqgetGlobalVar('absolute_dl',$absolute_dl, SQ_GET); |
| 34 | if ( sqgetGlobalVar('passed_id', $temp, SQ_GET) ) { |
| 35 | $passed_id = (int) $temp; |
| 36 | } |
| 37 | if (!sqgetGlobalVar('account', $account, SQ_GET) ) { |
| 38 | $account = 0; |
| 39 | } |
| 40 | |
| 41 | global $default_charset; |
| 42 | set_my_charset(); |
| 43 | |
| 44 | /* end globals */ |
| 45 | |
| 46 | $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0); |
| 47 | $aMailbox = sqm_api_mailbox_select($imapConnection, $account, $mailbox,array(),array()); |
| 48 | |
| 49 | if (isset($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) && |
| 50 | is_object($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) ) { |
| 51 | $message = $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']; |
| 52 | } else { |
| 53 | $message = sqimap_get_message($imapConnection, $passed_id, $mailbox); |
| 54 | $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'] = $message; |
| 55 | } |
| 56 | |
| 57 | $subject = $message->rfc822_header->subject; |
| 58 | if ($ent_id) { |
| 59 | // replace message with message part, if message part is requested. |
| 60 | $message = $message->getEntity($ent_id); |
| 61 | $header = $message->header; |
| 62 | |
| 63 | if ($message->rfc822_header) { |
| 64 | $subject = $message->rfc822_header->subject; |
| 65 | } else { |
| 66 | $header = $message->header; |
| 67 | } |
| 68 | $type0 = $header->type0; |
| 69 | $type1 = $header->type1; |
| 70 | $encoding = strtolower($header->encoding); |
| 71 | } else { |
| 72 | /* raw message */ |
| 73 | $type0 = 'message'; |
| 74 | $type1 = 'rfc822'; |
| 75 | $encoding = 'US-ASCII'; |
| 76 | $header = $message->header; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * lets redefine message as this particular entity that we wish to display. |
| 81 | * it should hold only the header for this entity. We need to fetch the body |
| 82 | * yet before we can display anything. |
| 83 | */ |
| 84 | |
| 85 | if (isset($override_type0)) { |
| 86 | $type0 = $override_type0; |
| 87 | } |
| 88 | if (isset($override_type1)) { |
| 89 | $type1 = $override_type1; |
| 90 | } |
| 91 | $filename = ''; |
| 92 | if (is_object($message->header->disposition)) { |
| 93 | $filename = $header->disposition->getProperty('filename'); |
| 94 | if (!$filename) { |
| 95 | $filename = $header->disposition->getProperty('name'); |
| 96 | } |
| 97 | if (!$filename) { |
| 98 | $filename = $header->getParameter('name'); |
| 99 | } |
| 100 | } else { |
| 101 | $filename = $header->getParameter('name'); |
| 102 | } |
| 103 | |
| 104 | $filename = decodeHeader($filename,true,false); |
| 105 | $filename = charset_encode($filename,$default_charset,false); |
| 106 | |
| 107 | // If name is not set, use subject of email |
| 108 | if (strlen($filename) < 1) { |
| 109 | $filename = decodeHeader($subject, true, true); |
| 110 | $filename = charset_encode($filename,$default_charset,false); |
| 111 | if ($type1 == 'plain' && $type0 == 'text') |
| 112 | $suffix = 'txt'; |
| 113 | else if ($type1 == 'richtext' && $type0 == 'text') |
| 114 | $suffix = 'rtf'; |
| 115 | else if ($type1 == 'postscript' && $type0 == 'application') |
| 116 | $suffix = 'ps'; |
| 117 | else if ($type1 == 'rfc822' && $type0 == 'message') |
| 118 | $suffix = 'msg'; |
| 119 | else |
| 120 | $suffix = $type1; |
| 121 | |
| 122 | if ($filename == '') |
| 123 | $filename = 'untitled' . strip_tags($ent_id); |
| 124 | $filename = $filename . '.' . $suffix; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Update mailbox_cache and close session in order to prevent |
| 129 | * script locking on larger downloads. SendDownloadHeaders() and |
| 130 | * mime_print_body_lines() don't write information to session. |
| 131 | * mime_print_body_lines() call duration depends on size of |
| 132 | * attachment and script can cause interface lockups, if session |
| 133 | * is not closed. |
| 134 | */ |
| 135 | $mailbox_cache[$aMailbox['NAME']] = $aMailbox; |
| 136 | sqsession_register($mailbox_cache,'mailbox_cache'); |
| 137 | session_write_close(); |
| 138 | |
| 139 | /* |
| 140 | * Note: |
| 141 | * The following sections display the attachment in different |
| 142 | * ways depending on how they choose. The first way will download |
| 143 | * under any circumstance. This sets the Content-type to be |
| 144 | * applicatin/octet-stream, which should be interpreted by the |
| 145 | * browser as "download me". |
| 146 | * The second method (view) is used for images or other formats |
| 147 | * that should be able to be handled by the browser. It will |
| 148 | * most likely display the attachment inline inside the browser. |
| 149 | * And finally, the third one will be used by default. If it |
| 150 | * is displayable (text or html), it will load them up in a text |
| 151 | * viewer (built in to SquirrelMail). Otherwise, it sets the |
| 152 | * content-type as application/octet-stream |
| 153 | */ |
| 154 | if (isset($absolute_dl) && $absolute_dl) { |
| 155 | SendDownloadHeaders($type0, $type1, $filename, 1); |
| 156 | } else { |
| 157 | SendDownloadHeaders($type0, $type1, $filename, 0); |
| 158 | } |
| 159 | /* be aware that any warning caused by download.php will corrupt the |
| 160 | * attachment in case of ERROR reporting = E_ALL and the output is the screen */ |
| 161 | mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding); |
| 162 | |