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