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