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.
14 define('SM_PATH','../');
16 /* SquirrelMail required files. */
17 require_once(SM_PATH
. 'include/validate.php');
18 require_once(SM_PATH
. 'functions/imap.php');
19 require_once(SM_PATH
. 'functions/mime.php');
20 require_once(SM_PATH
. 'config/config.php');
21 require_once(SM_PATH
. 'functions/prefs.php');
23 global $color, $uid_support;
25 function CalcEntity($entString, $direction) {
27 if ($direction == -1) {
28 $pos = strrpos($entString,'.');
29 $result = substr($entString,0,$pos);
34 $pos = strrpos($entString,'.');
40 $level = substr($entString,0,$pos);
41 $sublevel = substr($entString,$pos+
1);
43 $result = "$level".'.'."$sublevel";
47 $result = "$entString".".0";
55 function returnTime($start) {
56 $stop = gettimeofday();
57 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
61 function GetMimeProperties($header) {
64 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
65 $read = sqimap_mailbox_select($imapConnection, $mailbox);
66 $start = gettimeofday();
67 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, $uid_support);
72 $messageheader = true;
73 $messageheaderstart=false;
74 $boundaries = array();
76 session_unregister("entities");
86 $body[$count-1] = substr($body[$count-1], -1);
87 for ($i=1; $i < $count; $i++
) {
88 $line = trim($body[$i]);
95 } else if ($messageheader) {
98 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" ID="'.$entStr.'B">'."\n \n";
100 $mimepart = -$header;
102 if ($messageheaderstart) {
103 $messageheaderstart=false;
105 } else if ($messageheaderstart) {
106 $messageheader= false;
110 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" ID="'.$entStr.'B">'."\n \n";
118 if (!$header && $messageheader) {
119 $messageheaderstart=true;
121 $pre = '<i><font color ='."$color[1]>";
122 $end = '</i></font>';
125 if (!$messageheader && !$header ) {
133 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
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);
143 $bndreg = str_replace(")","\\)",$bndreg);
145 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
146 $messageheader = false;
147 $messageheaderstart=false;
152 $entStr = CalcEntity("$entStr",1);
156 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
157 $cnt=count($boundaries)-1;
158 $bnd = $boundaries[$cnt]['bnd'];
159 $bndreg = $boundaries[$cnt]['bndreg'];
161 $regstr = '/^--'."($bndreg)".".*".'/';
162 if (preg_match($regstr,$line,$reg) ) {
163 $bndlen = strlen($reg[1]);
165 if (strlen($line) > ($bndlen +
3)) {
166 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
170 $entStr = CalcEntity("$entStr",-1);
171 array_pop($boundaries);
172 $pre .= '<b><font color ='."$color[2]>";
173 $end .= '</font></b>';
181 $entStr = CalcEntity("$entStr",0);
183 $content[$content_indx]=array();
184 $content[$content_indx]['ent'] = '<A HREF="#'."$entStr \">$entStr".'</a>';
185 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" ID="'.$entStr.'H"><a name="'."$entStr".'"><b><font color ='."$color[2]>";
186 $end .= '</font></b>'."\n";
193 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
194 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
195 $messageheader = true;
197 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
199 if ($reg[2] == 'image') {
200 $entities["$entStr"] = array();
201 $entities["$entStr"]['entity'] = $entStr;
202 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
204 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
205 $name = htmlspecialchars($reg[2]);
206 $content[$content_indx]['name'] = decodeHeader($name);
208 if (isset($entities["$entStr"])) {
209 $entities["$entStr"]['name'] = urlEncode($reg[2]);
211 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
213 if (isset($entities["$entStr"])) {
214 $entities["$entStr"]['encoding']=$reg[2];
216 $content[$content_indx]['encoding'] = $encoding;
220 $pre .= '<b><font color ='."$color[7]>";
221 $end .= '</font></b>';
228 if (isset($entities["$entStr"])) {
229 if (isset($encoding) && $encoding == 'base64') {
230 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
231 $entities["$entStr"]['content'] .= $line;
236 $line = htmlspecialchars($line);
237 $message_body .= "$pre"."$line"."$end".'<BR>'."\r\n";
239 //echo returnTime($start).'<BR>';
257 border-style: solid none solid none;
267 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
269 echo '<body><code>'."\n";
270 echo '<font face = "monospace">'."\n";
272 //session_register("entities");
273 //$keys = array_keys($entities);
274 //$start = gettimeofday();
275 //foreach ($keys as $key) {
276 // if (isset($entities[$key])) {
277 // if ($entities[$key]['encoding'] == 'base64') {
278 // echo '<img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br>';
282 //session_unregister("entities");
284 if (count($content) > 0) {
285 echo '<h2> Bodystructure </h2>'."\n\n";
286 echo '<table border=1 width="98%"><thead>'.
287 '<tr bgcolor="'."$color[7]".'">'.
288 '<td><b><font color="'."$color[5]".'">Entity</font></b></td>'.
289 '<td><b><font color="'."$color[5]".'">Content-Type</font></b></td>'.
290 '<td><b><font color="'."$color[5]".'">Name</font></b></td>'.
291 '<td><b><font color="'."$color[5]".'">Encoding</font></b></td>'.
295 for ($i = 0; $i < count($content);$i++
) {
297 echo $content[$i]['ent'].'</td><td>';
298 if (isset($content[$i]['type'])) {
299 echo $content[$i]['type'];
300 } else echo 'TEXT/PLAIN';
302 if (isset($content[$i]['name'])) {
303 echo $content[$i]['name'];
306 if (isset($content[$i]['encoding'])) {
307 echo $content[$i]['encoding'];
309 echo '</td></tr>'."\n";
311 echo '</tbody></table><br>'."\n";
313 echo '<h2> RFC822 Message body </h2>'."\n\n";
314 echo '<div><div class="header">'."\n\n";
316 echo '</div></div></font></code></body></html>';