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