Code cleanup brigage...
[squirrelmail.git] / src / draft_actions.php
1 <?php
2
3 /**
4 * draft_actions.php
5 *
6 * Copyright (c) 1999-2001 The Squirrelmail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * $Id$
10 */
11
12 /*****************************************************************/
13 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
14 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
15 /*** + Base level indent should begin at left margin, as ***/
16 /*** the require_once below looks. ***/
17 /*** + All identation should consist of four space blocks ***/
18 /*** + Tab characters are evil. ***/
19 /*** + all comments should use "slash-star ... star-slash" ***/
20 /*** style -- no pound characters, no slash-slash style ***/
21 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
22 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
23 /*** + Please use ' instead of ", when possible. Note " ***/
24 /*** should always be used in _( ) function calls. ***/
25 /*** Thank you for your help making the SM code more readable. ***/
26 /*****************************************************************/
27
28 require_once ('../src/validate.php');
29
30 /* Print all the needed RFC822 headers */
31 function write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers) {
32 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
33 global $data_dir, $username, $popuser, $domain, $version, $useSendmail;
34 global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
35 global $REMOTE_HOST, $identity;
36
37 // Storing the header to make sure the header is the same
38 // everytime the header is printed.
39 static $header, $headerlength;
40
41 if ($header == '') {
42 if (isset($identity) && ($identity != 'default')) {
43 $reply_to = getPref($data_dir, $username, 'reply_to' . $identity);
44 $from = getPref($data_dir, $username, 'full_name' . $identity);
45 $from_addr = getPref($data_dir, $username, 'email_address' . $identity);
46 } else {
47 $reply_to = getPref($data_dir, $username, 'reply_to');
48 $from = getPref($data_dir, $username, 'full_name');
49 $from_addr = getPref($data_dir, $username, 'email_address');
50 }
51
52 if ($from_addr == '') {
53 $from_addr = $popuser.'@'.$domain;
54 }
55
56 /* Encoding 8-bit characters and making from line */
57 $subject = encodeHeader($subject);
58 if ($from == '') {
59 $from = "<$from_addr>";
60 } else {
61 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
62 }
63
64 /* This creates an RFC 822 date */
65 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
66
67 /* Create a message-id */
68 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
69 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
70
71 /* Insert header fields */
72 $header = "Message-ID: $message_id\r\n";
73 $header .= "Date: $date\r\n";
74 $header .= "Subject: $subject\r\n";
75 $header .= "From: $from\r\n";
76 $header .= "To: $t\r\n"; // Who it's TO
77
78 /* Insert headers from the $more_headers array */
79 if(is_array($more_headers)) {
80 reset($more_headers);
81 while(list($h_name, $h_val) = each($more_headers)) {
82 $header .= sprintf("%s: %s\r\n", $h_name, $h_val);
83 }
84 }
85
86 if ($c) {
87 $header .= "Cc: $c\r\n"; // Who the CCs are
88 }
89
90 if ($b) {
91 $header .= "Bcc: $b\r\n"; // Who the BCCs are
92 }
93
94 if ($reply_to != '')
95 $header .= "Reply-To: $reply_to\r\n";
96
97 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
98
99 /* Do the MIME-stuff */
100 $header .= "MIME-Version: 1.0\r\n";
101
102 if (isMultipart()) {
103 $header .= 'Content-Type: multipart/mixed; boundary="';
104 $header .= mimeBoundary();
105 $header .= "\"\r\n";
106 } else {
107 if ($default_charset != '')
108 $header .= "Content-Type: text/plain; charset=$default_charset\r\n";
109 else
110 $header .= "Content-Type: text/plain;\r\n";
111 $header .= "Content-Transfer-Encoding: 8bit\r\n";
112 }
113 $header .= "\r\n"; // One blank line to separate header and body
114
115 $headerlength = strlen($header);
116 }
117
118 // Write the header
119 fputs ($fp, $header);
120
121 return $headerlength;
122 }
123
124 // Send the body
125 function writeBodyForDraft ($fp, $passedBody) {
126 global $default_charset;
127
128 $attachmentlength = 0;
129
130 if (isMultipart()) {
131 $body = '--'.mimeBoundary()."\r\n";
132
133 if ($default_charset != "")
134 $body .= "Content-Type: text/plain; charset=$default_charset\r\n";
135 else
136 $body .= "Content-Type: text/plain\r\n";
137
138 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
139 $body .= $passedBody . "\r\n\r\n";
140 fputs ($fp, $body);
141
142 $attachmentlength = attachFiles($fp);
143
144 if (!isset($postbody)) $postbody = "";
145 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
146 fputs ($fp, $postbody);
147 } else {
148 $body = $passedBody . "\r\n";
149 fputs ($fp, $body);
150 $postbody = "\r\n";
151 fputs ($fp, $postbody);
152 }
153
154 return (strlen($body) + strlen($postbody) + $attachmentlength);
155 }
156
157
158 function saveMessageAsDraft($t, $c, $b, $subject, $body, $reply_id) {
159 global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad;
160 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
161 global $draft_folder, $attachment_dir;
162 $more_headers = Array();
163
164 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
165
166 $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7);
167 while ( file_exists($attachment_dir .$tmpDraftFile) )
168 $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7);
169 $fp = fopen($attachment_dir . $tmpDraftFile, 'w');
170
171 $headerlength = write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers, FALSE);
172 $bodylength = writeBodyForDraft ($fp, $body, FALSE);
173 fclose($fp);
174
175 $length = ($headerlength + $bodylength);
176
177 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
178 sqimap_append ($imap_stream, $draft_folder, $length);
179 write822HeaderForDraft ($imap_stream, $t, $c, $b, $subject, $more_headers, TRUE);
180 writeBodyForDraft ($imap_stream, $body, TRUE);
181 sqimap_append_done ($imap_stream);
182 }
183 sqimap_logout($imap_stream);
184 if ($length)
185 ClearAttachments();
186 if (file_exists($attachment_dir . $tmpDraftFile) )
187 unlink ($attachment_dir . $tmpDraftFile);
188 return $length;
189 }
190 ?>