4 * Message Details plugin - bottom frame with message structure and rfc822 body
6 * Plugin to view the RFC822 raw message output and the bodystructure of a message
8 * @author Marc Groot Koerkamp
9 * @copyright © 2002 Marc Groot Koerkamp, The Netherlands
10 * @copyright © 2002-2007 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @subpackage message_details
18 * Include the SquirrelMail initialization file.
20 require('../../include/init.php');
21 require(SM_PATH
. 'functions/imap_general.php');
22 require(SM_PATH
. 'functions/imap_messages.php');
23 require(SM_PATH
. 'functions/mime.php');
25 sqgetGlobalVar('get_message_details', $md_action, SQ_GET
);
28 * Controls display of 8bit symbols in message source
29 * @global boolean $msgd_8bit_in_hex;
31 global $msgd_8bit_in_hex;
32 $msgd_8bit_in_hex=false;
34 if (!empty($md_action)) {
35 sqgetGlobalVar('passed_id', $passed_id, SQ_GET
);
36 sqgetGlobalVar('mailbox', $mailbox, SQ_GET
);
38 * add third function argument, if you want to see
39 * message source without formating
41 echo get_message_details($mailbox, $passed_id);
45 // ---------- function definitions ----------
48 * Converts 8bit string to hex
50 * Replaces 8bit symbols with their hex strings,
51 * encloses them in curly brackets and uses different color.
52 * @param string $string text
56 function msgd_convert_to_hex($string) {
58 return preg_replace("/([\200-\377])/e","'<font color=\"$color[2]\">{'.dechex(ord('\\1')).'}</font>'",$string);
62 * Calculates id of MIME entity
63 * @param string $entString
64 * @param integer $direction
68 function CalcEntity($entString, $direction) {
70 if ($direction == -1) {
71 $pos = strrpos($entString,'.');
72 $result = substr($entString,0,$pos);
77 $pos = strrpos($entString,'.');
83 $level = substr($entString,0,$pos);
84 $sublevel = substr($entString,$pos+
1);
86 $result = "$level".'.'."$sublevel";
90 $result = "$entString".".0";
100 * Returns actual message details
101 * @param string $mailbox
102 * @param string $passed_id
103 * @param boolean $stripHTML If TRUE, only plain text is returned,
104 * default is FALSE, wherein output contains
105 * pretty-HTMLification of message body
106 * @return string The formatted message details
109 function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
110 global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex, $username;
114 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
115 $read = sqimap_mailbox_select($imapConnection, $mailbox);
116 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
121 $messageheader = true;
122 $messageheaderstart=false;
123 $boundaries = array();
125 session_unregister("entities");
135 $body[$count-1] = substr($body[$count-1], -1);
136 for ($i=1; $i < $count; $i++
) {
137 $line = rtrim($body[$i]);
144 } else if ($messageheader) {
147 $end = '</div><div class="ent_body" id="'.$entStr.'B">';
149 $mimepart = -$header;
151 if ($messageheaderstart) {
152 $messageheaderstart=false;
154 } else if ($messageheaderstart) {
155 $messageheader= false;
159 $end = '</div><div class="ent_body" id="'.$entStr.'B">';
167 if (!$header && $messageheader) {
168 $messageheaderstart=true;
170 $pre = '<i><font color ="'.$color[1].'">';
171 $end = '</i></font>';
174 if (!$messageheader && !$header ) {
182 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
185 $bndreg = str_replace("\\","\\\\",$bndreg);
186 $bndreg = str_replace("?","\\?",$bndreg);
187 $bndreg = str_replace("+","\\+",$bndreg);
188 $bndreg = str_replace(".","\\.",$bndreg);
189 $bndreg = str_replace("/","\\/",$bndreg);
190 $bndreg = str_replace("-","\\-",$bndreg);
191 $bndreg = str_replace("(","\\(",$bndreg);
192 $bndreg = str_replace(")","\\)",$bndreg);
194 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
195 $messageheader = false;
196 $messageheaderstart=false;
201 $entStr = CalcEntity("$entStr",1);
205 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
206 $cnt=count($boundaries)-1;
207 $bnd = $boundaries[$cnt]['bnd'];
208 $bndreg = $boundaries[$cnt]['bndreg'];
210 $regstr = '/^--'."($bndreg)".".*".'/';
211 if (preg_match($regstr,$line,$reg) ) {
212 $bndlen = strlen($reg[1]);
214 if (strlen($line) > ($bndlen +
3)) {
215 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
219 $entStr = CalcEntity("$entStr",-1);
220 array_pop($boundaries);
221 $pre .= '<b><font color ="'.$color[2].'">';
222 $end .= '</font></b>';
230 $entStr = CalcEntity("$entStr",0);
232 $content[$content_indx]=array();
233 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
234 $pre .= '</div><div class="entheader" id="'.
235 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
236 $end .= '</font></b>';
243 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
244 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
245 $messageheader = true;
247 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
249 if ($reg[2] == 'image') {
250 $entities["$entStr"] = array();
251 $entities["$entStr"]['entity'] = $entStr;
252 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
254 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
255 $name = htmlspecialchars($reg[2]);
256 $content[$content_indx]['name'] = decodeHeader($name);
258 if (isset($entities["$entStr"])) {
259 $entities["$entStr"]['name'] = urlEncode($reg[2]);
261 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
263 if (isset($entities["$entStr"])) {
264 $entities["$entStr"]['encoding']=$reg[2];
266 $content[$content_indx]['encoding'] = $encoding;
270 $pre .= '<b><font color='.$color[7].'">';
271 $end .= '</font></b>';
278 $message_body .= $line . "\r\n";
280 $line = htmlspecialchars($line);
281 if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
282 $message_body .= "$pre"."$line"."$end"."\r\n";
286 //$returnValue .= returnTime($start).'<br />';
289 <style type="text/css">
312 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
313 $returnValue .= ob_get_contents();
319 $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
320 $returnValue .= '<font face="monospace">'."\n";
321 $returnValue .= '<br />'."\n";
324 if (count($content) > 0 && !$stripHTML) {
325 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
326 $returnValue .= '<table border="1" width="98%"><thead>'.
327 '<tr bgcolor="'.$color[7].'">'.
328 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
329 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
330 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
331 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
334 for ($i = 0; $i < count($content);$i++
) {
335 $returnValue .= '<tr><td>';
336 $returnValue .= $content[$i]['ent'].'</td><td>';
337 if (isset($content[$i]['type'])) {
338 $returnValue .= $content[$i]['type'];
339 } else $returnValue .= 'TEXT/PLAIN';
340 $returnValue .= '</td><td>';
341 if (isset($content[$i]['name'])) {
342 $returnValue .= $content[$i]['name'];
343 } else $returnValue .= ' ';
344 $returnValue .= '</td><td>';
345 if (isset($content[$i]['encoding'])) {
346 $returnValue .= $content[$i]['encoding'];
347 } else $returnValue .= ' ';
348 $returnValue .= '</td></tr>'."\n";
350 $returnValue .= '</tbody></table><br />'."\n";
354 $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
355 $returnValue .= '<pre><div><div class="header">';
357 $returnValue .= '<pre>';
360 $returnValue .= $message_body;
363 $returnValue .= '</div></div></pre></font></body></html>';
365 $returnValue .= '</pre>';