Do not use ob_end_flush because the fetch method returns the content of the
[squirrelmail.git] / plugins / message_details / message_details_bottom.php
CommitLineData
bbdea028 1<?php
4b4abf93 2
86c62251 3/**
4 * Message Details plugin - bottom frame with message structure and rfc822 body
8d6a115b 5 *
6 * Plugin to view the RFC822 raw message output and the bodystructure of a message
7 *
86c62251 8 * @author Marc Groot Koerkamp
4b4abf93 9 * @copyright &copy; 2002 Marc Groot Koerkamp, The Netherlands
47ccfad4 10 * @copyright &copy; 2002-2006 The SquirrelMail Project Team
86c62251 11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @version $Id$
ea5f4b8e 13 * @package plugins
14 * @subpackage message_details
8d6a115b 15 */
bbdea028 16
202bcbcc 17/**
18 * Include the SquirrelMail initialization file.
19 */
20require('../../include/init.php');
21require(SM_PATH . 'functions/imap_general.php');
22require(SM_PATH . 'functions/imap_messages.php');
23require(SM_PATH . 'functions/mime.php');
bbdea028 24
f4450417 25sqgetGlobalVar('get_message_details', $md_action, SQ_GET);
bbdea028 26
24e1fb21 27/**
28 * Controls display of 8bit symbols in message source
29 * @global boolean $msgd_8bit_in_hex;
30 */
31global $msgd_8bit_in_hex;
32$msgd_8bit_in_hex=false;
33
f4450417 34if (!empty($md_action))
35{
36 sqgetGlobalVar('passed_id', $passed_id, SQ_GET);
37 sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
38 echo get_message_details($mailbox, $passed_id);
39}
40
41
42// ---------- function definitions ----------
04f6008a 43
24e1fb21 44/**
45 * Converts 8bit string to hex
46 *
f8a1ed5a 47 * Replaces 8bit symbols with their hex strings,
24e1fb21 48 * encloses them in curly brackets and uses different color.
49 * @param string $string text
50 * @return string
51 * @since 1.5.1
52 */
53function msgd_convert_to_hex($string) {
54 global $color;
55 return preg_replace("/([\200-\377])/e","'<font color=\"$color[2]\">{'.dechex(ord('\\1')).'}</font>'",$string);
56}
04f6008a 57
5d73c31d 58/**
59 * Calculates id of MIME entity
60 * @param string $entString
61 * @param integer $direction
62 * @return string
e9ec1bd8 63 * @access private
5d73c31d 64 */
bbdea028 65function CalcEntity($entString, $direction) {
66 $result = $entString;
67 if ($direction == -1) {
5c325683 68 $pos = strrpos($entString,'.');
69 $result = substr($entString,0,$pos);
bbdea028 70 }
71
72 switch ($direction) {
796f91d9 73 case 0:
74 $pos = strrpos($entString,'.');
75 if ($pos === false) {
76 $entString++;
77 $result= $entString;
91e0dccc 78 }
796f91d9 79 else {
80 $level = substr($entString,0,$pos);
81 $sublevel = substr($entString,$pos+1);
82 $sublevel++;
83 $result = "$level".'.'."$sublevel";
84 }
85 break;
86 case 1:
87 $result = "$entString".".0";
88 break;
89 default:
90 break;
bbdea028 91 }
92 return ($result);
93}
94
f4450417 95
96
5d73c31d 97/**
98 * Returns time in microseconds between selected and current timestamp
99 *
100 * @param array $start see details about array format at http://www.php.net/gettimeofday
101 * @return integer time in microseconds
102 * @access private
103 */
bbdea028 104function returnTime($start) {
105 $stop = gettimeofday();
106 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
107 return $timepassed;
108}
109
f4450417 110
111
112/**
113 * Returns actual message details
114 * @param string $mailbox
115 * @param string $passed_id
f8a1ed5a 116 * @param boolean $stripHTML If TRUE, only plain text is returned,
117 * default is FALSE, wherein output contains
f4450417 118 * pretty-HTMLification of message body
119 * @return string The formatted message details
120 * @access public
121 */
122function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
123
24e1fb21 124global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex;
f4450417 125
126$returnValue = '';
127
128sqgetGlobalVar('username', $username, SQ_SESSION);
f4450417 129
045ec1a1 130$imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
bbdea028 131$read = sqimap_mailbox_select($imapConnection, $mailbox);
132$start = gettimeofday();
6201339c 133$body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
bbdea028 134$message_body = '';
135$header = false;
136$mimepart = false;
137$bnd_end = false;
138$messageheader = true;
139$messageheaderstart=false;
140$boundaries = array();
141$entities = array();
142session_unregister("entities");
143$pre = '<b>';
144$end = '</b>';
145$entStr = '';
146$bla ='';
147$content = array ();
148$content_indx = -1;
149$contentset = false;
150
151$count=count($body);
152$body[$count-1] = substr($body[$count-1], -1);
153for ($i=1; $i < $count; $i++) {
154 $line = trim($body[$i]);
155 if ($line == '') {
796f91d9 156 $pre = '';
157 $end = '';
bbdea028 158 if ($bnd_end) {
796f91d9 159 $header = true;
160 $mimepart = false;
161 } else if ($messageheader) {
162 if ($header) {
163 $header=false;
91e0dccc 164 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
796f91d9 165 }
166 $mimepart = -$header;
167 $bnd_end = false;
168 if ($messageheaderstart) {
169 $messageheaderstart=false;
170 }
171 } else if ($messageheaderstart) {
172 $messageheader= false;
173 } else {
174 if ($header) {
175 $pre = '';
91e0dccc 176 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
796f91d9 177 }
178 $header = false;
179 $mimepart=true;
91e0dccc 180 }
796f91d9 181 $contentset = false;
182 $nameset = false;
bbdea028 183 } else {
184 if (!$header && $messageheader) {
796f91d9 185 $messageheaderstart=true;
186 if ($pre != '<b>') {
187 $pre = '<i><font color ="'.$color[1].'">';
188 $end = '</i></font>';
189 }
91e0dccc 190 }
796f91d9 191 if (!$messageheader && !$header ) {
192 $mimepart=true;
193 } else {
194 $mimepart=false;
195 }
196 $pre = '';
197 $end = '';
bbdea028 198 }
199 if ( ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
796f91d9 200 $bnd = $reg[1];
91e0dccc 201 $bndreg = $bnd;
796f91d9 202 $bndreg = str_replace("\\","\\\\",$bndreg);
203 $bndreg = str_replace("?","\\?",$bndreg);
204 $bndreg = str_replace("+","\\+",$bndreg);
205 $bndreg = str_replace(".","\\.",$bndreg);
206 $bndreg = str_replace("/","\\/",$bndreg);
207 $bndreg = str_replace("-","\\-",$bndreg);
208 $bndreg = str_replace("(","\\(",$bndreg);
209 $bndreg = str_replace(")","\\)",$bndreg);
bbdea028 210
796f91d9 211 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
212 $messageheader = false;
213 $messageheaderstart=false;
214 $mimepart=false;
215 if ($entStr=='') {
216 $entStr='0';
217 } else {
218 $entStr = CalcEntity("$entStr",1);
219 }
bbdea028 220 }
91e0dccc 221
bbdea028 222 if (($line != '' && $line{0} == '-' || $header) && isset($boundaries[0])) {
223 $cnt=count($boundaries)-1;
796f91d9 224 $bnd = $boundaries[$cnt]['bnd'];
225 $bndreg = $boundaries[$cnt]['bndreg'];
91e0dccc 226
796f91d9 227 $regstr = '/^--'."($bndreg)".".*".'/';
228 if (preg_match($regstr,$line,$reg) ) {
229 $bndlen = strlen($reg[1]);
91e0dccc 230 $bndend = false;
bbdea028 231 if (strlen($line) > ($bndlen + 3)) {
91e0dccc 232 if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
796f91d9 233 $bndend = true;
234 }
235 if ($bndend) {
236 $entStr = CalcEntity("$entStr",-1);
237 array_pop($boundaries);
238 $pre .= '<b><font color ="'.$color[2].'">';
239 $end .= '</font></b>';
240 $header = true;
241 $mimepart = false;
242 $bnd_end = true;
243 $encoding = '';
244 } else {
245 $header = true;
246 $bnd_end = false;
247 $entStr = CalcEntity("$entStr",0);
248 $content_indx++;
249 $content[$content_indx]=array();
250 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
5d73c31d 251 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
252 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
796f91d9 253 $end .= '</font></b>'."\n";
254 $header = true;
255 $mimepart = false;
256 $encoding = '';
257 }
258 } else {
259 if ($header) {
260 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
261 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
262 $messageheader = true;
263 }
264 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
265 $contentset = true;
266 if ($reg[2] == 'image') {
267 $entities["$entStr"] = array();
268 $entities["$entStr"]['entity'] = $entStr;
269 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
91e0dccc 270 }
796f91d9 271 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
272 $name = htmlspecialchars($reg[2]);
273 $content[$content_indx]['name'] = decodeHeader($name);
274 $nameset = true;
275 if (isset($entities["$entStr"])) {
276 $entities["$entStr"]['name'] = urlEncode($reg[2]);
277 }
278 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
279 $encoding = $reg[2];
280 if (isset($entities["$entStr"])) {
281 $entities["$entStr"]['encoding']=$reg[2];
282 }
283 $content[$content_indx]['encoding'] = $encoding;
284 $mimeentity = '';
285 }
bbdea028 286
796f91d9 287 $pre .= '<b><font color='.$color[7].'">';
288 $end .= '</font></b>';
289 //$mimepart=false;
290 }
291 }
292 }
bbdea028 293/*
294 if ($mimepart) {
796f91d9 295 if (isset($entities["$entStr"])) {
296 if (isset($encoding) && $encoding == 'base64') {
297 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
298 $entities["$entStr"]['content'] .= $line;
299 }
bbdea028 300 }
796f91d9 301 }
bbdea028 302*/
f4450417 303 if ($stripHTML) {
304 $message_body .= $line . "\r\n";
305 } else {
306 $line = htmlspecialchars($line);
24e1fb21 307 if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
f4450417 308 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
309 }
bbdea028 310}
24e1fb21 311
f4450417 312//$returnValue .= returnTime($start).'<br />';
bbdea028 313$xtra = <<<ECHO
314
a74103dd 315<style type="text/css">
bbdea028 316
317<!--
318.ent_body {
319 display:inline;
320}
321
322.header {
323 display:inline;
324}
325
326.entheader {
327 display:inline;
bbdea028 328 width:99%;
329}
330//-->
331
796f91d9 332</style>
bbdea028 333
334ECHO;
335
f4450417 336if (!$stripHTML) {
337 ob_start();
338 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
339 $returnValue .= ob_get_contents();
340 ob_end_clean();
341}
342
bbdea028 343/* body */
f4450417 344if (!$stripHTML) {
345 $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
346 $returnValue .= '<code>'."\n";
347 $returnValue .= '<font face="monospace">'."\n";
348 $returnValue .= '<br />'."\n";
349}
c157ce7e 350
351
bbdea028 352//session_register("entities");
353//$keys = array_keys($entities);
354//$start = gettimeofday();
355//foreach ($keys as $key) {
356// if (isset($entities[$key])) {
357// if ($entities[$key]['encoding'] == 'base64') {
f4450417 358// if (!$stripHTML) {
359// $returnValue .= 'img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&amp;name='.$entities[$key]['name'].'"><br />';
360// }
bbdea028 361// }
362// }
363//}
364//session_unregister("entities");
365
f4450417 366if (count($content) > 0 && !$stripHTML) {
367 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
368 $returnValue .= '<table border="1" width="98%"><thead>'.
796f91d9 369 '<tr bgcolor="'.$color[7].'">'.
86c62251 370 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
371 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
372 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
373 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
796f91d9 374 '</tr>'.
375 '</thead><tbody>';
bbdea028 376 for ($i = 0; $i < count($content);$i++) {
f4450417 377 $returnValue .= '<tr><td>';
378 $returnValue .= $content[$i]['ent'].'</td><td>';
796f91d9 379 if (isset($content[$i]['type'])) {
f4450417 380 $returnValue .= $content[$i]['type'];
381 } else $returnValue .= 'TEXT/PLAIN';
382 $returnValue .= '</td><td>';
796f91d9 383 if (isset($content[$i]['name'])) {
f4450417 384 $returnValue .= $content[$i]['name'];
385 } else $returnValue .= '&nbsp;';
386 $returnValue .= '</td><td>';
796f91d9 387 if (isset($content[$i]['encoding'])) {
f4450417 388 $returnValue .= $content[$i]['encoding'];
389 } else $returnValue .= '&nbsp;';
390 $returnValue .= '</td></tr>'."\n";
bbdea028 391 }
f4450417 392 $returnValue .= '</tbody></table><br />'."\n";
bbdea028 393}
f4450417 394
395if (!$stripHTML) {
396 $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
397 $returnValue .= '<div><div class="header">'."\n\n";
398}
399
400$returnValue .= $message_body;
401
f8a1ed5a 402if (!$stripHTML)
f4450417 403 $returnValue .= '</div></div></font></code></body></html>';
404
405return $returnValue;
406
407}
408
f8a1ed5a 409?>