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