f3eee63a9cf36b0ab3b7ed251ce863e45b7ef57a
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 * 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))
36 sqgetGlobalVar('passed_id', $passed_id, SQ_GET
);
37 sqgetGlobalVar('mailbox', $mailbox, SQ_GET
);
38 echo get_message_details($mailbox, $passed_id);
42 // ---------- function definitions ----------
45 * Converts 8bit string to hex
47 * Replaces 8bit symbols with their hex strings,
48 * encloses them in curly brackets and uses different color.
49 * @param string $string text
53 function msgd_convert_to_hex($string) {
55 return preg_replace("/([\200-\377])/e","'<font color=\"$color[2]\">{'.dechex(ord('\\1')).'}</font>'",$string);
59 * Calculates id of MIME entity
60 * @param string $entString
61 * @param integer $direction
65 function CalcEntity($entString, $direction) {
67 if ($direction == -1) {
68 $pos = strrpos($entString,'.');
69 $result = substr($entString,0,$pos);
74 $pos = strrpos($entString,'.');
80 $level = substr($entString,0,$pos);
81 $sublevel = substr($entString,$pos+
1);
83 $result = "$level".'.'."$sublevel";
87 $result = "$entString".".0";
98 * Returns time in microseconds between selected and current timestamp
100 * @param array $start see details about array format at http://www.php.net/gettimeofday
101 * @return integer time in microseconds
104 function returnTime($start) {
105 $stop = gettimeofday();
106 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
113 * Returns actual message details
114 * @param string $mailbox
115 * @param string $passed_id
116 * @param boolean $stripHTML If TRUE, only plain text is returned,
117 * default is FALSE, wherein output contains
118 * pretty-HTMLification of message body
119 * @return string The formatted message details
122 function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
124 global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex;
128 sqgetGlobalVar('username', $username, SQ_SESSION
);
129 sqgetGlobalVar('key', $key, SQ_COOKIE
);
131 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
132 $read = sqimap_mailbox_select($imapConnection, $mailbox);
133 $start = gettimeofday();
134 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
139 $messageheader = true;
140 $messageheaderstart=false;
141 $boundaries = array();
143 session_unregister("entities");
153 $body[$count-1] = substr($body[$count-1], -1);
154 for ($i=1; $i < $count; $i++
) {
155 $line = trim($body[$i]);
162 } else if ($messageheader) {
165 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
167 $mimepart = -$header;
169 if ($messageheaderstart) {
170 $messageheaderstart=false;
172 } else if ($messageheaderstart) {
173 $messageheader= false;
177 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
185 if (!$header && $messageheader) {
186 $messageheaderstart=true;
188 $pre = '<i><font color ="'.$color[1].'">';
189 $end = '</i></font>';
192 if (!$messageheader && !$header ) {
200 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
203 $bndreg = str_replace("\\","\\\\",$bndreg);
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);
212 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
213 $messageheader = false;
214 $messageheaderstart=false;
219 $entStr = CalcEntity("$entStr",1);
223 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
224 $cnt=count($boundaries)-1;
225 $bnd = $boundaries[$cnt]['bnd'];
226 $bndreg = $boundaries[$cnt]['bndreg'];
228 $regstr = '/^--'."($bndreg)".".*".'/';
229 if (preg_match($regstr,$line,$reg) ) {
230 $bndlen = strlen($reg[1]);
232 if (strlen($line) > ($bndlen +
3)) {
233 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
237 $entStr = CalcEntity("$entStr",-1);
238 array_pop($boundaries);
239 $pre .= '<b><font color ="'.$color[2].'">';
240 $end .= '</font></b>';
248 $entStr = CalcEntity("$entStr",0);
250 $content[$content_indx]=array();
251 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
252 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
253 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
254 $end .= '</font></b>'."\n";
261 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
262 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
263 $messageheader = true;
265 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
267 if ($reg[2] == 'image') {
268 $entities["$entStr"] = array();
269 $entities["$entStr"]['entity'] = $entStr;
270 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
272 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
273 $name = htmlspecialchars($reg[2]);
274 $content[$content_indx]['name'] = decodeHeader($name);
276 if (isset($entities["$entStr"])) {
277 $entities["$entStr"]['name'] = urlEncode($reg[2]);
279 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
281 if (isset($entities["$entStr"])) {
282 $entities["$entStr"]['encoding']=$reg[2];
284 $content[$content_indx]['encoding'] = $encoding;
288 $pre .= '<b><font color='.$color[7].'">';
289 $end .= '</font></b>';
296 if (isset($entities["$entStr"])) {
297 if (isset($encoding) && $encoding == 'base64') {
298 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
299 $entities["$entStr"]['content'] .= $line;
305 $message_body .= $line . "\r\n";
307 $line = htmlspecialchars($line);
308 if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
309 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
313 //$returnValue .= returnTime($start).'<br />';
316 <style type="text/css">
339 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
340 $returnValue .= ob_get_contents();
346 $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
347 $returnValue .= '<code>'."\n";
348 $returnValue .= '<font face="monospace">'."\n";
349 $returnValue .= '<br />'."\n";
353 //session_register("entities");
354 //$keys = array_keys($entities);
355 //$start = gettimeofday();
356 //foreach ($keys as $key) {
357 // if (isset($entities[$key])) {
358 // if ($entities[$key]['encoding'] == 'base64') {
359 // if (!$stripHTML) {
360 // $returnValue .= 'img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br />';
365 //session_unregister("entities");
367 if (count($content) > 0 && !$stripHTML) {
368 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
369 $returnValue .= '<table border="1" width="98%"><thead>'.
370 '<tr bgcolor="'.$color[7].'">'.
371 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
372 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
373 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
374 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
377 for ($i = 0; $i < count($content);$i++
) {
378 $returnValue .= '<tr><td>';
379 $returnValue .= $content[$i]['ent'].'</td><td>';
380 if (isset($content[$i]['type'])) {
381 $returnValue .= $content[$i]['type'];
382 } else $returnValue .= 'TEXT/PLAIN';
383 $returnValue .= '</td><td>';
384 if (isset($content[$i]['name'])) {
385 $returnValue .= $content[$i]['name'];
386 } else $returnValue .= ' ';
387 $returnValue .= '</td><td>';
388 if (isset($content[$i]['encoding'])) {
389 $returnValue .= $content[$i]['encoding'];
390 } else $returnValue .= ' ';
391 $returnValue .= '</td></tr>'."\n";
393 $returnValue .= '</tbody></table><br />'."\n";
397 $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
398 $returnValue .= '<div><div class="header">'."\n\n";
401 $returnValue .= $message_body;
404 $returnValue .= '</div></div></font></code></body></html>';