+$MESSAGE_ID = get_input_value('_id', RCUBE_INPUT_GET);
+if (!is_array($_SESSION['compose']) || $_SESSION['compose']['id'] != $MESSAGE_ID)
+{
+ rcmail_compose_cleanup();
+
+ // Infinite redirect prevention in case of broken session (#1487028)
+ if ($MESSAGE_ID)
+ raise_error(array('code' => 500, 'type' => 'php',
+ 'file' => __FILE__, 'line' => __LINE__,
+ 'message' => "Invalid session"), true, true);
+
+ $_SESSION['compose'] = array(
+ 'id' => uniqid(mt_rand()),
+ 'param' => request2param(RCUBE_INPUT_GET),
+ 'mailbox' => $IMAP->get_mailbox_name(),
+ );
+
+ // process values like "mailto:foo@bar.com?subject=new+message&cc=another"
+ if ($_SESSION['compose']['param']['to']) {
+ // #1486037: remove "mailto:" prefix
+ $_SESSION['compose']['param']['to'] = preg_replace('/^mailto:/i', '', $_SESSION['compose']['param']['to']);
+ $mailto = explode('?', $_SESSION['compose']['param']['to']);
+ if (count($mailto) > 1) {
+ $_SESSION['compose']['param']['to'] = $mailto[0];
+ parse_str($mailto[1], $query);
+ foreach ($query as $f => $val)
+ $_SESSION['compose']['param'][$f] = $val;
+ }
+ }
+
+ // select folder where to save the sent message
+ $_SESSION['compose']['param']['sent_mbox'] = $RCMAIL->config->get('sent_mbox');
+
+ // pipe compose parameters thru plugins
+ $plugin = $RCMAIL->plugins->exec_hook('message_compose', $_SESSION['compose']);
+ $_SESSION['compose']['param'] = array_merge($_SESSION['compose']['param'], $plugin['param']);
+
+ // add attachments listed by message_compose hook
+ if (is_array($plugin['attachments'])) {
+ foreach ($plugin['attachments'] as $attach) {
+ // we have structured data
+ if (is_array($attach)) {
+ $attachment = $attach;
+ }
+ // only a file path is given
+ else {
+ $filename = basename($attach);
+ $attachment = array(
+ 'name' => $filename,
+ 'mimetype' => rc_mime_content_type($attach, $filename),
+ 'path' => $attach
+ );
+ }
+
+ // save attachment if valid
+ if (($attachment['data'] && $attachment['name']) || ($attachment['path'] && file_exists($attachment['path']))) {
+ $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
+ }
+
+ if ($attachment['status'] && !$attachment['abort']) {
+ unset($attachment['data'], $attachment['status'], $attachment['abort']);
+ $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
+ }
+ }
+ }