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