Added session variable $is_logged_in.
[squirrelmail.git] / src / compose.php
1 <?php
2 /** This code sends a mail.
3 **
4 ** There are 3 modes of operation:
5 ** - Start new mail
6 ** - Add an attachment
7 ** - Send mail
8 **/
9
10 session_start();
11
12 if (!isset($config_php))
13 include("../config/config.php");
14 if (!isset($strings_php))
15 include("../functions/strings.php");
16 if (!isset($page_header_php))
17 include("../functions/page_header.php");
18 if (!isset($imap_php))
19 include("../functions/imap.php");
20 if (!isset($date_php))
21 include("../functions/date.php");
22 if (!isset($mime_php))
23 include("../functions/mime.php");
24 if (!isset($smtp_php))
25 include("../functions/smtp.php");
26 if (!isset($display_messages_php))
27 include("../functions/display_messages.php");
28
29 include("../src/load_prefs.php");
30
31 // This function is used when not sending or adding attachments
32 function newMail () {
33 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
34 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc;
35
36 $send_to = decodeHeader($send_to);
37 $send_to_cc = decodeHeader($send_to_cc);
38
39 if ($forward_id)
40 $id = $forward_id;
41 else if ($reply_id)
42 $id = $reply_id;
43
44 if ($id) {
45 sqimap_mailbox_select($imapConnection, $mailbox);
46 $message = sqimap_get_message($imapConnection, $id, $mailbox);
47 $message = getEntity($message, $ent_num);
48
49 if ($message->header->type0 == "text" || $message->header->type1 == "message") {
50 $body = decodeBody(mime_fetch_body($imapConnection, $id, $message->header->entity_id), $message->header->encoding);
51 } else {
52 $body = "";
53 }
54
55 if ($forward_id)
56 $tmp = _("-------- Original Message ---------\n");
57 if ($message->header->type1 == "html")
58 $body = strip_tags($body);
59
60 $body_ary = explode("\n", $body);
61 $body = "";
62 for ($i=0; $i < count($body_ary); $i++) {
63 $tmp = $body_ary[$i];
64
65 if ($forward_id)
66 $body = "$body$tmp\n";
67 else
68 $body = "$body> $tmp\n";
69 }
70
71 }
72
73 if (!$send_to) {
74 $send_to = sqimap_find_email($send_to);
75 }
76
77 $send_to = ereg_replace("\"", "", $send_to);
78 $send_to = stripslashes($send_to);
79
80 /** This formats a CC string if they hit "reply all" **/
81 if ($send_to_cc != "") {
82 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
83 $sendcc = explode(",", $send_to_cc);
84 $send_to_cc = "";
85
86 for ($i = 0; $i < count($sendcc); $i++) {
87 $sendcc[$i] = trim($sendcc[$i]);
88 if ($sendcc[$i] == "")
89 continue;
90
91 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
92 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
93 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
94
95 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
96 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
97 (trim($sendcc[$i]) != "")) {
98 $send_to_cc .= trim($sendcc[$i]) . ", ";
99 }
100 }
101 $send_to_cc = trim($send_to_cc);
102 if (substr($send_to_cc, -1) == ",") {
103 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
104 }
105 }
106 } // function newMail()
107
108 function showInputForm () {
109 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
110 $passed_body, $color, $use_signature, $signature, $editor_size,
111 $attachments, $subject, $newmail, $use_javascript_addr_book,
112 $send_to_bcc, $reply_id, $mailbox;
113
114 $subject = decodeHeader($subject);
115 $reply_subj = decodeHeader($reply_subj);
116 $forward_subj = decodeHeader($forward_subj);
117
118 if ($use_javascript_addr_book) {
119 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
120 echo "function open_abook() { \n";
121 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
122 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
123 echo " if((!nwin.opener) && (document.windows != null))\n";
124 echo " nwin.opener = document.windows;\n";
125 echo "}\n";
126 echo "// --></SCRIPT>\n\n";
127 }
128
129 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\">\n";
130 if ($reply_id) {
131 echo "<input type=hidden name=reply_id value=$reply_id>\n";
132 }
133 echo "<input type=hidden name=mailbox value=$mailbox>\n";
134 echo "<TABLE WIDTH=50 ALIGN=center CELLSPACING=0 BORDER=0>\n";
135 echo " <TR>\n";
136 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
137 echo _("To:");
138 echo " </TD><TD colspan=2 WIDTH=\"100%\" BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
139 if ($send_to)
140 echo " <INPUT TYPE=TEXT NAME=\"send_to\" VALUE=\"$send_to\" SIZE=60><BR>\n";
141 else
142 echo " <INPUT TYPE=TEXT NAME=\"send_to\" SIZE=60><BR>\n";
143 echo " </TD>\n";
144 echo " </TR>\n";
145 echo " <TR>\n";
146 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
147 echo _("CC:");
148 echo " </TD><TD colspan=2 BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
149 if ($send_to_cc)
150 echo " <INPUT TYPE=TEXT NAME=\"send_to_cc\" SIZE=60 VALUE=\"$send_to_cc\"><BR>\n";
151 else
152 echo " <INPUT TYPE=TEXT NAME=\"send_to_cc\" SIZE=60><BR>\n";
153 echo " </TD>\n";
154 echo " </TR>\n";
155 echo " <TR>\n";
156 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
157 echo _("BCC:");
158 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
159 if ($send_to_bcc)
160 echo " <INPUT TYPE=TEXT NAME=\"send_to_bcc\" VALUE=\"$send_to_bcc\" SIZE=60><BR>\n";
161 else
162 echo " <INPUT TYPE=TEXT NAME=\"send_to_bcc\" SIZE=60><BR>";
163
164 echo "</TD></TR>\n";
165
166 echo " <TR>\n";
167 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
168 echo _("Subject:");
169 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
170 if ($reply_subj) {
171 $reply_subj = str_replace("\"", "'", $reply_subj);
172 $reply_subj = stripslashes($reply_subj);
173 $reply_subj = trim($reply_subj);
174 if (substr(strtolower($reply_subj), 0, 3) != "re:")
175 $reply_subj = "Re: $reply_subj";
176 echo " <INPUT TYPE=TEXT NAME=subject SIZE=60 VALUE=\"$reply_subj\">";
177 } else if ($forward_subj) {
178 $forward_subj = str_replace("\"", "'", $forward_subj);
179 $forward_subj = stripslashes($forward_subj);
180 $forward_subj = trim($forward_subj);
181 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
182 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
183 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
184 $forward_subj = "[Fwd: $forward_subj]";
185 echo " <INPUT TYPE=TEXT NAME=subject SIZE=60 VALUE=\"$forward_subj\">";
186 } else {
187 echo " <INPUT TYPE=TEXT NAME=subject VALUE=\"$subject\" SIZE=60>";
188 }
189 echo "</td></tr>\n\n";
190
191 echo " <TR><td>\n </td><td>\n";
192 if ($use_javascript_addr_book) {
193 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
194 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
195 echo " // --></SCRIPT><NOSCRIPT>\n";
196 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
197 echo " </NOSCRIPT>\n";
198 } else {
199 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
200 }
201 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
202 echo " </TD>\n";
203 echo " </TR>\n\n";
204
205
206 echo " <TR>\n";
207 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=3>\n";
208 if ($use_signature == true && $newmail == true)
209 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>". $body . "\n\n-- \n".$signature."</TEXTAREA><BR>";
210 else
211 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>".$body."</TEXTAREA><BR>\n";
212 echo " </TD>\n";
213 echo " </TR>\n";
214 echo " <TR><TD COLSPAN=3 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=send VALUE=\"";
215 echo _("Send");
216 echo "\"></TD></TR>\n";
217
218 // This code is for attachments
219 echo " <tr>\n";
220 echo " <TD WIDTH=50 BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
221 echo " <SMALL><BR></SMALL>"._("Attach:");
222 echo " </td><td colspan=2 ALIGN=left BGCOLOR=\"$color[0]\">\n";
223 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
224 // echo " value=\"10000\">\n";
225 echo " <INPUT NAME=\"attachfile\" TYPE=\"file\">\n";
226 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"\n";
227 echo " value=\"" . _("Add") ."\">\n";
228 echo " </td>\n";
229 echo " </tr>\n";
230 if (isset($attachments) && count($attachments)>0) {
231 echo "</tr><tr><td width=50 bgcolor=\"$color[0]\" align=right>\n";
232 echo "&nbsp;";
233 echo "</td><td align=left colspan=2 bgcolor=\"$color[0]\">";
234 while (list($localname, $remotename) = each($attachments)) {
235 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
236 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
237 }
238
239 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
240 echo "</td></tr>";
241 }
242 // End of attachment code
243
244 echo "</TABLE>\n";
245 echo "</FORM>";
246 }
247
248 function showSentForm () {
249 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
250 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
251 echo "</CENTER>";
252 }
253
254 function checkInput ($show) {
255 /** I implemented the $show variable because the error messages
256 were getting sent before the page header. So, I check once
257 using $show=false, and then when i'm ready to display the
258 error message, show=true **/
259 global $body, $send_to, $subject, $color;
260
261 if ($body == "" && $subject == "") {
262 if ($show)
263 plain_error_message(_("You have not entered a message body or a subject."), $color);
264 return false;
265 } else if ($send_to == "") {
266 if ($show)
267 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
268 return false;
269 }
270 return true;
271 } // function checkInput()
272
273
274 if (($mailbox == "") || ($mailbox == "None"))
275 $mailbox = "INBOX";
276
277 if(isset($send)) {
278 if (checkInput(false)) {
279 $urlMailbox = urlencode ($mailbox);
280 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
281 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
282 } else {
283 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
284 displayPageHeader($color, $mailbox);
285 checkInput(true);
286
287 showInputForm();
288 }
289 } else if ($html_addr_search_done) {
290 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
291 displayPageHeader($color, $mailbox);
292
293 $body = stripslashes($body);
294 $send_to = stripslashes($send_to);
295 $send_to_cc = stripslashes($send_to_cc);
296 $send_to_bcc = stripslashes($send_to_bcc);
297 $subject = stripslashes($subject);
298
299 for ($i=0; $i < count($send_to_search); $i++) {
300 if ($send_to)
301 $send_to .= ", ";
302 $send_to .= $send_to_search[$i];
303 }
304
305 for ($i=0; $i < count($send_to_cc_search); $i++) {
306 if ($send_to_cc)
307 $send_to_cc .= ", ";
308 $send_to_cc .= $send_to_cc_search[$i];
309 }
310
311 showInputForm();
312 } else if ($html_addr_search) {
313 //* I am using an include so as to elminiate an extra unnecessary click. If you
314 //* can think of a better way, please implement it.
315 include ("addrbook_search_html.php");
316 } else if (isset($attach)) {
317 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
318 displayPageHeader($color, $mailbox);
319
320 $localfilename = md5("$attachfile, $attachfile_name, $REMOTE_IP, $REMOTE_PORT, $UNIQUE_ID, and everything else that may add entropy");
321 $localfilename = $localfilename;
322
323 // Put the file in a better place
324 error_reporting(0); // Rename will produce error output if it fails
325 if (!rename($attachfile, $attachment_dir.$localfilename)) {
326 if (!copy($attachfile, $attachment_dir.$localfilename)) {
327 plain_error_message(_("Could not move/copy file. File not attached"));
328 $failed = true;
329 }
330 }
331 // If it still exists, PHP will remove the original file
332
333 if (!$failed) {
334 // Write information about the file
335 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
336 fputs ($fp, "$attachfile_type\n$attachfile_name\n");
337 fclose ($fp);
338
339 $attachments[$localfilename] = $attachfile_name;
340 }
341
342 showInputForm();
343 } else if (isset($do_delete)) {
344 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
345 displayPageHeader($color, $mailbox);
346
347 while (list($key, $localname) = each($delete)) {
348 array_splice ($attachments, $key, 1);
349 unlink ($attachment_dir.$localname);
350 unlink ($attachment_dir.$localname.".info");
351 }
352
353 showInputForm();
354 } else {
355 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
356 displayPageHeader($color, $mailbox);
357
358 $newmail = true;
359 newMail();
360 showInputForm();
361 }
362 ?>