Fixed ary_sort problem.
[squirrelmail.git] / src / compose.php
CommitLineData
59177427 1<?php
ef870322 2 /**
3 ** compose.php
4 **
5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
8 ** This code sends a mail.
df15de21 9 **
10 ** There are 3 modes of operation:
11 ** - Start new mail
12 ** - Add an attachment
13 ** - Send mail
14 **/
15
2a32fc83 16 session_start();
17
d068c0ec 18 if (!isset($config_php))
19 include("../config/config.php");
20 if (!isset($strings_php))
21 include("../functions/strings.php");
22 if (!isset($page_header_php))
23 include("../functions/page_header.php");
24 if (!isset($imap_php))
25 include("../functions/imap.php");
26 if (!isset($date_php))
27 include("../functions/date.php");
28 if (!isset($mime_php))
29 include("../functions/mime.php");
30 if (!isset($smtp_php))
31 include("../functions/smtp.php");
32 if (!isset($display_messages_php))
33 include("../functions/display_messages.php");
3c13b9fb 34 if (!isset($auth_php))
35 include ("../functions/auth.php");
15bfc1bc 36 if (!isset($plugin_php))
37 include ("../functions/plugin.php");
f7fb20fe 38
d3cdb279 39 include("../src/load_prefs.php");
8467bf00 40
4ba45d11 41 // This function is used when not sending or adding attachments
df15de21 42 function newMail () {
43 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
a794e82c 44 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size;
e39d73e5 45
7aaa81fc 46 $send_to = sqStripSlashes(decodeHeader($send_to));
47 $send_to_cc = sqStripSlashes(decodeHeader($send_to_cc));
6e79bfe2 48 $send_to_bcc = sqStripSlashes(decodeHeader($send_to_bcc));
a53e5469 49
429f8906 50 if ($forward_id)
51 $id = $forward_id;
52 else if ($reply_id)
53 $id = $reply_id;
54
1195c340 55
429f8906 56 if ($id) {
813eba2f 57 sqimap_mailbox_select($imapConnection, $mailbox);
429f8906 58 $message = sqimap_get_message($imapConnection, $id, $mailbox);
8d8ab69a 59 $orig_header = $message->header;
1195c340 60 if ($ent_num)
61 $message = getEntity($message, $ent_num);
429f8906 62
63 if ($message->header->type0 == "text" || $message->header->type1 == "message") {
1195c340 64 if ($ent_num)
65 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header->encoding);
66 else
67 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header->encoding);
429f8906 68 } else {
69 $body = "";
df15de21 70 }
71
429f8906 72 if ($message->header->type1 == "html")
73 $body = strip_tags($body);
01aab860 74
75 sqUnWordWrap($body);
df15de21 76 $body_ary = explode("\n", $body);
77 $body = "";
429f8906 78 for ($i=0; $i < count($body_ary); $i++) {
01aab860 79 if (! $forward_id)
80 {
81 if (preg_match('/^[\s>]+/', $body_ary[$i]))
82 {
83 $body_ary[$i] = '>' . $body_ary[$i];
84 }
85 else
86 {
87 $body_ary[$i] = '> ' . $body_ary[$i];
88 }
89 }
a794e82c 90 sqWordWrap($body_ary[$i], $editor_size - 1);
01aab860 91 $body .= $body_ary[$i] . "\n";
92 $body_ary[$i] = '';
a794e82c 93 }
01aab860 94 if ($forward_id)
95 {
96 $bodyTop = "-------- " . _("Original Message") . " --------\n";
97 $bodyTop .= _("Subject") . ": " . $orig_header->subject . "\n";
98 $bodyTop .= _("From") . ": " . $orig_header->from . "\n";
99 $bodyTop .= _("To") . ": " . $orig_header->to[0] . "\n";
100 if (count($orig_header->to) > 1) {
101 for ($x=1; $x < count($orig_header->to); $x++) {
102 $bodyTop .= " " . $orig_header->to[$x] . "\n";
103 }
104 }
105 $bodyTop .= "\n";
106 $body = $bodyTop . $body;
78509c54 107 }
a794e82c 108
a2790a61 109 sqimap_mailbox_close($imapConnection);
01aab860 110 return;
78509c54 111 }
429f8906 112
29d08a52 113 if (!$send_to) {
114 $send_to = sqimap_find_email($send_to);
115 }
116
df15de21 117 /** This formats a CC string if they hit "reply all" **/
118 if ($send_to_cc != "") {
a48fbf9b 119 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
df15de21 120 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
121 $sendcc = explode(",", $send_to_cc);
122 $send_to_cc = "";
123
124 for ($i = 0; $i < count($sendcc); $i++) {
125 $sendcc[$i] = trim($sendcc[$i]);
126 if ($sendcc[$i] == "")
127 continue;
128
a53e5469 129 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
813eba2f 130 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
131 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
df15de21 132
133 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
134 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
135 (trim($sendcc[$i]) != "")) {
136 $send_to_cc .= trim($sendcc[$i]) . ", ";
137 }
138 }
139 $send_to_cc = trim($send_to_cc);
140 if (substr($send_to_cc, -1) == ",") {
141 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
142 }
143 }
144 } // function newMail()
78509c54 145
df15de21 146 function showInputForm () {
147 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
4ba45d11 148 $passed_body, $color, $use_signature, $signature, $editor_size,
3806fa52 149 $attachments, $subject, $newmail, $use_javascript_addr_book,
6e79bfe2 150 $send_to_bcc, $reply_id, $mailbox, $from_htmladdr_search;
78509c54 151
5f104808 152 $subject = sqStripSlashes(decodeHeader($subject));
2e434774 153 $reply_subj = decodeHeader($reply_subj);
154 $forward_subj = decodeHeader($forward_subj);
162efb6f 155 $body = sqStripSlashes($body);
01aab860 156
3806fa52 157 if ($use_javascript_addr_book) {
158 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
159 echo "function open_abook() { \n";
160 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
161 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
162 echo " if((!nwin.opener) && (document.windows != null))\n";
163 echo " nwin.opener = document.windows;\n";
164 echo "}\n";
165 echo "// --></SCRIPT>\n\n";
166 }
5100704d 167
cacf2747 168 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\">\n";
169 //echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST>\n";
966286ae 170 if ($reply_id) {
171 echo "<input type=hidden name=reply_id value=$reply_id>\n";
6e79bfe2 172 }
cf8758c7 173 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
c5d828b3 174 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
df15de21 175 echo " <TR>\n";
c5d828b3 176 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
df15de21 177 echo _("To:");
c5d828b3 178 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
cf8758c7 179 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
6e79bfe2 180 htmlspecialchars($send_to));
df15de21 181 echo " </TD>\n";
182 echo " </TR>\n";
183 echo " <TR>\n";
c5d828b3 184 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
aae41ae9 185 echo _("CC:");
c5d828b3 186 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
cf8758c7 187 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
6e79bfe2 188 htmlspecialchars($send_to_cc));
df15de21 189 echo " </TD>\n";
190 echo " </TR>\n";
191 echo " <TR>\n";
c5d828b3 192 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
aae41ae9 193 echo _("BCC:");
761d149e 194 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
cf8758c7 195 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
6e79bfe2 196 htmlspecialchars($send_to_bcc));
3806fa52 197 echo "</TD></TR>\n";
5100704d 198
df15de21 199 echo " <TR>\n";
c5d828b3 200 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
df15de21 201 echo _("Subject:");
761d149e 202 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
df15de21 203 if ($reply_subj) {
204 $reply_subj = str_replace("\"", "'", $reply_subj);
7aaa81fc 205 $reply_subj = sqStripSlashes($reply_subj);
df15de21 206 $reply_subj = trim($reply_subj);
207 if (substr(strtolower($reply_subj), 0, 3) != "re:")
208 $reply_subj = "Re: $reply_subj";
cf8758c7 209 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
6e79bfe2 210 htmlspecialchars($reply_subj));
df15de21 211 } else if ($forward_subj) {
212 $forward_subj = str_replace("\"", "'", $forward_subj);
7aaa81fc 213 $forward_subj = sqStripSlashes($forward_subj);
df15de21 214 $forward_subj = trim($forward_subj);
215 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
216 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
217 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
218 $forward_subj = "[Fwd: $forward_subj]";
cf8758c7 219 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
6e79bfe2 220 htmlspecialchars($forward_subj));
df15de21 221 } else {
6e79bfe2 222 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
223 htmlspecialchars($subject));
31f3d7c0 224 }
480feea7 225 echo "</td></tr>\n\n";
226
227 echo " <TR><td>\n </td><td>\n";
228 if ($use_javascript_addr_book) {
229 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
230 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
231 echo " // --></SCRIPT><NOSCRIPT>\n";
232 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
233 echo " </NOSCRIPT>\n";
234 } else {
235 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
236 }
237 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
15bfc1bc 238
239 do_hook("compose_button_row");
240
480feea7 241 echo " </TD>\n";
242 echo " </TR>\n\n";
243
4ba45d11 244
e5b23ff2 245 echo " <TR>\n";
c5d828b3 246 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
cf8758c7 247 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
6e79bfe2 248 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
ef3c69f0 249 echo (htmlspecialchars($body)) . "\n\n-- \n" . htmlspecialchars($signature);
cf8758c7 250 } else {
ef3c69f0 251 echo (htmlspecialchars($body));
cf8758c7 252 }
253 echo "</TEXTAREA><BR>\n";
e5b23ff2 254 echo " </TD>\n";
255 echo " </TR>\n";
e9d611cb 256 echo " <TR><TD>&nbsp;</TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
e5b23ff2 257
4ba45d11 258 // This code is for attachments
259 echo " <tr>\n";
c5d828b3 260 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
aae41ae9 261 echo " <SMALL><BR></SMALL>"._("Attach:");
c5d828b3 262 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
4ba45d11 263 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
264 // echo " value=\"10000\">\n";
944eb785 265 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
95de6c91 266 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"";
267 echo " value=\"" . _("Add") ."\">\n";
469eb37b 268 echo " </td>\n";
469eb37b 269 echo " </tr>\n";
4ba45d11 270 if (isset($attachments) && count($attachments)>0) {
162efb6f 271 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
e5b23ff2 272 echo "&nbsp;";
c5d828b3 273 echo "</td><td align=left bgcolor=\"$color[0]\">";
4ba45d11 274 while (list($localname, $remotename) = each($attachments)) {
275 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
276 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
277 }
278
279 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
469eb37b 280 echo "</td></tr>";
4ba45d11 281 }
4ba45d11 282 // End of attachment code
283
ffc2ccbc 284 echo "</TABLE>\n";
df15de21 285 echo "</FORM>";
d7d3c4d4 286 do_hook("compose_bottom");
31f3d7c0 287 }
8467bf00 288
df15de21 289 function showSentForm () {
df15de21 290 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
9f2215a1 291 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
aae41ae9 292 echo "</CENTER>";
df15de21 293 }
b278172f 294
0ad7dbda 295 function checkInput ($show) {
296 /** I implemented the $show variable because the error messages
297 were getting sent before the page header. So, I check once
298 using $show=false, and then when i'm ready to display the
299 error message, show=true **/
300 global $body, $send_to, $subject, $color;
b278172f 301
99fa2b21 302 if ($send_to == "") {
0ad7dbda 303 if ($show)
304 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
df15de21 305 return false;
b278172f 306 }
df15de21 307 return true;
308 } // function checkInput()
309
3806fa52 310
ecf51658 311 if (($mailbox == "") || ($mailbox == "None"))
dcb7f454 312 $mailbox = "INBOX";
3806fa52 313
4ba45d11 314 if(isset($send)) {
0ad7dbda 315 if (checkInput(false)) {
966286ae 316 $urlMailbox = urlencode ($mailbox);
317 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
318 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
df15de21 319 } else {
01aab860 320 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
dcb7f454 321 displayPageHeader($color, $mailbox);
0ad7dbda 322 checkInput(true);
323
df15de21 324 showInputForm();
01aab860 325 //sqimap_logout($imapConnection);
7c6cb7ca 326 }
3806fa52 327 } else if ($html_addr_search_done) {
3c13b9fb 328 is_logged_in();
dcb7f454 329 displayPageHeader($color, $mailbox);
3806fa52 330
7aaa81fc 331 $send_to = sqStripSlashes($send_to);
332 $send_to_cc = sqStripSlashes($send_to_cc);
333 $send_to_bcc = sqStripSlashes($send_to_bcc);
3806fa52 334
6c7fd6ca 335 for ($i=0; $i < count($send_to_search); $i++) {
336 if ($send_to)
337 $send_to .= ", ";
338 $send_to .= $send_to_search[$i];
339 }
340
341 for ($i=0; $i < count($send_to_cc_search); $i++) {
342 if ($send_to_cc)
343 $send_to_cc .= ", ";
344 $send_to_cc .= $send_to_cc_search[$i];
345 }
346
3806fa52 347 showInputForm();
6c7fd6ca 348 } else if ($html_addr_search) {
591d2a88 349 // I am using an include so as to elminiate an extra unnecessary click. If you
350 // can think of a better way, please implement it.
6c7fd6ca 351 include ("addrbook_search_html.php");
4ba45d11 352 } else if (isset($attach)) {
3c13b9fb 353 is_logged_in();
dcb7f454 354 displayPageHeader($color, $mailbox);
fc3348ac 355
6519e12b 356 $localfilename = md5($HTTP_POST_FILES['attachfile']['tmp_name'].", ".$HTTP_POST_FILES['attachfile']['name'].", $REMOTE_IP, $REMOTE_PORT, $UNIQUE_ID, and everything else that may add entropy");
357// $localfilename = $localfilename; // ??
4ba45d11 358
359 // Put the file in a better place
162efb6f 360 // This shouldn't be here... Ondrej Sury <ondrej@sury.cz>
361 //$tmp=explode('/',$attachfile);
362 //$attachfile=$tmp[count($tmp)-1];
363 //$attachfile=ereg_replace('\.{2,}','',$attachfile);
cacf2747 364
162efb6f 365 //error_reporting(0); // Rename will produce error output if it fails
366 //if (!rename($attachfile, $attachment_dir.$localfilename)) {
367 // if (!copy($attachfile, $attachment_dir.$localfilename)) {
6519e12b 368 if (!@rename($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
369 if (!@copy($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
22ef7536 370 plain_error_message(_("Could not move/copy file. File not attached"), $color);
c3c37167 371 $failed = true;
4ba45d11 372 }
373 }
374 // If it still exists, PHP will remove the original file
375
c3c37167 376 if (!$failed) {
377 // Write information about the file
378 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
6519e12b 379 fputs ($fp, $HTTP_POST_FILES['attachfile']['type']."\n".$HTTP_POST_FILES['attachfile']['name']."\n");
c3c37167 380 fclose ($fp);
4ba45d11 381
6519e12b 382 $attachments[$localfilename] = $HTTP_POST_FILES['attachfile']['name'];
c3c37167 383 }
4ba45d11 384
385 showInputForm();
386 } else if (isset($do_delete)) {
3c13b9fb 387 is_logged_in();
dcb7f454 388 displayPageHeader($color, $mailbox);
fc3348ac 389
fb16d219 390 while (list($lkey, $localname) = each($delete)) {
391 array_splice ($attachments, $lkey, 1);
c3c37167 392 unlink ($attachment_dir.$localname);
393 unlink ($attachment_dir.$localname.".info");
4ba45d11 394 }
4bfed9f3 395
4ba45d11 396 showInputForm();
397 } else {
a60b9989 398 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
dcb7f454 399 displayPageHeader($color, $mailbox);
fc3348ac 400
b57c4e63 401 $newmail = true;
1220e677 402 newMail();
4ba45d11 403 showInputForm();
1195c340 404 sqimap_logout($imapConnection);
4ba45d11 405 }
da79853a 406?>