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 function CalcEntity($entString, $direction) {
26 if ($direction == -1) {
27 $pos = strrpos($entString,'.');
28 $result = substr($entString,0,$pos);
33 $pos = strrpos($entString,'.');
39 $level = substr($entString,0,$pos);
40 $sublevel = substr($entString,$pos+
1);
42 $result = "$level".'.'."$sublevel";
46 $result = "$entString".".0";
54 function returnTime($start) {
55 $stop = gettimeofday();
56 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
60 function GetMimeProperties($header) {
63 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
64 $read = sqimap_mailbox_select($imapConnection, $mailbox);
65 $start = gettimeofday();
66 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, $uid_support);
71 $messageheader = true;
72 $messageheaderstart=false;
73 $boundaries = array();
75 session_unregister("entities");
85 $body[$count-1] = substr($body[$count-1], -1);
86 for ($i=1; $i < $count; $i++
) {
87 $line = trim($body[$i]);
94 } else if ($messageheader) {
97 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" ID="'.$entStr.'B">'."\n \n";
101 if ($messageheaderstart) {
102 $messageheaderstart=false;
104 } else if ($messageheaderstart) {
105 $messageheader= false;
109 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" ID="'.$entStr.'B">'."\n \n";
117 if (!$header && $messageheader) {
118 $messageheaderstart=true;
120 $pre = '<i><font color ='."$color[1]>";
121 $end = '</i></font>';
124 if (!$messageheader && !$header ) {
132 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
135 $bndreg = str_replace("\\","\\\\",$bndreg);
136 $bndreg = str_replace("?","\\?",$bndreg);
137 $bndreg = str_replace("+","\\+",$bndreg);
138 $bndreg = str_replace(".","\\.",$bndreg);
139 $bndreg = str_replace("/","\\/",$bndreg);
140 $bndreg = str_replace("-","\\-",$bndreg);
141 $bndreg = str_replace("(","\\(",$bndreg);
142 $bndreg = str_replace(")","\\)",$bndreg);
144 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
145 $messageheader = false;
146 $messageheaderstart=false;
151 $entStr = CalcEntity("$entStr",1);
155 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
156 $cnt=count($boundaries)-1;
157 $bnd = $boundaries[$cnt]['bnd'];
158 $bndreg = $boundaries[$cnt]['bndreg'];
160 $regstr = '/^--'."($bndreg)".".*".'/';
161 if (preg_match($regstr,$line,$reg) ) {
162 $bndlen = strlen($reg[1]);
164 if (strlen($line) > ($bndlen +
3)) {
165 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
169 $entStr = CalcEntity("$entStr",-1);
170 array_pop($boundaries);
171 $pre .= '<b><font color ='."$color[2]>";
172 $end .= '</font></b>';
180 $entStr = CalcEntity("$entStr",0);
182 $content[$content_indx]=array();
183 $content[$content_indx]['ent'] = '<A HREF="#'."$entStr \">$entStr".'</a>';
184 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" ID="'.$entStr.'H"><a name="'."$entStr".'"><b><font color ='."$color[2]>";
185 $end .= '</font></b>'."\n";
192 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
193 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
194 $messageheader = true;
196 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
198 if ($reg[2] == 'image') {
199 $entities["$entStr"] = array();
200 $entities["$entStr"]['entity'] = $entStr;
201 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
203 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
204 $name = htmlspecialchars($reg[2]);
205 $content[$content_indx]['name'] = decodeHeader($name);
207 if (isset($entities["$entStr"])) {
208 $entities["$entStr"]['name'] = urlEncode($reg[2]);
210 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
212 if (isset($entities["$entStr"])) {
213 $entities["$entStr"]['encoding']=$reg[2];
215 $content[$content_indx]['encoding'] = $encoding;
219 $pre .= '<b><font color ='."$color[7]>";
220 $end .= '</font></b>';
227 if (isset($entities["$entStr"])) {
228 if (isset($encoding) && $encoding == 'base64') {
229 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
230 $entities["$entStr"]['content'] .= $line;
235 $line = htmlspecialchars($line);
236 $message_body .= "$pre"."$line"."$end".'<BR>'."\r\n";
238 //echo returnTime($start).'<BR>';
256 border-style: solid none solid none;
266 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
268 echo '<body><code>'."\n";
269 echo '<font face = "monospace">'."\n";
271 //session_register("entities");
272 //$keys = array_keys($entities);
273 //$start = gettimeofday();
274 //foreach ($keys as $key) {
275 // if (isset($entities[$key])) {
276 // if ($entities[$key]['encoding'] == 'base64') {
277 // echo '<img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br>';
281 //session_unregister("entities");
283 if (count($content) > 0) {
284 echo '<h2> Bodystructure </h2>'."\n\n";
285 echo '<table border=1 width="98%"><thead>'.
286 '<tr bgcolor="'."$color[7]".'">'.
287 '<td><b><font color="'."$color[5]".'">Entity</font></b></td>'.
288 '<td><b><font color="'."$color[5]".'">Content-Type</font></b></td>'.
289 '<td><b><font color="'."$color[5]".'">Name</font></b></td>'.
290 '<td><b><font color="'."$color[5]".'">Encoding</font></b></td>'.
294 for ($i = 0; $i < count($content);$i++
) {
296 echo $content[$i]['ent'].'</td><td>';
297 if (isset($content[$i]['type'])) {
298 echo $content[$i]['type'];
299 } else echo 'TEXT/PLAIN';
301 if (isset($content[$i]['name'])) {
302 echo $content[$i]['name'];
305 if (isset($content[$i]['encoding'])) {
306 echo $content[$i]['encoding'];
308 echo '</td></tr>'."\n";
310 echo '</tbody></table><br>'."\n";
312 echo '<h2> RFC822 Message body </h2>'."\n\n";
313 echo '<div><div class="header">'."\n\n";
315 echo '</div></div></font></code></body></html>';