Placing main message details code inside of a function where it is immensely more...
[squirrelmail.git] / plugins / message_details / message_details_bottom.php
1 <?php
2 /**
3 * Message Details plugin - bottom frame with message structure and rfc822 body
4 *
5 * Plugin to view the RFC822 raw message output and the bodystructure of a message
6 *
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * @author Marc Groot Koerkamp
10 * @copyright Copyright &copy; 2002 Marc Groot Koerkamp, The Netherlands
11 * @copyright Copyright &copy; 2004-2005 The SquirrelMail Project Team
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @version $Id$
14 * @package plugins
15 * @subpackage message_details
16 */
17
18 /** @ignore */
19 if (!defined('SM_PATH'))
20 define('SM_PATH','../../');
21
22 /* SquirrelMail required files. */
23 require_once(SM_PATH . 'include/validate.php');
24 require_once(SM_PATH . 'functions/imap.php');
25 require_once(SM_PATH . 'functions/mime.php');
26
27 sqgetGlobalVar('get_message_details', $md_action, SQ_GET);
28
29 if (!empty($md_action))
30 {
31 sqgetGlobalVar('passed_id', $passed_id, SQ_GET);
32 sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
33 echo get_message_details($mailbox, $passed_id);
34 }
35
36
37 // ---------- function definitions ----------
38
39
40 /**
41 * Calculates id of MIME entity
42 * @param string $entString
43 * @param integer $direction
44 * @return string
45 * @access private
46 */
47 function CalcEntity($entString, $direction) {
48 $result = $entString;
49 if ($direction == -1) {
50 $pos = strrpos($entString,'.');
51 $result = substr($entString,0,$pos);
52 }
53
54 switch ($direction) {
55 case 0:
56 $pos = strrpos($entString,'.');
57 if ($pos === false) {
58 $entString++;
59 $result= $entString;
60 }
61 else {
62 $level = substr($entString,0,$pos);
63 $sublevel = substr($entString,$pos+1);
64 $sublevel++;
65 $result = "$level".'.'."$sublevel";
66 }
67 break;
68 case 1:
69 $result = "$entString".".0";
70 break;
71 default:
72 break;
73 }
74 return ($result);
75 }
76
77
78
79 /**
80 * Returns time in microseconds between selected and current timestamp
81 *
82 * @param array $start see details about array format at http://www.php.net/gettimeofday
83 * @return integer time in microseconds
84 * @access private
85 */
86 function returnTime($start) {
87 $stop = gettimeofday();
88 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
89 return $timepassed;
90 }
91
92
93
94 /**
95 * Returns actual message details
96 * @param string $mailbox
97 * @param string $passed_id
98 * @param boolean $stripHTML If TRUE, only plain text is returned,
99 * default is FALSE, wherein output contains
100 * pretty-HTMLification of message body
101 * @return string The formatted message details
102 * @access public
103 */
104 function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
105
106 global $imapServerAddress, $imapPort, $color;
107
108 $returnValue = '';
109
110 sqgetGlobalVar('username', $username, SQ_SESSION);
111 sqgetGlobalVar('key', $key, SQ_COOKIE);
112
113 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
114 $read = sqimap_mailbox_select($imapConnection, $mailbox);
115 $start = gettimeofday();
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 = trim($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 = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
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 = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
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 .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
235 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
236 $end .= '</font></b>'."\n";
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 ($mimepart) {
278 if (isset($entities["$entStr"])) {
279 if (isset($encoding) && $encoding == 'base64') {
280 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
281 $entities["$entStr"]['content'] .= $line;
282 }
283 }
284 }
285 */
286 if ($stripHTML) {
287 $message_body .= $line . "\r\n";
288 } else {
289 $line = htmlspecialchars($line);
290 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
291 }
292 }
293 //$returnValue .= returnTime($start).'<br />';
294 $xtra = <<<ECHO
295
296 <style>
297
298 <!--
299 .ent_body {
300 display:inline;
301 }
302
303 .header {
304 display:inline;
305 }
306
307 .entheader {
308 display:inline;
309 width:99%;
310 }
311 //-->
312
313 </style>
314
315 ECHO;
316
317 if (!$stripHTML) {
318 ob_start();
319 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
320 $returnValue .= ob_get_contents();
321 ob_end_clean();
322 }
323
324 /* body */
325 if (!$stripHTML) {
326 $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
327 $returnValue .= '<code>'."\n";
328 $returnValue .= '<font face="monospace">'."\n";
329 $returnValue .= '<br />'."\n";
330 }
331
332
333 //session_register("entities");
334 //$keys = array_keys($entities);
335 //$start = gettimeofday();
336 //foreach ($keys as $key) {
337 // if (isset($entities[$key])) {
338 // if ($entities[$key]['encoding'] == 'base64') {
339 // if (!$stripHTML) {
340 // $returnValue .= 'img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&amp;name='.$entities[$key]['name'].'"><br />';
341 // }
342 // }
343 // }
344 //}
345 //session_unregister("entities");
346
347 if (count($content) > 0 && !$stripHTML) {
348 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
349 $returnValue .= '<table border="1" width="98%"><thead>'.
350 '<tr bgcolor="'.$color[7].'">'.
351 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
352 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
353 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
354 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
355 '</tr>'.
356 '</thead><tbody>';
357 for ($i = 0; $i < count($content);$i++) {
358 $returnValue .= '<tr><td>';
359 $returnValue .= $content[$i]['ent'].'</td><td>';
360 if (isset($content[$i]['type'])) {
361 $returnValue .= $content[$i]['type'];
362 } else $returnValue .= 'TEXT/PLAIN';
363 $returnValue .= '</td><td>';
364 if (isset($content[$i]['name'])) {
365 $returnValue .= $content[$i]['name'];
366 } else $returnValue .= '&nbsp;';
367 $returnValue .= '</td><td>';
368 if (isset($content[$i]['encoding'])) {
369 $returnValue .= $content[$i]['encoding'];
370 } else $returnValue .= '&nbsp;';
371 $returnValue .= '</td></tr>'."\n";
372 }
373 $returnValue .= '</tbody></table><br />'."\n";
374 }
375
376 if (!$stripHTML) {
377 $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
378 $returnValue .= '<div><div class="header">'."\n\n";
379 }
380
381 $returnValue .= $message_body;
382
383 if (!$stripHTML)
384 $returnValue .= '</div></div></font></code></body></html>';
385
386 return $returnValue;
387
388 }
389
390 ?>