f5f68db9f28174253cdf948812ec2146bfcd2ffc
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
);
29 if (!empty($md_action))
31 sqgetGlobalVar('passed_id', $passed_id, SQ_GET
);
32 sqgetGlobalVar('mailbox', $mailbox, SQ_GET
);
33 echo get_message_details($mailbox, $passed_id);
37 // ---------- function definitions ----------
41 * Calculates id of MIME entity
42 * @param string $entString
43 * @param integer $direction
47 function CalcEntity($entString, $direction) {
49 if ($direction == -1) {
50 $pos = strrpos($entString,'.');
51 $result = substr($entString,0,$pos);
56 $pos = strrpos($entString,'.');
62 $level = substr($entString,0,$pos);
63 $sublevel = substr($entString,$pos+
1);
65 $result = "$level".'.'."$sublevel";
69 $result = "$entString".".0";
80 * Returns time in microseconds between selected and current timestamp
82 * @param array $start see details about array format at http://www.php.net/gettimeofday
83 * @return integer time in microseconds
86 function returnTime($start) {
87 $stop = gettimeofday();
88 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
95 * Returns actual message details
96 * @param string $mailbox
97 * @param string $passed_id
98 * @param boolean $stripHTML If TRUE, only plain text is returned,
99 * default is FALSE, wherein output contains
100 * pretty-HTMLification of message body
101 * @return string The formatted message details
104 function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
106 global $imapServerAddress, $imapPort, $color;
110 sqgetGlobalVar('username', $username, SQ_SESSION
);
111 sqgetGlobalVar('key', $key, SQ_COOKIE
);
113 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
114 $read = sqimap_mailbox_select($imapConnection, $mailbox);
115 $start = gettimeofday();
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 = trim($body[$i]);
144 } else if ($messageheader) {
147 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
149 $mimepart = -$header;
151 if ($messageheaderstart) {
152 $messageheaderstart=false;
154 } else if ($messageheaderstart) {
155 $messageheader= false;
159 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
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 .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
235 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
236 $end .= '</font></b>'."\n";
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 if (isset($entities["$entStr"])) {
279 if (isset($encoding) && $encoding == 'base64') {
280 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
281 $entities["$entStr"]['content'] .= $line;
287 $message_body .= $line . "\r\n";
289 $line = htmlspecialchars($line);
290 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
293 //$returnValue .= returnTime($start).'<br />';
319 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
320 $returnValue .= ob_get_contents();
326 $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
327 $returnValue .= '<code>'."\n";
328 $returnValue .= '<font face="monospace">'."\n";
329 $returnValue .= '<br />'."\n";
333 //session_register("entities");
334 //$keys = array_keys($entities);
335 //$start = gettimeofday();
336 //foreach ($keys as $key) {
337 // if (isset($entities[$key])) {
338 // if ($entities[$key]['encoding'] == 'base64') {
339 // if (!$stripHTML) {
340 // $returnValue .= 'img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br />';
345 //session_unregister("entities");
347 if (count($content) > 0 && !$stripHTML) {
348 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
349 $returnValue .= '<table border="1" width="98%"><thead>'.
350 '<tr bgcolor="'.$color[7].'">'.
351 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
352 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
353 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
354 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
357 for ($i = 0; $i < count($content);$i++
) {
358 $returnValue .= '<tr><td>';
359 $returnValue .= $content[$i]['ent'].'</td><td>';
360 if (isset($content[$i]['type'])) {
361 $returnValue .= $content[$i]['type'];
362 } else $returnValue .= 'TEXT/PLAIN';
363 $returnValue .= '</td><td>';
364 if (isset($content[$i]['name'])) {
365 $returnValue .= $content[$i]['name'];
366 } else $returnValue .= ' ';
367 $returnValue .= '</td><td>';
368 if (isset($content[$i]['encoding'])) {
369 $returnValue .= $content[$i]['encoding'];
370 } else $returnValue .= ' ';
371 $returnValue .= '</td></tr>'."\n";
373 $returnValue .= '</tbody></table><br />'."\n";
377 $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
378 $returnValue .= '<div><div class="header">'."\n\n";
381 $returnValue .= $message_body;
384 $returnValue .= '</div></div></font></code></body></html>';