I have no idea how the first `p' in <?php got capitalized.
[squirrelmail.git] / src / draft_actions.php
CommitLineData
bd9906cd 1<?php
2d367c68 2
35586184 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 */
2d367c68 11
35586184 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
28require_once ('../src/validate.php');
bd9906cd 29
30 /* Print all the needed RFC822 headers */
e5eadbf5 31 function write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers) {
bd9906cd 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 == '') {
f7b1b3b1 42 if (isset($identity) && ($identity != 'default')) {
bd9906cd 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);
f7b1b3b1 46 } else {
bd9906cd 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
f7b1b3b1 52 if ($from_addr == '') {
bd9906cd 53 $from_addr = $popuser.'@'.$domain;
f7b1b3b1 54 }
bd9906cd 55
bd9906cd 56 /* Encoding 8-bit characters and making from line */
57 $subject = encodeHeader($subject);
f7b1b3b1 58 if ($from == '') {
bd9906cd 59 $from = "<$from_addr>";
f7b1b3b1 60 } else {
bd9906cd 61 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
f7b1b3b1 62 }
bd9906cd 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
1e000d2f 71 /* Insert header fields */
72 $header = "Message-ID: $message_id\r\n";
bd9906cd 73 $header .= "Date: $date\r\n";
74 $header .= "Subject: $subject\r\n";
75 $header .= "From: $from\r\n";
1e000d2f 76 $header .= "To: $t\r\n"; // Who it's TO
bd9906cd 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
1e000d2f 86 if ($c) {
87 $header .= "Cc: $c\r\n"; // Who the CCs are
bd9906cd 88 }
89
1e000d2f 90 if ($b) {
91 $header .= "Bcc: $b\r\n"; // Who the BCCs are
bd9906cd 92 }
93
94 if ($reply_to != '')
95 $header .= "Reply-To: $reply_to\r\n";
96
bd9906cd 97 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
98
f7b1b3b1 99 /* Do the MIME-stuff */
bd9906cd 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
e5eadbf5 119 fputs ($fp, $header);
bd9906cd 120
121 return $headerlength;
122 }
123
8f7315af 124 // Send the body
e5eadbf5 125 function writeBodyForDraft ($fp, $passedBody) {
8f7315af 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";
e5eadbf5 140 fputs ($fp, $body);
8f7315af 141
142 $attachmentlength = attachFiles($fp);
143
144 if (!isset($postbody)) $postbody = "";
145 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
e5eadbf5 146 fputs ($fp, $postbody);
8f7315af 147 } else {
148 $body = $passedBody . "\r\n";
e5eadbf5 149 fputs ($fp, $body);
8f7315af 150 $postbody = "\r\n";
e5eadbf5 151 fputs ($fp, $postbody);
8f7315af 152 }
153
154 return (strlen($body) + strlen($postbody) + $attachmentlength);
155 }
156
157
f7b1b3b1 158 function saveMessageAsDraft($t, $c, $b, $subject, $body, $reply_id) {
bd9906cd 159 global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad;
160 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
e5eadbf5 161 global $draft_folder, $attachment_dir;
bd9906cd 162 $more_headers = Array();
163
164 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
165
e5eadbf5 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
8f7315af 171 $headerlength = write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers, FALSE);
172 $bodylength = writeBodyForDraft ($fp, $body, FALSE);
e5eadbf5 173 fclose($fp);
bd9906cd 174
175 $length = ($headerlength + $bodylength);
176
177 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
178 sqimap_append ($imap_stream, $draft_folder, $length);
8f7315af 179 write822HeaderForDraft ($imap_stream, $t, $c, $b, $subject, $more_headers, TRUE);
180 writeBodyForDraft ($imap_stream, $body, TRUE);
bd9906cd 181 sqimap_append_done ($imap_stream);
182 }
183 sqimap_logout($imap_stream);
184 if ($length)
185 ClearAttachments();
e5eadbf5 186 if (file_exists($attachment_dir . $tmpDraftFile) )
187 unlink ($attachment_dir . $tmpDraftFile);
bd9906cd 188 return $length;
189}
190?>