Placing main message details code inside of a function where it is immensely more...
[squirrelmail.git] / plugins / message_details / message_details_bottom.php
CommitLineData
bbdea028 1<?php
86c62251 2/**
3 * Message Details plugin - bottom frame with message structure and rfc822 body
8d6a115b 4 *
5 * Plugin to view the RFC822 raw message output and the bodystructure of a message
6 *
8d6a115b 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
91e0dccc 8 *
86c62251 9 * @author Marc Groot Koerkamp
10 * @copyright Copyright &copy; 2002 Marc Groot Koerkamp, The Netherlands
6c84ba1e 11 * @copyright Copyright &copy; 2004-2005 The SquirrelMail Project Team
86c62251 12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @version $Id$
ea5f4b8e 14 * @package plugins
15 * @subpackage message_details
8d6a115b 16 */
bbdea028 17
ea5f4b8e 18/** @ignore */
f4450417 19if (!defined('SM_PATH'))
20 define('SM_PATH','../../');
89252d71 21
22/* SquirrelMail required files. */
23require_once(SM_PATH . 'include/validate.php');
24require_once(SM_PATH . 'functions/imap.php');
25require_once(SM_PATH . 'functions/mime.php');
bbdea028 26
f4450417 27sqgetGlobalVar('get_message_details', $md_action, SQ_GET);
bbdea028 28
f4450417 29if (!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 ----------
04f6008a 38
39
5d73c31d 40/**
41 * Calculates id of MIME entity
42 * @param string $entString
43 * @param integer $direction
44 * @return string
e9ec1bd8 45 * @access private
5d73c31d 46 */
bbdea028 47function CalcEntity($entString, $direction) {
48 $result = $entString;
49 if ($direction == -1) {
5c325683 50 $pos = strrpos($entString,'.');
51 $result = substr($entString,0,$pos);
bbdea028 52 }
53
54 switch ($direction) {
796f91d9 55 case 0:
56 $pos = strrpos($entString,'.');
57 if ($pos === false) {
58 $entString++;
59 $result= $entString;
91e0dccc 60 }
796f91d9 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;
bbdea028 73 }
74 return ($result);
75}
76
f4450417 77
78
5d73c31d 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 */
bbdea028 86function returnTime($start) {
87 $stop = gettimeofday();
88 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
89 return $timepassed;
90}
91
f4450417 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 */
104function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
105
106global $imapServerAddress, $imapPort, $color;
107
108$returnValue = '';
109
110sqgetGlobalVar('username', $username, SQ_SESSION);
111sqgetGlobalVar('key', $key, SQ_COOKIE);
112
bbdea028 113$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
114$read = sqimap_mailbox_select($imapConnection, $mailbox);
115$start = gettimeofday();
6201339c 116$body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
bbdea028 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();
125session_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);
136for ($i=1; $i < $count; $i++) {
137 $line = trim($body[$i]);
138 if ($line == '') {
796f91d9 139 $pre = '';
140 $end = '';
bbdea028 141 if ($bnd_end) {
796f91d9 142 $header = true;
143 $mimepart = false;
144 } else if ($messageheader) {
145 if ($header) {
146 $header=false;
91e0dccc 147 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
796f91d9 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 = '';
91e0dccc 159 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
796f91d9 160 }
161 $header = false;
162 $mimepart=true;
91e0dccc 163 }
796f91d9 164 $contentset = false;
165 $nameset = false;
bbdea028 166 } else {
167 if (!$header && $messageheader) {
796f91d9 168 $messageheaderstart=true;
169 if ($pre != '<b>') {
170 $pre = '<i><font color ="'.$color[1].'">';
171 $end = '</i></font>';
172 }
91e0dccc 173 }
796f91d9 174 if (!$messageheader && !$header ) {
175 $mimepart=true;
176 } else {
177 $mimepart=false;
178 }
179 $pre = '';
180 $end = '';
bbdea028 181 }
182 if ( ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
796f91d9 183 $bnd = $reg[1];
91e0dccc 184 $bndreg = $bnd;
796f91d9 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);
bbdea028 193
796f91d9 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 }
bbdea028 203 }
91e0dccc 204
bbdea028 205 if (($line != '' && $line{0} == '-' || $header) && isset($boundaries[0])) {
206 $cnt=count($boundaries)-1;
796f91d9 207 $bnd = $boundaries[$cnt]['bnd'];
208 $bndreg = $boundaries[$cnt]['bndreg'];
91e0dccc 209
796f91d9 210 $regstr = '/^--'."($bndreg)".".*".'/';
211 if (preg_match($regstr,$line,$reg) ) {
212 $bndlen = strlen($reg[1]);
91e0dccc 213 $bndend = false;
bbdea028 214 if (strlen($line) > ($bndlen + 3)) {
91e0dccc 215 if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
796f91d9 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>';
5d73c31d 234 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
235 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
796f91d9 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];
91e0dccc 253 }
796f91d9 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
796f91d9 270 $pre .= '<b><font color='.$color[7].'">';
271 $end .= '</font></b>';
272 //$mimepart=false;
273 }
274 }
275 }
bbdea028 276/*
277 if ($mimepart) {
796f91d9 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 }
bbdea028 283 }
796f91d9 284 }
bbdea028 285*/
f4450417 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 }
bbdea028 292}
f4450417 293//$returnValue .= returnTime($start).'<br />';
bbdea028 294$xtra = <<<ECHO
295
796f91d9 296<style>
bbdea028 297
298<!--
299.ent_body {
300 display:inline;
301}
302
303.header {
304 display:inline;
305}
306
307.entheader {
308 display:inline;
bbdea028 309 width:99%;
310}
311//-->
312
796f91d9 313</style>
bbdea028 314
315ECHO;
316
f4450417 317if (!$stripHTML) {
318 ob_start();
319 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
320 $returnValue .= ob_get_contents();
321 ob_end_clean();
322}
323
bbdea028 324/* body */
f4450417 325if (!$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}
c157ce7e 331
332
bbdea028 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') {
f4450417 339// if (!$stripHTML) {
340// $returnValue .= 'img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&amp;name='.$entities[$key]['name'].'"><br />';
341// }
bbdea028 342// }
343// }
344//}
345//session_unregister("entities");
346
f4450417 347if (count($content) > 0 && !$stripHTML) {
348 $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
349 $returnValue .= '<table border="1" width="98%"><thead>'.
796f91d9 350 '<tr bgcolor="'.$color[7].'">'.
86c62251 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>'.
796f91d9 355 '</tr>'.
356 '</thead><tbody>';
bbdea028 357 for ($i = 0; $i < count($content);$i++) {
f4450417 358 $returnValue .= '<tr><td>';
359 $returnValue .= $content[$i]['ent'].'</td><td>';
796f91d9 360 if (isset($content[$i]['type'])) {
f4450417 361 $returnValue .= $content[$i]['type'];
362 } else $returnValue .= 'TEXT/PLAIN';
363 $returnValue .= '</td><td>';
796f91d9 364 if (isset($content[$i]['name'])) {
f4450417 365 $returnValue .= $content[$i]['name'];
366 } else $returnValue .= '&nbsp;';
367 $returnValue .= '</td><td>';
796f91d9 368 if (isset($content[$i]['encoding'])) {
f4450417 369 $returnValue .= $content[$i]['encoding'];
370 } else $returnValue .= '&nbsp;';
371 $returnValue .= '</td></tr>'."\n";
bbdea028 372 }
f4450417 373 $returnValue .= '</tbody></table><br />'."\n";
bbdea028 374}
f4450417 375
376if (!$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
383if (!$stripHTML)
384 $returnValue .= '</div></div></font></code></body></html>';
385
386return $returnValue;
387
388}
389
390?>