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
);
36 * Calculates id of MIME entity
37 * @param string $entString
38 * @param integer $direction
41 function CalcEntity($entString, $direction) {
43 if ($direction == -1) {
44 $pos = strrpos($entString,'.');
45 $result = substr($entString,0,$pos);
50 $pos = strrpos($entString,'.');
56 $level = substr($entString,0,$pos);
57 $sublevel = substr($entString,$pos+
1);
59 $result = "$level".'.'."$sublevel";
63 $result = "$entString".".0";
72 * Returns time in microseconds between selected and current timestamp
74 * @param array $start see details about array format at http://www.php.net/gettimeofday
75 * @return integer time in microseconds
78 function returnTime($start) {
79 $stop = gettimeofday();
80 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
84 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
85 $read = sqimap_mailbox_select($imapConnection, $mailbox);
86 $start = gettimeofday();
87 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
92 $messageheader = true;
93 $messageheaderstart=false;
94 $boundaries = array();
96 session_unregister("entities");
106 $body[$count-1] = substr($body[$count-1], -1);
107 for ($i=1; $i < $count; $i++
) {
108 $line = trim($body[$i]);
115 } else if ($messageheader) {
118 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
120 $mimepart = -$header;
122 if ($messageheaderstart) {
123 $messageheaderstart=false;
125 } else if ($messageheaderstart) {
126 $messageheader= false;
130 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
138 if (!$header && $messageheader) {
139 $messageheaderstart=true;
141 $pre = '<i><font color ="'.$color[1].'">';
142 $end = '</i></font>';
145 if (!$messageheader && !$header ) {
153 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
156 $bndreg = str_replace("\\","\\\\",$bndreg);
157 $bndreg = str_replace("?","\\?",$bndreg);
158 $bndreg = str_replace("+","\\+",$bndreg);
159 $bndreg = str_replace(".","\\.",$bndreg);
160 $bndreg = str_replace("/","\\/",$bndreg);
161 $bndreg = str_replace("-","\\-",$bndreg);
162 $bndreg = str_replace("(","\\(",$bndreg);
163 $bndreg = str_replace(")","\\)",$bndreg);
165 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
166 $messageheader = false;
167 $messageheaderstart=false;
172 $entStr = CalcEntity("$entStr",1);
176 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
177 $cnt=count($boundaries)-1;
178 $bnd = $boundaries[$cnt]['bnd'];
179 $bndreg = $boundaries[$cnt]['bndreg'];
181 $regstr = '/^--'."($bndreg)".".*".'/';
182 if (preg_match($regstr,$line,$reg) ) {
183 $bndlen = strlen($reg[1]);
185 if (strlen($line) > ($bndlen +
3)) {
186 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
190 $entStr = CalcEntity("$entStr",-1);
191 array_pop($boundaries);
192 $pre .= '<b><font color ="'.$color[2].'">';
193 $end .= '</font></b>';
201 $entStr = CalcEntity("$entStr",0);
203 $content[$content_indx]=array();
204 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
205 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
206 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
207 $end .= '</font></b>'."\n";
214 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
215 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
216 $messageheader = true;
218 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
220 if ($reg[2] == 'image') {
221 $entities["$entStr"] = array();
222 $entities["$entStr"]['entity'] = $entStr;
223 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
225 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
226 $name = htmlspecialchars($reg[2]);
227 $content[$content_indx]['name'] = decodeHeader($name);
229 if (isset($entities["$entStr"])) {
230 $entities["$entStr"]['name'] = urlEncode($reg[2]);
232 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
234 if (isset($entities["$entStr"])) {
235 $entities["$entStr"]['encoding']=$reg[2];
237 $content[$content_indx]['encoding'] = $encoding;
241 $pre .= '<b><font color='.$color[7].'">';
242 $end .= '</font></b>';
249 if (isset($entities["$entStr"])) {
250 if (isset($encoding) && $encoding == 'base64') {
251 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
252 $entities["$entStr"]['content'] .= $line;
257 $line = htmlspecialchars($line);
258 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
260 //echo returnTime($start).'<br />';
284 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
286 echo "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
288 echo '<font face="monospace">'."\n";
292 //session_register("entities");
293 //$keys = array_keys($entities);
294 //$start = gettimeofday();
295 //foreach ($keys as $key) {
296 // if (isset($entities[$key])) {
297 // if ($entities[$key]['encoding'] == 'base64') {
298 // echo '<img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br />';
302 //session_unregister("entities");
304 if (count($content) > 0) {
305 echo '<h2>'._("Bodystructure")."</h2>\n\n";
306 echo '<table border="1" width="98%"><thead>'.
307 '<tr bgcolor="'.$color[7].'">'.
308 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
309 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
310 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
311 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
314 for ($i = 0; $i < count($content);$i++
) {
316 echo $content[$i]['ent'].'</td><td>';
317 if (isset($content[$i]['type'])) {
318 echo $content[$i]['type'];
319 } else echo 'TEXT/PLAIN';
321 if (isset($content[$i]['name'])) {
322 echo $content[$i]['name'];
323 } else echo ' ';
325 if (isset($content[$i]['encoding'])) {
326 echo $content[$i]['encoding'];
327 } else echo ' ';
328 echo '</td></tr>'."\n";
330 echo '</tbody></table><br />'."\n";
332 echo '<h2>'._("RFC822 Message body")."</h2>\n\n";
333 echo '<div><div class="header">'."\n\n";
335 echo '</div></div></font></code></body></html>';