| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/steps/mail/compose.inc | |
|---|
| 6 | | | |
|---|
| 7 | | This file is part of the RoundCube Webmail client | |
|---|
| 8 | | Copyright (C) 2005, RoundCube Dev. - Switzerland | |
|---|
| 9 | | Licensed under the GNU GPL | |
|---|
| 10 | | | |
|---|
| 11 | | PURPOSE: | |
|---|
| 12 | | Compose a new mail message with all headers and attachments | |
|---|
| 13 | | | |
|---|
| 14 | +-----------------------------------------------------------------------+ |
|---|
| 15 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 16 | +-----------------------------------------------------------------------+ |
|---|
| 17 | |
|---|
| 18 | $Id$ |
|---|
| 19 | |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | require_once('Mail/mimeDecode.php'); |
|---|
| 23 | |
|---|
| 24 | // define constants for message compose mode |
|---|
| 25 | define('RCUBE_COMPOSE_REPLY', 0x0106); |
|---|
| 26 | define('RCUBE_COMPOSE_FORWARD', 0x0107); |
|---|
| 27 | define('RCUBE_COMPOSE_DRAFT', 0x0108); |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | // remove an attachment |
|---|
| 31 | if ($_action=='remove-attachment' && preg_match('/^rcmfile([0-9]+)$/', $_GET['_file'], $regs)) |
|---|
| 32 | { |
|---|
| 33 | $id = $regs[1]; |
|---|
| 34 | if (is_array($_SESSION['compose']['attachments'][$id])) |
|---|
| 35 | { |
|---|
| 36 | @unlink($_SESSION['compose']['attachments'][$id]['path']); |
|---|
| 37 | $_SESSION['compose']['attachments'][$id] = NULL; |
|---|
| 38 | $commands = sprintf("parent.%s.remove_from_attachment_list('rcmfile%d');\n", $JS_OBJECT_NAME, $id); |
|---|
| 39 | rcube_remote_response($commands); |
|---|
| 40 | exit; |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | $MESSAGE_FORM = NULL; |
|---|
| 46 | $MESSAGE = NULL; |
|---|
| 47 | |
|---|
| 48 | // nothing below is called during message composition, only at "new/forward/reply/draft" initialization |
|---|
| 49 | // since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old |
|---|
| 50 | // compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear |
|---|
| 51 | |
|---|
| 52 | rcmail_compose_cleanup(); |
|---|
| 53 | $_SESSION['compose'] = array('id' => uniqid(rand())); |
|---|
| 54 | |
|---|
| 55 | // add some labels to client |
|---|
| 56 | rcube_add_label('nosubject', 'norecipientwarning', 'nosubjectwarning', 'nobodywarning', 'notsentwarning', 'savingmessage', 'sendingmessage', 'messagesaved'); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | // get reference message and set compose mode |
|---|
| 60 | if ($msg_uid = get_input_value('_reply_uid', RCUBE_INPUT_GET)) |
|---|
| 61 | $compose_mode = RCUBE_COMPOSE_REPLY; |
|---|
| 62 | else if ($msg_uid = get_input_value('_forward_uid', RCUBE_INPUT_GET)) |
|---|
| 63 | $compose_mode = RCUBE_COMPOSE_FORWARD; |
|---|
| 64 | else if ($msg_uid = get_input_value('_draft_uid', RCUBE_INPUT_GET)) |
|---|
| 65 | $compose_mode = RCUBE_COMPOSE_DRAFT; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | if (!empty($msg_uid)) |
|---|
| 69 | { |
|---|
| 70 | // similar as in program/steps/mail/show.inc |
|---|
| 71 | $MESSAGE = array('UID' => $msg_uid); |
|---|
| 72 | $MESSAGE['headers'] = &$IMAP->get_headers($msg_uid); |
|---|
| 73 | $MESSAGE['structure'] = &$IMAP->get_structure($msg_uid); |
|---|
| 74 | $MESSAGE['subject'] = $IMAP->decode_header($MESSAGE['headers']->subject); |
|---|
| 75 | $MESSAGE['parts'] = $IMAP->get_mime_numbers($MESSAGE['structure']); |
|---|
| 76 | |
|---|
| 77 | if ($compose_mode == RCUBE_COMPOSE_REPLY) |
|---|
| 78 | { |
|---|
| 79 | $_SESSION['compose']['reply_uid'] = $msg_uid; |
|---|
| 80 | $_SESSION['compose']['reply_msgid'] = $MESSAGE['headers']->messageID; |
|---|
| 81 | $_SESSION['compose']['references'] = $MESSAGE['headers']->reference; |
|---|
| 82 | $_SESSION['compose']['references'] .= !empty($MESSAGE['headers']->reference) ? ' ' : ''; |
|---|
| 83 | $_SESSION['compose']['references'] .= $MESSAGE['headers']->messageID; |
|---|
| 84 | |
|---|
| 85 | if (!empty($_GET['_all'])) |
|---|
| 86 | $MESSAGE['reply_all'] = 1; |
|---|
| 87 | } |
|---|
| 88 | else if ($compose_mode == RCUBE_COMPOSE_FORWARD) |
|---|
| 89 | { |
|---|
| 90 | $_SESSION['compose']['forward_uid'] = $msg_uid; |
|---|
| 91 | } |
|---|
| 92 | else if ($compose_mode == RCUBE_COMPOSE_DRAFT) |
|---|
| 93 | { |
|---|
| 94 | $_SESSION['compose']['draft_uid'] = $msg_uid; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /****** compose mode functions ********/ |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | function rcmail_compose_headers($attrib) |
|---|
| 103 | { |
|---|
| 104 | global $IMAP, $MESSAGE, $DB, $compose_mode; |
|---|
| 105 | static $sa_recipients = array(); |
|---|
| 106 | |
|---|
| 107 | list($form_start, $form_end) = get_form_tags($attrib); |
|---|
| 108 | |
|---|
| 109 | $out = ''; |
|---|
| 110 | $part = strtolower($attrib['part']); |
|---|
| 111 | |
|---|
| 112 | switch ($part) |
|---|
| 113 | { |
|---|
| 114 | case 'from': |
|---|
| 115 | return rcmail_compose_header_from($attrib); |
|---|
| 116 | |
|---|
| 117 | case 'to': |
|---|
| 118 | $fname = '_to'; |
|---|
| 119 | $header = 'to'; |
|---|
| 120 | |
|---|
| 121 | // we have contact id's as get parameters |
|---|
| 122 | if (!empty($_GET['_to']) && preg_match('/^[0-9]+(,[0-9]+)*$/', $_GET['_to'])) |
|---|
| 123 | { |
|---|
| 124 | $a_recipients = array(); |
|---|
| 125 | $sql_result = $DB->query("SELECT name, email |
|---|
| 126 | FROM ".get_table_name('contacts')." |
|---|
| 127 | WHERE user_id=? |
|---|
| 128 | AND del<>1 |
|---|
| 129 | AND contact_id IN (".$_GET['_to'].")", |
|---|
| 130 | $_SESSION['user_id']); |
|---|
| 131 | |
|---|
| 132 | while ($sql_arr = $DB->fetch_assoc($sql_result)) |
|---|
| 133 | $a_recipients[] = format_email_recipient($sql_arr['email'], $sql_arr['name']); |
|---|
| 134 | |
|---|
| 135 | if (sizeof($a_recipients)) |
|---|
| 136 | $fvalue = join(', ', $a_recipients); |
|---|
| 137 | } |
|---|
| 138 | else if (!empty($_GET['_to'])) |
|---|
| 139 | $fvalue = get_input_value('_to', RCUBE_INPUT_GET); |
|---|
| 140 | |
|---|
| 141 | case 'cc': |
|---|
| 142 | if (!$fname) |
|---|
| 143 | { |
|---|
| 144 | $fname = '_cc'; |
|---|
| 145 | $header = 'cc'; |
|---|
| 146 | } |
|---|
| 147 | case 'bcc': |
|---|
| 148 | if (!$fname) |
|---|
| 149 | $fname = '_bcc'; |
|---|
| 150 | |
|---|
| 151 | $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'wrap', 'tabindex'); |
|---|
| 152 | $field_type = 'textarea'; |
|---|
| 153 | break; |
|---|
| 154 | |
|---|
| 155 | case 'replyto': |
|---|
| 156 | case 'reply-to': |
|---|
| 157 | $fname = '_replyto'; |
|---|
| 158 | $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex'); |
|---|
| 159 | $field_type = 'textfield'; |
|---|
| 160 | break; |
|---|
| 161 | |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | if ($fname && !empty($_POST[$fname])) |
|---|
| 165 | $fvalue = get_input_value($fname, RCUBE_INPUT_POST, TRUE); |
|---|
| 166 | |
|---|
| 167 | else if ($header && $compose_mode == RCUBE_COMPOSE_REPLY) |
|---|
| 168 | { |
|---|
| 169 | // get recipent address(es) out of the message headers |
|---|
| 170 | if ($header=='to' && !empty($MESSAGE['headers']->replyto)) |
|---|
| 171 | $fvalue = $IMAP->decode_header($MESSAGE['headers']->replyto); |
|---|
| 172 | |
|---|
| 173 | else if ($header=='to' && !empty($MESSAGE['headers']->from)) |
|---|
| 174 | $fvalue = $IMAP->decode_header($MESSAGE['headers']->from); |
|---|
| 175 | |
|---|
| 176 | // add recipent of original message if reply to all |
|---|
| 177 | else if ($header=='cc' && !empty($MESSAGE['reply_all'])) |
|---|
| 178 | { |
|---|
| 179 | if ($v = $IMAP->decode_header($MESSAGE['headers']->to)) |
|---|
| 180 | $fvalue .= $v; |
|---|
| 181 | |
|---|
| 182 | if ($v = $IMAP->decode_header($MESSAGE['headers']->cc)) |
|---|
| 183 | $fvalue .= (!empty($fvalue) ? ', ' : '') . $v; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | // split recipients and put them back together in a unique way |
|---|
| 187 | if (!empty($fvalue)) |
|---|
| 188 | { |
|---|
| 189 | $to_addresses = $IMAP->decode_address_list($fvalue); |
|---|
| 190 | $fvalue = ''; |
|---|
| 191 | foreach ($to_addresses as $addr_part) |
|---|
| 192 | { |
|---|
| 193 | if (!in_array($addr_part['mailto'], $sa_recipients) && (!$MESSAGE['FROM'] || !in_array($addr_part['mailto'], $MESSAGE['FROM']))) |
|---|
| 194 | { |
|---|
| 195 | $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string']; |
|---|
| 196 | $sa_recipients[] = $addr_part['mailto']; |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | else if ($header && $compose_mode == RCUBE_COMPOSE_DRAFT) |
|---|
| 202 | { |
|---|
| 203 | // get drafted headers |
|---|
| 204 | if ($header=='to' && !empty($MESSAGE['headers']->to)) |
|---|
| 205 | $fvalue = $IMAP->decode_header($MESSAGE['headers']->to); |
|---|
| 206 | |
|---|
| 207 | if ($header=='cc' && !empty($MESSAGE['headers']->cc)) |
|---|
| 208 | $fvalue = $IMAP->decode_header($MESSAGE['headers']->cc); |
|---|
| 209 | |
|---|
| 210 | if ($header=='bcc' && !empty($MESSAGE['headers']->bcc)) |
|---|
| 211 | $fvalue = $IMAP->decode_header($MESSAGE['headers']->bcc); |
|---|
| 212 | |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | if ($fname && $field_type) |
|---|
| 217 | { |
|---|
| 218 | // pass the following attributes to the form class |
|---|
| 219 | $field_attrib = array('name' => $fname); |
|---|
| 220 | foreach ($attrib as $attr => $value) |
|---|
| 221 | if (in_array($attr, $allow_attrib)) |
|---|
| 222 | $field_attrib[$attr] = $value; |
|---|
| 223 | |
|---|
| 224 | // create teaxtarea object |
|---|
| 225 | $input = new $field_type($field_attrib); |
|---|
| 226 | $out = $input->show($fvalue); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | if ($form_start) |
|---|
| 230 | $out = $form_start.$out; |
|---|
| 231 | |
|---|
| 232 | return $out; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | function rcmail_compose_header_from($attrib) |
|---|
| 238 | { |
|---|
| 239 | global $IMAP, $MESSAGE, $DB, $OUTPUT, $JS_OBJECT_NAME, $compose_mode; |
|---|
| 240 | |
|---|
| 241 | // pass the following attributes to the form class |
|---|
| 242 | $field_attrib = array('name' => '_from'); |
|---|
| 243 | foreach ($attrib as $attr => $value) |
|---|
| 244 | if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex'))) |
|---|
| 245 | $field_attrib[$attr] = $value; |
|---|
| 246 | |
|---|
| 247 | // extract all recipients of the reply-message |
|---|
| 248 | $a_recipients = array(); |
|---|
| 249 | if ($compose_mode == RCUBE_COMPOSE_REPLY && is_object($MESSAGE['headers'])) |
|---|
| 250 | { |
|---|
| 251 | $MESSAGE['FROM'] = array(); |
|---|
| 252 | |
|---|
| 253 | $a_to = $IMAP->decode_address_list($MESSAGE['headers']->to); |
|---|
| 254 | foreach ($a_to as $addr) |
|---|
| 255 | { |
|---|
| 256 | if (!empty($addr['mailto'])) |
|---|
| 257 | $a_recipients[] = $addr['mailto']; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | if (!empty($MESSAGE['headers']->cc)) |
|---|
| 261 | { |
|---|
| 262 | $a_cc = $IMAP->decode_address_list($MESSAGE['headers']->cc); |
|---|
| 263 | foreach ($a_cc as $addr) |
|---|
| 264 | { |
|---|
| 265 | if (!empty($addr['mailto'])) |
|---|
| 266 | $a_recipients[] = $addr['mailto']; |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | // get this user's identities |
|---|
| 272 | $sql_result = $DB->query("SELECT identity_id, name, email, signature |
|---|
| 273 | FROM ".get_table_name('identities')." |
|---|
| 274 | WHERE user_id=? |
|---|
| 275 | AND del<>1 |
|---|
| 276 | ORDER BY ".$DB->quoteIdentifier('standard')." DESC, name ASC", |
|---|
| 277 | $_SESSION['user_id']); |
|---|
| 278 | |
|---|
| 279 | if ($DB->num_rows($sql_result)) |
|---|
| 280 | { |
|---|
| 281 | $from_id = 0; |
|---|
| 282 | $a_signatures = array(); |
|---|
| 283 | |
|---|
| 284 | $field_attrib['onchange'] = "$JS_OBJECT_NAME.change_identity(this)"; |
|---|
| 285 | $select_from = new select($field_attrib); |
|---|
| 286 | |
|---|
| 287 | while ($sql_arr = $DB->fetch_assoc($sql_result)) |
|---|
| 288 | { |
|---|
| 289 | $select_from->add(format_email_recipient($sql_arr['email'], $sql_arr['name']), $sql_arr['identity_id']); |
|---|
| 290 | |
|---|
| 291 | // add signature to array |
|---|
| 292 | if (!empty($sql_arr['signature'])) |
|---|
| 293 | $a_signatures[$sql_arr['identity_id']] = $sql_arr['signature']; |
|---|
| 294 | |
|---|
| 295 | // set identity if it's one of the reply-message recipients |
|---|
| 296 | if (in_array($sql_arr['email'], $a_recipients)) |
|---|
| 297 | $from_id = $sql_arr['identity_id']; |
|---|
| 298 | |
|---|
| 299 | if ($compose_mode == RCUBE_COMPOSE_REPLY && is_array($MESSAGE['FROM'])) |
|---|
| 300 | $MESSAGE['FROM'][] = $sql_arr['email']; |
|---|
| 301 | |
|---|
| 302 | if ($compose_mode == RCUBE_COMPOSE_DRAFT && strstr($MESSAGE['headers']->from, $sql_arr['email'])) |
|---|
| 303 | $from_id = $sql_arr['identity_id']; |
|---|
| 304 | |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | // overwrite identity selection with post parameter |
|---|
| 308 | if (isset($_POST['_from'])) |
|---|
| 309 | $from_id = get_input_value('_from', RCUBE_INPUT_POST); |
|---|
| 310 | |
|---|
| 311 | $out = $select_from->show($from_id); |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | // add signatures to client |
|---|
| 315 | $OUTPUT->add_script(sprintf("%s.set_env('signatures', %s);", $JS_OBJECT_NAME, array2js($a_signatures))); |
|---|
| 316 | } |
|---|
| 317 | else |
|---|
| 318 | { |
|---|
| 319 | $input_from = new textfield($field_attrib); |
|---|
| 320 | $out = $input_from->show($_POST['_from']); |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | if ($form_start) |
|---|
| 324 | $out = $form_start.$out; |
|---|
| 325 | |
|---|
| 326 | return $out; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | function rcmail_compose_body($attrib) |
|---|
| 332 | { |
|---|
| 333 | global $CONFIG, $OUTPUT, $MESSAGE, $JS_OBJECT_NAME, $compose_mode; |
|---|
| 334 | |
|---|
| 335 | list($form_start, $form_end) = get_form_tags($attrib); |
|---|
| 336 | unset($attrib['form']); |
|---|
| 337 | |
|---|
| 338 | if (empty($attrib['id'])) |
|---|
| 339 | $attrib['id'] = 'rcmComposeMessage'; |
|---|
| 340 | |
|---|
| 341 | $attrib['name'] = '_message'; |
|---|
| 342 | $textarea = new textarea($attrib); |
|---|
| 343 | |
|---|
| 344 | $body = ''; |
|---|
| 345 | |
|---|
| 346 | // use posted message body |
|---|
| 347 | if (!empty($_POST['_message'])) |
|---|
| 348 | $body = get_input_value('_message', RCUBE_INPUT_POST, TRUE); |
|---|
| 349 | |
|---|
| 350 | // compose reply-body |
|---|
| 351 | else if ($compose_mode == RCUBE_COMPOSE_REPLY) |
|---|
| 352 | { |
|---|
| 353 | $body = rcmail_first_text_part($MESSAGE); |
|---|
| 354 | if (strlen($body)) |
|---|
| 355 | $body = rcmail_create_reply_body($body); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | // forward message body inline |
|---|
| 359 | else if ($compose_mode == RCUBE_COMPOSE_FORWARD) |
|---|
| 360 | { |
|---|
| 361 | $body = rcmail_first_text_part($MESSAGE); |
|---|
| 362 | if (strlen($body)) |
|---|
| 363 | $body = rcmail_create_forward_body($body); |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | // forward message body inline |
|---|
| 367 | else if ($compose_mode == RCUBE_COMPOSE_DRAFT) |
|---|
| 368 | { |
|---|
| 369 | $body = rcmail_first_text_part($MESSAGE); |
|---|
| 370 | if (strlen($body)) |
|---|
| 371 | $body = rcmail_create_draft_body($body); |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | $out = $form_start ? "$form_start\n" : ''; |
|---|
| 375 | |
|---|
| 376 | $saveid = new hiddenfield(array('name' => '_draft_saveid', 'value' => str_replace(array('<','>'),"",$MESSAGE['headers']->messageID) )); |
|---|
| 377 | $out .= $saveid->show(); |
|---|
| 378 | |
|---|
| 379 | $drafttoggle = new hiddenfield(array('name' => '_draft', 'value' => 'yes')); |
|---|
| 380 | $out .= $drafttoggle->show(); |
|---|
| 381 | |
|---|
| 382 | $out .= $textarea->show($body); |
|---|
| 383 | $out .= $form_end ? "\n$form_end" : ''; |
|---|
| 384 | |
|---|
| 385 | // include GoogieSpell |
|---|
| 386 | if (!empty($CONFIG['enable_spellcheck'])) |
|---|
| 387 | { |
|---|
| 388 | $lang_set = ''; |
|---|
| 389 | if (!empty($CONFIG['spellcheck_languages']) && is_array($CONFIG['spellcheck_languages'])) |
|---|
| 390 | $lang_set = "googie.setLanguages(".array2js($CONFIG['spellcheck_languages']).");\n"; |
|---|
| 391 | |
|---|
| 392 | $OUTPUT->include_script('googiespell.js'); |
|---|
| 393 | $OUTPUT->add_script(sprintf("var googie = new GoogieSpell('\$__skin_path/images/googiespell/','%s&_action=spell&lang=');\n". |
|---|
| 394 | "googie.lang_chck_spell = \"%s\";\n". |
|---|
| 395 | "googie.lang_rsm_edt = \"%s\";\n". |
|---|
| 396 | "googie.lang_close = \"%s\";\n". |
|---|
| 397 | "googie.lang_revert = \"%s\";\n". |
|---|
| 398 | "googie.lang_no_error_found = \"%s\";\n%s". |
|---|
| 399 | "googie.decorateTextarea('%s');\n". |
|---|
| 400 | "%s.set_env('spellcheck', googie);", |
|---|
| 401 | $GLOBALS['COMM_PATH'], |
|---|
| 402 | rep_specialchars_output(rcube_label('checkspelling')), |
|---|
| 403 | rep_specialchars_output(rcube_label('resumeediting')), |
|---|
| 404 | rep_specialchars_output(rcube_label('close')), |
|---|
| 405 | rep_specialchars_output(rcube_label('revertto')), |
|---|
| 406 | rep_specialchars_output(rcube_label('nospellerrors')), |
|---|
| 407 | $lang_set, |
|---|
| 408 | $attrib['id'], |
|---|
| 409 | $JS_OBJECT_NAME), 'foot'); |
|---|
| 410 | |
|---|
| 411 | rcube_add_label('checking'); |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>'; |
|---|
| 415 | |
|---|
| 416 | return $out; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | function rcmail_create_reply_body($body) |
|---|
| 421 | { |
|---|
| 422 | global $IMAP, $MESSAGE; |
|---|
| 423 | |
|---|
| 424 | // soft-wrap message first |
|---|
| 425 | $body = wordwrap($body, 75); |
|---|
| 426 | |
|---|
| 427 | // split body into single lines |
|---|
| 428 | $a_lines = preg_split('/\r?\n/', $body); |
|---|
| 429 | |
|---|
| 430 | // add > to each line |
|---|
| 431 | for($n=0; $n<sizeof($a_lines); $n++) |
|---|
| 432 | { |
|---|
| 433 | if (strpos($a_lines[$n], '>')===0) |
|---|
| 434 | $a_lines[$n] = '>'.$a_lines[$n]; |
|---|
| 435 | else |
|---|
| 436 | $a_lines[$n] = '> '.$a_lines[$n]; |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | $body = join("\n", $a_lines); |
|---|
| 440 | |
|---|
| 441 | // add title line |
|---|
| 442 | $pefix = sprintf("\n\n\nOn %s, %s wrote:\n", |
|---|
| 443 | $MESSAGE['headers']->date, |
|---|
| 444 | $IMAP->decode_header($MESSAGE['headers']->from)); |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | // try to remove the signature |
|---|
| 448 | if ($sp = strrpos($body, '-- ')) |
|---|
| 449 | { |
|---|
| 450 | if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r") |
|---|
| 451 | $body = substr($body, 0, $sp-1); |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | return $pefix.$body; |
|---|
| 455 | } |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | function rcmail_create_forward_body($body) |
|---|
| 459 | { |
|---|
| 460 | global $IMAP, $MESSAGE; |
|---|
| 461 | |
|---|
| 462 | // soft-wrap message first |
|---|
| 463 | $body = wordwrap($body, 80); |
|---|
| 464 | |
|---|
| 465 | $prefix = sprintf("\n\n\n-------- Original Message --------\nSubject: %s\nDate: %s\nFrom: %s\nTo: %s\n\n", |
|---|
| 466 | $MESSAGE['subject'], |
|---|
| 467 | $MESSAGE['headers']->date, |
|---|
| 468 | $IMAP->decode_header($MESSAGE['headers']->from), |
|---|
| 469 | $IMAP->decode_header($MESSAGE['headers']->to)); |
|---|
| 470 | |
|---|
| 471 | // add attachments |
|---|
| 472 | if (!isset($_SESSION['compose']['forward_attachments']) && |
|---|
| 473 | is_array($MESSAGE['parts']) && sizeof($MESSAGE['parts'])>1) |
|---|
| 474 | rcmail_write_compose_attachments($MESSAGE); |
|---|
| 475 | |
|---|
| 476 | return $prefix.$body; |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | function rcmail_create_draft_body($body) |
|---|
| 481 | { |
|---|
| 482 | global $IMAP, $MESSAGE; |
|---|
| 483 | |
|---|
| 484 | // add attachments |
|---|
| 485 | if (!isset($_SESSION['compose']['forward_attachments']) && |
|---|
| 486 | is_array($MESSAGE['parts']) && sizeof($MESSAGE['parts'])>1) |
|---|
| 487 | rcmail_write_compose_attachments($MESSAGE); |
|---|
| 488 | |
|---|
| 489 | return $body; |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | function rcmail_write_compose_attachments(&$message) |
|---|
| 494 | { |
|---|
| 495 | global $IMAP; |
|---|
| 496 | |
|---|
| 497 | $temp_dir = rcmail_create_compose_tempdir(); |
|---|
| 498 | |
|---|
| 499 | if (!is_array($_SESSION['compose']['attachments'])) |
|---|
| 500 | $_SESSION['compose']['attachments'] = array(); |
|---|
| 501 | |
|---|
| 502 | foreach ($message['parts'] as $pid => $part) |
|---|
| 503 | { |
|---|
| 504 | if ($part->ctype_primary != 'message' && $part->ctype_primary != 'text' && |
|---|
| 505 | ($part->disposition=='attachment' || $part->disposition=='inline' || $part->headers['content-id'] || |
|---|
| 506 | (empty($part->disposition) && ($part->d_parameters['filename'] || $part->ctype_parameters['name'])))) |
|---|
| 507 | { |
|---|
| 508 | $tmp_path = tempnam($temp_dir, 'rcmAttmnt'); |
|---|
| 509 | if ($fp = fopen($tmp_path, 'w')) |
|---|
| 510 | { |
|---|
| 511 | fwrite($fp, $IMAP->get_message_part($message['UID'], $pid, $part->encoding)); |
|---|
| 512 | fclose($fp); |
|---|
| 513 | |
|---|
| 514 | $filename = !empty($part->d_parameters['filename']) ? $part->d_parameters['filename'] : |
|---|
| 515 | (!empty($part->ctype_parameters['name']) ? $part->ctype_parameters['name'] : |
|---|
| 516 | (!empty($part->headers['content-description']) ? $part->headers['content-description'] : 'file')); |
|---|
| 517 | |
|---|
| 518 | $_SESSION['compose']['attachments'][] = array( |
|---|
| 519 | 'name' => rcube_imap::decode_mime_string($filename), |
|---|
| 520 | 'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary, |
|---|
| 521 | 'path' => $tmp_path |
|---|
| 522 | ); |
|---|
| 523 | } |
|---|
| 524 | } |
|---|
| 525 | } |
|---|
| 526 | |
|---|
| 527 | $_SESSION['compose']['forward_attachments'] = TRUE; |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | function rcmail_compose_subject($attrib) |
|---|
| 532 | { |
|---|
| 533 | global $CONFIG, $MESSAGE, $compose_mode; |
|---|
| 534 | |
|---|
| 535 | list($form_start, $form_end) = get_form_tags($attrib); |
|---|
| 536 | unset($attrib['form']); |
|---|
| 537 | |
|---|
| 538 | $attrib['name'] = '_subject'; |
|---|
| 539 | $textfield = new textfield($attrib); |
|---|
| 540 | |
|---|
| 541 | $subject = ''; |
|---|
| 542 | |
|---|
| 543 | // use subject from post |
|---|
| 544 | if (isset($_POST['_subject'])) |
|---|
| 545 | $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE); |
|---|
| 546 | |
|---|
| 547 | // create a reply-subject |
|---|
| 548 | else if ($compose_mode == RCUBE_COMPOSE_REPLY) |
|---|
| 549 | { |
|---|
| 550 | if (eregi('^re:', $MESSAGE['subject'])) |
|---|
| 551 | $subject = $MESSAGE['subject']; |
|---|
| 552 | else |
|---|
| 553 | $subject = 'Re: '.$MESSAGE['subject']; |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | // create a forward-subject |
|---|
| 557 | else if ($compose_mode == RCUBE_COMPOSE_FORWARD) |
|---|
| 558 | { |
|---|
| 559 | if (eregi('^fwd:', $MESSAGE['subject'])) |
|---|
| 560 | $subject = $MESSAGE['subject']; |
|---|
| 561 | else |
|---|
| 562 | $subject = 'Fwd: '.$MESSAGE['subject']; |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | // creeate a draft-subject |
|---|
| 566 | else if ($compose_mode == RCUBE_COMPOSE_DRAFT) |
|---|
| 567 | $subject = $MESSAGE['subject']; |
|---|
| 568 | |
|---|
| 569 | $out = $form_start ? "$form_start\n" : ''; |
|---|
| 570 | $out .= $textfield->show($subject); |
|---|
| 571 | $out .= $form_end ? "\n$form_end" : ''; |
|---|
| 572 | |
|---|
| 573 | return $out; |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | |
|---|
| 577 | function rcmail_compose_attachment_list($attrib) |
|---|
| 578 | { |
|---|
| 579 | global $OUTPUT, $JS_OBJECT_NAME; |
|---|
| 580 | |
|---|
| 581 | // add ID if not given |
|---|
| 582 | if (!$attrib['id']) |
|---|
| 583 | $attrib['id'] = 'rcmAttachmentList'; |
|---|
| 584 | |
|---|
| 585 | // allow the following attributes to be added to the <ul> tag |
|---|
| 586 | $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style')); |
|---|
| 587 | |
|---|
| 588 | $out = '<ul'. $attrib_str . ">\n"; |
|---|
| 589 | |
|---|
| 590 | if (is_array($_SESSION['compose']['attachments'])) |
|---|
| 591 | { |
|---|
| 592 | if ($attrib['deleteicon']) |
|---|
| 593 | $button = sprintf('<img src="%s%s" alt="%s" border="0" style="padding-right:2px;vertical-align:middle" />', |
|---|
| 594 | $CONFIG['skin_path'], |
|---|
| 595 | $attrib['deleteicon'], |
|---|
| 596 | rcube_label('delete')); |
|---|
| 597 | else |
|---|
| 598 | $button = rcube_label('delete'); |
|---|
| 599 | |
|---|
| 600 | foreach ($_SESSION['compose']['attachments'] as $id => $a_prop) |
|---|
| 601 | $out .= sprintf('<li id="rcmfile%d"><a href="#delete" onclick="return %s.command(\'remove-attachment\',\'rcmfile%d\', this)" title="%s">%s</a>%s</li>', |
|---|
| 602 | $id, |
|---|
| 603 | $JS_OBJECT_NAME, |
|---|
| 604 | $id, |
|---|
| 605 | rcube_label('delete'), |
|---|
| 606 | $button, |
|---|
| 607 | rep_specialchars_output($a_prop['name'])); |
|---|
| 608 | } |
|---|
| 609 | |
|---|
| 610 | $OUTPUT->add_script(sprintf("%s.gui_object('attachmentlist', '%s');", $JS_OBJECT_NAME, $attrib['id'])); |
|---|
| 611 | |
|---|
| 612 | $out .= '</ul>'; |
|---|
| 613 | return $out; |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | |
|---|
| 618 | function rcmail_compose_attachment_form($attrib) |
|---|
| 619 | { |
|---|
| 620 | global $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD; |
|---|
| 621 | |
|---|
| 622 | // add ID if not given |
|---|
| 623 | if (!$attrib['id']) |
|---|
| 624 | $attrib['id'] = 'rcmUploadbox'; |
|---|
| 625 | |
|---|
| 626 | // allow the following attributes to be added to the <div> tag |
|---|
| 627 | $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style')); |
|---|
| 628 | $input_field = rcmail_compose_attachment_field(array()); |
|---|
| 629 | $label_send = rcube_label('upload'); |
|---|
| 630 | $label_close = rcube_label('close'); |
|---|
| 631 | |
|---|
| 632 | $out = <<<EOF |
|---|
| 633 | <div$attrib_str> |
|---|
| 634 | <form action="./" method="post" enctype="multipart/form-data"> |
|---|
| 635 | $SESS_HIDDEN_FIELD |
|---|
| 636 | $input_field<br /> |
|---|
| 637 | <input type="button" value="$label_close" class="button" onclick="document.getElementById('$attrib[id]').style.visibility='hidden'" /> |
|---|
| 638 | <input type="button" value="$label_send" class="button" onclick="$JS_OBJECT_NAME.command('send-attachment', this.form)" /> |
|---|
| 639 | </form> |
|---|
| 640 | </div> |
|---|
| 641 | EOF; |
|---|
| 642 | |
|---|
| 643 | |
|---|
| 644 | $OUTPUT->add_script(sprintf("%s.gui_object('uploadbox', '%s');", $JS_OBJECT_NAME, $attrib['id'])); |
|---|
| 645 | return $out; |
|---|
| 646 | } |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | function rcmail_compose_attachment_field($attrib) |
|---|
| 650 | { |
|---|
| 651 | // allow the following attributes to be added to the <input> tag |
|---|
| 652 | $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'size')); |
|---|
| 653 | |
|---|
| 654 | $out = '<input type="file" name="_attachments[]"'. $attrib_str . " />"; |
|---|
| 655 | return $out; |
|---|
| 656 | } |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | function rcmail_priority_selector($attrib) |
|---|
| 660 | { |
|---|
| 661 | list($form_start, $form_end) = get_form_tags($attrib); |
|---|
| 662 | unset($attrib['form']); |
|---|
| 663 | |
|---|
| 664 | $attrib['name'] = '_priority'; |
|---|
| 665 | $selector = new select($attrib); |
|---|
| 666 | |
|---|
| 667 | $selector->add(array(rcube_label('lowest'), |
|---|
| 668 | rcube_label('low'), |
|---|
| 669 | rcube_label('normal'), |
|---|
| 670 | rcube_label('high'), |
|---|
| 671 | rcube_label('highest')), |
|---|
| 672 | array(5, 4, 0, 2, 1)); |
|---|
| 673 | |
|---|
| 674 | $sel = isset($_POST['_priority']) ? $_POST['_priority'] : 0; |
|---|
| 675 | |
|---|
| 676 | $out = $form_start ? "$form_start\n" : ''; |
|---|
| 677 | $out .= $selector->show($sel); |
|---|
| 678 | $out .= $form_end ? "\n$form_end" : ''; |
|---|
| 679 | |
|---|
| 680 | return $out; |
|---|
| 681 | } |
|---|
| 682 | |
|---|
| 683 | |
|---|
| 684 | function rcmail_receipt_checkbox($attrib) |
|---|
| 685 | { |
|---|
| 686 | list($form_start, $form_end) = get_form_tags($attrib); |
|---|
| 687 | unset($attrib['form']); |
|---|
| 688 | |
|---|
| 689 | if (!isset($attrib['id'])) |
|---|
| 690 | $attrib['id'] = 'receipt'; |
|---|
| 691 | |
|---|
| 692 | $attrib['name'] = '_receipt'; |
|---|
| 693 | $attrib['value'] = '1'; |
|---|
| 694 | $checkbox = new checkbox($attrib); |
|---|
| 695 | |
|---|
| 696 | $out = $form_start ? "$form_start\n" : ''; |
|---|
| 697 | $out .= $checkbox->show(0); |
|---|
| 698 | $out .= $form_end ? "\n$form_end" : ''; |
|---|
| 699 | |
|---|
| 700 | return $out; |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | function get_form_tags($attrib) |
|---|
| 705 | { |
|---|
| 706 | global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $MESSAGE_FORM, $SESS_HIDDEN_FIELD; |
|---|
| 707 | |
|---|
| 708 | $form_start = ''; |
|---|
| 709 | if (!strlen($MESSAGE_FORM)) |
|---|
| 710 | { |
|---|
| 711 | $hiddenfields = new hiddenfield(array('name' => '_task', 'value' => $GLOBALS['_task'])); |
|---|
| 712 | $hiddenfields->add(array('name' => '_action', 'value' => 'send')); |
|---|
| 713 | |
|---|
| 714 | $form_start = empty($attrib['form']) ? '<form name="form" action="./" method="post">' : ''; |
|---|
| 715 | $form_start .= "\n$SESS_HIDDEN_FIELD\n"; |
|---|
| 716 | $form_start .= $hiddenfields->show(); |
|---|
| 717 | } |
|---|
| 718 | |
|---|
| 719 | $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : ''; |
|---|
| 720 | $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form'; |
|---|
| 721 | |
|---|
| 722 | if (!strlen($MESSAGE_FORM)) |
|---|
| 723 | $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('messageform', '$form_name');"); |
|---|
| 724 | |
|---|
| 725 | $MESSAGE_FORM = $form_name; |
|---|
| 726 | |
|---|
| 727 | return array($form_start, $form_end); |
|---|
| 728 | } |
|---|
| 729 | |
|---|
| 730 | |
|---|
| 731 | function format_email_recipient($email, $name='') |
|---|
| 732 | { |
|---|
| 733 | if ($name && $name != $email) |
|---|
| 734 | return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email); |
|---|
| 735 | else |
|---|
| 736 | return $email; |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | |
|---|
| 740 | function rcmail_charset_pulldown($selected='ISO-8859-1') |
|---|
| 741 | { |
|---|
| 742 | $select = new select(); |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | return $select->show($selected); |
|---|
| 746 | } |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | /****** get contacts for this user and add them to client scripts ********/ |
|---|
| 750 | |
|---|
| 751 | $sql_result = $DB->query("SELECT name, email |
|---|
| 752 | FROM ".get_table_name('contacts')." WHERE user_id=? |
|---|
| 753 | AND del<>1",$_SESSION['user_id']); |
|---|
| 754 | |
|---|
| 755 | if ($DB->num_rows($sql_result)) |
|---|
| 756 | { |
|---|
| 757 | $a_contacts = array(); |
|---|
| 758 | while ($sql_arr = $DB->fetch_assoc($sql_result)) |
|---|
| 759 | if ($sql_arr['email']) |
|---|
| 760 | $a_contacts[] = format_email_recipient($sql_arr['email'], rep_specialchars_output($sql_arr['name'], 'js')); |
|---|
| 761 | |
|---|
| 762 | $OUTPUT->add_script(sprintf("$JS_OBJECT_NAME.set_env('contacts', %s);", array2js($a_contacts))); |
|---|
| 763 | } |
|---|
| 764 | |
|---|
| 765 | |
|---|
| 766 | parse_template('compose'); |
|---|
| 767 | ?> |
|---|