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