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