72d1d209927b90579b0774f349b5b10c527362b3
[squirrelmail.git] / src / compose.php
1 <?php
2
3 /**
4 * compose.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This code sends a mail.
10 *
11 * There are 4 modes of operation:
12 * - Start new mail
13 * - Add an attachment
14 * - Send mail
15 * - Save As Draft
16 *
17 * $Id$
18 */
19
20 /*****************************************************************/
21 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
22 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
23 /*** + Base level indent should begin at left margin, as ***/
24 /*** the require_once below looks. ***/
25 /*** + All identation should consist of four space blocks ***/
26 /*** + Tab characters are evil. ***/
27 /*** + all comments should use "slash-star ... star-slash" ***/
28 /*** style -- no pound characters, no slash-slash style ***/
29 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
30 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
31 /*** + Please use ' instead of ", when possible. Note " ***/
32 /*** should always be used in _( ) function calls. ***/
33 /*** Thank you for your help making the SM code more readable. ***/
34 /*****************************************************************/
35
36 require_once('../src/validate.php');
37 require_once('../functions/imap.php');
38 require_once('../functions/date.php');
39 require_once('../functions/mime.php');
40 require_once('../functions/smtp.php');
41 require_once('../functions/display_messages.php');
42 require_once('../functions/plugin.php');
43
44 if (!isset($attachments)) {
45 $attachments = array();
46 session_register('attachments');
47 }
48
49 /* This function is used when not sending or adding attachments */
50 function newMail () {
51 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
52 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size,
53 $draft_id, $use_signature;
54
55 $send_to = decodeHeader($send_to);
56 $send_to_cc = decodeHeader($send_to_cc);
57 $send_to_bcc = decodeHeader($send_to_bcc);
58
59 if ($forward_id) {
60 $id = $forward_id;
61 } elseif ($reply_id) {
62 $id = $reply_id;
63 }
64
65 if ($draft_id){
66 $id = $draft_id;
67 $use_signature = FALSE;
68 }
69
70 if (isset($id)) {
71 sqimap_mailbox_select($imapConnection, $mailbox);
72 $message = sqimap_get_message($imapConnection, $id, $mailbox);
73 $orig_header = $message->header;
74 if ($ent_num) {
75 $message = getEntity($message, $ent_num);
76 }
77 if ($message->header->type0 == 'text' ||
78 $message->header->type1 == 'message') {
79 if ($ent_num) {
80 $body = decodeBody(
81 mime_fetch_body($imapConnection, $id, $ent_num),
82 $message->header->encoding);
83 } else {
84 $body = decodeBody(
85 mime_fetch_body($imapConnection, $id, 1),
86 $message->header->encoding);
87 }
88 }
89 else {
90 $body = '';
91 }
92
93 if ($message->header->type1 == 'html') {
94 $body = strip_tags($body);
95 }
96
97 sqUnWordWrap($body);
98 $body_ary = explode("\n", $body);
99 $i = count($body_ary) - 1;
100 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
101 unset($body_ary[$i]);
102 $i --;
103 }
104 $body = '';
105 for ($i=0; isset($body_ary[$i]); $i++) {
106 if ($reply_id) {
107 if (ereg('^[ >]+', $body_ary[$i])) {
108 $body_ary[$i] = '>' . $body_ary[$i];
109 }
110 else {
111 $body_ary[$i] = '> ' . $body_ary[$i];
112 }
113 }
114 if (!$draft_id) {
115 sqWordWrap($body_ary[$i], $editor_size - 1);
116 }
117 $body .= $body_ary[$i] . "\n";
118 unset($body_ary[$i]);
119 }
120 if ($forward_id) {
121 $bodyTop = '-------- ' . _("Original Message") . " --------\n" .
122 _("Subject") . ': ' . $orig_header->subject . "\n" .
123 _("From") . ': ' . $orig_header->from . "\n" .
124 _("Date") . ': ' .
125 getLongDateString( $orig_header->date ). "\n" .
126 _("To") . ': ' . $orig_header->to[0] . "\n";
127 if (count($orig_header->to) > 1) {
128 for ($x=1; $x < count($orig_header->to); $x++) {
129 $bodyTop .= ' ' . $orig_header->to[$x] . "\n";
130 }
131 }
132 $bodyTop .= "\n";
133 $body = $bodyTop . $body;
134 }
135 elseif ($reply_id) {
136 $orig_from = decodeHeader($orig_header->from);
137 $body = getReplyCitation($orig_from) . $body;
138 }
139
140 return;
141 }
142
143 if (!$send_to) {
144 $send_to = sqimap_find_email($send_to);
145 }
146
147 /* This formats a CC string if they hit "reply all" */
148 if ($send_to_cc != '') {
149 $send_to_cc = ereg_replace('"[^"]*"', '', $send_to_cc);
150 $send_to_cc = ereg_replace(';', ',', $send_to_cc);
151 $sendcc = explode(',', $send_to_cc);
152 $send_to_cc = '';
153
154 for ($i = 0; $i < count($sendcc); $i++) {
155 $sendcc[$i] = trim($sendcc[$i]);
156 if ($sendcc[$i] == '') {
157 continue;
158 }
159
160 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
161 $whofrom = sqimap_find_displayable_name($msg['HEADER']['FROM']);
162 $whoreplyto = sqimap_find_email($msg['HEADER']['REPLYTO']);
163
164 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
165 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
166 (trim($sendcc[$i]) != '')) {
167 $send_to_cc .= trim($sendcc[$i]) . ', ';
168 }
169 }
170 $send_to_cc = trim($send_to_cc);
171 if (substr($send_to_cc, -1) == ',') {
172 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
173 }
174 }
175 } /* function newMail() */
176
177
178 function getAttachments($message) {
179 global $mailbox, $attachments, $attachment_dir, $imapConnection,
180 $ent_num, $forward_id, $draft_id, $username;
181
182 if (isset($draft_id)) {
183 $id = $draft_id;
184 }
185 else {
186 $id = $forward_id;
187 }
188
189 if (!$message) {
190 sqimap_mailbox_select($imapConnection, $mailbox);
191 $message = sqimap_get_message($imapConnection, $id, $mailbox);
192 }
193
194 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
195 if (count($message->entities) == 0) {
196 if ($message->header->entity_id != $ent_num) {
197 $filename = decodeHeader($message->header->filename);
198
199 if ($filename == "") {
200 $filename = "untitled-".$message->header->entity_id;
201 }
202
203 $localfilename = GenerateRandomString(32, '', 7);
204 $full_localfilename = "$hashed_attachment_dir/$localfilename";
205 while (file_exists($full_localfilename)) {
206 $localfilename = GenerateRandomString(32, '', 7);
207 $full_localfilename = "$hashed_attachment_dir/$localfilename";
208 }
209
210 $newAttachment = array();
211 $newAttachment['localfilename'] = $localfilename;
212 $newAttachment['remotefilename'] = $filename;
213 $newAttachment['type'] = strtolower($message->header->type0 .
214 '/' . $message->header->type1);
215
216 /* Write Attachment to file */
217 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'w');
218 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
219 $id, $message->header->entity_id),
220 $message->header->encoding));
221 fclose ($fp);
222
223 $attachments[] = $newAttachment;
224 }
225 }
226 else {
227 for ($i = 0; $i < count($message->entities); $i++) {
228 getAttachments($message->entities[$i]);
229 }
230 }
231 return;
232 }
233
234 function showInputForm () {
235 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
236 $passed_body, $color, $use_signature, $signature, $prefix_sig,
237 $editor_size, $attachments, $subject, $newmail,
238 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
239 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
240 $username, $data_dir, $identity, $draft_id, $delete_draft,
241 $mailprio;
242
243 $subject = decodeHeader($subject);
244 $reply_subj = decodeHeader($reply_subj);
245 $forward_subj = decodeHeader($forward_subj);
246
247 if ($use_javascript_addr_book) {
248 echo "\n". '<SCRIPT LANGUAGE=JavaScript><!--' . "\n" .
249 'function open_abook() { ' . "\n" .
250 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
251 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
252 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
253 ' nwin.opener = document.windows;' . "\n" .
254 "}\n" .
255 '// --></SCRIPT>' . "\n\n";
256 }
257
258 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
259 'ENCTYPE="multipart/form-data"';
260 do_hook("compose_form");
261 echo ">\n";
262
263 if (isset($draft_id)) {
264 echo '<input type="hidden" name="delete_draft" value="' . $draft_id . "\">\n";
265 }
266 if (isset($delete_draft)) {
267 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
268 }
269
270 echo '<TABLE WIDTH="100%" ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
271
272 if ($location_of_buttons == 'top') {
273 showComposeButtonRow();
274 }
275
276 $idents = getPref($data_dir, $username, 'identities');
277 if ($idents != '' && $idents > 1) {
278 echo ' <TR>' . "\n" .
279 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
280 "\n" .
281 _("From:") .
282 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
283 '<select name=identity>' . "\n" .
284 '<option value=default>' .
285 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
286 $em = getPref($data_dir, $username, 'email_address');
287 if ($em != '') {
288 echo htmlspecialchars(' <' . $em . '>') . "\n";
289 }
290 for ($i = 1; $i < $idents; $i ++) {
291 echo '<option value="' . $i . '"';
292 if (isset($identity) && $identity == $i) {
293 echo ' SELECTED';
294 }
295 echo '>' . htmlspecialchars(getPref($data_dir, $username,
296 'full_name' . $i));
297 $em = getPref($data_dir, $username, 'email_address' . $i);
298 if ($em != '') {
299 echo htmlspecialchars(' <' . $em . '>') . "\n";
300 }
301 }
302 echo '</select>' . "\n" .
303 ' </TD>' . "\n" .
304 ' </TR>' . "\n";
305 }
306 echo ' <TR>' . "\n" .
307 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' . "\n" .
308 _("To:") .
309 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
310 ' <INPUT TYPE=text NAME="send_to" VALUE="' .
311 htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
312 ' </TD>' . "\n" .
313 ' </TR>' . "\n" .
314 ' <TR>' . "\n" .
315 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
316 _("CC:") .
317 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
318 ' <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
319 htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
320 ' </TD>' . "\n" .
321 ' </TR>' . "\n" .
322 ' <TR>' . "\n" .
323 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
324 _("BCC:") .
325 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
326 ' <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
327 htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
328 '</TD></TR>' . "\n" .
329 ' <TR>' . "\n" .
330 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
331 _("Subject:") .
332 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
333 if ($reply_subj) {
334 $reply_subj = str_replace('"', "'", $reply_subj);
335 $reply_subj = trim($reply_subj);
336 if (substr(strtolower($reply_subj), 0, 3) != 're:') {
337 $reply_subj = 'Re: ' . $reply_subj;
338 }
339 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
340 htmlspecialchars($reply_subj) . '">';
341 }
342 elseif ($forward_subj) {
343 $forward_subj = trim($forward_subj);
344 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
345 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
346 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:')) {
347 $forward_subj = '[Fwd: ' . $forward_subj . ']';
348 }
349 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
350 htmlspecialchars($forward_subj) . '">';
351 }
352 else {
353 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
354 htmlspecialchars($subject) . '">';
355 }
356 echo '</td></tr>' . "\n\n";
357
358 if ($location_of_buttons == 'between') {
359 showComposeButtonRow();
360 }
361
362 echo ' <TR>' . "\n" .
363 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
364 ' &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS="' .
365 $editor_size . '" WRAP=HARD>' . htmlspecialchars($body);
366 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
367 if ( $prefix_sig == true ) {
368 echo "\n\n-- \n" . htmlspecialchars($signature);
369 } else {
370 echo "\n\n" . htmlspecialchars($signature);
371 }
372 }
373 echo '</TEXTAREA><BR>' . "\n" .
374 ' </TD>' . "\n" .
375 ' </TR>' . "\n";
376
377 if ($location_of_buttons == 'bottom') {
378 showComposeButtonRow();
379 } else {
380 echo ' <TR><TD>&nbsp;</TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT ' .
381 'NAME=send VALUE="' . _("Send") . '"></TD></TR>' . "\n";
382 }
383
384 /* This code is for attachments */
385 echo ' <TR>' . "\n" .
386 ' <TD VALIGN=MIDDLE ALIGN=RIGHT>' . "\n" .
387 _("Attach:") .
388 ' </TD>' . "\n" .
389 ' <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
390 ' <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
391 ' &nbsp;&nbsp;<input type="submit" name="attach"' .
392 ' value="' . _("Add") .'">' . "\n" .
393 ' </TD>' . "\n" .
394 ' </TR>' . "\n";
395 if (count($attachments)) {
396 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
397 echo '<tr><td bgcolor="' . $color[0] . '" align=right>' . "\n" .
398 '&nbsp;' .
399 '</td><td align=left bgcolor="' . $color[0] . '">';
400 foreach ($attachments as $key => $info) {
401 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
402 echo '<input type="checkbox" name="delete[]" value="' . $key . "\">\n" .
403 $info['remotefilename'] . ' - ' . $info['type'] . ' (' .
404 show_readable_size(filesize($attached_file)) . ")<br>\n";
405 }
406
407 echo '<input type="submit" name="do_delete" value="' .
408 _("Delete selected attachments") . "\">\n" .
409 '</td></tr>';
410 }
411 /* End of attachment code */
412
413 echo '</TABLE>' . "\n";
414 if ($reply_id) {
415 echo '<input type=hidden name=reply_id value=' . $reply_id . ">\n";
416 }
417 echo '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
418 "\">\n" .
419 '</FORM>';
420 do_hook("compose_bottom");
421 echo '</BODY></HTML>' . "\n";
422 }
423
424
425 function showComposeButtonRow()
426 {
427 global $use_javascript_addr_book, $save_as_draft,
428 $default_use_priority, $mailprio;
429
430 echo " <TR><td>\n </td><td>\n";
431 if ($use_javascript_addr_book) {
432 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
433 echo " <input type=button value=\\\""._("Addresses").
434 "\\\" onclick='javascript:open_abook();'>\");";
435 echo " // --></SCRIPT><NOSCRIPT>\n";
436 echo " <input type=submit name=\"html_addr_search\" value=\"".
437 _("Addresses")."\">";
438 echo " </NOSCRIPT>\n";
439 }
440 else {
441 echo " <input type=submit name=\"html_addr_search\" value=\"".
442 _("Addresses")."\">";
443 }
444 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
445
446 if ($save_as_draft) {
447 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
448 }
449 if ($default_use_priority) {
450 if(!isset($mailprio)) {
451 $mailprio = "3";
452 }
453 echo "\n\t". _("Priority") .':<select name="mailprio">'.
454 "\n\t\t<option value=1".($mailprio=='1'?' selected':'').'>'. _("High") .'</option>'.
455 "\n\t\t<option value=3".($mailprio=='3'?' selected':'').'>'. _("Normal") .'</option>'.
456 "\n\t\t<option value=5".($mailprio=='5'?' selected':'').'>'. _("Low").'</option>'.
457 "\n\t</select>";
458 }
459
460 do_hook('compose_button_row');
461
462 echo " </TD>\n";
463 echo " </TR>\n\n";
464 }
465
466 function checkInput ($show)
467 {
468 /*
469 * I implemented the $show variable because the error messages
470 * were getting sent before the page header. So, I check once
471 * using $show=false, and then when i'm ready to display the error
472 * message, show=true
473 */
474 global $body, $send_to, $subject, $color;
475
476 if ($send_to == "") {
477 if ($show) {
478 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
479 }
480 return false;
481 }
482 return true;
483 } /* function checkInput() */
484
485
486 // True if FAILURE
487 function saveAttachedFiles() {
488 global $HTTP_POST_FILES, $attachment_dir, $attachments, $username;
489
490 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
491 $localfilename = GenerateRandomString(32, '', 7);
492 $full_localfilename = "$hashed_attachment_dir/$localfilename";
493 while (file_exists($full_localfilename)) {
494 $localfilename = GenerateRandomString(32, '', 7);
495 $full_localfilename = "$hashed_attachment_dir/$localfilename";
496 }
497
498 if (!@rename($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
499 if (!@copy($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
500 return true;
501 }
502 }
503
504 $newAttachment['localfilename'] = $localfilename;
505 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
506 $newAttachment['type'] =
507 strtolower($HTTP_POST_FILES['attachfile']['type']);
508
509 if ($newAttachment['type'] == "")
510 $newAttachment['type'] = 'application/octet-stream';
511
512 $attachments[] = $newAttachment;
513 }
514
515 if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None'))
516 $mailbox = "INBOX";
517
518 if (isset($draft)) {
519 require_once ('../src/draft_actions.php');
520 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
521 showInputForm();
522 exit();
523 } else {
524 $draft_message = _("Draft Email Saved");
525 /* If this is a resumed draft, then delete the original */
526 if(isset($delete_draft)) {
527 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
528 exit();
529 } else {
530 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort&startMessage=1&note=$draft_message");
531 exit();
532 }
533 }
534 }
535
536 if (isset($send)) {
537 if (isset($HTTP_POST_FILES['attachfile']) &&
538 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
539 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
540 $AttachFailure = saveAttachedFiles();
541 if (checkInput(false) && !isset($AttachFailure)) {
542 $urlMailbox = urlencode (trim($mailbox));
543 if (! isset($reply_id))
544 $reply_id = 0;
545 // Set $default_charset to correspond with the user's selection
546 // of language interface.
547 set_my_charset();
548
549 // This is to change all newlines to \n
550 // We'll change them to \r\n later (in the sendMessage function)
551 $body = str_replace("\r\n", "\n", $body);
552 $body = str_replace("\r", "\n", $body);
553
554 // Rewrap $body so that no line is bigger than $editor_size
555 // This should only really kick in the sqWordWrap function
556 // if the browser doesn't support "HARD" as the wrap type
557 // Or, in Opera's case, something goes wrong.
558 $body = explode("\n", $body);
559 $newBody = '';
560 foreach ($body as $line) {
561 if( $line <> '-- ' )
562 $line = rtrim($line);
563 if (strlen($line) <= $editor_size + 1)
564 $newBody .= $line . "\n";
565 else {
566 sqWordWrap($line, $editor_size) . "\n";
567 $newBody .= $line;
568 }
569 }
570 $body = $newBody;
571
572 do_hook("compose_send");
573
574 if (! isset($mailprio))
575 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
576 $subject, $body, $reply_id);
577 else
578 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
579 $subject, $body, $reply_id, $mailprio);
580 if (! $Result) {
581 showInputForm();
582 exit();
583 }
584 if ( isset($delete_draft)) {
585 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
586 exit();
587 }
588
589 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
590 } else {
591 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
592 displayPageHeader($color, $mailbox);
593
594 if (isset($AttachFailure))
595 plain_error_message(_("Could not move/copy file. File not attached"), $color);
596
597 checkInput(true);
598
599 showInputForm();
600 //sqimap_logout($imapConnection);
601 }
602 } else if (isset($html_addr_search_done)) {
603 displayPageHeader($color, $mailbox);
604
605 if (isset($send_to_search) && is_array($send_to_search)) {
606 foreach ($send_to_search as $k => $v) {
607 if (substr($k, 0, 1) == 'T') {
608 if ($send_to)
609 $send_to .= ', ';
610 $send_to .= $v;
611 }
612 elseif (substr($k, 0, 1) == 'C') {
613 if ($send_to_cc)
614 $send_to_cc .= ', ';
615 $send_to_cc .= $v;
616 }
617 elseif (substr($k, 0, 1) == 'B') {
618 if ($send_to_bcc)
619 $send_to_bcc .= ', ';
620 $send_to_bcc .= $v;
621 }
622 }
623 }
624
625 showInputForm();
626 } else if (isset($html_addr_search)) {
627 if (isset($HTTP_POST_FILES['attachfile']) &&
628 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
629 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
630 {
631 if (saveAttachedFiles())
632 plain_error_message(_("Could not move/copy file. File not attached"), $color);
633 }
634 // I am using an include so as to elminiate an extra unnecessary click. If you
635 // can think of a better way, please implement it.
636 include_once('./addrbook_search_html.php');
637 } else if (isset($attach)) {
638 if (saveAttachedFiles())
639 plain_error_message(_("Could not move/copy file. File not attached"), $color);
640 displayPageHeader($color, $mailbox);
641 showInputForm();
642 } else if (isset($do_delete)) {
643 displayPageHeader($color, $mailbox);
644
645 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
646 if (isset($delete) && is_array($delete)) {
647 foreach($delete as $index) {
648 $attached_file = $hashed_attachment_dir . '/'
649 . $attachments[$index]['localfilename'];
650 unlink ($attached_file);
651 unset ($attachments[$index]);
652 }
653 }
654
655 showInputForm();
656 } else {
657 // This handles the default case as well as the error case
658 // (they had the same code) --> if (isset($smtpErrors))
659 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
660 $imapPort, 0);
661 displayPageHeader($color, $mailbox);
662
663 $newmail = true;
664
665 ClearAttachments();
666
667 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
668 getAttachments(0);
669
670 if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num)
671 getAttachments(0);
672
673 newMail();
674 showInputForm();
675 sqimap_logout($imapConnection);
676 }
677
678 function ClearAttachments() {
679 global $username, $attachments, $attachment_dir;
680 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
681
682 foreach ($attachments as $info) {
683 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
684 if (file_exists($attached_file)) {
685 unlink($attached_file);
686 }
687 }
688
689 $attachments = array();
690 }
691
692 function getReplyCitation($orig_from) {
693 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
694
695 /* First, return an empty string when no citation style selected. */
696 if (($reply_citation_style == '') || ($reply_citation_style == 'none')) {
697 return ('');
698 }
699
700 /* Decode the users name. */
701 $parpos = strpos($orig_from, '(');
702 if ($parpos === false) {
703 $orig_from = trim(substr($orig_from, 0, strpos($orig_from, '<')));
704 $orig_from = str_replace('"', '', $orig_from);
705 $orig_from = str_replace("'", '', $orig_from);
706 } else {
707 $end_parpos = strrpos($orig_from, ')');
708 $end_parpos -= ($end_parpos === false ? $end_parpos : $parpos + 1);
709 $orig_from = trim(substr($orig_from, $parpos + 1, $end_parpos));
710 }
711
712 /* Make sure our final value isn't an empty string. */
713 if ($orig_from == '') {
714 return ('');
715 }
716
717 /* Otherwise, try to select the desired citation style. */
718 switch ($reply_citation_style) {
719 case 'author_said':
720 $start = '';
721 $end = ' ' . _("said") . ':';
722 break;
723 case 'quote_who':
724 $start = '<' . _("quote") . ' ' . _("who") . '="';
725 $end = '">';
726 break;
727 case 'user-defined':
728 $start = $reply_citation_start;
729 $end = $reply_citation_end;
730 break;
731 default: return ('');
732 }
733
734 /* Build and return the citation string. */
735 return ($start . $orig_from . $end . "\n");
736 }
737 ?>