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