Centralized init
[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);
129sqgetGlobalVar('key', $key, SQ_COOKIE);
130
bbdea028 131$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
132$read = sqimap_mailbox_select($imapConnection, $mailbox);
133$start = gettimeofday();
6201339c 134$body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
bbdea028 135$message_body = '';
136$header = false;
137$mimepart = false;
138$bnd_end = false;
139$messageheader = true;
140$messageheaderstart=false;
141$boundaries = array();
142$entities = array();
143session_unregister("entities");
144$pre = '<b>';
145$end = '</b>';
146$entStr = '';
147$bla ='';
148$content = array ();
149$content_indx = -1;
150$contentset = false;
151
152$count=count($body);
153$body[$count-1] = substr($body[$count-1], -1);
154for ($i=1; $i < $count; $i++) {
155 $line = trim($body[$i]);
156 if ($line == '') {
796f91d9 157 $pre = '';
158 $end = '';
bbdea028 159 if ($bnd_end) {
796f91d9 160 $header = true;
161 $mimepart = false;
162 } else if ($messageheader) {
163 if ($header) {
164 $header=false;
91e0dccc 165 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
796f91d9 166 }
167 $mimepart = -$header;
168 $bnd_end = false;
169 if ($messageheaderstart) {
170 $messageheaderstart=false;
171 }
172 } else if ($messageheaderstart) {
173 $messageheader= false;
174 } else {
175 if ($header) {
176 $pre = '';
91e0dccc 177 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
796f91d9 178 }
179 $header = false;
180 $mimepart=true;
91e0dccc 181 }
796f91d9 182 $contentset = false;
183 $nameset = false;
bbdea028 184 } else {
185 if (!$header && $messageheader) {
796f91d9 186 $messageheaderstart=true;
187 if ($pre != '<b>') {
188 $pre = '<i><font color ="'.$color[1].'">';
189 $end = '</i></font>';
190 }
91e0dccc 191 }
796f91d9 192 if (!$messageheader && !$header ) {
193 $mimepart=true;
194 } else {
195 $mimepart=false;
196 }
197 $pre = '';
198 $end = '';
bbdea028 199 }
200 if ( ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
796f91d9 201 $bnd = $reg[1];
91e0dccc 202 $bndreg = $bnd;
796f91d9 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);
210 $bndreg = str_replace(")","\\)",$bndreg);
bbdea028 211
796f91d9 212 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
213 $messageheader = false;
214 $messageheaderstart=false;
215 $mimepart=false;
216 if ($entStr=='') {
217 $entStr='0';
218 } else {
219 $entStr = CalcEntity("$entStr",1);
220 }
bbdea028 221 }
91e0dccc 222
bbdea028 223 if (($line != '' && $line{0} == '-' || $header) && isset($boundaries[0])) {
224 $cnt=count($boundaries)-1;
796f91d9 225 $bnd = $boundaries[$cnt]['bnd'];
226 $bndreg = $boundaries[$cnt]['bndreg'];
91e0dccc 227
796f91d9 228 $regstr = '/^--'."($bndreg)".".*".'/';
229 if (preg_match($regstr,$line,$reg) ) {
230 $bndlen = strlen($reg[1]);
91e0dccc 231 $bndend = false;
bbdea028 232 if (strlen($line) > ($bndlen + 3)) {
91e0dccc 233 if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
796f91d9 234 $bndend = true;
235 }
236 if ($bndend) {
237 $entStr = CalcEntity("$entStr",-1);
238 array_pop($boundaries);
239 $pre .= '<b><font color ="'.$color[2].'">';
240 $end .= '</font></b>';
241 $header = true;
242 $mimepart = false;
243 $bnd_end = true;
244 $encoding = '';
245 } else {
246 $header = true;
247 $bnd_end = false;
248 $entStr = CalcEntity("$entStr",0);
249 $content_indx++;
250 $content[$content_indx]=array();
251 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
5d73c31d 252 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
253 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
796f91d9 254 $end .= '</font></b>'."\n";
255 $header = true;
256 $mimepart = false;
257 $encoding = '';
258 }
259 } else {
260 if ($header) {
261 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
262 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
263 $messageheader = true;
264 }
265 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
266 $contentset = true;
267 if ($reg[2] == 'image') {
268 $entities["$entStr"] = array();
269 $entities["$entStr"]['entity'] = $entStr;
270 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
91e0dccc 271 }
796f91d9 272 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
273 $name = htmlspecialchars($reg[2]);
274 $content[$content_indx]['name'] = decodeHeader($name);
275 $nameset = true;
276 if (isset($entities["$entStr"])) {
277 $entities["$entStr"]['name'] = urlEncode($reg[2]);
278 }
279 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
280 $encoding = $reg[2];
281 if (isset($entities["$entStr"])) {
282 $entities["$entStr"]['encoding']=$reg[2];
283 }
284 $content[$content_indx]['encoding'] = $encoding;
285 $mimeentity = '';
286 }
bbdea028 287
796f91d9 288 $pre .= '<b><font color='.$color[7].'">';
289 $end .= '</font></b>';
290 //$mimepart=false;
291 }
292 }
293 }
bbdea028 294/*
295 if ($mimepart) {
796f91d9 296 if (isset($entities["$entStr"])) {
297 if (isset($encoding) && $encoding == 'base64') {
298 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
299 $entities["$entStr"]['content'] .= $line;
300 }
bbdea028 301 }
796f91d9 302 }
bbdea028 303*/
f4450417 304 if ($stripHTML) {
305 $message_body .= $line . "\r\n";
306 } else {
307 $line = htmlspecialchars($line);
24e1fb21 308 if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
f4450417 309 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
310 }
bbdea028 311}
24e1fb21 312
f4450417 313//$returnValue .= returnTime($start).'<br />';
bbdea028 314$xtra = <<<ECHO
315
a74103dd 316<style type="text/css">
bbdea028 317
318<!--
319.ent_body {
320 display:inline;
321}
322
323.header {
324 display:inline;
325}
326
327.entheader {
328 display:inline;
bbdea028 329 width:99%;
330}
331//-->
332
796f91d9 333</style>
bbdea028 334
335ECHO;
336
f4450417 337if (!$stripHTML) {
338 ob_start();
339 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
340 $returnValue .= ob_get_contents();
341 ob_end_clean();
342}
343
bbdea028 344/* body */
f4450417 345if (!$stripHTML) {
346 $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
347 $returnValue .= '<code>'."\n";
348 $returnValue .= '<font face="monospace">'."\n";
349 $returnValue .= '<br />'."\n";
350}
c157ce7e 351
352
bbdea028 353//session_register("entities");
354//$keys = array_keys($entities);
355//$start = gettimeofday();
356//foreach ($keys as $key) {
357// if (isset($entities[$key])) {
358// if ($entities[$key]['encoding'] == 'base64') {
f4450417 359// if (!$stripHTML) {
360// $returnValue .= 'img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&amp;name='.$entities[$key]['name'].'"><br />';
361// }
bbdea028 362// }
363// }
364//}
365//session_unregister("entities");
366
f4450417 367if (count($content) > 0 && !$stripHTML) {
368 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
369 $returnValue .= '<table border="1" width="98%"><thead>'.
796f91d9 370 '<tr bgcolor="'.$color[7].'">'.
86c62251 371 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
372 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
373 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
374 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
796f91d9 375 '</tr>'.
376 '</thead><tbody>';
bbdea028 377 for ($i = 0; $i < count($content);$i++) {
f4450417 378 $returnValue .= '<tr><td>';
379 $returnValue .= $content[$i]['ent'].'</td><td>';
796f91d9 380 if (isset($content[$i]['type'])) {
f4450417 381 $returnValue .= $content[$i]['type'];
382 } else $returnValue .= 'TEXT/PLAIN';
383 $returnValue .= '</td><td>';
796f91d9 384 if (isset($content[$i]['name'])) {
f4450417 385 $returnValue .= $content[$i]['name'];
386 } else $returnValue .= '&nbsp;';
387 $returnValue .= '</td><td>';
796f91d9 388 if (isset($content[$i]['encoding'])) {
f4450417 389 $returnValue .= $content[$i]['encoding'];
390 } else $returnValue .= '&nbsp;';
391 $returnValue .= '</td></tr>'."\n";
bbdea028 392 }
f4450417 393 $returnValue .= '</tbody></table><br />'."\n";
bbdea028 394}
f4450417 395
396if (!$stripHTML) {
397 $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
398 $returnValue .= '<div><div class="header">'."\n\n";
399}
400
401$returnValue .= $message_body;
402
f8a1ed5a 403if (!$stripHTML)
f4450417 404 $returnValue .= '</div></div></font></code></body></html>';
405
406return $returnValue;
407
408}
409
f8a1ed5a 410?>