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