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.
13 define('SM_PATH','../../');
15 /* SquirrelMail required files. */
16 require_once(SM_PATH
. 'include/validate.php');
17 require_once(SM_PATH
. 'functions/imap.php');
18 require_once(SM_PATH
. 'functions/mime.php');
19 require_once(SM_PATH
. 'config/config.php');
20 require_once(SM_PATH
. 'functions/prefs.php');
22 global $color, $uid_support;
24 $passed_id = $_GET['passed_id'];
25 $mailbox = $_GET['mailbox'];
27 $username = $_SESSION['username'];
28 $key = $_COOKIE['key'];
29 $onetimepad = $_SESSION['onetimepad'];
32 function CalcEntity($entString, $direction) {
34 if ($direction == -1) {
35 $pos = strrpos($entString,'.');
36 $result = substr($entString,0,$pos);
41 $pos = strrpos($entString,'.');
47 $level = substr($entString,0,$pos);
48 $sublevel = substr($entString,$pos+
1);
50 $result = "$level".'.'."$sublevel";
54 $result = "$entString".".0";
62 function returnTime($start) {
63 $stop = gettimeofday();
64 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
68 function GetMimeProperties($header) {
71 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
72 $read = sqimap_mailbox_select($imapConnection, $mailbox);
73 $start = gettimeofday();
74 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, $uid_support);
79 $messageheader = true;
80 $messageheaderstart=false;
81 $boundaries = array();
83 session_unregister("entities");
93 $body[$count-1] = substr($body[$count-1], -1);
94 for ($i=1; $i < $count; $i++
) {
95 $line = trim($body[$i]);
102 } else if ($messageheader) {
105 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" ID="'.$entStr.'B">'."\n \n";
107 $mimepart = -$header;
109 if ($messageheaderstart) {
110 $messageheaderstart=false;
112 } else if ($messageheaderstart) {
113 $messageheader= false;
117 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" ID="'.$entStr.'B">'."\n \n";
125 if (!$header && $messageheader) {
126 $messageheaderstart=true;
128 $pre = '<i><font color ='."$color[1]>";
129 $end = '</i></font>';
132 if (!$messageheader && !$header ) {
140 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
143 $bndreg = str_replace("\\","\\\\",$bndreg);
144 $bndreg = str_replace("?","\\?",$bndreg);
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);
152 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
153 $messageheader = false;
154 $messageheaderstart=false;
159 $entStr = CalcEntity("$entStr",1);
163 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
164 $cnt=count($boundaries)-1;
165 $bnd = $boundaries[$cnt]['bnd'];
166 $bndreg = $boundaries[$cnt]['bndreg'];
168 $regstr = '/^--'."($bndreg)".".*".'/';
169 if (preg_match($regstr,$line,$reg) ) {
170 $bndlen = strlen($reg[1]);
172 if (strlen($line) > ($bndlen +
3)) {
173 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
177 $entStr = CalcEntity("$entStr",-1);
178 array_pop($boundaries);
179 $pre .= '<b><font color ='."$color[2]>";
180 $end .= '</font></b>';
188 $entStr = CalcEntity("$entStr",0);
190 $content[$content_indx]=array();
191 $content[$content_indx]['ent'] = '<A HREF="#'."$entStr \">$entStr".'</a>';
192 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" ID="'.$entStr.'H"><a name="'."$entStr".'"><b><font color ='."$color[2]>";
193 $end .= '</font></b>'."\n";
200 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
201 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
202 $messageheader = true;
204 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
206 if ($reg[2] == 'image') {
207 $entities["$entStr"] = array();
208 $entities["$entStr"]['entity'] = $entStr;
209 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
211 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
212 $name = htmlspecialchars($reg[2]);
213 $content[$content_indx]['name'] = decodeHeader($name);
215 if (isset($entities["$entStr"])) {
216 $entities["$entStr"]['name'] = urlEncode($reg[2]);
218 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
220 if (isset($entities["$entStr"])) {
221 $entities["$entStr"]['encoding']=$reg[2];
223 $content[$content_indx]['encoding'] = $encoding;
227 $pre .= '<b><font color ='."$color[7]>";
228 $end .= '</font></b>';
235 if (isset($entities["$entStr"])) {
236 if (isset($encoding) && $encoding == 'base64') {
237 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
238 $entities["$entStr"]['content'] .= $line;
243 $line = htmlspecialchars($line);
244 $message_body .= "$pre"."$line"."$end".'<BR>'."\r\n";
246 //echo returnTime($start).'<BR>';
264 border-style: solid none solid none;
274 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
276 echo '<body><code>'."\n";
277 echo '<font face = "monospace">'."\n";
279 //session_register("entities");
280 //$keys = array_keys($entities);
281 //$start = gettimeofday();
282 //foreach ($keys as $key) {
283 // if (isset($entities[$key])) {
284 // if ($entities[$key]['encoding'] == 'base64') {
285 // echo '<img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br>';
289 //session_unregister("entities");
291 if (count($content) > 0) {
292 echo '<h2> Bodystructure </h2>'."\n\n";
293 echo '<table border=1 width="98%"><thead>'.
294 '<tr bgcolor="'."$color[7]".'">'.
295 '<td><b><font color="'."$color[5]".'">Entity</font></b></td>'.
296 '<td><b><font color="'."$color[5]".'">Content-Type</font></b></td>'.
297 '<td><b><font color="'."$color[5]".'">Name</font></b></td>'.
298 '<td><b><font color="'."$color[5]".'">Encoding</font></b></td>'.
302 for ($i = 0; $i < count($content);$i++
) {
304 echo $content[$i]['ent'].'</td><td>';
305 if (isset($content[$i]['type'])) {
306 echo $content[$i]['type'];
307 } else echo 'TEXT/PLAIN';
309 if (isset($content[$i]['name'])) {
310 echo $content[$i]['name'];
311 } else echo ' ';
313 if (isset($content[$i]['encoding'])) {
314 echo $content[$i]['encoding'];
315 } else echo ' ';
316 echo '</td></tr>'."\n";
318 echo '</tbody></table><br>'."\n";
320 echo '<h2> RFC822 Message body </h2>'."\n\n";
321 echo '<div><div class="header">'."\n\n";
323 echo '</div></div></font></code></body></html>';