use constant instead of integer for hour format pref default
[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 */
92219031 19define('SM_PATH','../../');
89252d71 20
21/* SquirrelMail required files. */
22require_once(SM_PATH . 'include/validate.php');
23require_once(SM_PATH . 'functions/imap.php');
24require_once(SM_PATH . 'functions/mime.php');
bbdea028 25
6201339c 26global $color;
bbdea028 27
b587ac51 28sqgetGlobalVar('passed_id', $passed_id, SQ_GET);
29sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
04f6008a 30
b587ac51 31sqgetGlobalVar('username', $username, SQ_SESSION);
32sqgetGlobalVar('key', $key, SQ_COOKIE);
33sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
04f6008a 34
5d73c31d 35/**
36 * Calculates id of MIME entity
37 * @param string $entString
38 * @param integer $direction
39 * @return string
e9ec1bd8 40 * @access private
5d73c31d 41 */
bbdea028 42function CalcEntity($entString, $direction) {
43 $result = $entString;
44 if ($direction == -1) {
5c325683 45 $pos = strrpos($entString,'.');
46 $result = substr($entString,0,$pos);
bbdea028 47 }
48
49 switch ($direction) {
796f91d9 50 case 0:
51 $pos = strrpos($entString,'.');
52 if ($pos === false) {
53 $entString++;
54 $result= $entString;
91e0dccc 55 }
796f91d9 56 else {
57 $level = substr($entString,0,$pos);
58 $sublevel = substr($entString,$pos+1);
59 $sublevel++;
60 $result = "$level".'.'."$sublevel";
61 }
62 break;
63 case 1:
64 $result = "$entString".".0";
65 break;
66 default:
67 break;
bbdea028 68 }
69 return ($result);
70}
71
5d73c31d 72/**
73 * Returns time in microseconds between selected and current timestamp
74 *
75 * @param array $start see details about array format at http://www.php.net/gettimeofday
76 * @return integer time in microseconds
77 * @access private
78 */
bbdea028 79function returnTime($start) {
80 $stop = gettimeofday();
81 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
82 return $timepassed;
83}
84
bbdea028 85$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
86$read = sqimap_mailbox_select($imapConnection, $mailbox);
87$start = gettimeofday();
6201339c 88$body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
bbdea028 89$message_body = '';
90$header = false;
91$mimepart = false;
92$bnd_end = false;
93$messageheader = true;
94$messageheaderstart=false;
95$boundaries = array();
96$entities = array();
97session_unregister("entities");
98$pre = '<b>';
99$end = '</b>';
100$entStr = '';
101$bla ='';
102$content = array ();
103$content_indx = -1;
104$contentset = false;
105
106$count=count($body);
107$body[$count-1] = substr($body[$count-1], -1);
108for ($i=1; $i < $count; $i++) {
109 $line = trim($body[$i]);
110 if ($line == '') {
796f91d9 111 $pre = '';
112 $end = '';
bbdea028 113 if ($bnd_end) {
796f91d9 114 $header = true;
115 $mimepart = false;
116 } else if ($messageheader) {
117 if ($header) {
118 $header=false;
91e0dccc 119 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
796f91d9 120 }
121 $mimepart = -$header;
122 $bnd_end = false;
123 if ($messageheaderstart) {
124 $messageheaderstart=false;
125 }
126 } else if ($messageheaderstart) {
127 $messageheader= false;
128 } else {
129 if ($header) {
130 $pre = '';
91e0dccc 131 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
796f91d9 132 }
133 $header = false;
134 $mimepart=true;
91e0dccc 135 }
796f91d9 136 $contentset = false;
137 $nameset = false;
bbdea028 138 } else {
139 if (!$header && $messageheader) {
796f91d9 140 $messageheaderstart=true;
141 if ($pre != '<b>') {
142 $pre = '<i><font color ="'.$color[1].'">';
143 $end = '</i></font>';
144 }
91e0dccc 145 }
796f91d9 146 if (!$messageheader && !$header ) {
147 $mimepart=true;
148 } else {
149 $mimepart=false;
150 }
151 $pre = '';
152 $end = '';
bbdea028 153 }
154 if ( ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
796f91d9 155 $bnd = $reg[1];
91e0dccc 156 $bndreg = $bnd;
796f91d9 157 $bndreg = str_replace("\\","\\\\",$bndreg);
158 $bndreg = str_replace("?","\\?",$bndreg);
159 $bndreg = str_replace("+","\\+",$bndreg);
160 $bndreg = str_replace(".","\\.",$bndreg);
161 $bndreg = str_replace("/","\\/",$bndreg);
162 $bndreg = str_replace("-","\\-",$bndreg);
163 $bndreg = str_replace("(","\\(",$bndreg);
164 $bndreg = str_replace(")","\\)",$bndreg);
bbdea028 165
796f91d9 166 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
167 $messageheader = false;
168 $messageheaderstart=false;
169 $mimepart=false;
170 if ($entStr=='') {
171 $entStr='0';
172 } else {
173 $entStr = CalcEntity("$entStr",1);
174 }
bbdea028 175 }
91e0dccc 176
bbdea028 177 if (($line != '' && $line{0} == '-' || $header) && isset($boundaries[0])) {
178 $cnt=count($boundaries)-1;
796f91d9 179 $bnd = $boundaries[$cnt]['bnd'];
180 $bndreg = $boundaries[$cnt]['bndreg'];
91e0dccc 181
796f91d9 182 $regstr = '/^--'."($bndreg)".".*".'/';
183 if (preg_match($regstr,$line,$reg) ) {
184 $bndlen = strlen($reg[1]);
91e0dccc 185 $bndend = false;
bbdea028 186 if (strlen($line) > ($bndlen + 3)) {
91e0dccc 187 if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
796f91d9 188 $bndend = true;
189 }
190 if ($bndend) {
191 $entStr = CalcEntity("$entStr",-1);
192 array_pop($boundaries);
193 $pre .= '<b><font color ="'.$color[2].'">';
194 $end .= '</font></b>';
195 $header = true;
196 $mimepart = false;
197 $bnd_end = true;
198 $encoding = '';
199 } else {
200 $header = true;
201 $bnd_end = false;
202 $entStr = CalcEntity("$entStr",0);
203 $content_indx++;
204 $content[$content_indx]=array();
205 $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
5d73c31d 206 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
207 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
796f91d9 208 $end .= '</font></b>'."\n";
209 $header = true;
210 $mimepart = false;
211 $encoding = '';
212 }
213 } else {
214 if ($header) {
215 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
216 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
217 $messageheader = true;
218 }
219 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
220 $contentset = true;
221 if ($reg[2] == 'image') {
222 $entities["$entStr"] = array();
223 $entities["$entStr"]['entity'] = $entStr;
224 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
91e0dccc 225 }
796f91d9 226 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
227 $name = htmlspecialchars($reg[2]);
228 $content[$content_indx]['name'] = decodeHeader($name);
229 $nameset = true;
230 if (isset($entities["$entStr"])) {
231 $entities["$entStr"]['name'] = urlEncode($reg[2]);
232 }
233 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
234 $encoding = $reg[2];
235 if (isset($entities["$entStr"])) {
236 $entities["$entStr"]['encoding']=$reg[2];
237 }
238 $content[$content_indx]['encoding'] = $encoding;
239 $mimeentity = '';
240 }
bbdea028 241
796f91d9 242 $pre .= '<b><font color='.$color[7].'">';
243 $end .= '</font></b>';
244 //$mimepart=false;
245 }
246 }
247 }
bbdea028 248/*
249 if ($mimepart) {
796f91d9 250 if (isset($entities["$entStr"])) {
251 if (isset($encoding) && $encoding == 'base64') {
252 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
253 $entities["$entStr"]['content'] .= $line;
254 }
bbdea028 255 }
796f91d9 256 }
bbdea028 257*/
258 $line = htmlspecialchars($line);
796f91d9 259 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
bbdea028 260}
796f91d9 261//echo returnTime($start).'<br />';
bbdea028 262$xtra = <<<ECHO
263
796f91d9 264<style>
bbdea028 265
266<!--
267.ent_body {
268 display:inline;
269}
270
271.header {
272 display:inline;
273}
274
275.entheader {
276 display:inline;
bbdea028 277 width:99%;
278}
279//-->
280
796f91d9 281</style>
bbdea028 282
283ECHO;
284
285displayHtmlHeader( _("Message Details"), $xtra, FALSE );
286/* body */
c157ce7e 287echo "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
288echo '<code>'."\n";
796f91d9 289echo '<font face="monospace">'."\n";
290echo '<br />'."\n";
c157ce7e 291
292
bbdea028 293//session_register("entities");
294//$keys = array_keys($entities);
295//$start = gettimeofday();
296//foreach ($keys as $key) {
297// if (isset($entities[$key])) {
298// if ($entities[$key]['encoding'] == 'base64') {
796f91d9 299// echo '<img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&amp;name='.$entities[$key]['name'].'"><br />';
bbdea028 300// }
301// }
302//}
303//session_unregister("entities");
304
305if (count($content) > 0) {
86c62251 306 echo '<h2>'._("Bodystructure")."</h2>\n\n";
796f91d9 307 echo '<table border="1" width="98%"><thead>'.
308 '<tr bgcolor="'.$color[7].'">'.
86c62251 309 '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
310 '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
311 '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
312 '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
796f91d9 313 '</tr>'.
314 '</thead><tbody>';
bbdea028 315 for ($i = 0; $i < count($content);$i++) {
796f91d9 316 echo '<tr><td>';
317 echo $content[$i]['ent'].'</td><td>';
318 if (isset($content[$i]['type'])) {
319 echo $content[$i]['type'];
320 } else echo 'TEXT/PLAIN';
321 echo '</td><td>';
322 if (isset($content[$i]['name'])) {
323 echo $content[$i]['name'];
324 } else echo '&nbsp;';
325 echo '</td><td>';
326 if (isset($content[$i]['encoding'])) {
327 echo $content[$i]['encoding'];
328 } else echo '&nbsp;';
329 echo '</td></tr>'."\n";
bbdea028 330 }
796f91d9 331 echo '</tbody></table><br />'."\n";
bbdea028 332}
86c62251 333echo '<h2>'._("RFC822 Message body")."</h2>\n\n";
bbdea028 334echo '<div><div class="header">'."\n\n";
335echo $message_body;
336echo '</div></div></font></code></body></html>';
91e0dccc 337?>