- } elseif ( $structure{$pos} == '(' ) {
- $pos = mime_match_parenthesis ($pos, $structure);
- }
- }
- echo "Error decoding mime structure. Report this as a bug!<br>\n";
- return( $pos );
- }
-
- function mime_fetch_body ($imap_stream, $id, $ent_id ) {
- // do a bit of error correction. If we couldn't find the entity id, just guess
- // that it is the first one. That is usually the case anyway.
- if (!$ent_id)
- $ent_id = 1;
- $sid = sqimap_session_id();
- fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
- $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
- $topline = array_shift($data);
- while (! ereg('\\* [0-9]+ FETCH ', $topline) && $data)
- $topline = array_shift($data);
- $wholemessage = implode('', $data);
- if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
- $ret = substr( $wholemessage, 0, $regs[1] );
- /*
- There is some information in the content info header that could be important
- in order to parse html messages. Let's get them here.
- */
- if( $ret{0} == '<' ) {
- fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id.MIME]\r\n");
- $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
- $base = '';
- $k = 10;
- foreach( $data as $d ) {
- if( substr( $d, 0, 13 ) == 'Content-Base:' ) {
- $j = strlen( $d );
- $i = 13;
- $base = '';
- while( $i < $j &&
- ( !isNoSep( $d{$i} ) || $d{$i} == '"' ) )
- $i++;
- while( $i < $j ) {
- if( isNoSep( $d{$i} ) )
- $base .= $d{$i};
- $i++;
- }
- $k = 0;
- } elseif( $k == 1 && !isnosep( $d{0} ) ) {
- $base .= substr( $d, 1 );
- }
- $k++;
- }
- if( $base <> '' )
- $ret = "<base href=\"$base\">" . $ret;
+ $line = '<font color="' . $color[14] . '">' . $line . '</font>';
+ }
+
+ $body_ary[$i] = $line;
+ }
+ $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
+}
+
+/**
+ * This returns a parsed string called $body. That string can then
+ * be displayed as the actual message in the HTML. It contains
+ * everything needed, including HTML Tags, Attachments at the
+ * bottom, etc.
+ *
+ * Since 1.2.0 function uses message_body hook.
+ * Till 1.3.0 function included output of formatAttachments().
+ *
+ * @param resource $imap_stream imap connection resource
+ * @param object $message squirrelmail message object
+ * @param array $color squirrelmail color theme array
+ * @param integer $wrap_at number of characters per line
+ * @param string $ent_num (since 1.3.0) message part id
+ * @param integer $id (since 1.3.0) message id
+ * @param string $mailbox (since 1.3.0) imap folder name
+ * @param boolean $clean (since 1.5.1) Do not output stuff that's irrelevant for the printable version.
+ * @return string html formated message text
+ */
+function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $mailbox='INBOX', $clean=FALSE) {
+ /* This if statement checks for the entity to show as the
+ * primary message. To add more of them, just put them in the
+ * order that is their priority.
+ */
+ global $startMessage, $languages, $squirrelmail_language,
+ $show_html_default, $sort, $has_unsafe_images, $passed_ent_id;
+
+ if( !sqgetGlobalVar('view_unsafe_images', $view_unsafe_images, SQ_GET) ) {
+ $view_unsafe_images = false;
+ }
+
+ $body = '';
+ $urlmailbox = urlencode($mailbox);
+ $body_message = getEntity($message, $ent_num);
+ if (($body_message->header->type0 == 'text') ||
+ ($body_message->header->type0 == 'rfc822')) {
+ $body = mime_fetch_body ($imap_stream, $id, $ent_num);
+ $body = decodeBody($body, $body_message->header->encoding);
+
+ if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
+ function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
+ if (mb_detect_encoding($body) != 'ASCII') {
+ $body = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode',$body);
+ }
+ }
+ $hookResults = do_hook("message_body", $body);
+ $body = $hookResults[1];
+
+ /* If there are other types that shouldn't be formatted, add
+ * them here.
+ */
+
+ if ($body_message->header->type1 == 'html') {
+ if ($show_html_default <> 1) {
+ $entity_conv = array(' ' => ' ',
+ '<p>' => "\n",
+ '<P>' => "\n",
+ '<br>' => "\n",
+ '<BR>' => "\n",
+ '<br />' => "\n",
+ '<BR />' => "\n",
+ '>' => '>',
+ '<' => '<');
+ $body = strtr($body, $entity_conv);
+ $body = strip_tags($body);
+ $body = trim($body);
+ translateText($body, $wrap_at,
+ $body_message->header->getParameter('charset'));
+ } else {
+ $body = magicHTML($body, $id, $message, $mailbox);
+ }
+ } else {
+ translateText($body, $wrap_at,
+ $body_message->header->getParameter('charset'));
+ }
+
+ // if this is the clean display (i.e. printer friendly), stop here.
+ if ( $clean ) {
+ return $body;
+ }
+
+ $link = 'passed_id=' . $id . '&ent_id='.$ent_num.
+ '&mailbox=' . $urlmailbox .'&sort=' . $sort .
+ '&startMessage=' . $startMessage . '&show_more=0';
+ if (isset($passed_ent_id)) {
+ $link .= '&passed_ent_id='.$passed_ent_id;
+ }
+ $body .= '<center><small><a href="download.php?absolute_dl=true&' .
+ $link . '">' . _("Download this as a file") . '</a>';
+ if ($view_unsafe_images) {
+ $text = _("Hide Unsafe Images");
+ } else {
+ if (isset($has_unsafe_images) && $has_unsafe_images) {
+ $link .= '&view_unsafe_images=1';
+ $text = _("View Unsafe Images");
+ } else {
+ $text = '';
+ }
+ }
+ if($text != '') {
+ $body .= ' | <a href="read_body.php?' . $link . '">' . $text . '</a>';
+ }
+ $body .= '</small></center><br />' . "\n";
+ }
+ return $body;
+}
+
+/**
+ * Displays attachment links and information
+ * FIXME: SM_PATH is used in URLs
+ *
+ * Since 1.3.0 function is not included in formatBody() call.
+ *
+ * Since 1.0.2 uses attachment $type0/$type1 hook.
+ * Since 1.2.5 uses attachment $type0/* hook.
+ * Since 1.5.0 uses attachments_bottom hook.
+ *
+ * @param object $message SquirrelMail message object
+ * @param array $exclude_id message parts that are not attachments.
+ * @param string $mailbox mailbox name
+ * @param integer $id message id
+ * @return string html formated attachment information.
+ */
+function formatAttachments($message, $exclude_id, $mailbox, $id) {
+ global $where, $what, $startMessage, $color, $passed_ent_id;
+
+ $att_ar = $message->getAttachments($exclude_id);
+
+ if (!count($att_ar)) return '';
+
+ $attachments = '';
+
+ $urlMailbox = urlencode($mailbox);
+
+ foreach ($att_ar as $att) {
+ $ent = $att->entity_id;
+ $header = $att->header;
+ $type0 = strtolower($header->type0);
+ $type1 = strtolower($header->type1);
+ $name = '';
+ $links['download link']['text'] = _("Download");
+ $links['download link']['href'] = SM_PATH .
+ "src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&ent_id=$ent";
+ if ($type0 =='message' && $type1 == 'rfc822') {
+ $default_page = SM_PATH . 'src/read_body.php';
+ $rfc822_header = $att->rfc822_header;
+ $filename = $rfc822_header->subject;
+ if (trim( $filename ) == '') {
+ $filename = 'untitled-[' . $ent . ']' ;
+ }
+ $from_o = $rfc822_header->from;
+ if (is_object($from_o)) {
+ $from_name = decodeHeader($from_o->getAddress(false));
+ } else {
+ $from_name = _("Unknown sender");