Removed dependance upon /dev/null allowing for use Windows/IIS.
[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, $send_it) {
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 if (isset($identity) && ($identity != 'default')) {
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);
20 } else {
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
26 if ($from_addr == '') {
27 $from_addr = $popuser.'@'.$domain;
28 }
29
30 /* Encoding 8-bit characters and making from line */
31 $subject = encodeHeader($subject);
32 if ($from == '') {
33 $from = "<$from_addr>";
34 } else {
35 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
36 }
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
45 /* Insert header fields */
46 $header = "Message-ID: $message_id\r\n";
47 $header .= "Date: $date\r\n";
48 $header .= "Subject: $subject\r\n";
49 $header .= "From: $from\r\n";
50 $header .= "To: $t\r\n"; // Who it's TO
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
60 if ($c) {
61 $header .= "Cc: $c\r\n"; // Who the CCs are
62 }
63
64 if ($b) {
65 $header .= "Bcc: $b\r\n"; // Who the BCCs are
66 }
67
68 if ($reply_to != '')
69 $header .= "Reply-To: $reply_to\r\n";
70
71 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
72
73 /* Do the MIME-stuff */
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
93 if ($send_it) {
94 fputs ($fp, $header);
95 }
96
97 return $headerlength;
98 }
99
100 // Send the body
101 function writeBodyForDraft ($fp, $passedBody, $send_it) {
102 global $default_charset;
103
104 $attachmentlength = 0;
105
106 if (isMultipart()) {
107 $body = '--'.mimeBoundary()."\r\n";
108
109 if ($default_charset != "")
110 $body .= "Content-Type: text/plain; charset=$default_charset\r\n";
111 else
112 $body .= "Content-Type: text/plain\r\n";
113
114 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
115 $body .= $passedBody . "\r\n\r\n";
116 if ($send_it) {
117 fputs ($fp, $body);
118 }
119
120 $attachmentlength = attachFiles($fp);
121
122 if (!isset($postbody)) $postbody = "";
123 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
124 if ($send_it) {
125 fputs ($fp, $postbody);
126 }
127 } else {
128 $body = $passedBody . "\r\n";
129 if ($send_it) {
130 fputs ($fp, $body);
131 }
132 $postbody = "\r\n";
133 if ($send_it) {
134 fputs ($fp, $postbody);
135 }
136 }
137
138 return (strlen($body) + strlen($postbody) + $attachmentlength);
139 }
140
141
142 function saveMessageAsDraft($t, $c, $b, $subject, $body, $reply_id) {
143 global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad;
144 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
145 global $draft_folder;
146 $more_headers = Array();
147
148 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
149
150 $fp = "";
151 $headerlength = write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers, FALSE);
152 $bodylength = writeBodyForDraft ($fp, $body, FALSE);
153
154 $length = ($headerlength + $bodylength);
155
156 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
157 sqimap_append ($imap_stream, $draft_folder, $length);
158 write822HeaderForDraft ($imap_stream, $t, $c, $b, $subject, $more_headers, TRUE);
159 writeBodyForDraft ($imap_stream, $body, TRUE);
160 sqimap_append_done ($imap_stream);
161 }
162 sqimap_logout($imap_stream);
163 if ($length)
164 ClearAttachments();
165 return $length;
166 }
167 ?>