source: github/program/steps/mail/compose.inc @ 491a6e2

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