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