| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * draft_actions.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * $Id$ |
| 10 | */ |
| 11 | |
| 12 | require_once ('../src/validate.php'); |
| 13 | |
| 14 | /* Print all the needed RFC822 headers */ |
| 15 | function write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers) { |
| 16 | global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT; |
| 17 | global $data_dir, $username, $popuser, $domain, $version, $useSendmail; |
| 18 | global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR; |
| 19 | global $REMOTE_HOST, $identity; |
| 20 | |
| 21 | /* Storing the header to make sure the header is the same */ |
| 22 | /* everytime the header is printed. */ |
| 23 | static $header, $headerlength; |
| 24 | |
| 25 | if ($header == '') { |
| 26 | if (isset($identity) && ($identity != 'default')) { |
| 27 | $reply_to = getPref($data_dir, $username, 'reply_to' . $identity); |
| 28 | $from = getPref($data_dir, $username, 'full_name' . $identity); |
| 29 | $from_addr = getPref($data_dir, $username, 'email_address' . $identity); |
| 30 | } else { |
| 31 | $reply_to = getPref($data_dir, $username, 'reply_to'); |
| 32 | $from = getPref($data_dir, $username, 'full_name'); |
| 33 | $from_addr = getPref($data_dir, $username, 'email_address'); |
| 34 | } |
| 35 | |
| 36 | if ($from_addr == '') { |
| 37 | $from_addr = $popuser.'@'.$domain; |
| 38 | } |
| 39 | |
| 40 | /* Encoding 8-bit characters and making from line */ |
| 41 | $subject = encodeHeader($subject); |
| 42 | if ($from == '') { |
| 43 | $from = "<$from_addr>"; |
| 44 | } else { |
| 45 | $from = '"' . encodeHeader($from) . "\" <$from_addr>"; |
| 46 | } |
| 47 | |
| 48 | /* This creates an RFC 822 date */ |
| 49 | $date = date("D, j M Y H:i:s ", mktime()) . timezone(); |
| 50 | |
| 51 | /* Create a message-id */ |
| 52 | $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.'; |
| 53 | $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>'; |
| 54 | |
| 55 | /* Insert header fields */ |
| 56 | $header = "Message-ID: $message_id\r\n"; |
| 57 | $header .= "Date: $date\r\n"; |
| 58 | $header .= "Subject: $subject\r\n"; |
| 59 | $header .= "From: $from\r\n"; |
| 60 | $header .= "To: $t\r\n"; // Who it's TO |
| 61 | |
| 62 | /* Insert headers from the $more_headers array */ |
| 63 | if(is_array($more_headers)) { |
| 64 | reset($more_headers); |
| 65 | while(list($h_name, $h_val) = each($more_headers)) { |
| 66 | $header .= sprintf("%s: %s\r\n", $h_name, $h_val); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if ($c) { |
| 71 | $header .= "Cc: $c\r\n"; // Who the CCs are |
| 72 | } |
| 73 | |
| 74 | if ($b) { |
| 75 | $header .= "Bcc: $b\r\n"; // Who the BCCs are |
| 76 | } |
| 77 | |
| 78 | if ($reply_to != '') { |
| 79 | $header .= "Reply-To: $reply_to\r\n"; |
| 80 | } |
| 81 | |
| 82 | $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail |
| 83 | |
| 84 | /* Do the MIME-stuff */ |
| 85 | $header .= "MIME-Version: 1.0\r\n"; |
| 86 | |
| 87 | if (isMultipart()) { |
| 88 | $header .= 'Content-Type: multipart/mixed; boundary="'; |
| 89 | $header .= mimeBoundary(); |
| 90 | $header .= "\"\r\n"; |
| 91 | } else { |
| 92 | if ($default_charset != '') |
| 93 | $header .= "Content-Type: text/plain; charset=$default_charset\r\n"; |
| 94 | else |
| 95 | $header .= "Content-Type: text/plain;\r\n"; |
| 96 | $header .= "Content-Transfer-Encoding: 8bit\r\n"; |
| 97 | } |
| 98 | $header .= "\r\n"; // One blank line to separate header and body |
| 99 | |
| 100 | $headerlength = strlen($header); |
| 101 | } |
| 102 | |
| 103 | /* Write the header */ |
| 104 | fputs ($fp, $header); |
| 105 | |
| 106 | return $headerlength; |
| 107 | } |
| 108 | |
| 109 | /* Send the body */ |
| 110 | function writeBodyForDraft ($fp, $passedBody) { |
| 111 | global $default_charset; |
| 112 | |
| 113 | $attachmentlength = 0; |
| 114 | |
| 115 | if (isMultipart()) { |
| 116 | $body = '--'.mimeBoundary()."\r\n"; |
| 117 | |
| 118 | if ($default_charset != ""){ |
| 119 | $body .= "Content-Type: text/plain; charset=$default_charset\r\n"; |
| 120 | } else { |
| 121 | $body .= "Content-Type: text/plain\r\n"; |
| 122 | } |
| 123 | |
| 124 | $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n"; |
| 125 | $body .= $passedBody . "\r\n\r\n"; |
| 126 | fputs ($fp, $body); |
| 127 | |
| 128 | $attachmentlength = attachFiles($fp); |
| 129 | |
| 130 | if (!isset($postbody)) $postbody = ""; |
| 131 | $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n"; |
| 132 | fputs ($fp, $postbody); |
| 133 | } else { |
| 134 | $body = $passedBody . "\r\n"; |
| 135 | fputs ($fp, $body); |
| 136 | $postbody = "\r\n"; |
| 137 | fputs ($fp, $postbody); |
| 138 | } |
| 139 | |
| 140 | return (strlen($body) + strlen($postbody) + $attachmentlength); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | function saveMessageAsDraft($t, $c, $b, $subject, $body, $reply_id) { |
| 145 | global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad, |
| 146 | $data_dir, $username, $domain, $key, $version, $sent_folder, |
| 147 | $imapServerAddress, $imapPort, $draft_folder, $attachment_dir; |
| 148 | $more_headers = Array(); |
| 149 | |
| 150 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1); |
| 151 | |
| 152 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir); |
| 153 | |
| 154 | $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7); |
| 155 | $full_tmpDraftFile = "$hashed_attachment_dir/$tmpDraftFile"; |
| 156 | while (file_exists($full_tmpDraftFile)){ |
| 157 | $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7); |
| 158 | $full_tmpDraftFile = "$hashed_attachment_dir/$tmpDraftFile"; |
| 159 | } |
| 160 | $fp = fopen($full_tmpDraftFile, 'w'); |
| 161 | |
| 162 | $headerlength = write822HeaderForDraft |
| 163 | ($fp, $t, $c, $b, $subject, $more_headers, FALSE); |
| 164 | $bodylength = writeBodyForDraft ($fp, $body, FALSE); |
| 165 | fclose($fp); |
| 166 | |
| 167 | $length = ($headerlength + $bodylength); |
| 168 | |
| 169 | if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) { |
| 170 | sqimap_append ($imap_stream, $draft_folder, $length); |
| 171 | write822HeaderForDraft |
| 172 | ($imap_stream, $t, $c, $b, $subject, $more_headers, TRUE); |
| 173 | writeBodyForDraft ($imap_stream, $body, TRUE); |
| 174 | sqimap_append_done ($imap_stream); |
| 175 | } |
| 176 | sqimap_logout($imap_stream); |
| 177 | if ($length){ |
| 178 | ClearAttachments(); |
| 179 | } |
| 180 | if (file_exists($full_tmpDraftFile)){ |
| 181 | unlink ($full_tmpDraftFile); |
| 182 | } |
| 183 | return $length; |
| 184 | } |
| 185 | ?> |