code cleanup:
[squirrelmail.git] / plugins / message_details / message_details_bottom.php
1 <?php
2 /**
3 * Message Details plugin - bottom frame with message structure and rfc822 body
4 *
5 * Plugin to view the RFC822 raw message output and the bodystructure of a message
6 *
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
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$
14 * @package plugins
15 * @subpackage message_details
16 */
17
18 /** @ignore */
19 define('SM_PATH','../../');
20
21 /* SquirrelMail required files. */
22 require_once(SM_PATH . 'include/validate.php');
23 require_once(SM_PATH . 'functions/imap.php');
24 require_once(SM_PATH . 'functions/mime.php');
25
26 global $color;
27
28 sqgetGlobalVar('passed_id', $passed_id, SQ_GET);
29 sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
30
31 sqgetGlobalVar('username', $username, SQ_SESSION);
32 sqgetGlobalVar('key', $key, SQ_COOKIE);
33 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
34
35 /**
36 * Calculates id of MIME entity
37 * @param string $entString
38 * @param integer $direction
39 * @return string
40 */
41 function CalcEntity($entString, $direction) {
42 $result = $entString;
43 if ($direction == -1) {
44 $pos = strrpos($entString,'.');
45 $result = substr($entString,0,$pos);
46 }
47
48 switch ($direction) {
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;
67 }
68 return ($result);
69 }
70
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 */
78 function returnTime($start) {
79 $stop = gettimeofday();
80 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
81 return $timepassed;
82 }
83
84 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
85 $read = sqimap_mailbox_select($imapConnection, $mailbox);
86 $start = gettimeofday();
87 $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
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();
96 session_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);
107 for ($i=1; $i < $count; $i++) {
108 $line = trim($body[$i]);
109 if ($line == '') {
110 $pre = '';
111 $end = '';
112 if ($bnd_end) {
113 $header = true;
114 $mimepart = false;
115 } else if ($messageheader) {
116 if ($header) {
117 $header=false;
118 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
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 = '';
130 $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
131 }
132 $header = false;
133 $mimepart=true;
134 }
135 $contentset = false;
136 $nameset = false;
137 } else {
138 if (!$header && $messageheader) {
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 = '';
152 }
153 if ( ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
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);
164
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 }
174 }
175
176 if (($line != '' && $line{0} == '-' || $header) && isset($boundaries[0])) {
177 $cnt=count($boundaries)-1;
178 $bnd = $boundaries[$cnt]['bnd'];
179 $bndreg = $boundaries[$cnt]['bndreg'];
180
181 $regstr = '/^--'."($bndreg)".".*".'/';
182 if (preg_match($regstr,$line,$reg) ) {
183 $bndlen = strlen($reg[1]);
184 $bndend = false;
185 if (strlen($line) > ($bndlen + 3)) {
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>';
205 $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'.
206 $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
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 }
240
241 $pre .= '<b><font color='.$color[7].'">';
242 $end .= '</font></b>';
243 //$mimepart=false;
244 }
245 }
246 }
247 /*
248 if ($mimepart) {
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 }
254 }
255 }
256 */
257 $line = htmlspecialchars($line);
258 $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
259 }
260 //echo returnTime($start).'<br />';
261 $xtra = <<<ECHO
262
263 <style>
264
265 <!--
266 .ent_body {
267 display:inline;
268 }
269
270 .header {
271 display:inline;
272 }
273
274 .entheader {
275 display:inline;
276 width:99%;
277 }
278 //-->
279
280 </style>
281
282 ECHO;
283
284 displayHtmlHeader( _("Message Details"), $xtra, FALSE );
285 /* body */
286 echo "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
287 echo '<code>'."\n";
288 echo '<font face="monospace">'."\n";
289 echo '<br />'."\n";
290
291
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') {
298 // echo '<img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&amp;name='.$entities[$key]['name'].'"><br />';
299 // }
300 // }
301 //}
302 //session_unregister("entities");
303
304 if (count($content) > 0) {
305 echo '<h2>'._("Bodystructure")."</h2>\n\n";
306 echo '<table border="1" width="98%"><thead>'.
307 '<tr bgcolor="'.$color[7].'">'.
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>'.
312 '</tr>'.
313 '</thead><tbody>';
314 for ($i = 0; $i < count($content);$i++) {
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";
329 }
330 echo '</tbody></table><br />'."\n";
331 }
332 echo '<h2>'._("RFC822 Message body")."</h2>\n\n";
333 echo '<div><div class="header">'."\n\n";
334 echo $message_body;
335 echo '</div></div></font></code></body></html>';
336 ?>