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