All this document.write stuff is overkill when we already have checkForJavascript...
[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
4b5049de 10 * @copyright &copy; 2002-2007 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
4588f43d 34if (!empty($md_action)) {
f4450417 35 sqgetGlobalVar('passed_id', $passed_id, SQ_GET);
36 sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
4588f43d 37 /*
38 * add third function argument, if you want to see
39 * message source without formating
40 */
f4450417 41 echo get_message_details($mailbox, $passed_id);
42}
43
44
45// ---------- function definitions ----------
04f6008a 46
24e1fb21 47/**
48 * Converts 8bit string to hex
49 *
f8a1ed5a 50 * Replaces 8bit symbols with their hex strings,
24e1fb21 51 * encloses them in curly brackets and uses different color.
52 * @param string $string text
53 * @return string
54 * @since 1.5.1
55 */
56function msgd_convert_to_hex($string) {
57 global $color;
58 return preg_replace("/([\200-\377])/e","'<font color=\"$color[2]\">{'.dechex(ord('\\1')).'}</font>'",$string);
59}
04f6008a 60
5d73c31d 61/**
62 * Calculates id of MIME entity
63 * @param string $entString
64 * @param integer $direction
65 * @return string
e9ec1bd8 66 * @access private
5d73c31d 67 */
bbdea028 68function CalcEntity($entString, $direction) {
69 $result = $entString;
70 if ($direction == -1) {
5c325683 71 $pos = strrpos($entString,'.');
72 $result = substr($entString,0,$pos);
bbdea028 73 }
74
75 switch ($direction) {
796f91d9 76 case 0:
77 $pos = strrpos($entString,'.');
78 if ($pos === false) {
79 $entString++;
80 $result= $entString;
91e0dccc 81 }
796f91d9 82 else {
83 $level = substr($entString,0,$pos);
84 $sublevel = substr($entString,$pos+1);
85 $sublevel++;
86 $result = "$level".'.'."$sublevel";
87 }
88 break;
89 case 1:
90 $result = "$entString".".0";
91 break;
92 default:
93 break;
bbdea028 94 }
95 return ($result);
96}
97
f4450417 98
f4450417 99/**
100 * Returns actual message details
101 * @param string $mailbox
102 * @param string $passed_id
f8a1ed5a 103 * @param boolean $stripHTML If TRUE, only plain text is returned,
104 * default is FALSE, wherein output contains
f4450417 105 * pretty-HTMLification of message body
106 * @return string The formatted message details
107 * @access public
108 */
109function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
2128bbc6 110 global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex, $username;
4588f43d 111
112 $returnValue = '';
113
4588f43d 114 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
115 $read = sqimap_mailbox_select($imapConnection, $mailbox);
116 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
117 $message_body = '';
118 $header = false;
119 $mimepart = false;
120 $bnd_end = false;
121 $messageheader = true;
122 $messageheaderstart=false;
123 $boundaries = array();
124 $entities = array();
125 session_unregister("entities");
126 $pre = '<b>';
127 $end = '</b>';
128 $entStr = '';
129 $bla ='';
130 $content = array ();
131 $content_indx = -1;
132 $contentset = false;
133
134 $count=count($body);
135 $body[$count-1] = substr($body[$count-1], -1);
136 for ($i=1; $i < $count; $i++) {
137 $line = rtrim($body[$i]);
138 if ($line == '') {
139 $pre = '';
140 $end = '';
141 if ($bnd_end) {
142 $header = true;
143 $mimepart = false;
144 } else if ($messageheader) {
145 if ($header) {
146 $header=false;
147 $end = '</div><div class="ent_body" id="'.$entStr.'B">';
148 }
149 $mimepart = -$header;
150 $bnd_end = false;
151 if ($messageheaderstart) {
152 $messageheaderstart=false;
153 }
154 } else if ($messageheaderstart) {
155 $messageheader= false;
156 } else {
157 if ($header) {
158 $pre = '';
159 $end = '</div><div class="ent_body" id="'.$entStr.'B">';
160 }
161 $header = false;
162 $mimepart=true;
796f91d9 163 }
4588f43d 164 $contentset = false;
165 $nameset = false;
796f91d9 166 } else {
4588f43d 167 if (!$header && $messageheader) {
168 $messageheaderstart=true;
169 if ($pre != '<b>') {
170 $pre = '<i><font color ="'.$color[1].'">';
171 $end = '</i></font>';
172 }
796f91d9 173 }
4588f43d 174 if (!$messageheader && !$header ) {
175 $mimepart=true;
176 } else {
177 $mimepart=false;
796f91d9 178 }
4588f43d 179 $pre = '';
180 $end = '';
91e0dccc 181 }
4588f43d 182 if ( ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
183 $bnd = $reg[1];
184 $bndreg = $bnd;
185 $bndreg = str_replace("\\","\\\\",$bndreg);
186 $bndreg = str_replace("?","\\?",$bndreg);
187 $bndreg = str_replace("+","\\+",$bndreg);
188 $bndreg = str_replace(".","\\.",$bndreg);
189 $bndreg = str_replace("/","\\/",$bndreg);
190 $bndreg = str_replace("-","\\-",$bndreg);
191 $bndreg = str_replace("(","\\(",$bndreg);
192 $bndreg = str_replace(")","\\)",$bndreg);
193
194 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
195 $messageheader = false;
196 $messageheaderstart=false;
796f91d9 197 $mimepart=false;
4588f43d 198 if ($entStr=='') {
199 $entStr='0';
796f91d9 200 } else {
4588f43d 201 $entStr = CalcEntity("$entStr",1);
796f91d9 202 }
4588f43d 203 }
204
205 if (($line != '' && $line{0} == '-' || $header) && isset($boundaries[0])) {
206 $cnt=count($boundaries)-1;
207 $bnd = $boundaries[$cnt]['bnd'];
208 $bndreg = $boundaries[$cnt]['bndreg'];
209
210 $regstr = '/^--'."($bndreg)".".*".'/';
211 if (preg_match($regstr,$line,$reg) ) {
212 $bndlen = strlen($reg[1]);
213 $bndend = false;
214 if (strlen($line) > ($bndlen + 3)) {
215 if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
216 $bndend = true;
217 }
218 if ($bndend) {
219 $entStr = CalcEntity("$entStr",-1);
220 array_pop($boundaries);
221 $pre .= '<b><font color ="'.$color[2].'">';
222 $end .= '</font></b>';
223 $header = true;
224 $mimepart = false;
225 $bnd_end = true;
226 $encoding = '';
227 } else {
228 $header = true;
229 $bnd_end = false;
230 $entStr = CalcEntity("$entStr",0);
231 $content_indx++;
232 $content[$content_indx]=array();
233 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
234 $pre .= '</div><div class="entheader" id="'.
235 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
236 $end .= '</font></b>';
237 $header = true;
238 $mimepart = false;
239 $encoding = '';
796f91d9 240 }
4588f43d 241 } else {
242 if ($header) {
243 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
244 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
245 $messageheader = true;
246 }
247 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
248 $contentset = true;
249 if ($reg[2] == 'image') {
250 $entities["$entStr"] = array();
251 $entities["$entStr"]['entity'] = $entStr;
252 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
253 }
254 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
255 $name = htmlspecialchars($reg[2]);
256 $content[$content_indx]['name'] = decodeHeader($name);
257 $nameset = true;
258 if (isset($entities["$entStr"])) {
259 $entities["$entStr"]['name'] = urlEncode($reg[2]);
260 }
261 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
262 $encoding = $reg[2];
263 if (isset($entities["$entStr"])) {
264 $entities["$entStr"]['encoding']=$reg[2];
265 }
266 $content[$content_indx]['encoding'] = $encoding;
267 $mimeentity = '';
268 }
bbdea028 269
4588f43d 270 $pre .= '<b><font color='.$color[7].'">';
271 $end .= '</font></b>';
272 //$mimepart=false;
273 }
796f91d9 274 }
275 }
4588f43d 276
277 if ($stripHTML) {
278 $message_body .= $line . "\r\n";
279 } else {
280 $line = htmlspecialchars($line);
281 if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
282 $message_body .= "$pre"."$line"."$end"."\r\n";
bbdea028 283 }
796f91d9 284 }
4588f43d 285
f4450417 286//$returnValue .= returnTime($start).'<br />';
bbdea028 287$xtra = <<<ECHO
288
a74103dd 289<style type="text/css">
bbdea028 290
291<!--
292.ent_body {
293 display:inline;
294}
295
296.header {
297 display:inline;
298}
299
300.entheader {
301 display:inline;
bbdea028 302 width:99%;
303}
304//-->
305
796f91d9 306</style>
bbdea028 307
308ECHO;
309
4588f43d 310 if (!$stripHTML) {
311 ob_start();
312 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
313 $returnValue .= ob_get_contents();
314 ob_end_clean();
bbdea028 315 }
f4450417 316
4588f43d 317 /* body */
318 if (!$stripHTML) {
319 $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
320 $returnValue .= '<font face="monospace">'."\n";
321 $returnValue .= '<br />'."\n";
322 }
f4450417 323
4588f43d 324 if (count($content) > 0 && !$stripHTML) {
325 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
326 $returnValue .= '<table border="1" width="98%"><thead>'.
327 '<tr bgcolor="'.$color[7].'">'.
328 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
329 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
330 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
331 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
332 '</tr>'.
333 '</thead><tbody>';
334 for ($i = 0; $i < count($content);$i++) {
335 $returnValue .= '<tr><td>';
336 $returnValue .= $content[$i]['ent'].'</td><td>';
337 if (isset($content[$i]['type'])) {
338 $returnValue .= $content[$i]['type'];
339 } else $returnValue .= 'TEXT/PLAIN';
340 $returnValue .= '</td><td>';
341 if (isset($content[$i]['name'])) {
342 $returnValue .= $content[$i]['name'];
343 } else $returnValue .= '&nbsp;';
344 $returnValue .= '</td><td>';
345 if (isset($content[$i]['encoding'])) {
346 $returnValue .= $content[$i]['encoding'];
347 } else $returnValue .= '&nbsp;';
348 $returnValue .= '</td></tr>'."\n";
349 }
350 $returnValue .= '</tbody></table><br />'."\n";
351 }
f4450417 352
4588f43d 353 if (!$stripHTML) {
354 $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
355 $returnValue .= '<pre><div><div class="header">';
356 } else {
357 $returnValue .= '<pre>';
358 }
f4450417 359
4588f43d 360 $returnValue .= $message_body;
361
362 if (!$stripHTML) {
363 $returnValue .= '</div></div></pre></font></body></html>';
364 } else {
365 $returnValue .= '</pre>';
366 }
367 return $returnValue;
f4450417 368}
2128bbc6 369?>