removed debug function
[squirrelmail.git] / plugins / message_details / message_details_bottom.php
... / ...
CommitLineData
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/**
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');
24
25sqgetGlobalVar('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 */
31global $msgd_8bit_in_hex;
32$msgd_8bit_in_hex=false;
33
34if (!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 */
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}
60
61/**
62 * Calculates id of MIME entity
63 * @param string $entString
64 * @param integer $direction
65 * @return string
66 * @access private
67 */
68function 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 */
109function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
110 global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex;
111
112 $returnValue = '';
113
114 sqgetGlobalVar('username', $username, SQ_SESSION);
115
116 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
117 $read = sqimap_mailbox_select($imapConnection, $mailbox);
118 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
119 $message_body = '';
120 $header = false;
121 $mimepart = false;
122 $bnd_end = false;
123 $messageheader = true;
124 $messageheaderstart=false;
125 $boundaries = array();
126 $entities = array();
127 session_unregister("entities");
128 $pre = '<b>';
129 $end = '</b>';
130 $entStr = '';
131 $bla ='';
132 $content = array ();
133 $content_indx = -1;
134 $contentset = false;
135
136 $count=count($body);
137 $body[$count-1] = substr($body[$count-1], -1);
138 for ($i=1; $i < $count; $i++) {
139 $line = rtrim($body[$i]);
140 if ($line == '') {
141 $pre = '';
142 $end = '';
143 if ($bnd_end) {
144 $header = true;
145 $mimepart = false;
146 } else if ($messageheader) {
147 if ($header) {
148 $header=false;
149 $end = '</div><div class="ent_body" id="'.$entStr.'B">';
150 }
151 $mimepart = -$header;
152 $bnd_end = false;
153 if ($messageheaderstart) {
154 $messageheaderstart=false;
155 }
156 } else if ($messageheaderstart) {
157 $messageheader= false;
158 } else {
159 if ($header) {
160 $pre = '';
161 $end = '</div><div class="ent_body" id="'.$entStr.'B">';
162 }
163 $header = false;
164 $mimepart=true;
165 }
166 $contentset = false;
167 $nameset = false;
168 } else {
169 if (!$header && $messageheader) {
170 $messageheaderstart=true;
171 if ($pre != '<b>') {
172 $pre = '<i><font color ="'.$color[1].'">';
173 $end = '</i></font>';
174 }
175 }
176 if (!$messageheader && !$header ) {
177 $mimepart=true;
178 } else {
179 $mimepart=false;
180 }
181 $pre = '';
182 $end = '';
183 }
184 if ( ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
185 $bnd = $reg[1];
186 $bndreg = $bnd;
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 $bndreg = str_replace("(","\\(",$bndreg);
194 $bndreg = str_replace(")","\\)",$bndreg);
195
196 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
197 $messageheader = false;
198 $messageheaderstart=false;
199 $mimepart=false;
200 if ($entStr=='') {
201 $entStr='0';
202 } else {
203 $entStr = CalcEntity("$entStr",1);
204 }
205 }
206
207 if (($line != '' && $line{0} == '-' || $header) && isset($boundaries[0])) {
208 $cnt=count($boundaries)-1;
209 $bnd = $boundaries[$cnt]['bnd'];
210 $bndreg = $boundaries[$cnt]['bndreg'];
211
212 $regstr = '/^--'."($bndreg)".".*".'/';
213 if (preg_match($regstr,$line,$reg) ) {
214 $bndlen = strlen($reg[1]);
215 $bndend = false;
216 if (strlen($line) > ($bndlen + 3)) {
217 if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
218 $bndend = true;
219 }
220 if ($bndend) {
221 $entStr = CalcEntity("$entStr",-1);
222 array_pop($boundaries);
223 $pre .= '<b><font color ="'.$color[2].'">';
224 $end .= '</font></b>';
225 $header = true;
226 $mimepart = false;
227 $bnd_end = true;
228 $encoding = '';
229 } else {
230 $header = true;
231 $bnd_end = false;
232 $entStr = CalcEntity("$entStr",0);
233 $content_indx++;
234 $content[$content_indx]=array();
235 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
236 $pre .= '</div><div class="entheader" id="'.
237 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
238 $end .= '</font></b>';
239 $header = true;
240 $mimepart = false;
241 $encoding = '';
242 }
243 } else {
244 if ($header) {
245 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
246 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
247 $messageheader = true;
248 }
249 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
250 $contentset = true;
251 if ($reg[2] == 'image') {
252 $entities["$entStr"] = array();
253 $entities["$entStr"]['entity'] = $entStr;
254 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
255 }
256 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
257 $name = htmlspecialchars($reg[2]);
258 $content[$content_indx]['name'] = decodeHeader($name);
259 $nameset = true;
260 if (isset($entities["$entStr"])) {
261 $entities["$entStr"]['name'] = urlEncode($reg[2]);
262 }
263 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
264 $encoding = $reg[2];
265 if (isset($entities["$entStr"])) {
266 $entities["$entStr"]['encoding']=$reg[2];
267 }
268 $content[$content_indx]['encoding'] = $encoding;
269 $mimeentity = '';
270 }
271
272 $pre .= '<b><font color='.$color[7].'">';
273 $end .= '</font></b>';
274 //$mimepart=false;
275 }
276 }
277 }
278
279 if ($stripHTML) {
280 $message_body .= $line . "\r\n";
281 } else {
282 $line = htmlspecialchars($line);
283 if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
284 $message_body .= "$pre"."$line"."$end"."\r\n";
285 }
286 }
287
288//$returnValue .= returnTime($start).'<br />';
289$xtra = <<<ECHO
290
291<style type="text/css">
292
293<!--
294.ent_body {
295 display:inline;
296}
297
298.header {
299 display:inline;
300}
301
302.entheader {
303 display:inline;
304 width:99%;
305}
306//-->
307
308</style>
309
310ECHO;
311
312 if (!$stripHTML) {
313 ob_start();
314 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
315 $returnValue .= ob_get_contents();
316 ob_end_clean();
317 }
318
319 /* body */
320 if (!$stripHTML) {
321 $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
322 $returnValue .= '<font face="monospace">'."\n";
323 $returnValue .= '<br />'."\n";
324 }
325
326 if (count($content) > 0 && !$stripHTML) {
327 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
328 $returnValue .= '<table border="1" width="98%"><thead>'.
329 '<tr bgcolor="'.$color[7].'">'.
330 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
331 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
332 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
333 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
334 '</tr>'.
335 '</thead><tbody>';
336 for ($i = 0; $i < count($content);$i++) {
337 $returnValue .= '<tr><td>';
338 $returnValue .= $content[$i]['ent'].'</td><td>';
339 if (isset($content[$i]['type'])) {
340 $returnValue .= $content[$i]['type'];
341 } else $returnValue .= 'TEXT/PLAIN';
342 $returnValue .= '</td><td>';
343 if (isset($content[$i]['name'])) {
344 $returnValue .= $content[$i]['name'];
345 } else $returnValue .= '&nbsp;';
346 $returnValue .= '</td><td>';
347 if (isset($content[$i]['encoding'])) {
348 $returnValue .= $content[$i]['encoding'];
349 } else $returnValue .= '&nbsp;';
350 $returnValue .= '</td></tr>'."\n";
351 }
352 $returnValue .= '</tbody></table><br />'."\n";
353 }
354
355 if (!$stripHTML) {
356 $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
357 $returnValue .= '<pre><div><div class="header">';
358 } else {
359 $returnValue .= '<pre>';
360 }
361
362 $returnValue .= $message_body;
363
364 if (!$stripHTML) {
365 $returnValue .= '</div></div></pre></font></body></html>';
366 } else {
367 $returnValue .= '</pre>';
368 }
369 return $returnValue;
370}
371?>