copyright update
[squirrelmail.git] / plugins / message_details / message_details_bottom.php
1 <?php
2
3 /**
4 * Message Details plugin - bottom frame with message structure and rfc822 body
5 *
6 * Plugin to view the RFC822 raw message output and the bodystructure of a message
7 *
8 * @author Marc Groot Koerkamp
9 * @copyright &copy; 2002 Marc Groot Koerkamp, The Netherlands
10 * @copyright &copy; 2002-2006 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @version $Id$
13 * @package plugins
14 * @subpackage message_details
15 */
16
17 /** @ignore */
18 if (!defined('SM_PATH'))
19 define('SM_PATH','../../');
20
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');
25
26 sqgetGlobalVar('get_message_details', $md_action, SQ_GET);
27
28 /**
29 * Controls display of 8bit symbols in message source
30 * @global boolean $msgd_8bit_in_hex;
31 */
32 global $msgd_8bit_in_hex;
33 $msgd_8bit_in_hex=false;
34
35 if (!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 ----------
44
45 /**
46 * Converts 8bit string to hex
47 *
48 * Replaces 8bit symbols with their hex strings,
49 * encloses them in curly brackets and uses different color.
50 * @param string $string text
51 * @return string
52 * @since 1.5.1
53 */
54 function 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 }
58
59 /**
60 * Calculates id of MIME entity
61 * @param string $entString
62 * @param integer $direction
63 * @return string
64 * @access private
65 */
66 function CalcEntity($entString, $direction) {
67 $result = $entString;
68 if ($direction == -1) {
69 $pos = strrpos($entString,'.');
70 $result = substr($entString,0,$pos);
71 }
72
73 switch ($direction) {
74 case 0:
75 $pos = strrpos($entString,'.');
76 if ($pos === false) {
77 $entString++;
78 $result= $entString;
79 }
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;
92 }
93 return ($result);
94 }
95
96
97
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 */
105 function returnTime($start) {
106 $stop = gettimeofday();
107 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
108 return $timepassed;
109 }
110
111
112
113 /**
114 * Returns actual message details
115 * @param string $mailbox
116 * @param string $passed_id
117 * @param boolean $stripHTML If TRUE, only plain text is returned,
118 * default is FALSE, wherein output contains
119 * pretty-HTMLification of message body
120 * @return string The formatted message details
121 * @access public
122 */
123 function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
124
125 global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex;
126
127 $returnValue = '';
128
129 sqgetGlobalVar('username', $username, SQ_SESSION);
130 sqgetGlobalVar('key', $key, SQ_COOKIE);
131
132 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
133 $read = sqimap_mailbox_select($imapConnection, $mailbox);
134 $start = gettimeofday();
135 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
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();
144 session_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);
155 for ($i=1; $i < $count; $i++) {
156 $line = trim($body[$i]);
157 if ($line == '') {
158 $pre = '';
159 $end = '';
160 if ($bnd_end) {
161 $header = true;
162 $mimepart = false;
163 } else if ($messageheader) {
164 if ($header) {
165 $header=false;
166 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
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 = '';
178 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
179 }
180 $header = false;
181 $mimepart=true;
182 }
183 $contentset = false;
184 $nameset = false;
185 } else {
186 if (!$header && $messageheader) {
187 $messageheaderstart=true;
188 if ($pre != '<b>') {
189 $pre = '<i><font color ="'.$color[1].'">';
190 $end = '</i></font>';
191 }
192 }
193 if (!$messageheader && !$header ) {
194 $mimepart=true;
195 } else {
196 $mimepart=false;
197 }
198 $pre = '';
199 $end = '';
200 }
201 if ( ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
202 $bnd = $reg[1];
203 $bndreg = $bnd;
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);
212
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 }
222 }
223
224 if (($line != '' && $line{0} == '-' || $header) && isset($boundaries[0])) {
225 $cnt=count($boundaries)-1;
226 $bnd = $boundaries[$cnt]['bnd'];
227 $bndreg = $boundaries[$cnt]['bndreg'];
228
229 $regstr = '/^--'."($bndreg)".".*".'/';
230 if (preg_match($regstr,$line,$reg) ) {
231 $bndlen = strlen($reg[1]);
232 $bndend = false;
233 if (strlen($line) > ($bndlen + 3)) {
234 if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
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>';
253 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
254 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
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];
272 }
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 }
288
289 $pre .= '<b><font color='.$color[7].'">';
290 $end .= '</font></b>';
291 //$mimepart=false;
292 }
293 }
294 }
295 /*
296 if ($mimepart) {
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 }
302 }
303 }
304 */
305 if ($stripHTML) {
306 $message_body .= $line . "\r\n";
307 } else {
308 $line = htmlspecialchars($line);
309 if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
310 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
311 }
312 }
313
314 //$returnValue .= returnTime($start).'<br />';
315 $xtra = <<<ECHO
316
317 <style type="text/css">
318
319 <!--
320 .ent_body {
321 display:inline;
322 }
323
324 .header {
325 display:inline;
326 }
327
328 .entheader {
329 display:inline;
330 width:99%;
331 }
332 //-->
333
334 </style>
335
336 ECHO;
337
338 if (!$stripHTML) {
339 ob_start();
340 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
341 $returnValue .= ob_get_contents();
342 ob_end_clean();
343 }
344
345 /* body */
346 if (!$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 }
352
353
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') {
360 // if (!$stripHTML) {
361 // $returnValue .= 'img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&amp;name='.$entities[$key]['name'].'"><br />';
362 // }
363 // }
364 // }
365 //}
366 //session_unregister("entities");
367
368 if (count($content) > 0 && !$stripHTML) {
369 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
370 $returnValue .= '<table border="1" width="98%"><thead>'.
371 '<tr bgcolor="'.$color[7].'">'.
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>'.
376 '</tr>'.
377 '</thead><tbody>';
378 for ($i = 0; $i < count($content);$i++) {
379 $returnValue .= '<tr><td>';
380 $returnValue .= $content[$i]['ent'].'</td><td>';
381 if (isset($content[$i]['type'])) {
382 $returnValue .= $content[$i]['type'];
383 } else $returnValue .= 'TEXT/PLAIN';
384 $returnValue .= '</td><td>';
385 if (isset($content[$i]['name'])) {
386 $returnValue .= $content[$i]['name'];
387 } else $returnValue .= '&nbsp;';
388 $returnValue .= '</td><td>';
389 if (isset($content[$i]['encoding'])) {
390 $returnValue .= $content[$i]['encoding'];
391 } else $returnValue .= '&nbsp;';
392 $returnValue .= '</td></tr>'."\n";
393 }
394 $returnValue .= '</tbody></table><br />'."\n";
395 }
396
397 if (!$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
404 if (!$stripHTML)
405 $returnValue .= '</div></div></font></code></body></html>';
406
407 return $returnValue;
408
409 }
410
411 ?>