Added option to do data and attachment directory hashing, up to four levels. Will...
[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 12require_once ('../src/validate.php');
bd9906cd 13
e7e73ef1 14/* Print all the needed RFC822 headers */
15function write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers) {
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
e7e73ef1 87 if (isMultipart()) {
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 */
110function writeBodyForDraft ($fp, $passedBody) {
111 global $default_charset;
8f7315af 112
e7e73ef1 113 $attachmentlength = 0;
8f7315af 114
e7e73ef1 115 if (isMultipart()) {
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
e7e73ef1 128 $attachmentlength = attachFiles($fp);
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
e7e73ef1 144function saveMessageAsDraft($t, $c, $b, $subject, $body, $reply_id) {
3392dc86 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;
e7e73ef1 148 $more_headers = Array();
bd9906cd 149
e7e73ef1 150 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
bd9906cd 151
3392dc86 152 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
153
e7e73ef1 154 $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7);
3392dc86 155 $full_tmpDraftFile = "$hashed_attachment_dir/$tmpDraftFile";
156 while (file_exists($full_tmpDraftFile)){
e5eadbf5 157 $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7);
3392dc86 158 $full_tmpDraftFile = "$hashed_attachment_dir/$tmpDraftFile";
e7e73ef1 159 }
3392dc86 160 $fp = fopen($full_tmpDraftFile, 'w');
e7e73ef1 161
3392dc86 162 $headerlength = write822HeaderForDraft
163 ($fp, $t, $c, $b, $subject, $more_headers, FALSE);
e7e73ef1 164 $bodylength = writeBodyForDraft ($fp, $body, FALSE);
165 fclose($fp);
166
167 $length = ($headerlength + $bodylength);
168
169 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
170 sqimap_append ($imap_stream, $draft_folder, $length);
3392dc86 171 write822HeaderForDraft
172 ($imap_stream, $t, $c, $b, $subject, $more_headers, TRUE);
e7e73ef1 173 writeBodyForDraft ($imap_stream, $body, TRUE);
174 sqimap_append_done ($imap_stream);
175 }
176 sqimap_logout($imap_stream);
177 if ($length){
178 ClearAttachments();
179 }
3392dc86 180 if (file_exists($full_tmpDraftFile)){
181 unlink ($full_tmpDraftFile);
e7e73ef1 182 }
183 return $length;
bd9906cd 184}
185?>