5 * Plugin to view the RFC822 raw message output and the bodystructure of a message
7 * Copyright (c) 2002 Marc Groot Koerkamp, The Netherlands
8 * Licensed under the GNU GPL. For full terms see the file COPYING.
12 * @subpackage message_details
16 define('SM_PATH','../../');
18 /* SquirrelMail required files. */
19 require_once(SM_PATH
. 'include/validate.php');
20 require_once(SM_PATH
. 'functions/imap.php');
21 require_once(SM_PATH
. 'functions/mime.php');
22 require_once(SM_PATH
. 'config/config.php');
23 require_once(SM_PATH
. 'functions/prefs.php');
25 global $color, $uid_support;
27 sqgetGlobalVar('passed_id', $passed_id, SQ_GET
);
28 sqgetGlobalVar('mailbox', $mailbox, SQ_GET
);
30 sqgetGlobalVar('username', $username, SQ_SESSION
);
31 sqgetGlobalVar('key', $key, SQ_COOKIE
);
32 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION
);
34 function CalcEntity($entString, $direction) {
36 if ($direction == -1) {
37 $pos = strrpos($entString,'.');
38 $result = substr($entString,0,$pos);
43 $pos = strrpos($entString,'.');
49 $level = substr($entString,0,$pos);
50 $sublevel = substr($entString,$pos+
1);
52 $result = "$level".'.'."$sublevel";
56 $result = "$entString".".0";
64 function returnTime($start) {
65 $stop = gettimeofday();
66 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
70 function GetMimeProperties($header) {
73 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
74 $read = sqimap_mailbox_select($imapConnection, $mailbox);
75 $start = gettimeofday();
76 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, $uid_support);
81 $messageheader = true;
82 $messageheaderstart=false;
83 $boundaries = array();
85 session_unregister("entities");
95 $body[$count-1] = substr($body[$count-1], -1);
96 for ($i=1; $i < $count; $i++
) {
97 $line = trim($body[$i]);
104 } else if ($messageheader) {
107 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" ID="'.$entStr.'B">'."\n \n";
109 $mimepart = -$header;
111 if ($messageheaderstart) {
112 $messageheaderstart=false;
114 } else if ($messageheaderstart) {
115 $messageheader= false;
119 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" ID="'.$entStr.'B">'."\n \n";
127 if (!$header && $messageheader) {
128 $messageheaderstart=true;
130 $pre = '<i><font color ='."$color[1]>";
131 $end = '</i></font>';
134 if (!$messageheader && !$header ) {
142 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
145 $bndreg = str_replace("\\","\\\\",$bndreg);
146 $bndreg = str_replace("?","\\?",$bndreg);
147 $bndreg = str_replace("+","\\+",$bndreg);
148 $bndreg = str_replace(".","\\.",$bndreg);
149 $bndreg = str_replace("/","\\/",$bndreg);
150 $bndreg = str_replace("-","\\-",$bndreg);
151 $bndreg = str_replace("(","\\(",$bndreg);
152 $bndreg = str_replace(")","\\)",$bndreg);
154 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
155 $messageheader = false;
156 $messageheaderstart=false;
161 $entStr = CalcEntity("$entStr",1);
165 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
166 $cnt=count($boundaries)-1;
167 $bnd = $boundaries[$cnt]['bnd'];
168 $bndreg = $boundaries[$cnt]['bndreg'];
170 $regstr = '/^--'."($bndreg)".".*".'/';
171 if (preg_match($regstr,$line,$reg) ) {
172 $bndlen = strlen($reg[1]);
174 if (strlen($line) > ($bndlen +
3)) {
175 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
179 $entStr = CalcEntity("$entStr",-1);
180 array_pop($boundaries);
181 $pre .= '<b><font color ='."$color[2]>";
182 $end .= '</font></b>';
190 $entStr = CalcEntity("$entStr",0);
192 $content[$content_indx]=array();
193 $content[$content_indx]['ent'] = '<A HREF="#'."$entStr \">$entStr".'</a>';
194 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" ID="'.$entStr.'H"><a name="'."$entStr".'"><b><font color ='."$color[2]>";
195 $end .= '</font></b>'."\n";
202 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
203 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
204 $messageheader = true;
206 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
208 if ($reg[2] == 'image') {
209 $entities["$entStr"] = array();
210 $entities["$entStr"]['entity'] = $entStr;
211 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
213 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
214 $name = htmlspecialchars($reg[2]);
215 $content[$content_indx]['name'] = decodeHeader($name);
217 if (isset($entities["$entStr"])) {
218 $entities["$entStr"]['name'] = urlEncode($reg[2]);
220 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
222 if (isset($entities["$entStr"])) {
223 $entities["$entStr"]['encoding']=$reg[2];
225 $content[$content_indx]['encoding'] = $encoding;
229 $pre .= '<b><font color ='."$color[7]>";
230 $end .= '</font></b>';
237 if (isset($entities["$entStr"])) {
238 if (isset($encoding) && $encoding == 'base64') {
239 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
240 $entities["$entStr"]['content'] .= $line;
245 $line = htmlspecialchars($line);
246 $message_body .= "$pre"."$line"."$end".'<BR>'."\r\n";
248 //echo returnTime($start).'<BR>';
272 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
274 echo "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
276 echo '<font face = "monospace">'."\n";
280 //session_register("entities");
281 //$keys = array_keys($entities);
282 //$start = gettimeofday();
283 //foreach ($keys as $key) {
284 // if (isset($entities[$key])) {
285 // if ($entities[$key]['encoding'] == 'base64') {
286 // echo '<img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br>';
290 //session_unregister("entities");
292 if (count($content) > 0) {
293 echo '<h2> Bodystructure </h2>'."\n\n";
294 echo '<table border=1 width="98%"><thead>'.
295 '<tr bgcolor="'."$color[7]".'">'.
296 '<td><b><font color="'."$color[5]".'">Entity</font></b></td>'.
297 '<td><b><font color="'."$color[5]".'">Content-Type</font></b></td>'.
298 '<td><b><font color="'."$color[5]".'">Name</font></b></td>'.
299 '<td><b><font color="'."$color[5]".'">Encoding</font></b></td>'.
303 for ($i = 0; $i < count($content);$i++
) {
305 echo $content[$i]['ent'].'</td><td>';
306 if (isset($content[$i]['type'])) {
307 echo $content[$i]['type'];
308 } else echo 'TEXT/PLAIN';
310 if (isset($content[$i]['name'])) {
311 echo $content[$i]['name'];
312 } else echo ' ';
314 if (isset($content[$i]['encoding'])) {
315 echo $content[$i]['encoding'];
316 } else echo ' ';
317 echo '</td></tr>'."\n";
319 echo '</tbody></table><br>'."\n";
321 echo '<h2> RFC822 Message body </h2>'."\n\n";
322 echo '<div><div class="header">'."\n\n";
324 echo '</div></div></font></code></body></html>';