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-2009 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @subpackage message_details
16 //FIXME: this file uses HTML extensively and eventually needs to be "templatized" (don't echo HTML directly)
19 * Include the SquirrelMail initialization file.
21 require('../../include/init.php');
22 require(SM_PATH
. 'functions/imap_general.php');
23 require(SM_PATH
. 'functions/imap_messages.php');
24 require(SM_PATH
. 'functions/mime.php');
26 sqgetGlobalVar('get_message_details', $md_action, SQ_GET
);
29 * Controls display of 8bit symbols in message source
30 * @global boolean $msgd_8bit_in_hex;
32 global $msgd_8bit_in_hex;
33 $msgd_8bit_in_hex=false;
35 if (!empty($md_action)) {
36 sqgetGlobalVar('passed_id', $passed_id, SQ_GET
, NULL, SQ_TYPE_BIGINT
);
37 if (!sqgetGlobalVar('passed_ent_id', $passed_ent_id, SQ_GET
))
39 sqgetGlobalVar('mailbox', $mailbox, SQ_GET
);
41 * change $unformatted to TRUE if you want to see
42 * message source without formating
45 echo get_message_details($mailbox, $passed_id, $passed_ent_id, $unformatted);
49 // ---------- function definitions ----------
52 * Converts 8bit string to hex
54 * Replaces 8bit symbols with their hex strings,
55 * encloses them in curly brackets and uses different color.
56 * @param string $string text
60 function msgd_convert_to_hex($string) {
62 return preg_replace("/([\200-\377])/e","'<font color=\"$color[2]\">{'.dechex(ord('\\1')).'}</font>'",$string);
66 * Calculates id of MIME entity
67 * @param string $entString
68 * @param integer $direction
72 function CalcEntity($entString, $direction) {
74 if ($direction == -1) {
75 $pos = strrpos($entString,'.');
76 $result = substr($entString,0,$pos);
81 $pos = strrpos($entString,'.');
87 $level = substr($entString,0,$pos);
88 $sublevel = substr($entString,$pos+
1);
90 $result = "$level".'.'."$sublevel";
94 $result = "$entString".".0";
104 * Returns actual message details
105 * @param string $mailbox
106 * @param string $passed_id
107 * @param string $passed_ent_id
108 * @param boolean $stripHTML If TRUE, only plain text is returned,
109 * default is FALSE, wherein output contains
110 * pretty-HTMLification of message body
111 * @return string The formatted message details
114 function get_message_details($mailbox, $passed_id, $passed_ent_id=0, $stripHTML=FALSE) {
115 global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex, $username;
119 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
120 $read = sqimap_mailbox_select($imapConnection, $mailbox);
121 if (!empty($passed_ent_id))
122 $body = sqimap_run_command($imapConnection, "FETCH $passed_id BODY[$passed_ent_id]",true, $response, $readmessage, TRUE);
124 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
129 $messageheader = true;
130 $messageheaderstart=false;
131 $boundaries = array();
133 session_unregister("entities");
143 $body[$count-1] = substr($body[$count-1], -1);
144 for ($i=1; $i < $count; $i++
) {
145 $line = rtrim($body[$i]);
152 } else if ($messageheader) {
155 $end = '</div><div class="ent_body" id="'.$entStr.'B">';
157 $mimepart = -$header;
159 if ($messageheaderstart) {
160 $messageheaderstart=false;
162 } else if ($messageheaderstart) {
163 $messageheader= false;
167 $end = '</div><div class="ent_body" id="'.$entStr.'B">';
175 if (!$header && $messageheader) {
176 $messageheaderstart=true;
178 $pre = '<i><font color ="'.$color[1].'">';
179 $end = '</i></font>';
182 if (!$messageheader && !$header ) {
190 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
193 $bndreg = str_replace("\\","\\\\",$bndreg);
194 $bndreg = str_replace("?","\\?",$bndreg);
195 $bndreg = str_replace("+","\\+",$bndreg);
196 $bndreg = str_replace(".","\\.",$bndreg);
197 $bndreg = str_replace("/","\\/",$bndreg);
198 $bndreg = str_replace("-","\\-",$bndreg);
199 $bndreg = str_replace("(","\\(",$bndreg);
200 $bndreg = str_replace(")","\\)",$bndreg);
202 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
203 $messageheader = false;
204 $messageheaderstart=false;
209 $entStr = CalcEntity("$entStr",1);
213 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
214 $cnt=count($boundaries)-1;
215 $bnd = $boundaries[$cnt]['bnd'];
216 $bndreg = $boundaries[$cnt]['bndreg'];
218 $regstr = '/^--'."($bndreg)".".*".'/';
219 if (preg_match($regstr,$line,$reg) ) {
220 $bndlen = strlen($reg[1]);
222 if (strlen($line) > ($bndlen +
3)) {
223 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
227 $entStr = CalcEntity("$entStr",-1);
228 array_pop($boundaries);
229 $pre .= '<b><font color ="'.$color[2].'">';
230 $end .= '</font></b>';
238 $entStr = CalcEntity("$entStr",0);
240 $content[$content_indx]=array();
241 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
242 $pre .= '</div><div class="entheader" id="'.
243 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
244 $end .= '</font></b>';
251 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
252 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
253 $messageheader = true;
255 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
257 if ($reg[2] == 'image') {
258 $entities["$entStr"] = array();
259 $entities["$entStr"]['entity'] = $entStr;
260 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
262 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
263 $name = htmlspecialchars($reg[2]);
264 $content[$content_indx]['name'] = decodeHeader($name);
266 if (isset($entities["$entStr"])) {
267 $entities["$entStr"]['name'] = urlEncode($reg[2]);
269 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
271 if (isset($entities["$entStr"])) {
272 $entities["$entStr"]['encoding']=$reg[2];
274 $content[$content_indx]['encoding'] = $encoding;
278 $pre .= '<b><font color='.$color[7].'">';
279 $end .= '</font></b>';
286 $message_body .= $line . "\r\n";
288 $line = htmlspecialchars($line);
289 if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
290 $message_body .= "$pre"."$line"."$end"."\r\n";
294 //$returnValue .= returnTime($start).'<br />';
297 <style type="text/css">
318 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
319 $returnValue .= ob_get_contents();
325 $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
326 $returnValue .= '<font face="monospace">'."\n";
327 $returnValue .= '<br />'."\n";
330 if (count($content) > 0 && !$stripHTML) {
331 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
332 $returnValue .= '<table border="1" width="98%"><thead>'.
333 '<tr bgcolor="'.$color[7].'">'.
334 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
335 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
336 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
337 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
340 for ($i = 0; $i < count($content);$i++
) {
341 $returnValue .= '<tr><td>';
342 $returnValue .= $content[$i]['ent'].'</td><td>';
343 if (isset($content[$i]['type'])) {
344 $returnValue .= $content[$i]['type'];
345 } else $returnValue .= 'TEXT/PLAIN';
346 $returnValue .= '</td><td>';
347 if (isset($content[$i]['name'])) {
348 $returnValue .= $content[$i]['name'];
349 } else $returnValue .= ' ';
350 $returnValue .= '</td><td>';
351 if (isset($content[$i]['encoding'])) {
352 $returnValue .= $content[$i]['encoding'];
353 } else $returnValue .= ' ';
354 $returnValue .= '</td></tr>'."\n";
356 $returnValue .= '</tbody></table><br />'."\n";
360 $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
361 $returnValue .= '<pre><div><div class="header">';
363 $returnValue .= '<pre>';
366 $returnValue .= $message_body;
369 $returnValue .= '</div></div></pre></font></body></html>';
371 $returnValue .= '</pre>';