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 sqgetGlobalVar('passed_id', $passed_id, SQ_GET
);
25 sqgetGlobalVar('mailbox', $mailbox, SQ_GET
);
27 sqgetGlobalVar('username', $username, SQ_SESSION
);
28 sqgetGlobalVar('key', $key, SQ_COOKIE
);
29 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION
);
31 function CalcEntity($entString, $direction) {
33 if ($direction == -1) {
34 $pos = strrpos($entString,'.');
35 $result = substr($entString,0,$pos);
40 $pos = strrpos($entString,'.');
46 $level = substr($entString,0,$pos);
47 $sublevel = substr($entString,$pos+
1);
49 $result = "$level".'.'."$sublevel";
53 $result = "$entString".".0";
61 function returnTime($start) {
62 $stop = gettimeofday();
63 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
67 function GetMimeProperties($header) {
70 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
71 $read = sqimap_mailbox_select($imapConnection, $mailbox);
72 $start = gettimeofday();
73 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, $uid_support);
78 $messageheader = true;
79 $messageheaderstart=false;
80 $boundaries = array();
82 session_unregister("entities");
92 $body[$count-1] = substr($body[$count-1], -1);
93 for ($i=1; $i < $count; $i++
) {
94 $line = trim($body[$i]);
101 } else if ($messageheader) {
104 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" ID="'.$entStr.'B">'."\n \n";
106 $mimepart = -$header;
108 if ($messageheaderstart) {
109 $messageheaderstart=false;
111 } else if ($messageheaderstart) {
112 $messageheader= false;
116 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" ID="'.$entStr.'B">'."\n \n";
124 if (!$header && $messageheader) {
125 $messageheaderstart=true;
127 $pre = '<i><font color ='."$color[1]>";
128 $end = '</i></font>';
131 if (!$messageheader && !$header ) {
139 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
142 $bndreg = str_replace("\\","\\\\",$bndreg);
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);
151 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
152 $messageheader = false;
153 $messageheaderstart=false;
158 $entStr = CalcEntity("$entStr",1);
162 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
163 $cnt=count($boundaries)-1;
164 $bnd = $boundaries[$cnt]['bnd'];
165 $bndreg = $boundaries[$cnt]['bndreg'];
167 $regstr = '/^--'."($bndreg)".".*".'/';
168 if (preg_match($regstr,$line,$reg) ) {
169 $bndlen = strlen($reg[1]);
171 if (strlen($line) > ($bndlen +
3)) {
172 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
176 $entStr = CalcEntity("$entStr",-1);
177 array_pop($boundaries);
178 $pre .= '<b><font color ='."$color[2]>";
179 $end .= '</font></b>';
187 $entStr = CalcEntity("$entStr",0);
189 $content[$content_indx]=array();
190 $content[$content_indx]['ent'] = '<A HREF="#'."$entStr \">$entStr".'</a>';
191 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" ID="'.$entStr.'H"><a name="'."$entStr".'"><b><font color ='."$color[2]>";
192 $end .= '</font></b>'."\n";
199 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
200 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
201 $messageheader = true;
203 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
205 if ($reg[2] == 'image') {
206 $entities["$entStr"] = array();
207 $entities["$entStr"]['entity'] = $entStr;
208 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
210 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
211 $name = htmlspecialchars($reg[2]);
212 $content[$content_indx]['name'] = decodeHeader($name);
214 if (isset($entities["$entStr"])) {
215 $entities["$entStr"]['name'] = urlEncode($reg[2]);
217 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
219 if (isset($entities["$entStr"])) {
220 $entities["$entStr"]['encoding']=$reg[2];
222 $content[$content_indx]['encoding'] = $encoding;
226 $pre .= '<b><font color ='."$color[7]>";
227 $end .= '</font></b>';
234 if (isset($entities["$entStr"])) {
235 if (isset($encoding) && $encoding == 'base64') {
236 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
237 $entities["$entStr"]['content'] .= $line;
242 $line = htmlspecialchars($line);
243 $message_body .= "$pre"."$line"."$end".'<BR>'."\r\n";
245 //echo returnTime($start).'<BR>';
263 border-style: solid none solid none;
273 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
275 echo '<body><code>'."\n";
276 echo '<font face = "monospace">'."\n";
278 //session_register("entities");
279 //$keys = array_keys($entities);
280 //$start = gettimeofday();
281 //foreach ($keys as $key) {
282 // if (isset($entities[$key])) {
283 // if ($entities[$key]['encoding'] == 'base64') {
284 // echo '<img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br>';
288 //session_unregister("entities");
290 if (count($content) > 0) {
291 echo '<h2> Bodystructure </h2>'."\n\n";
292 echo '<table border=1 width="98%"><thead>'.
293 '<tr bgcolor="'."$color[7]".'">'.
294 '<td><b><font color="'."$color[5]".'">Entity</font></b></td>'.
295 '<td><b><font color="'."$color[5]".'">Content-Type</font></b></td>'.
296 '<td><b><font color="'."$color[5]".'">Name</font></b></td>'.
297 '<td><b><font color="'."$color[5]".'">Encoding</font></b></td>'.
301 for ($i = 0; $i < count($content);$i++
) {
303 echo $content[$i]['ent'].'</td><td>';
304 if (isset($content[$i]['type'])) {
305 echo $content[$i]['type'];
306 } else echo 'TEXT/PLAIN';
308 if (isset($content[$i]['name'])) {
309 echo $content[$i]['name'];
310 } else echo ' ';
312 if (isset($content[$i]['encoding'])) {
313 echo $content[$i]['encoding'];
314 } else echo ' ';
315 echo '</td></tr>'."\n";
317 echo '</tbody></table><br>'."\n";
319 echo '<h2> RFC822 Message body </h2>'."\n\n";
320 echo '<div><div class="header">'."\n\n";
322 echo '</div></div></font></code></body></html>';