| 1 | <?php |
| 2 | /** |
| 3 | ** download.php |
| 4 | ** |
| 5 | ** Copyright (c) 1999-2000 The SquirrelMail development team |
| 6 | ** Licensed under the GNU GPL. For full terms see the file COPYING. |
| 7 | ** |
| 8 | ** Handles attachment downloads to the users computer. |
| 9 | ** Also allows displaying of attachments when possible. |
| 10 | **/ |
| 11 | |
| 12 | session_start(); |
| 13 | |
| 14 | if (!isset($config_php)) |
| 15 | include("../config/config.php"); |
| 16 | if (!isset($strings_php)) |
| 17 | include("../functions/strings.php"); |
| 18 | if (!isset($page_header_php)) |
| 19 | include("../functions/page_header.php"); |
| 20 | if (!isset($imap_php)) |
| 21 | include("../functions/imap.php"); |
| 22 | if (!isset($mime_php)) |
| 23 | include("../functions/mime.php"); |
| 24 | if (!isset($date_php)) |
| 25 | include("../functions/date.php"); |
| 26 | |
| 27 | include("../src/load_prefs.php"); |
| 28 | |
| 29 | function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) { |
| 30 | displayPageHeader($color, "None"); |
| 31 | |
| 32 | echo "<BR><TABLE WIDTH=90% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">"; |
| 33 | echo "<B><CENTER>"; |
| 34 | echo _("Viewing a text attachment") . " - "; |
| 35 | echo "<a href=\"read_body.php?mailbox=$mailbox&passed_id=$id\">". _("View message") . "</a>"; |
| 36 | echo "</CENTER></B>"; |
| 37 | echo "</TD></TR><TR><TD BGCOLOR=\"$color[4]\">"; |
| 38 | $urlmailbox = urlencode($mailbox); |
| 39 | echo "<CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">"; |
| 40 | echo _("Download this as a file"); |
| 41 | echo "</A></CENTER><BR><BR><TT>"; |
| 42 | if ($type1 == "html") |
| 43 | echo $body; |
| 44 | else |
| 45 | echo translateText($body, $wrap_at); |
| 46 | |
| 47 | echo "</TT></TD></TR></TABLE>"; |
| 48 | } |
| 49 | |
| 50 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
| 51 | sqimap_mailbox_select($imapConnection, $mailbox); |
| 52 | |
| 53 | // $message contains all information about the message |
| 54 | // including header and body |
| 55 | $message = sqimap_get_message($imapConnection, $passed_id, $mailbox); |
| 56 | $top_header = $message->header; |
| 57 | |
| 58 | // lets redefine message as this particular entity that we wish to display. |
| 59 | // it should hold only the header for this entity. We need to fetch the body |
| 60 | // yet before we can display anything. |
| 61 | $message = getEntity($message, $passed_ent_id); |
| 62 | |
| 63 | $header = $message->header; |
| 64 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
| 65 | |
| 66 | $type0 = $header->type0; |
| 67 | $type1 = $header->type1; |
| 68 | $filename = decodeHeader($header->filename); |
| 69 | |
| 70 | if (strlen($filename) < 1) { |
| 71 | if ($type1 == "plain" && $type0 == "text") $suffix = "txt"; |
| 72 | else if ($type1 == "richtext" && $type0 == "text") $suffix = "rtf"; |
| 73 | else if ($type1 == "postscript" && $type0 == "application") $suffix = "ps"; |
| 74 | else if ($type1 == "message" && $type0 == "rfc822") $suffix = "msg"; |
| 75 | else $suffix = $type1; |
| 76 | |
| 77 | $filename = "untitled$passed_ent_id.$suffix"; |
| 78 | } |
| 79 | |
| 80 | // Note: |
| 81 | // The following sections display the attachment in different |
| 82 | // ways depending on how they choose. The first way will download |
| 83 | // under any circumstance. This sets the Content-type to be |
| 84 | // applicatin/octet-stream, which should be interpreted by the |
| 85 | // browser as "download me". |
| 86 | // The second method (view) is used for images or other formats |
| 87 | // that should be able to be handled by the browser. It will |
| 88 | // most likely display the attachment inline inside the browser. |
| 89 | // And finally, the third one will be used by default. If it |
| 90 | // is displayable (text or html), it will load them up in a text |
| 91 | // viewer (built in to squirrelmail). Otherwise, it sets the |
| 92 | // content-type as application/octet-stream |
| 93 | |
| 94 | if ($absolute_dl == "true") { |
| 95 | switch($type0) { |
| 96 | case "text": |
| 97 | $body = decodeBody($body, $header->encoding); |
| 98 | header("Content-type: application/octet-stream; name=\"$filename\""); |
| 99 | header("Content-Disposition: attachment; filename=\"$filename\""); |
| 100 | if ($type1 == "plain") { |
| 101 | echo _("Subject") . ": " . decodeHeader(stripslashes($top_header->subject)) . "\n"; |
| 102 | echo " " . _("From") . ": " . decodeHeader(stripslashes($top_header->from)) . "\n"; |
| 103 | echo " " . _("To") . ": " . decodeHeader(stripslashes(getLineOfAddrs($top_header->to))) . "\n"; |
| 104 | echo " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n"; |
| 105 | } |
| 106 | echo trim($body); |
| 107 | break; |
| 108 | default: |
| 109 | $body = decodeBody($body, $header->encoding); |
| 110 | header("Content-type: application/octet-stream; name=\"$filename\""); |
| 111 | header("Content-Disposition: attachment; filename=\"$filename\""); |
| 112 | echo $body; |
| 113 | break; |
| 114 | } |
| 115 | } else { |
| 116 | switch ($type0) { |
| 117 | case "text": |
| 118 | $body = decodeBody($body, $header->encoding); |
| 119 | viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); |
| 120 | break; |
| 121 | case "message": |
| 122 | $body = decodeBody($body, $header->encoding); |
| 123 | viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); |
| 124 | break; |
| 125 | default: |
| 126 | $body = decodeBody($body, $header->encoding); |
| 127 | header("Content-type: $type0/$type1; name=\"$filename\""); |
| 128 | header("Content-Disposition: attachment; filename=\"$filename\""); |
| 129 | echo $body; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | sqimap_logout($imapConnection); |
| 135 | ?> |