3 * Message Details plugin - bottom frame with message structure and rfc822 body
5 * Plugin to view the RFC822 raw message output and the bodystructure of a message
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * @author Marc Groot Koerkamp
10 * @copyright Copyright © 2002 Marc Groot Koerkamp, The Netherlands
11 * @copyright Copyright © 2004 The SquirrelMail Project Team
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
15 * @subpackage message_details
19 define('SM_PATH','../../');
21 /* SquirrelMail required files. */
22 require_once(SM_PATH
. 'include/validate.php');
23 require_once(SM_PATH
. 'functions/imap.php');
24 require_once(SM_PATH
. 'functions/mime.php');
28 sqgetGlobalVar('passed_id', $passed_id, SQ_GET
);
29 sqgetGlobalVar('mailbox', $mailbox, SQ_GET
);
31 sqgetGlobalVar('username', $username, SQ_SESSION
);
32 sqgetGlobalVar('key', $key, SQ_COOKIE
);
33 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION
);
35 function CalcEntity($entString, $direction) {
37 if ($direction == -1) {
38 $pos = strrpos($entString,'.');
39 $result = substr($entString,0,$pos);
44 $pos = strrpos($entString,'.');
50 $level = substr($entString,0,$pos);
51 $sublevel = substr($entString,$pos+
1);
53 $result = "$level".'.'."$sublevel";
57 $result = "$entString".".0";
65 function returnTime($start) {
66 $stop = gettimeofday();
67 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
71 function GetMimeProperties($header) {
74 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
75 $read = sqimap_mailbox_select($imapConnection, $mailbox);
76 $start = gettimeofday();
77 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
82 $messageheader = true;
83 $messageheaderstart=false;
84 $boundaries = array();
86 session_unregister("entities");
96 $body[$count-1] = substr($body[$count-1], -1);
97 for ($i=1; $i < $count; $i++
) {
98 $line = trim($body[$i]);
105 } else if ($messageheader) {
108 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
110 $mimepart = -$header;
112 if ($messageheaderstart) {
113 $messageheaderstart=false;
115 } else if ($messageheaderstart) {
116 $messageheader= false;
120 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
128 if (!$header && $messageheader) {
129 $messageheaderstart=true;
131 $pre = '<i><font color ="'.$color[1].'">';
132 $end = '</i></font>';
135 if (!$messageheader && !$header ) {
143 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
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);
153 $bndreg = str_replace(")","\\)",$bndreg);
155 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
156 $messageheader = false;
157 $messageheaderstart=false;
162 $entStr = CalcEntity("$entStr",1);
166 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
167 $cnt=count($boundaries)-1;
168 $bnd = $boundaries[$cnt]['bnd'];
169 $bndreg = $boundaries[$cnt]['bndreg'];
171 $regstr = '/^--'."($bndreg)".".*".'/';
172 if (preg_match($regstr,$line,$reg) ) {
173 $bndlen = strlen($reg[1]);
175 if (strlen($line) > ($bndlen +
3)) {
176 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
180 $entStr = CalcEntity("$entStr",-1);
181 array_pop($boundaries);
182 $pre .= '<b><font color ="'.$color[2].'">';
183 $end .= '</font></b>';
191 $entStr = CalcEntity("$entStr",0);
193 $content[$content_indx]=array();
194 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
195 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.$entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
196 $end .= '</font></b>'."\n";
203 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
204 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
205 $messageheader = true;
207 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
209 if ($reg[2] == 'image') {
210 $entities["$entStr"] = array();
211 $entities["$entStr"]['entity'] = $entStr;
212 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
214 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
215 $name = htmlspecialchars($reg[2]);
216 $content[$content_indx]['name'] = decodeHeader($name);
218 if (isset($entities["$entStr"])) {
219 $entities["$entStr"]['name'] = urlEncode($reg[2]);
221 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
223 if (isset($entities["$entStr"])) {
224 $entities["$entStr"]['encoding']=$reg[2];
226 $content[$content_indx]['encoding'] = $encoding;
230 $pre .= '<b><font color='.$color[7].'">';
231 $end .= '</font></b>';
238 if (isset($entities["$entStr"])) {
239 if (isset($encoding) && $encoding == 'base64') {
240 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
241 $entities["$entStr"]['content'] .= $line;
246 $line = htmlspecialchars($line);
247 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
249 //echo returnTime($start).'<br />';
273 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
275 echo "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
277 echo '<font face="monospace">'."\n";
281 //session_register("entities");
282 //$keys = array_keys($entities);
283 //$start = gettimeofday();
284 //foreach ($keys as $key) {
285 // if (isset($entities[$key])) {
286 // if ($entities[$key]['encoding'] == 'base64') {
287 // echo '<img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br />';
291 //session_unregister("entities");
293 if (count($content) > 0) {
294 echo '<h2>'._("Bodystructure")."</h2>\n\n";
295 echo '<table border="1" width="98%"><thead>'.
296 '<tr bgcolor="'.$color[7].'">'.
297 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
298 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
299 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
300 '<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>';