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