Some fixup:
[squirrelmail.git] / src / draft_actions.php
1 <?php
2
3 /**
4 ** draft_actions.php
5 **
6 ** Copyright (c) 1999-2000 The SquirrelMail development team
7 ** Licensed under the GNU GPL. For full terms see the file COPYING.
8 **
9 **
10 ** $Id$
11 **/
12
13 require_once ('../src/validate.php');
14
15 /* Print all the needed RFC822 headers */
16 function write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers) {
17 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
18 global $data_dir, $username, $popuser, $domain, $version, $useSendmail;
19 global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
20 global $REMOTE_HOST, $identity;
21
22 // Storing the header to make sure the header is the same
23 // everytime the header is printed.
24 static $header, $headerlength;
25
26 if ($header == '') {
27 if (isset($identity) && ($identity != 'default')) {
28 $reply_to = getPref($data_dir, $username, 'reply_to' . $identity);
29 $from = getPref($data_dir, $username, 'full_name' . $identity);
30 $from_addr = getPref($data_dir, $username, 'email_address' . $identity);
31 } else {
32 $reply_to = getPref($data_dir, $username, 'reply_to');
33 $from = getPref($data_dir, $username, 'full_name');
34 $from_addr = getPref($data_dir, $username, 'email_address');
35 }
36
37 if ($from_addr == '') {
38 $from_addr = $popuser.'@'.$domain;
39 }
40
41 /* Encoding 8-bit characters and making from line */
42 $subject = encodeHeader($subject);
43 if ($from == '') {
44 $from = "<$from_addr>";
45 } else {
46 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
47 }
48
49 /* This creates an RFC 822 date */
50 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
51
52 /* Create a message-id */
53 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
54 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
55
56 /* Insert header fields */
57 $header = "Message-ID: $message_id\r\n";
58 $header .= "Date: $date\r\n";
59 $header .= "Subject: $subject\r\n";
60 $header .= "From: $from\r\n";
61 $header .= "To: $t\r\n"; // Who it's TO
62
63 /* Insert headers from the $more_headers array */
64 if(is_array($more_headers)) {
65 reset($more_headers);
66 while(list($h_name, $h_val) = each($more_headers)) {
67 $header .= sprintf("%s: %s\r\n", $h_name, $h_val);
68 }
69 }
70
71 if ($c) {
72 $header .= "Cc: $c\r\n"; // Who the CCs are
73 }
74
75 if ($b) {
76 $header .= "Bcc: $b\r\n"; // Who the BCCs are
77 }
78
79 if ($reply_to != '')
80 $header .= "Reply-To: $reply_to\r\n";
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 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
124 $body .= $passedBody . "\r\n\r\n";
125 fputs ($fp, $body);
126
127 $attachmentlength = attachFiles($fp);
128
129 if (!isset($postbody)) $postbody = "";
130 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
131 fputs ($fp, $postbody);
132 } else {
133 $body = $passedBody . "\r\n";
134 fputs ($fp, $body);
135 $postbody = "\r\n";
136 fputs ($fp, $postbody);
137 }
138
139 return (strlen($body) + strlen($postbody) + $attachmentlength);
140 }
141
142
143 function saveMessageAsDraft($t, $c, $b, $subject, $body, $reply_id) {
144 global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad;
145 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
146 global $draft_folder, $attachment_dir;
147 $more_headers = Array();
148
149 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
150
151 $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7);
152 while ( file_exists($attachment_dir .$tmpDraftFile) )
153 $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7);
154 $fp = fopen($attachment_dir . $tmpDraftFile, 'w');
155
156 $headerlength = write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers, FALSE);
157 $bodylength = writeBodyForDraft ($fp, $body, FALSE);
158 fclose($fp);
159
160 $length = ($headerlength + $bodylength);
161
162 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
163 sqimap_append ($imap_stream, $draft_folder, $length);
164 write822HeaderForDraft ($imap_stream, $t, $c, $b, $subject, $more_headers, TRUE);
165 writeBodyForDraft ($imap_stream, $body, TRUE);
166 sqimap_append_done ($imap_stream);
167 }
168 sqimap_logout($imap_stream);
169 if ($length)
170 ClearAttachments();
171 if (file_exists($attachment_dir . $tmpDraftFile) )
172 unlink ($attachment_dir . $tmpDraftFile);
173 return $length;
174 }
175 ?>