Some code cleanups to read_body.php
[squirrelmail.git] / plugins / message_details / message_details_bottom.php
1 <?php
2
3 /** Message Source
4 *
5 * Plugin to view the RFC822 raw message output and the bodystructure of a message
6 *
7 * Copyright (c) 2002 Marc Groot Koerkamp, The Netherlands
8 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 *
10 * $Id$
11 **/
12
13 chdir('..');
14 define('SM_PATH','../');
15
16 /* SquirrelMail required files. */
17 require_once(SM_PATH . 'include/validate.php');
18 require_once(SM_PATH . 'functions/imap.php');
19 require_once(SM_PATH . 'functions/mime.php');
20 require_once(SM_PATH . 'config/config.php');
21 require_once(SM_PATH . 'functions/prefs.php');
22
23 global $color, $uid_support;
24
25 function CalcEntity($entString, $direction) {
26 $result = $entString;
27 if ($direction == -1) {
28 $pos = strrpos($entString,'.');
29 $result = substr($entString,0,$pos);
30 }
31
32 switch ($direction) {
33 case 0:
34 $pos = strrpos($entString,'.');
35 if ($pos === false) {
36 $entString++;
37 $result= $entString;
38 }
39 else {
40 $level = substr($entString,0,$pos);
41 $sublevel = substr($entString,$pos+1);
42 $sublevel++;
43 $result = "$level".'.'."$sublevel";
44 }
45 break;
46 case 1:
47 $result = "$entString".".0";
48 break;
49 default:
50 break;
51 }
52 return ($result);
53 }
54
55 function returnTime($start) {
56 $stop = gettimeofday();
57 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
58 return $timepassed;
59 }
60
61 function GetMimeProperties($header) {
62 }
63
64 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
65 $read = sqimap_mailbox_select($imapConnection, $mailbox);
66 $start = gettimeofday();
67 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, $uid_support);
68 $message_body = '';
69 $header = false;
70 $mimepart = false;
71 $bnd_end = false;
72 $messageheader = true;
73 $messageheaderstart=false;
74 $boundaries = array();
75 $entities = array();
76 session_unregister("entities");
77 $pre = '<b>';
78 $end = '</b>';
79 $entStr = '';
80 $bla ='';
81 $content = array ();
82 $content_indx = -1;
83 $contentset = false;
84
85 $count=count($body);
86 $body[$count-1] = substr($body[$count-1], -1);
87 for ($i=1; $i < $count; $i++) {
88 $line = trim($body[$i]);
89 if ($line == '') {
90 $pre = '';
91 $end = '';
92 if ($bnd_end) {
93 $header = true;
94 $mimepart = false;
95 } else if ($messageheader) {
96 if ($header) {
97 $header=false;
98 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" ID="'.$entStr.'B">'."\n \n";
99 }
100 $mimepart = -$header;
101 $bnd_end = false;
102 if ($messageheaderstart) {
103 $messageheaderstart=false;
104 }
105 } else if ($messageheaderstart) {
106 $messageheader= false;
107 } else {
108 if ($header) {
109 $pre = '';
110 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" ID="'.$entStr.'B">'."\n \n";
111 }
112 $header = false;
113 $mimepart=true;
114 }
115 $contentset = false;
116 $nameset = false;
117 } else {
118 if (!$header && $messageheader) {
119 $messageheaderstart=true;
120 if ($pre != '<b>') {
121 $pre = '<i><font color ='."$color[1]>";
122 $end = '</i></font>';
123 }
124 }
125 if (!$messageheader && !$header ) {
126 $mimepart=true;
127 } else {
128 $mimepart=false;
129 }
130 $pre = '';
131 $end = '';
132 }
133 if ( ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
134 $bnd = $reg[1];
135 $bndreg = $bnd;
136 $bndreg = str_replace("\\","\\\\",$bndreg);
137 $bndreg = str_replace("?","\\?",$bndreg);
138 $bndreg = str_replace("+","\\+",$bndreg);
139 $bndreg = str_replace(".","\\.",$bndreg);
140 $bndreg = str_replace("/","\\/",$bndreg);
141 $bndreg = str_replace("-","\\-",$bndreg);
142 $bndreg = str_replace("(","\\(",$bndreg);
143 $bndreg = str_replace(")","\\)",$bndreg);
144
145 $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
146 $messageheader = false;
147 $messageheaderstart=false;
148 $mimepart=false;
149 if ($entStr=='') {
150 $entStr='0';
151 } else {
152 $entStr = CalcEntity("$entStr",1);
153 }
154 }
155
156 if (($line != '' && $line{0} == '-' || $header) && isset($boundaries[0])) {
157 $cnt=count($boundaries)-1;
158 $bnd = $boundaries[$cnt]['bnd'];
159 $bndreg = $boundaries[$cnt]['bndreg'];
160
161 $regstr = '/^--'."($bndreg)".".*".'/';
162 if (preg_match($regstr,$line,$reg) ) {
163 $bndlen = strlen($reg[1]);
164 $bndend = false;
165 if (strlen($line) > ($bndlen + 3)) {
166 if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
167 $bndend = true;
168 }
169 if ($bndend) {
170 $entStr = CalcEntity("$entStr",-1);
171 array_pop($boundaries);
172 $pre .= '<b><font color ='."$color[2]>";
173 $end .= '</font></b>';
174 $header = true;
175 $mimepart = false;
176 $bnd_end = true;
177 $encoding = '';
178 } else {
179 $header = true;
180 $bnd_end = false;
181 $entStr = CalcEntity("$entStr",0);
182 $content_indx++;
183 $content[$content_indx]=array();
184 $content[$content_indx]['ent'] = '<A HREF="#'."$entStr \">$entStr".'</a>';
185 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" ID="'.$entStr.'H"><a name="'."$entStr".'"><b><font color ='."$color[2]>";
186 $end .= '</font></b>'."\n";
187 $header = true;
188 $mimepart = false;
189 $encoding = '';
190 }
191 } else {
192 if ($header) {
193 if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
194 if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
195 $messageheader = true;
196 }
197 $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
198 $contentset = true;
199 if ($reg[2] == 'image') {
200 $entities["$entStr"] = array();
201 $entities["$entStr"]['entity'] = $entStr;
202 $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
203 }
204 } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
205 $name = htmlspecialchars($reg[2]);
206 $content[$content_indx]['name'] = decodeHeader($name);
207 $nameset = true;
208 if (isset($entities["$entStr"])) {
209 $entities["$entStr"]['name'] = urlEncode($reg[2]);
210 }
211 } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
212 $encoding = $reg[2];
213 if (isset($entities["$entStr"])) {
214 $entities["$entStr"]['encoding']=$reg[2];
215 }
216 $content[$content_indx]['encoding'] = $encoding;
217 $mimeentity = '';
218 }
219
220 $pre .= '<b><font color ='."$color[7]>";
221 $end .= '</font></b>';
222 //$mimepart=false;
223 }
224 }
225 }
226 /*
227 if ($mimepart) {
228 if (isset($entities["$entStr"])) {
229 if (isset($encoding) && $encoding == 'base64') {
230 if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = '';
231 $entities["$entStr"]['content'] .= $line;
232 }
233 }
234 }
235 */
236 $line = htmlspecialchars($line);
237 $message_body .= "$pre"."$line"."$end".'<BR>'."\r\n";
238 }
239 //echo returnTime($start).'<BR>';
240 $xtra = <<<ECHO
241
242 <STYLE>
243
244 <!--
245 .ent_body {
246 display:inline;
247 }
248
249 .header {
250 display:inline;
251 }
252
253 .entheader {
254 display:inline;
255 margin-bottom:1em;
256 padding-bottom:1em;
257 border-style: solid none solid none;
258 border-width:0.1em;
259 width:99%;
260 }
261 //-->
262
263 </STYLE>
264
265 ECHO;
266
267 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
268 /* body */
269 echo '<body><code>'."\n";
270 echo '<font face = "monospace">'."\n";
271 echo '<BR>'."\n";
272 //session_register("entities");
273 //$keys = array_keys($entities);
274 //$start = gettimeofday();
275 //foreach ($keys as $key) {
276 // if (isset($entities[$key])) {
277 // if ($entities[$key]['encoding'] == 'base64') {
278 // echo '<img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&amp;name='.$entities[$key]['name'].'"><br>';
279 // }
280 // }
281 //}
282 //session_unregister("entities");
283
284 if (count($content) > 0) {
285 echo '<h2> Bodystructure </h2>'."\n\n";
286 echo '<table border=1 width="98%"><thead>'.
287 '<tr bgcolor="'."$color[7]".'">'.
288 '<td><b><font color="'."$color[5]".'">Entity</font></b></td>'.
289 '<td><b><font color="'."$color[5]".'">Content-Type</font></b></td>'.
290 '<td><b><font color="'."$color[5]".'">Name</font></b></td>'.
291 '<td><b><font color="'."$color[5]".'">Encoding</font></b></td>'.
292
293 '</tr>'.
294 '</thead><tbody>';
295 for ($i = 0; $i < count($content);$i++) {
296 echo '<tr><td>';
297 echo $content[$i]['ent'].'</td><td>';
298 if (isset($content[$i]['type'])) {
299 echo $content[$i]['type'];
300 } else echo 'TEXT/PLAIN';
301 echo '</td><td>';
302 if (isset($content[$i]['name'])) {
303 echo $content[$i]['name'];
304 } else echo '&nbsp';
305 echo '</td><td>';
306 if (isset($content[$i]['encoding'])) {
307 echo $content[$i]['encoding'];
308 } else echo '&nbsp';
309 echo '</td></tr>'."\n";
310 }
311 echo '</tbody></table><br>'."\n";
312 }
313 echo '<h2> RFC822 Message body </h2>'."\n\n";
314 echo '<div><div class="header">'."\n\n";
315 echo $message_body;
316 echo '</div></div></font></code></body></html>';
317 ?>