source: github/program/steps/mail/compose.inc @ 27a12ed

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since 27a12ed was 27a12ed, checked in by alecpl <alec@…>, 5 years ago
  • #1485475: don't use 4th argument of htmlspecialchars()
  • Property mode set to 100644
File size: 29.5 KB
Line 
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-2008, 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// define constants for message compose mode
23define('RCUBE_COMPOSE_REPLY', 0x0106);
24define('RCUBE_COMPOSE_FORWARD', 0x0107);
25define('RCUBE_COMPOSE_DRAFT', 0x0108);
26
27
28// remove an attachment
29if ($RCMAIL->action=='remove-attachment' && preg_match('/^rcmfile([0-9]+)$/', $_POST['_file'], $regs))
30{
31  $id = $regs[1];
32  if (is_array($_SESSION['compose']['attachments'][$id]))
33  {
34    @unlink($_SESSION['compose']['attachments'][$id]['path']);
35    unset($_SESSION['compose']['attachments'][$id]);
36    $OUTPUT->command('remove_from_attachment_list', "rcmfile$id");
37    $OUTPUT->send();
38  }
39  exit;
40}
41
42if ($RCMAIL->action=='display-attachment' && preg_match('/^rcmfile([0-9]+)$/', $_GET['_file'], $regs))
43{
44  $id = $regs[1];
45  if (is_array($_SESSION['compose']['attachments'][$id]))
46  {
47    $apath = $_SESSION['compose']['attachments'][$id]['path'];
48    header('Content-Type: ' . $_SESSION['compose']['attachments'][$id]['mimetype']);
49    header('Content-Length: ' . filesize($apath));
50    readfile($apath);
51  }
52  exit;
53}
54
55$MESSAGE_FORM = NULL;
56$MESSAGE = NULL;
57
58// Nothing below is called during message composition, only at "new/forward/reply/draft" initialization or
59// if a compose-ID is given (i.e. when the compose step is opened in a new window/tab).
60// Since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old
61// compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear
62
63if (!is_array($_SESSION['compose']) || $_SESSION['compose']['id'] != get_input_value('_id', RCUBE_INPUT_GET))
64{
65  rcmail_compose_cleanup();
66  $_SESSION['compose'] = array('id' => uniqid(rand()), 'param' => array_map('strip_tags', $_GET));
67 
68  // process values like "mailto:foo@bar.com?subject=new+message&cc=another"
69  if ($_SESSION['compose']['param']['_to']) {
70    $mailto = explode('?', $_SESSION['compose']['param']['_to']);
71    if (count($mailto) > 1) {
72      $_SESSION['compose']['param']['_to'] = $mailto[0];
73      parse_str($mailto[1], $query);
74      foreach ($query as $f => $val)
75        $_SESSION['compose']['param']["_$f"] = $val;
76    }
77  }
78
79  // redirect to a unique URL with all parameters stored in session
80  $OUTPUT->redirect(array('_action' => 'compose', '_id' => $_SESSION['compose']['id']));
81}
82
83// add some labels to client
84$OUTPUT->add_label('nosubject', 'nosenderwarning', 'norecipientwarning', 'nosubjectwarning', 'nobodywarning', 'notsentwarning', 'savingmessage', 'sendingmessage', 'messagesaved', 'converting');
85
86// add config parameters to client script
87if (!empty($CONFIG['drafts_mbox'])) {
88  $OUTPUT->set_env('drafts_mailbox', $CONFIG['drafts_mbox']);
89  $OUTPUT->set_env('draft_autosave', $CONFIG['draft_autosave']);
90}
91// set current mailbox in client environment
92$OUTPUT->set_env('mailbox', $IMAP->get_mailbox_name());
93
94// get reference message and set compose mode
95if ($msg_uid = $_SESSION['compose']['param']['_reply_uid'])
96  $compose_mode = RCUBE_COMPOSE_REPLY;
97else if ($msg_uid = $_SESSION['compose']['param']['_forward_uid'])
98  $compose_mode = RCUBE_COMPOSE_FORWARD;
99else if ($msg_uid = $_SESSION['compose']['param']['_draft_uid'])
100  $compose_mode = RCUBE_COMPOSE_DRAFT;
101
102if (!empty($msg_uid))
103{
104  // similar as in program/steps/mail/show.inc
105  $MESSAGE = new rcube_message($msg_uid);
106 
107  if (!empty($MESSAGE->headers->charset))
108    $IMAP->set_charset($MESSAGE->headers->charset);
109   
110  if ($compose_mode == RCUBE_COMPOSE_REPLY)
111  {
112    $_SESSION['compose']['reply_uid'] = $msg_uid;
113    $_SESSION['compose']['reply_msgid'] = $MESSAGE->headers->messageID;
114    $_SESSION['compose']['references']  = trim($MESSAGE->headers->references . " " . $MESSAGE->headers->messageID);
115
116    if (!empty($_SESSION['compose']['param']['_all']))
117      $MESSAGE->reply_all = 1;
118  }
119  else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
120  {
121    if($MESSAGE->headers->in_reply_to)
122    {
123      // TODO: how to get reply_uid/forward_uid value, maybe we must set X-Reply-UID/X-Forward-UID
124      // $_SESSION['compose']['reply_uid'] = ?
125      // $_SESSION['compose']['forward_uid'] = ?
126      $_SESSION['compose']['reply_msgid'] = '<'.$MESSAGE->headers->in_reply_to.'>';
127    }
128    $_SESSION['compose']['references']  = $MESSAGE->headers->references;
129  }
130  else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
131  {
132    $_SESSION['compose']['forward_uid'] = $msg_uid;
133  }
134}
135
136/****** compose mode functions ********/
137
138
139function rcmail_compose_headers($attrib)
140{
141  global $IMAP, $MESSAGE, $DB, $compose_mode;
142  static $sa_recipients = array();
143
144  list($form_start, $form_end) = get_form_tags($attrib);
145 
146  $out = '';
147  $part = strtolower($attrib['part']);
148 
149  switch ($part)
150  {
151    case 'from':
152      return rcmail_compose_header_from($attrib);
153
154    case 'to':
155      $fname = '_to';
156      $header = 'to';
157     
158      // we have a set of recipients stored is session
159      if (($mailto_id = $_SESSION['compose']['param']['_mailto']) && $_SESSION['mailto'][$mailto_id])
160        $fvalue = urldecode($_SESSION['mailto'][$mailto_id]);
161     
162    case 'cc':
163      if (!$fname)
164      {
165        $fname = '_cc';
166        $header = 'cc';
167      }
168    case 'bcc':
169      if (!$fname)
170      {
171        $fname = '_bcc';
172        $header = 'bcc';
173      }
174       
175      $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'tabindex');
176      $field_type = 'html_textarea';
177      break;
178
179    case 'replyto':
180    case 'reply-to':
181      $fname = '_replyto';
182      $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
183      $field_type = 'html_inputfield';
184      break;
185  }
186 
187  if ($fname && !empty($_POST[$fname]))
188    $fvalue = get_input_value($fname, RCUBE_INPUT_POST, TRUE);
189  else if ($fname && !$fvalue && !empty($_SESSION['compose']['param'][$fname]))
190    $fvalue = $_SESSION['compose']['param'][$fname];
191
192  else if ($header && $compose_mode == RCUBE_COMPOSE_REPLY)
193  {
194    // get recipent address(es) out of the message headers
195    if ($header=='to' && !empty($MESSAGE->headers->replyto))
196      $fvalue = $MESSAGE->headers->replyto;
197
198    else if ($header=='to' && !empty($MESSAGE->headers->from))
199      $fvalue = $MESSAGE->headers->from;
200
201    // add recipent of original message if reply to all
202    else if ($header=='cc' && !empty($MESSAGE->reply_all))
203    {
204      if ($v = $MESSAGE->headers->to)
205        $fvalue .= $v;
206
207      if ($v = $MESSAGE->headers->cc)
208        $fvalue .= (!empty($fvalue) ? ', ' : '') . $v;
209    }
210
211    // split recipients and put them back together in a unique way
212    if (!empty($fvalue))
213    {
214      $to_addresses = $IMAP->decode_address_list($fvalue);
215      $fvalue = '';
216
217      foreach ($to_addresses as $addr_part)
218      {
219        if (!empty($addr_part['mailto'])
220            && !in_array($addr_part['mailto'], $sa_recipients)
221            && (!$MESSAGE->compose_from
222                || !in_array($addr_part['mailto'], $MESSAGE->compose_from)
223                || count($to_addresses)==1)) // allow reply to yourself
224        {
225          $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string'];
226          $sa_recipients[] = $addr_part['mailto'];
227        }
228      }
229    }
230  }
231  else if ($header && $compose_mode == RCUBE_COMPOSE_DRAFT)
232  {
233    // get drafted headers
234    if ($header=='to' && !empty($MESSAGE->headers->to))
235      $fvalue = $MESSAGE->get_header('to');
236
237    if ($header=='cc' && !empty($MESSAGE->headers->cc))
238      $fvalue = $MESSAGE->get_header('cc');
239
240    if ($header=='bcc' && !empty($MESSAGE->headers->bcc))
241      $fvalue = $MESSAGE->get_header('bcc');
242  }
243
244       
245  if ($fname && $field_type)
246  {
247    // pass the following attributes to the form class
248    $field_attrib = array('name' => $fname, 'spellcheck' => 'false');
249    foreach ($attrib as $attr => $value)
250      if (in_array($attr, $allow_attrib))
251        $field_attrib[$attr] = $value;
252
253    // create teaxtarea object
254    $input = new $field_type($field_attrib);
255    $out = $input->show($fvalue);
256  }
257 
258  if ($form_start)
259    $out = $form_start.$out;
260
261  return $out; 
262}
263
264
265
266function rcmail_compose_header_from($attrib)
267{
268  global $IMAP, $MESSAGE, $DB, $USER, $OUTPUT, $compose_mode;
269   
270  // pass the following attributes to the form class
271  $field_attrib = array('name' => '_from');
272  foreach ($attrib as $attr => $value)
273    if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
274      $field_attrib[$attr] = $value;
275
276  // extract all recipients of the reply-message
277  $a_recipients = array();
278  if ($compose_mode == RCUBE_COMPOSE_REPLY && is_object($MESSAGE->headers))
279  {
280    $MESSAGE->compose_from = array();
281
282    $a_to = $IMAP->decode_address_list($MESSAGE->headers->to);
283    foreach ($a_to as $addr)
284    {
285      if (!empty($addr['mailto']))
286        $a_recipients[] = $addr['mailto'];
287    }
288
289    if (!empty($MESSAGE->headers->cc))
290    {
291      $a_cc = $IMAP->decode_address_list($MESSAGE->headers->cc);
292      foreach ($a_cc as $addr)
293      {
294        if (!empty($addr['mailto']))
295          $a_recipients[] = $addr['mailto'];
296      }
297    }
298  }
299
300  // get this user's identities
301  $sql_result = $USER->list_identities();
302
303  if ($DB->num_rows($sql_result))
304  {
305    $from_id = 0;
306    $a_signatures = array();
307
308    $field_attrib['onchange'] = JS_OBJECT_NAME.".change_identity(this)";
309    $select_from = new html_select($field_attrib);
310
311    while ($sql_arr = $DB->fetch_assoc($sql_result))
312    {
313      $identity_id = $sql_arr['identity_id'];
314      $select_from->add(format_email_recipient($sql_arr['email'], $sql_arr['name']), $identity_id);
315
316      // add signature to array
317      if (!empty($sql_arr['signature']))
318      {
319        $a_signatures[$identity_id]['text'] = $sql_arr['signature'];
320        $a_signatures[$identity_id]['is_html'] = ($sql_arr['html_signature'] == 1) ? true : false;
321        if ($a_signatures[$identity_id]['is_html'])
322        {
323            $h2t = new html2text($a_signatures[$identity_id]['text'], false, false);
324            $plainTextPart = $h2t->get_text();
325            $a_signatures[$identity_id]['plain_text'] = trim(html_entity_decode($plainTextPart, ENT_NOQUOTES, 'UTF-8'));
326        }
327      }
328
329      // set identity if it's one of the reply-message recipients
330      if (in_array($sql_arr['email'], $a_recipients))
331        $from_id = $sql_arr['identity_id'];
332
333      if ($compose_mode == RCUBE_COMPOSE_REPLY && is_array($MESSAGE->compose_from))
334        $MESSAGE->compose_from[] = $sql_arr['email'];
335
336      if ($compose_mode == RCUBE_COMPOSE_DRAFT && strstr($MESSAGE->headers->from, $sql_arr['email']))
337        $from_id = $sql_arr['identity_id'];
338    }
339
340    // overwrite identity selection with post parameter
341    if (isset($_POST['_from']))
342      $from_id = get_input_value('_from', RCUBE_INPUT_POST);
343
344    $out = $select_from->show($from_id);
345
346    // add signatures to client
347    $OUTPUT->set_env('signatures', $a_signatures);
348  }
349  else
350  {
351    $input_from = new html_inputfield($field_attrib);
352    $out = $input_from->show($_POST['_from']);
353  }
354 
355  if ($form_start)
356    $out = $form_start.$out;
357
358  return $out;
359}
360
361
362function rcmail_compose_body($attrib)
363{
364  global $RCMAIL, $CONFIG, $OUTPUT, $MESSAGE, $compose_mode;
365 
366  list($form_start, $form_end) = get_form_tags($attrib);
367  unset($attrib['form']);
368 
369  if (empty($attrib['id']))
370    $attrib['id'] = 'rcmComposeMessage';
371
372  $attrib['name'] = '_message';
373
374  if ($CONFIG['htmleditor'])
375    $isHtml = true;
376  else
377    $isHtml = false;
378
379  $body = '';
380
381  // use posted message body
382  if (!empty($_POST['_message']))
383  {
384    $body = get_input_value('_message', RCUBE_INPUT_POST, true);
385  }
386  else if ($compose_mode)
387  {
388    if ($isHtml && $MESSAGE->has_html_part())
389    {
390      $body = $MESSAGE->first_html_part();
391      $isHtml = true;
392    }
393    else
394    {
395      $body = $MESSAGE->first_text_part();
396      $isHtml = false;
397    }
398   
399    // compose reply-body
400    if ($compose_mode == RCUBE_COMPOSE_REPLY)
401      $body = rcmail_create_reply_body($body, $isHtml);
402    // forward message body inline
403    else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
404      $body = rcmail_create_forward_body($body, $isHtml);
405    // load draft message body
406    else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
407      $body = rcmail_create_draft_body($body, $isHtml);
408
409    if ($isHtml) {
410      // replace cid with href in inline images links
411      foreach ((array)$_SESSION['compose']['attachments'] as $pid => $attachment) {
412        if ($attachment['content_id']) {
413          $body = str_replace('cid:'. $attachment['content_id'],
414            $OUTPUT->app->comm_path.'&_action=display-attachment&_file=rcmfile'.$pid, $body);
415        }
416      }
417    }
418  }
419  else if (!empty($_SESSION['compose']['param']['_body']))
420  {
421    $body = $_SESSION['compose']['param']['_body'];
422  }
423
424  $lang = $tinylang = strtolower(substr($_SESSION['language'], 0, 2));
425  if (!file_exists(INSTALL_PATH . 'program/js/tiny_mce/langs/'.$tinylang.'.js'))
426    $tinylang = 'en';
427
428  $OUTPUT->include_script('tiny_mce/tiny_mce.js');
429  $OUTPUT->include_script("editor.js");
430  $OUTPUT->add_script('rcmail_editor_init("$__skin_path", "'.JQ($tinylang).'", '.intval($CONFIG['enable_spellcheck']).');');
431
432  $out = $form_start ? "$form_start\n" : '';
433
434  $saveid = new html_hiddenfield(array('name' => '_draft_saveid', 'value' => $compose_mode==RCUBE_COMPOSE_DRAFT ? str_replace(array('<','>'), "", $MESSAGE->headers->messageID) : ''));
435  $out .= $saveid->show();
436
437  $drafttoggle = new html_hiddenfield(array('name' => '_draft', 'value' => 'yes'));
438  $out .= $drafttoggle->show();
439
440  $msgtype = new html_hiddenfield(array('name' => '_is_html', 'value' => ($isHtml?"1":"0")));
441  $out .= $msgtype->show();
442
443  // If desired, set this textarea to be editable by TinyMCE
444  if ($isHtml) $attrib['class'] = 'mce_editor';
445  $textarea = new html_textarea($attrib);
446  $out .= $textarea->show($body);
447  $out .= $form_end ? "\n$form_end" : '';
448
449  // include GoogieSpell
450  if (!empty($CONFIG['enable_spellcheck'])) {
451    $spellcheck_langs = (array)$RCMAIL->config->get('spellcheck_languages', array('da'=>'Dansk', 'de'=>'Deutsch', 'en' => 'English', 'es'=>'Español', 'fr'=>'Français', 'it'=>'Italiano', 'nl'=>'Nederlands', 'pl'=>'Polski', 'pt'=>'Português', 'fi'=>'Suomi', 'sv'=>'Svenska'));
452    if (!$spellcheck_langs[$lang])
453      $lang = 'en';
454   
455    $editor_lang_set = array();
456    foreach ($spellcheck_langs as $key => $name) {
457      $editor_lang_set[] = ($key == $lang ? '+' : '') . JQ($name).'='.JQ($key);
458    }
459   
460    $OUTPUT->include_script('googiespell.js');
461    $OUTPUT->add_script(sprintf(
462      "var googie = new GoogieSpell('\$__skin_path/images/googiespell/','%s&_action=spell&lang=');\n".
463      "googie.lang_chck_spell = \"%s\";\n".
464      "googie.lang_rsm_edt = \"%s\";\n".
465      "googie.lang_close = \"%s\";\n".
466      "googie.lang_revert = \"%s\";\n".
467      "googie.lang_no_error_found = \"%s\";\n".
468      "googie.setLanguages(%s);\n".
469      "googie.setCurrentLanguage('%s');\n".
470      "googie.decorateTextarea('%s');\n".
471      "%s.set_env('spellcheck', googie);",
472      $RCMAIL->comm_path,
473      JQ(Q(rcube_label('checkspelling'))),
474      JQ(Q(rcube_label('resumeediting'))),
475      JQ(Q(rcube_label('close'))),
476      JQ(Q(rcube_label('revertto'))),
477      JQ(Q(rcube_label('nospellerrors'))),
478      json_serialize($spellcheck_langs),
479      $lang,
480      $attrib['id'],
481      JS_OBJECT_NAME), 'foot');
482
483    $OUTPUT->add_label('checking');
484    $OUTPUT->set_env('spellcheck_langs', join(',', $editor_lang_set));
485  }
486 
487  $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;border:none;visibility:hidden;"></iframe>';
488
489  return $out;
490}
491
492
493function rcmail_create_reply_body($body, $bodyIsHtml)
494{
495  global $IMAP, $MESSAGE, $OUTPUT;
496
497  if (! $bodyIsHtml)
498  {
499    // soft-wrap message first
500    $body = rcmail_wrap_quoted($body, 75);
501 
502    // split body into single lines
503    $a_lines = preg_split('/\r?\n/', $body);
504 
505    // add > to each line
506    for($n=0; $n<sizeof($a_lines); $n++)
507    {
508      if (strpos($a_lines[$n], '>')===0)
509        $a_lines[$n] = '>'.$a_lines[$n];
510      else
511        $a_lines[$n] = '> '.$a_lines[$n];
512    }
513 
514    $body = join("\n", $a_lines);
515
516    // add title line
517    $prefix = sprintf("On %s, %s wrote:\n",
518      $MESSAGE->headers->date,
519      $MESSAGE->get_header('from'));
520
521    // try to remove the signature
522    if ($sp = strrpos($body, '-- '))
523      {
524      if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
525        $body = substr($body, 0, $sp-1);
526      }
527    $suffix = '';
528  }
529  else
530  {
531    $prefix = sprintf("On %s, %s wrote:<br />\n",
532      $MESSAGE->headers->date,
533      htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
534    $prefix .= '<blockquote type="cite" style="padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%">';
535    $suffix = "</blockquote>";
536
537    rcmail_write_inline_attachments($MESSAGE);
538  }
539
540  return $prefix.$body.$suffix;
541}
542
543
544function rcmail_create_forward_body($body, $bodyIsHtml)
545{
546  global $IMAP, $MESSAGE, $OUTPUT;
547
548  if (!$bodyIsHtml)
549  {
550    $prefix = "\n\n\n-------- Original Message --------\n";
551    $prefix .= 'Subject: ' . $MESSAGE->subject . "\n";
552    $prefix .= 'Date: ' . $MESSAGE->headers->date . "\n";
553    $prefix .= 'From: ' . $MESSAGE->get_header('from') . "\n";
554    $prefix .= 'To: ' . $MESSAGE->get_header('to') . "\n";
555    if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
556      $prefix .= 'Reply-To: ' . $MESSAGE->get_header('replyto') . "\n";
557    $prefix .= "\n";
558  }
559  else
560  {
561    $prefix = sprintf(
562      "<br><br>-------- Original Message --------" .
563        "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody>" .
564        "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Subject: </th><td>%s</td></tr>" .
565        "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Date: </th><td>%s</td></tr>" .
566        "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">From: </th><td>%s</td></tr>" .
567        "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">To: </th><td>%s</td></tr>",
568      Q($MESSAGE->subject),
569      Q($MESSAGE->headers->date),
570      htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset(), true),
571        htmlspecialchars(Q($MESSAGE->get_header('to'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset(), true));
572
573    if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
574      $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Reply-To: </th><td>%s</td></tr>",
575        htmlspecialchars(Q($MESSAGE->get_header('replyto'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset(), true));
576
577    $prefix .= "</tbody></table><br>";
578  }
579
580  // add attachments
581  if (!isset($_SESSION['compose']['forward_attachments']) && is_array($MESSAGE->mime_parts))
582    rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
583   
584  return $prefix.$body;
585}
586
587
588function rcmail_create_draft_body($body, $bodyIsHtml)
589{
590  global $MESSAGE;
591 
592  /**
593   * add attachments
594   * sizeof($MESSAGE->mime_parts can be 1 - e.g. attachment, but no text!
595   */
596  if (!isset($_SESSION['compose']['forward_attachments'])
597      && is_array($MESSAGE->mime_parts)
598      && count($MESSAGE->mime_parts) > 0)
599    rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
600
601  return $body;
602}
603 
604 
605function rcmail_write_compose_attachments(&$message, $bodyIsHtml)
606{
607  foreach ((array)$message->mime_parts as $pid => $part)
608  {
609    if (($part->ctype_primary != 'message' || !$bodyIsHtml) &&
610        ($part->disposition=='attachment' || $part->disposition=='inline' || $part->headers['content-id']
611        || (empty($part->disposition) && $part->filename)))
612    {
613      if ($attachment = rcmail_save_attachment($message, $pid))
614        $_SESSION['compose']['attachments'][] = $attachment;
615    }
616  }
617       
618  $_SESSION['compose']['forward_attachments'] = true;
619}
620
621
622function rcmail_write_inline_attachments(&$message)
623{
624  foreach ((array)$message->mime_parts as $pid => $part)
625  {
626    if ($part->content_id && $part->filename)
627    {
628      if ($attachment = rcmail_save_attachment($message, $pid))
629        $_SESSION['compose']['attachments'][] = $attachment;
630    }
631  }
632}
633
634function rcmail_save_attachment(&$message, $pid)
635{
636  global $RCMAIL;
637
638  $temp_dir = unslashify($RCMAIL->config->get('temp_dir'));
639  $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
640  $part = $message->mime_parts[$pid];
641 
642  if ($fp = fopen($tmp_path, 'w'))
643  {
644    $message->get_part_content($pid, $fp);
645    fclose($fp);
646
647    return array(
648        'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
649        'name' => $part->filename,
650        'path' => $tmp_path,
651        'content_id' => $part->content_id
652    );
653  }
654}
655
656
657function rcmail_compose_subject($attrib)
658{
659  global $MESSAGE, $compose_mode;
660 
661  list($form_start, $form_end) = get_form_tags($attrib);
662  unset($attrib['form']);
663 
664  $attrib['name'] = '_subject';
665  $attrib['spellcheck'] = 'true';
666  $textfield = new html_inputfield($attrib);
667
668  $subject = '';
669
670  // use subject from post
671  if (isset($_POST['_subject'])) {
672    $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
673  }
674  // create a reply-subject
675  else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
676    if (eregi('^re:', $MESSAGE->subject))
677      $subject = $MESSAGE->subject;
678    else
679      $subject = 'Re: '.$MESSAGE->subject;
680  }
681  // create a forward-subject
682  else if ($compose_mode == RCUBE_COMPOSE_FORWARD) {
683    if (eregi('^fwd:', $MESSAGE->subject))
684      $subject = $MESSAGE->subject;
685    else
686      $subject = 'Fwd: '.$MESSAGE->subject;
687  }
688  // creeate a draft-subject
689  else if ($compose_mode == RCUBE_COMPOSE_DRAFT) {
690    $subject = $MESSAGE->subject;
691  }
692  else if (!empty($_SESSION['compose']['param']['_subject'])) {
693    $subject = $_SESSION['compose']['param']['_subject'];
694  }
695 
696  $out = $form_start ? "$form_start\n" : '';
697  $out .= $textfield->show($subject);
698  $out .= $form_end ? "\n$form_end" : '';
699         
700  return $out;
701}
702
703
704function rcmail_compose_attachment_list($attrib)
705{
706  global $OUTPUT, $CONFIG;
707 
708  // add ID if not given
709  if (!$attrib['id'])
710    $attrib['id'] = 'rcmAttachmentList';
711 
712  $out = "\n";
713 
714  if (is_array($_SESSION['compose']['attachments']))
715  {
716    if ($attrib['deleteicon'])
717      $button = html::img(array(
718        'src' => $CONFIG['skin_path'] . $attrib['deleteicon'],
719        'alt' => rcube_label('delete'),
720        'style' => "border:0;padding-right:2px;vertical-align:middle"));
721    else
722      $button = Q(rcube_label('delete'));
723
724    foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
725    {
726      if (empty($a_prop))
727        continue;
728     
729      $out .= html::tag('li', array('id' => "rcmfile".$id),
730        html::a(array(
731            'href' => "#delete",
732            'title' => rcube_label('delete'),
733            'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%d', this)", JS_OBJECT_NAME, $id)),
734          $button) . Q($a_prop['name']));
735    }
736  }
737
738  $OUTPUT->add_gui_object('attachmentlist', $attrib['id']);
739   
740  return html::tag('ul', $attrib, $out, html::$common_attrib);
741}
742
743
744function rcmail_compose_attachment_form($attrib)
745{
746  global $OUTPUT;
747
748  // add ID if not given
749  if (!$attrib['id'])
750    $attrib['id'] = 'rcmUploadbox';
751 
752  $button = new html_inputfield(array('type' => 'button', 'class' => 'button'));
753 
754  $out = html::div($attrib,
755    $OUTPUT->form_tag(array('name' => 'form', 'method' => 'post', 'enctype' => 'multipart/form-data'),
756      html::div(null, rcmail_compose_attachment_field(array())) .
757      html::div('hint', rcube_label(array('name' => 'maxuploadsize', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))))) .
758      html::div('buttons',
759        $button->show(rcube_label('close'), array('onclick' => "document.getElementById('$attrib[id]').style.visibility='hidden'")) . ' ' .
760        $button->show(rcube_label('upload'), array('onclick' => JS_OBJECT_NAME . ".command('send-attachment', this.form)"))
761      )
762    )
763  );
764 
765  $OUTPUT->add_gui_object('uploadbox', $attrib['id']);
766  return $out;
767}
768
769
770function rcmail_compose_attachment_field($attrib)
771{
772  $attrib['type'] = 'file';
773  $attrib['name'] = '_attachments[]';
774  $field = new html_inputfield($attrib);
775  return $field->show();
776}
777
778
779function rcmail_priority_selector($attrib)
780{
781  global $MESSAGE;
782 
783  list($form_start, $form_end) = get_form_tags($attrib);
784  unset($attrib['form']);
785 
786  $attrib['name'] = '_priority';
787  $selector = new html_select($attrib);
788
789  $selector->add(array(rcube_label('lowest'),
790                       rcube_label('low'),
791                       rcube_label('normal'),
792                       rcube_label('high'),
793                       rcube_label('highest')),
794                 array(5, 4, 0, 2, 1));
795                 
796  $sel = isset($_POST['_priority']) ? $_POST['_priority'] : intval($MESSAGE->headers->priority);
797
798  $out = $form_start ? "$form_start\n" : '';
799  $out .= $selector->show($sel);
800  $out .= $form_end ? "\n$form_end" : '';
801         
802  return $out;
803}
804
805
806function rcmail_receipt_checkbox($attrib)
807{
808  global $MESSAGE, $compose_mode;
809 
810  list($form_start, $form_end) = get_form_tags($attrib);
811  unset($attrib['form']);
812 
813  if (!isset($attrib['id']))
814    $attrib['id'] = 'receipt'; 
815
816  $attrib['name'] = '_receipt';
817  $attrib['value'] = '1';
818  $checkbox = new html_checkbox($attrib);
819
820  $out = $form_start ? "$form_start\n" : '';
821  $out .= $checkbox->show(
822    $compose_mode == RCUBE_COMPOSE_DRAFT && $MESSAGE->headers->mdn_to ? 1 : 0);
823  $out .= $form_end ? "\n$form_end" : '';
824
825  return $out;
826}
827
828
829function rcmail_editor_selector($attrib)
830{
831  global $CONFIG, $MESSAGE, $compose_mode;
832
833  $choices = array(
834    'html'  => 'htmltoggle',
835    'plain' => 'plaintoggle'
836  );
837
838  // determine whether HTML or plain text should be checked
839  $useHtml = $CONFIG['htmleditor'] ? true : false;
840
841  if ($compose_mode)
842    $useHtml = ($useHtml && $MESSAGE->has_html_part());
843
844  $selector = '';
845  $chosenvalue = $useHtml ? 'html' : 'plain';
846  $radio = new html_radiobutton(array('name' => '_editorSelect', 'onclick' => 'return rcmail_toggle_editor(this)'));
847  foreach ($choices as $value => $text)
848  {
849    $attrib['id'] = '_' . $value;
850    $attrib['value'] = $value;
851    $selector .= $radio->show($chosenvalue, $attrib) . html::label($attrib['id'], Q(rcube_label($text)));
852  }
853
854  return $selector;
855}
856
857
858function rcmail_store_target_selection($attrib)
859{
860  $attrib['name'] = '_store_target';
861  $select = rcmail_mailbox_select(array_merge($attrib, array('noselection' => '- '.rcube_label('dontsave').' -')));
862  return $select->show(rcmail::get_instance()->config->get('sent_mbox'), $attrib);
863}
864
865
866function get_form_tags($attrib)
867{
868  global $RCMAIL, $MESSAGE_FORM;
869
870  $form_start = '';
871  if (!strlen($MESSAGE_FORM))
872  {
873    $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $RCMAIL->task));
874    $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
875
876    $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
877    $form_start .= $hiddenfields->show();
878  }
879   
880  $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
881  $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
882 
883  if (!strlen($MESSAGE_FORM))
884    $RCMAIL->output->add_gui_object('messageform', $form_name);
885 
886  $MESSAGE_FORM = $form_name;
887
888  return array($form_start, $form_end);
889}
890
891
892// register UI objects
893$OUTPUT->add_handlers(array(
894  'composeheaders' => 'rcmail_compose_headers',
895  'composesubject' => 'rcmail_compose_subject',
896  'composebody' => 'rcmail_compose_body',
897  'composeattachmentlist' => 'rcmail_compose_attachment_list',
898  'composeattachmentform' => 'rcmail_compose_attachment_form',
899  'composeattachment' => 'rcmail_compose_attachment_field',
900  'priorityselector' => 'rcmail_priority_selector',
901  'editorselector' => 'rcmail_editor_selector',
902  'receiptcheckbox' => 'rcmail_receipt_checkbox',
903  'storetarget' => 'rcmail_store_target_selection',
904));
905
906/****** get contacts for this user and add them to client scripts ********/
907
908$CONTACTS = new rcube_contacts($DB, $USER->ID);
909$CONTACTS->set_pagesize(1000);
910
911$a_contacts = array();
912                                   
913if ($result = $CONTACTS->list_records())
914  {
915  while ($sql_arr = $result->iterate())
916    if ($sql_arr['email'])
917      $a_contacts[] = format_email_recipient($sql_arr['email'], $sql_arr['name']);
918  }
919if (!empty($CONFIG['ldap_public']) && is_array($CONFIG['ldap_public']))
920  {
921  /* LDAP autocompletion */
922  foreach ($CONFIG['ldap_public'] as $ldapserv_config)
923    {
924    if ($ldapserv_config['fuzzy_search'] != 1 ||
925        $ldapserv_config['global_search'] != 1)
926      {
927      continue;
928      }
929         
930    $LDAP = new rcube_ldap($ldapserv_config);
931    $LDAP->connect();
932    $LDAP->set_pagesize(1000);
933 
934    $results = $LDAP->search($ldapserv_config['mail_field'], "");
935 
936    for ($i = 0; $i < $results->count; $i++)
937          {
938          if ($results->records[$i]['email'] != '')
939            {
940            $email = $results->records[$i]['email'];
941            $name = $results->records[$i]['name'];
942                 
943            $a_contacts[] = format_email_recipient($email, $name);
944            }
945          }
946    $LDAP->close();
947    }
948  }
949if ($a_contacts)
950  {
951        $OUTPUT->set_env('contacts', $a_contacts);
952  }
953
954$OUTPUT->send('compose');
955
956?>
Note: See TracBrowser for help on using the repository browser.