bd9906cd |
1 | <?php |
2d367c68 |
2 | |
3 | /** |
4 | ** draft_actions.php |
5 | ** |
895905c0 |
6 | ** Copyright (c) 1999-2001 The SquirrelMail development team |
2d367c68 |
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'); |
bd9906cd |
14 | |
15 | /* Print all the needed RFC822 headers */ |
e5eadbf5 |
16 | function write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers) { |
bd9906cd |
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 == '') { |
f7b1b3b1 |
27 | if (isset($identity) && ($identity != 'default')) { |
bd9906cd |
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); |
f7b1b3b1 |
31 | } else { |
bd9906cd |
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 | |
f7b1b3b1 |
37 | if ($from_addr == '') { |
bd9906cd |
38 | $from_addr = $popuser.'@'.$domain; |
f7b1b3b1 |
39 | } |
bd9906cd |
40 | |
bd9906cd |
41 | /* Encoding 8-bit characters and making from line */ |
42 | $subject = encodeHeader($subject); |
f7b1b3b1 |
43 | if ($from == '') { |
bd9906cd |
44 | $from = "<$from_addr>"; |
f7b1b3b1 |
45 | } else { |
bd9906cd |
46 | $from = '"' . encodeHeader($from) . "\" <$from_addr>"; |
f7b1b3b1 |
47 | } |
bd9906cd |
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 | |
1e000d2f |
56 | /* Insert header fields */ |
57 | $header = "Message-ID: $message_id\r\n"; |
bd9906cd |
58 | $header .= "Date: $date\r\n"; |
59 | $header .= "Subject: $subject\r\n"; |
60 | $header .= "From: $from\r\n"; |
1e000d2f |
61 | $header .= "To: $t\r\n"; // Who it's TO |
bd9906cd |
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 | |
1e000d2f |
71 | if ($c) { |
72 | $header .= "Cc: $c\r\n"; // Who the CCs are |
bd9906cd |
73 | } |
74 | |
1e000d2f |
75 | if ($b) { |
76 | $header .= "Bcc: $b\r\n"; // Who the BCCs are |
bd9906cd |
77 | } |
78 | |
79 | if ($reply_to != '') |
80 | $header .= "Reply-To: $reply_to\r\n"; |
81 | |
bd9906cd |
82 | $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail |
83 | |
f7b1b3b1 |
84 | /* Do the MIME-stuff */ |
bd9906cd |
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 |
e5eadbf5 |
104 | fputs ($fp, $header); |
bd9906cd |
105 | |
106 | return $headerlength; |
107 | } |
108 | |
8f7315af |
109 | // Send the body |
e5eadbf5 |
110 | function writeBodyForDraft ($fp, $passedBody) { |
8f7315af |
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"; |
e5eadbf5 |
125 | fputs ($fp, $body); |
8f7315af |
126 | |
127 | $attachmentlength = attachFiles($fp); |
128 | |
129 | if (!isset($postbody)) $postbody = ""; |
130 | $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n"; |
e5eadbf5 |
131 | fputs ($fp, $postbody); |
8f7315af |
132 | } else { |
133 | $body = $passedBody . "\r\n"; |
e5eadbf5 |
134 | fputs ($fp, $body); |
8f7315af |
135 | $postbody = "\r\n"; |
e5eadbf5 |
136 | fputs ($fp, $postbody); |
8f7315af |
137 | } |
138 | |
139 | return (strlen($body) + strlen($postbody) + $attachmentlength); |
140 | } |
141 | |
142 | |
f7b1b3b1 |
143 | function saveMessageAsDraft($t, $c, $b, $subject, $body, $reply_id) { |
bd9906cd |
144 | global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad; |
145 | global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort; |
e5eadbf5 |
146 | global $draft_folder, $attachment_dir; |
bd9906cd |
147 | $more_headers = Array(); |
148 | |
149 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1); |
150 | |
e5eadbf5 |
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 | |
8f7315af |
156 | $headerlength = write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers, FALSE); |
157 | $bodylength = writeBodyForDraft ($fp, $body, FALSE); |
e5eadbf5 |
158 | fclose($fp); |
bd9906cd |
159 | |
160 | $length = ($headerlength + $bodylength); |
161 | |
162 | if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) { |
163 | sqimap_append ($imap_stream, $draft_folder, $length); |
8f7315af |
164 | write822HeaderForDraft ($imap_stream, $t, $c, $b, $subject, $more_headers, TRUE); |
165 | writeBodyForDraft ($imap_stream, $body, TRUE); |
bd9906cd |
166 | sqimap_append_done ($imap_stream); |
167 | } |
168 | sqimap_logout($imap_stream); |
169 | if ($length) |
170 | ClearAttachments(); |
e5eadbf5 |
171 | if (file_exists($attachment_dir . $tmpDraftFile) ) |
172 | unlink ($attachment_dir . $tmpDraftFile); |
bd9906cd |
173 | return $length; |
174 | } |
175 | ?> |