3 * Message Details plugin - bottom frame with message structure and rfc822 body
5 * Plugin to view the RFC822 raw message output and the bodystructure of a message
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * @author Marc Groot Koerkamp
10 * @copyright Copyright © 2002 Marc Groot Koerkamp, The Netherlands
11 * @copyright Copyright © 2004-2005 The SquirrelMail Project Team
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
15 * @subpackage message_details
19 if (!defined('SM_PATH'))
20 define('SM_PATH','../../');
22 /* SquirrelMail required files. */
23 require_once(SM_PATH
. 'include/validate.php');
24 require_once(SM_PATH
. 'functions/imap.php');
25 require_once(SM_PATH
. 'functions/mime.php');
27 sqgetGlobalVar('get_message_details', $md_action, SQ_GET
);
30 * Controls display of 8bit symbols in message source
31 * @global boolean $msgd_8bit_in_hex;
33 global $msgd_8bit_in_hex;
34 $msgd_8bit_in_hex=false;
36 if (!empty($md_action))
38 sqgetGlobalVar('passed_id', $passed_id, SQ_GET
);
39 sqgetGlobalVar('mailbox', $mailbox, SQ_GET
);
40 echo get_message_details($mailbox, $passed_id);
44 // ---------- function definitions ----------
47 * Converts 8bit string to hex
49 * Replaces 8bit symbols with their hex strings,
50 * encloses them in curly brackets and uses different color.
51 * @param string $string text
55 function msgd_convert_to_hex($string) {
57 return preg_replace("/([\200-\377])/e","'<font color=\"$color[2]\">{'.dechex(ord('\\1')).'}</font>'",$string);
61 * Calculates id of MIME entity
62 * @param string $entString
63 * @param integer $direction
67 function CalcEntity($entString, $direction) {
69 if ($direction == -1) {
70 $pos = strrpos($entString,'.');
71 $result = substr($entString,0,$pos);
76 $pos = strrpos($entString,'.');
82 $level = substr($entString,0,$pos);
83 $sublevel = substr($entString,$pos+
1);
85 $result = "$level".'.'."$sublevel";
89 $result = "$entString".".0";
100 * Returns time in microseconds between selected and current timestamp
102 * @param array $start see details about array format at http://www.php.net/gettimeofday
103 * @return integer time in microseconds
106 function returnTime($start) {
107 $stop = gettimeofday();
108 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
115 * Returns actual message details
116 * @param string $mailbox
117 * @param string $passed_id
118 * @param boolean $stripHTML If TRUE, only plain text is returned,
119 * default is FALSE, wherein output contains
120 * pretty-HTMLification of message body
121 * @return string The formatted message details
124 function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
126 global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex;
130 sqgetGlobalVar('username', $username, SQ_SESSION
);
131 sqgetGlobalVar('key', $key, SQ_COOKIE
);
133 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
134 $read = sqimap_mailbox_select($imapConnection, $mailbox);
135 $start = gettimeofday();
136 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
141 $messageheader = true;
142 $messageheaderstart=false;
143 $boundaries = array();
145 session_unregister("entities");
155 $body[$count-1] = substr($body[$count-1], -1);
156 for ($i=1; $i < $count; $i++
) {
157 $line = trim($body[$i]);
164 } else if ($messageheader) {
167 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
169 $mimepart = -$header;
171 if ($messageheaderstart) {
172 $messageheaderstart=false;
174 } else if ($messageheaderstart) {
175 $messageheader= false;
179 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
187 if (!$header && $messageheader) {
188 $messageheaderstart=true;
190 $pre = '<i><font color ="'.$color[1].'">';
191 $end = '</i></font>';
194 if (!$messageheader && !$header ) {
202 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
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);
212 $bndreg = str_replace(")","\\)",$bndreg);
214 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
215 $messageheader = false;
216 $messageheaderstart=false;
221 $entStr = CalcEntity("$entStr",1);
225 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
226 $cnt=count($boundaries)-1;
227 $bnd = $boundaries[$cnt]['bnd'];
228 $bndreg = $boundaries[$cnt]['bndreg'];
230 $regstr = '/^--'."($bndreg)".".*".'/';
231 if (preg_match($regstr,$line,$reg) ) {
232 $bndlen = strlen($reg[1]);
234 if (strlen($line) > ($bndlen +
3)) {
235 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
239 $entStr = CalcEntity("$entStr",-1);
240 array_pop($boundaries);
241 $pre .= '<b><font color ="'.$color[2].'">';
242 $end .= '</font></b>';
250 $entStr = CalcEntity("$entStr",0);
252 $content[$content_indx]=array();
253 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
254 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
255 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
256 $end .= '</font></b>'."\n";
263 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
264 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
265 $messageheader = true;
267 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
269 if ($reg[2] == 'image') {
270 $entities["$entStr"] = array();
271 $entities["$entStr"]['entity'] = $entStr;
272 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
274 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
275 $name = htmlspecialchars($reg[2]);
276 $content[$content_indx]['name'] = decodeHeader($name);
278 if (isset($entities["$entStr"])) {
279 $entities["$entStr"]['name'] = urlEncode($reg[2]);
281 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
283 if (isset($entities["$entStr"])) {
284 $entities["$entStr"]['encoding']=$reg[2];
286 $content[$content_indx]['encoding'] = $encoding;
290 $pre .= '<b><font color='.$color[7].'">';
291 $end .= '</font></b>';
298 if (isset($entities["$entStr"])) {
299 if (isset($encoding) && $encoding == 'base64') {
300 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
301 $entities["$entStr"]['content'] .= $line;
307 $message_body .= $line . "\r\n";
309 $line = htmlspecialchars($line);
310 if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
311 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
315 //$returnValue .= returnTime($start).'<br />';
341 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
342 $returnValue .= ob_get_contents();
348 $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
349 $returnValue .= '<code>'."\n";
350 $returnValue .= '<font face="monospace">'."\n";
351 $returnValue .= '<br />'."\n";
355 //session_register("entities");
356 //$keys = array_keys($entities);
357 //$start = gettimeofday();
358 //foreach ($keys as $key) {
359 // if (isset($entities[$key])) {
360 // if ($entities[$key]['encoding'] == 'base64') {
361 // if (!$stripHTML) {
362 // $returnValue .= 'img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br />';
367 //session_unregister("entities");
369 if (count($content) > 0 && !$stripHTML) {
370 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
371 $returnValue .= '<table border="1" width="98%"><thead>'.
372 '<tr bgcolor="'.$color[7].'">'.
373 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
374 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
375 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
376 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
379 for ($i = 0; $i < count($content);$i++
) {
380 $returnValue .= '<tr><td>';
381 $returnValue .= $content[$i]['ent'].'</td><td>';
382 if (isset($content[$i]['type'])) {
383 $returnValue .= $content[$i]['type'];
384 } else $returnValue .= 'TEXT/PLAIN';
385 $returnValue .= '</td><td>';
386 if (isset($content[$i]['name'])) {
387 $returnValue .= $content[$i]['name'];
388 } else $returnValue .= ' ';
389 $returnValue .= '</td><td>';
390 if (isset($content[$i]['encoding'])) {
391 $returnValue .= $content[$i]['encoding'];
392 } else $returnValue .= ' ';
393 $returnValue .= '</td></tr>'."\n";
395 $returnValue .= '</tbody></table><br />'."\n";
399 $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
400 $returnValue .= '<div><div class="header">'."\n\n";
403 $returnValue .= $message_body;
406 $returnValue .= '</div></div></font></code></body></html>';