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-2006 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @subpackage message_details
18 if (!defined('SM_PATH'))
19 define('SM_PATH','../../');
21 /* SquirrelMail required files. */
22 require_once(SM_PATH
. 'include/validate.php');
23 require_once(SM_PATH
. 'functions/imap.php');
24 require_once(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))
37 sqgetGlobalVar('passed_id', $passed_id, SQ_GET
);
38 sqgetGlobalVar('mailbox', $mailbox, SQ_GET
);
39 echo get_message_details($mailbox, $passed_id);
43 // ---------- function definitions ----------
46 * Converts 8bit string to hex
48 * Replaces 8bit symbols with their hex strings,
49 * encloses them in curly brackets and uses different color.
50 * @param string $string text
54 function msgd_convert_to_hex($string) {
56 return preg_replace("/([\200-\377])/e","'<font color=\"$color[2]\">{'.dechex(ord('\\1')).'}</font>'",$string);
60 * Calculates id of MIME entity
61 * @param string $entString
62 * @param integer $direction
66 function CalcEntity($entString, $direction) {
68 if ($direction == -1) {
69 $pos = strrpos($entString,'.');
70 $result = substr($entString,0,$pos);
75 $pos = strrpos($entString,'.');
81 $level = substr($entString,0,$pos);
82 $sublevel = substr($entString,$pos+
1);
84 $result = "$level".'.'."$sublevel";
88 $result = "$entString".".0";
99 * Returns time in microseconds between selected and current timestamp
101 * @param array $start see details about array format at http://www.php.net/gettimeofday
102 * @return integer time in microseconds
105 function returnTime($start) {
106 $stop = gettimeofday();
107 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
114 * Returns actual message details
115 * @param string $mailbox
116 * @param string $passed_id
117 * @param boolean $stripHTML If TRUE, only plain text is returned,
118 * default is FALSE, wherein output contains
119 * pretty-HTMLification of message body
120 * @return string The formatted message details
123 function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
125 global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex;
129 sqgetGlobalVar('username', $username, SQ_SESSION
);
130 sqgetGlobalVar('key', $key, SQ_COOKIE
);
132 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
133 $read = sqimap_mailbox_select($imapConnection, $mailbox);
134 $start = gettimeofday();
135 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
140 $messageheader = true;
141 $messageheaderstart=false;
142 $boundaries = array();
144 session_unregister("entities");
154 $body[$count-1] = substr($body[$count-1], -1);
155 for ($i=1; $i < $count; $i++
) {
156 $line = trim($body[$i]);
163 } else if ($messageheader) {
166 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
168 $mimepart = -$header;
170 if ($messageheaderstart) {
171 $messageheaderstart=false;
173 } else if ($messageheaderstart) {
174 $messageheader= false;
178 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
186 if (!$header && $messageheader) {
187 $messageheaderstart=true;
189 $pre = '<i><font color ="'.$color[1].'">';
190 $end = '</i></font>';
193 if (!$messageheader && !$header ) {
201 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
204 $bndreg = str_replace("\\","\\\\",$bndreg);
205 $bndreg = str_replace("?","\\?",$bndreg);
206 $bndreg = str_replace("+","\\+",$bndreg);
207 $bndreg = str_replace(".","\\.",$bndreg);
208 $bndreg = str_replace("/","\\/",$bndreg);
209 $bndreg = str_replace("-","\\-",$bndreg);
210 $bndreg = str_replace("(","\\(",$bndreg);
211 $bndreg = str_replace(")","\\)",$bndreg);
213 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
214 $messageheader = false;
215 $messageheaderstart=false;
220 $entStr = CalcEntity("$entStr",1);
224 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
225 $cnt=count($boundaries)-1;
226 $bnd = $boundaries[$cnt]['bnd'];
227 $bndreg = $boundaries[$cnt]['bndreg'];
229 $regstr = '/^--'."($bndreg)".".*".'/';
230 if (preg_match($regstr,$line,$reg) ) {
231 $bndlen = strlen($reg[1]);
233 if (strlen($line) > ($bndlen +
3)) {
234 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
238 $entStr = CalcEntity("$entStr",-1);
239 array_pop($boundaries);
240 $pre .= '<b><font color ="'.$color[2].'">';
241 $end .= '</font></b>';
249 $entStr = CalcEntity("$entStr",0);
251 $content[$content_indx]=array();
252 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
253 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
254 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
255 $end .= '</font></b>'."\n";
262 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
263 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
264 $messageheader = true;
266 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
268 if ($reg[2] == 'image') {
269 $entities["$entStr"] = array();
270 $entities["$entStr"]['entity'] = $entStr;
271 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
273 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
274 $name = htmlspecialchars($reg[2]);
275 $content[$content_indx]['name'] = decodeHeader($name);
277 if (isset($entities["$entStr"])) {
278 $entities["$entStr"]['name'] = urlEncode($reg[2]);
280 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
282 if (isset($entities["$entStr"])) {
283 $entities["$entStr"]['encoding']=$reg[2];
285 $content[$content_indx]['encoding'] = $encoding;
289 $pre .= '<b><font color='.$color[7].'">';
290 $end .= '</font></b>';
297 if (isset($entities["$entStr"])) {
298 if (isset($encoding) && $encoding == 'base64') {
299 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
300 $entities["$entStr"]['content'] .= $line;
306 $message_body .= $line . "\r\n";
308 $line = htmlspecialchars($line);
309 if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
310 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
314 //$returnValue .= returnTime($start).'<br />';
317 <style type="text/css">
340 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
341 $returnValue .= ob_get_contents();
347 $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
348 $returnValue .= '<code>'."\n";
349 $returnValue .= '<font face="monospace">'."\n";
350 $returnValue .= '<br />'."\n";
354 //session_register("entities");
355 //$keys = array_keys($entities);
356 //$start = gettimeofday();
357 //foreach ($keys as $key) {
358 // if (isset($entities[$key])) {
359 // if ($entities[$key]['encoding'] == 'base64') {
360 // if (!$stripHTML) {
361 // $returnValue .= 'img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br />';
366 //session_unregister("entities");
368 if (count($content) > 0 && !$stripHTML) {
369 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
370 $returnValue .= '<table border="1" width="98%"><thead>'.
371 '<tr bgcolor="'.$color[7].'">'.
372 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
373 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
374 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
375 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
378 for ($i = 0; $i < count($content);$i++
) {
379 $returnValue .= '<tr><td>';
380 $returnValue .= $content[$i]['ent'].'</td><td>';
381 if (isset($content[$i]['type'])) {
382 $returnValue .= $content[$i]['type'];
383 } else $returnValue .= 'TEXT/PLAIN';
384 $returnValue .= '</td><td>';
385 if (isset($content[$i]['name'])) {
386 $returnValue .= $content[$i]['name'];
387 } else $returnValue .= ' ';
388 $returnValue .= '</td><td>';
389 if (isset($content[$i]['encoding'])) {
390 $returnValue .= $content[$i]['encoding'];
391 } else $returnValue .= ' ';
392 $returnValue .= '</td></tr>'."\n";
394 $returnValue .= '</tbody></table><br />'."\n";
398 $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
399 $returnValue .= '<div><div class="header">'."\n\n";
402 $returnValue .= $message_body;
405 $returnValue .= '</div></div></font></code></body></html>';