Plugins like listcommands should not close their own pages without displaying errors...
[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-2007 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 /**
18 * Include the SquirrelMail initialization file.
19 */
20 require('../../include/init.php');
21 require(SM_PATH . 'functions/imap_general.php');
22 require(SM_PATH . 'functions/imap_messages.php');
23 require(SM_PATH . 'functions/mime.php');
24
25 sqgetGlobalVar('get_message_details', $md_action, SQ_GET);
26
27 /**
28 * Controls display of 8bit symbols in message source
29 * @global boolean $msgd_8bit_in_hex;
30 */
31 global $msgd_8bit_in_hex;
32 $msgd_8bit_in_hex=false;
33
34 if (!empty($md_action)) {
35 sqgetGlobalVar('passed_id', $passed_id, SQ_GET);
36 sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
37 /*
38 * add third function argument, if you want to see
39 * message source without formating
40 */
41 echo get_message_details($mailbox, $passed_id);
42 }
43
44
45 // ---------- function definitions ----------
46
47 /**
48 * Converts 8bit string to hex
49 *
50 * Replaces 8bit symbols with their hex strings,
51 * encloses them in curly brackets and uses different color.
52 * @param string $string text
53 * @return string
54 * @since 1.5.1
55 */
56 function 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 }
60
61 /**
62 * Calculates id of MIME entity
63 * @param string $entString
64 * @param integer $direction
65 * @return string
66 * @access private
67 */
68 function CalcEntity($entString, $direction) {
69 $result = $entString;
70 if ($direction == -1) {
71 $pos = strrpos($entString,'.');
72 $result = substr($entString,0,$pos);
73 }
74
75 switch ($direction) {
76 case 0:
77 $pos = strrpos($entString,'.');
78 if ($pos === false) {
79 $entString++;
80 $result= $entString;
81 }
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;
94 }
95 return ($result);
96 }
97
98
99 /**
100 * Returns actual message details
101 * @param string $mailbox
102 * @param string $passed_id
103 * @param boolean $stripHTML If TRUE, only plain text is returned,
104 * default is FALSE, wherein output contains
105 * pretty-HTMLification of message body
106 * @return string The formatted message details
107 * @access public
108 */
109 function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
110 global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex, $username;
111
112 $returnValue = '';
113
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;
163 }
164 $contentset = false;
165 $nameset = false;
166 } else {
167 if (!$header && $messageheader) {
168 $messageheaderstart=true;
169 if ($pre != '<b>') {
170 $pre = '<i><font color ="'.$color[1].'">';
171 $end = '</i></font>';
172 }
173 }
174 if (!$messageheader && !$header ) {
175 $mimepart=true;
176 } else {
177 $mimepart=false;
178 }
179 $pre = '';
180 $end = '';
181 }
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;
197 $mimepart=false;
198 if ($entStr=='') {
199 $entStr='0';
200 } else {
201 $entStr = CalcEntity("$entStr",1);
202 }
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 = '';
240 }
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 }
269
270 $pre .= '<b><font color='.$color[7].'">';
271 $end .= '</font></b>';
272 //$mimepart=false;
273 }
274 }
275 }
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";
283 }
284 }
285
286 //$returnValue .= returnTime($start).'<br />';
287 $xtra = <<<ECHO
288
289 <style type="text/css">
290
291 <!--
292 .ent_body {
293 display:inline;
294 }
295
296 .header {
297 display:inline;
298 }
299
300 .entheader {
301 display:inline;
302 width:99%;
303 }
304 //-->
305
306 </style>
307
308 ECHO;
309
310 if (!$stripHTML) {
311 ob_start();
312 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
313 $returnValue .= ob_get_contents();
314 ob_end_clean();
315 }
316
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 }
323
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 }
352
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 }
359
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;
368 }
369 ?>