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-2005 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
42 function CalcEntity($entString, $direction) {
44 if ($direction == -1) {
45 $pos = strrpos($entString,'.');
46 $result = substr($entString,0,$pos);
51 $pos = strrpos($entString,'.');
57 $level = substr($entString,0,$pos);
58 $sublevel = substr($entString,$pos+
1);
60 $result = "$level".'.'."$sublevel";
64 $result = "$entString".".0";
73 * Returns time in microseconds between selected and current timestamp
75 * @param array $start see details about array format at http://www.php.net/gettimeofday
76 * @return integer time in microseconds
79 function returnTime($start) {
80 $stop = gettimeofday();
81 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
85 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
86 $read = sqimap_mailbox_select($imapConnection, $mailbox);
87 $start = gettimeofday();
88 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
93 $messageheader = true;
94 $messageheaderstart=false;
95 $boundaries = array();
97 session_unregister("entities");
107 $body[$count-1] = substr($body[$count-1], -1);
108 for ($i=1; $i < $count; $i++
) {
109 $line = trim($body[$i]);
116 } else if ($messageheader) {
119 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
121 $mimepart = -$header;
123 if ($messageheaderstart) {
124 $messageheaderstart=false;
126 } else if ($messageheaderstart) {
127 $messageheader= false;
131 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
139 if (!$header && $messageheader) {
140 $messageheaderstart=true;
142 $pre = '<i><font color ="'.$color[1].'">';
143 $end = '</i></font>';
146 if (!$messageheader && !$header ) {
154 if ( ( $header ||
$messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
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);
164 $bndreg = str_replace(")","\\)",$bndreg);
166 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
167 $messageheader = false;
168 $messageheaderstart=false;
173 $entStr = CalcEntity("$entStr",1);
177 if (($line != '' && $line{0} == '-' ||
$header) && isset($boundaries[0])) {
178 $cnt=count($boundaries)-1;
179 $bnd = $boundaries[$cnt]['bnd'];
180 $bndreg = $boundaries[$cnt]['bndreg'];
182 $regstr = '/^--'."($bndreg)".".*".'/';
183 if (preg_match($regstr,$line,$reg) ) {
184 $bndlen = strlen($reg[1]);
186 if (strlen($line) > ($bndlen +
3)) {
187 if ($line{$bndlen+
2} == '-' && $line{$bndlen+
3} == '-')
191 $entStr = CalcEntity("$entStr",-1);
192 array_pop($boundaries);
193 $pre .= '<b><font color ="'.$color[2].'">';
194 $end .= '</font></b>';
202 $entStr = CalcEntity("$entStr",0);
204 $content[$content_indx]=array();
205 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
206 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
207 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
208 $end .= '</font></b>'."\n";
215 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
216 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
217 $messageheader = true;
219 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
221 if ($reg[2] == 'image') {
222 $entities["$entStr"] = array();
223 $entities["$entStr"]['entity'] = $entStr;
224 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
226 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
227 $name = htmlspecialchars($reg[2]);
228 $content[$content_indx]['name'] = decodeHeader($name);
230 if (isset($entities["$entStr"])) {
231 $entities["$entStr"]['name'] = urlEncode($reg[2]);
233 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
235 if (isset($entities["$entStr"])) {
236 $entities["$entStr"]['encoding']=$reg[2];
238 $content[$content_indx]['encoding'] = $encoding;
242 $pre .= '<b><font color='.$color[7].'">';
243 $end .= '</font></b>';
250 if (isset($entities["$entStr"])) {
251 if (isset($encoding) && $encoding == 'base64') {
252 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
253 $entities["$entStr"]['content'] .= $line;
258 $line = htmlspecialchars($line);
259 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
261 //echo returnTime($start).'<br />';
285 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
287 echo "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
289 echo '<font face="monospace">'."\n";
293 //session_register("entities");
294 //$keys = array_keys($entities);
295 //$start = gettimeofday();
296 //foreach ($keys as $key) {
297 // if (isset($entities[$key])) {
298 // if ($entities[$key]['encoding'] == 'base64') {
299 // echo '<img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br />';
303 //session_unregister("entities");
305 if (count($content) > 0) {
306 echo '<h2>'._("Bodystructure")."</h2>\n\n";
307 echo '<table border="1" width="98%"><thead>'.
308 '<tr bgcolor="'.$color[7].'">'.
309 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
310 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
311 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
312 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
315 for ($i = 0; $i < count($content);$i++
) {
317 echo $content[$i]['ent'].'</td><td>';
318 if (isset($content[$i]['type'])) {
319 echo $content[$i]['type'];
320 } else echo 'TEXT/PLAIN';
322 if (isset($content[$i]['name'])) {
323 echo $content[$i]['name'];
324 } else echo ' ';
326 if (isset($content[$i]['encoding'])) {
327 echo $content[$i]['encoding'];
328 } else echo ' ';
329 echo '</td></tr>'."\n";
331 echo '</tbody></table><br />'."\n";
333 echo '<h2>'._("RFC822 Message body")."</h2>\n\n";
334 echo '<div><div class="header">'."\n\n";
336 echo '</div></div></font></code></body></html>';