6 * Copyright (c) 1999-2001 The Squirrelmail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
12 /*****************************************************************/
13 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
14 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
15 /*** + Base level indent should begin at left margin, as ***/
16 /*** the require_once below looks. ***/
17 /*** + All identation should consist of four space blocks ***/
18 /*** + Tab characters are evil. ***/
19 /*** + all comments should use "slash-star ... star-slash" ***/
20 /*** style -- no pound characters, no slash-slash style ***/
21 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
22 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
23 /*** + Please use ' instead of ", when possible. Note " ***/
24 /*** should always be used in _( ) function calls. ***/
25 /*** Thank you for your help making the SM code more readable. ***/
26 /*****************************************************************/
28 require_once ('../src/validate.php');
30 /* Print all the needed RFC822 headers */
31 function write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers) {
32 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
33 global $data_dir, $username, $popuser, $domain, $version, $useSendmail;
34 global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
35 global $REMOTE_HOST, $identity;
37 // Storing the header to make sure the header is the same
38 // everytime the header is printed.
39 static $header, $headerlength;
42 if (isset($identity) && ($identity != 'default')) {
43 $reply_to = getPref($data_dir, $username, 'reply_to' . $identity);
44 $from = getPref($data_dir, $username, 'full_name' . $identity);
45 $from_addr = getPref($data_dir, $username, 'email_address' . $identity);
47 $reply_to = getPref($data_dir, $username, 'reply_to');
48 $from = getPref($data_dir, $username, 'full_name');
49 $from_addr = getPref($data_dir, $username, 'email_address');
52 if ($from_addr == '') {
53 $from_addr = $popuser.'@'.$domain;
56 /* Encoding 8-bit characters and making from line */
57 $subject = encodeHeader($subject);
59 $from = "<$from_addr>";
61 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
64 /* This creates an RFC 822 date */
65 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
67 /* Create a message-id */
68 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
69 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
71 /* Insert header fields */
72 $header = "Message-ID: $message_id\r\n";
73 $header .= "Date: $date\r\n";
74 $header .= "Subject: $subject\r\n";
75 $header .= "From: $from\r\n";
76 $header .= "To: $t\r\n"; // Who it's TO
78 /* Insert headers from the $more_headers array */
79 if(is_array($more_headers)) {
81 while(list($h_name, $h_val) = each($more_headers)) {
82 $header .= sprintf("%s: %s\r\n", $h_name, $h_val);
87 $header .= "Cc: $c\r\n"; // Who the CCs are
91 $header .= "Bcc: $b\r\n"; // Who the BCCs are
95 $header .= "Reply-To: $reply_to\r\n";
97 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
99 /* Do the MIME-stuff */
100 $header .= "MIME-Version: 1.0\r\n";
103 $header .= 'Content-Type: multipart/mixed; boundary="';
104 $header .= mimeBoundary();
107 if ($default_charset != '')
108 $header .= "Content-Type: text/plain; charset=$default_charset\r\n";
110 $header .= "Content-Type: text/plain;\r\n";
111 $header .= "Content-Transfer-Encoding: 8bit\r\n";
113 $header .= "\r\n"; // One blank line to separate header and body
115 $headerlength = strlen($header);
119 fputs ($fp, $header);
121 return $headerlength;
125 function writeBodyForDraft ($fp, $passedBody) {
126 global $default_charset;
128 $attachmentlength = 0;
131 $body = '--'.mimeBoundary()."\r\n";
133 if ($default_charset != "")
134 $body .= "Content-Type: text/plain; charset=$default_charset\r\n";
136 $body .= "Content-Type: text/plain\r\n";
138 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
139 $body .= $passedBody . "\r\n\r\n";
142 $attachmentlength = attachFiles($fp);
144 if (!isset($postbody)) $postbody = "";
145 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
146 fputs ($fp, $postbody);
148 $body = $passedBody . "\r\n";
151 fputs ($fp, $postbody);
154 return (strlen($body) +
strlen($postbody) +
$attachmentlength);
158 function saveMessageAsDraft($t, $c, $b, $subject, $body, $reply_id) {
159 global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad;
160 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
161 global $draft_folder, $attachment_dir;
162 $more_headers = Array();
164 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
166 $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7);
167 while ( file_exists($attachment_dir .$tmpDraftFile) )
168 $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7);
169 $fp = fopen($attachment_dir . $tmpDraftFile, 'w');
171 $headerlength = write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers, FALSE);
172 $bodylength = writeBodyForDraft ($fp, $body, FALSE);
175 $length = ($headerlength +
$bodylength);
177 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
178 sqimap_append ($imap_stream, $draft_folder, $length);
179 write822HeaderForDraft ($imap_stream, $t, $c, $b, $subject, $more_headers, TRUE);
180 writeBodyForDraft ($imap_stream, $body, TRUE);
181 sqimap_append_done ($imap_stream);
183 sqimap_logout($imap_stream);
186 if (file_exists($attachment_dir . $tmpDraftFile) )
187 unlink ($attachment_dir . $tmpDraftFile);