| 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 © 2002 Marc Groot Koerkamp, The Netherlands |
| 11 | * @copyright Copyright © 2004-2005 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 | if (!defined('SM_PATH')) |
| 20 | define('SM_PATH','../../'); |
| 21 | |
| 22 | /* SquirrelMail required files. */ |
| 23 | require_once(SM_PATH . 'include/validate.php'); |
| 24 | require_once(SM_PATH . 'functions/imap.php'); |
| 25 | require_once(SM_PATH . 'functions/mime.php'); |
| 26 | |
| 27 | sqgetGlobalVar('get_message_details', $md_action, SQ_GET); |
| 28 | |
| 29 | /** |
| 30 | * Controls display of 8bit symbols in message source |
| 31 | * @global boolean $msgd_8bit_in_hex; |
| 32 | */ |
| 33 | global $msgd_8bit_in_hex; |
| 34 | $msgd_8bit_in_hex=false; |
| 35 | |
| 36 | if (!empty($md_action)) |
| 37 | { |
| 38 | sqgetGlobalVar('passed_id', $passed_id, SQ_GET); |
| 39 | sqgetGlobalVar('mailbox', $mailbox, SQ_GET); |
| 40 | echo get_message_details($mailbox, $passed_id); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | // ---------- function definitions ---------- |
| 45 | |
| 46 | /** |
| 47 | * Converts 8bit string to hex |
| 48 | * |
| 49 | * Replaces 8bit symbols with their hex strings, |
| 50 | * encloses them in curly brackets and uses different color. |
| 51 | * @param string $string text |
| 52 | * @return string |
| 53 | * @since 1.5.1 |
| 54 | */ |
| 55 | function msgd_convert_to_hex($string) { |
| 56 | global $color; |
| 57 | return preg_replace("/([\200-\377])/e","'<font color=\"$color[2]\">{'.dechex(ord('\\1')).'}</font>'",$string); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Calculates id of MIME entity |
| 62 | * @param string $entString |
| 63 | * @param integer $direction |
| 64 | * @return string |
| 65 | * @access private |
| 66 | */ |
| 67 | function CalcEntity($entString, $direction) { |
| 68 | $result = $entString; |
| 69 | if ($direction == -1) { |
| 70 | $pos = strrpos($entString,'.'); |
| 71 | $result = substr($entString,0,$pos); |
| 72 | } |
| 73 | |
| 74 | switch ($direction) { |
| 75 | case 0: |
| 76 | $pos = strrpos($entString,'.'); |
| 77 | if ($pos === false) { |
| 78 | $entString++; |
| 79 | $result= $entString; |
| 80 | } |
| 81 | else { |
| 82 | $level = substr($entString,0,$pos); |
| 83 | $sublevel = substr($entString,$pos+1); |
| 84 | $sublevel++; |
| 85 | $result = "$level".'.'."$sublevel"; |
| 86 | } |
| 87 | break; |
| 88 | case 1: |
| 89 | $result = "$entString".".0"; |
| 90 | break; |
| 91 | default: |
| 92 | break; |
| 93 | } |
| 94 | return ($result); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * Returns time in microseconds between selected and current timestamp |
| 101 | * |
| 102 | * @param array $start see details about array format at http://www.php.net/gettimeofday |
| 103 | * @return integer time in microseconds |
| 104 | * @access private |
| 105 | */ |
| 106 | function returnTime($start) { |
| 107 | $stop = gettimeofday(); |
| 108 | $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec']; |
| 109 | return $timepassed; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Returns actual message details |
| 116 | * @param string $mailbox |
| 117 | * @param string $passed_id |
| 118 | * @param boolean $stripHTML If TRUE, only plain text is returned, |
| 119 | * default is FALSE, wherein output contains |
| 120 | * pretty-HTMLification of message body |
| 121 | * @return string The formatted message details |
| 122 | * @access public |
| 123 | */ |
| 124 | function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) { |
| 125 | |
| 126 | global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex; |
| 127 | |
| 128 | $returnValue = ''; |
| 129 | |
| 130 | sqgetGlobalVar('username', $username, SQ_SESSION); |
| 131 | sqgetGlobalVar('key', $key, SQ_COOKIE); |
| 132 | |
| 133 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
| 134 | $read = sqimap_mailbox_select($imapConnection, $mailbox); |
| 135 | $start = gettimeofday(); |
| 136 | $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE); |
| 137 | $message_body = ''; |
| 138 | $header = false; |
| 139 | $mimepart = false; |
| 140 | $bnd_end = false; |
| 141 | $messageheader = true; |
| 142 | $messageheaderstart=false; |
| 143 | $boundaries = array(); |
| 144 | $entities = array(); |
| 145 | session_unregister("entities"); |
| 146 | $pre = '<b>'; |
| 147 | $end = '</b>'; |
| 148 | $entStr = ''; |
| 149 | $bla =''; |
| 150 | $content = array (); |
| 151 | $content_indx = -1; |
| 152 | $contentset = false; |
| 153 | |
| 154 | $count=count($body); |
| 155 | $body[$count-1] = substr($body[$count-1], -1); |
| 156 | for ($i=1; $i < $count; $i++) { |
| 157 | $line = trim($body[$i]); |
| 158 | if ($line == '') { |
| 159 | $pre = ''; |
| 160 | $end = ''; |
| 161 | if ($bnd_end) { |
| 162 | $header = true; |
| 163 | $mimepart = false; |
| 164 | } else if ($messageheader) { |
| 165 | if ($header) { |
| 166 | $header=false; |
| 167 | $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n"; |
| 168 | } |
| 169 | $mimepart = -$header; |
| 170 | $bnd_end = false; |
| 171 | if ($messageheaderstart) { |
| 172 | $messageheaderstart=false; |
| 173 | } |
| 174 | } else if ($messageheaderstart) { |
| 175 | $messageheader= false; |
| 176 | } else { |
| 177 | if ($header) { |
| 178 | $pre = ''; |
| 179 | $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n"; |
| 180 | } |
| 181 | $header = false; |
| 182 | $mimepart=true; |
| 183 | } |
| 184 | $contentset = false; |
| 185 | $nameset = false; |
| 186 | } else { |
| 187 | if (!$header && $messageheader) { |
| 188 | $messageheaderstart=true; |
| 189 | if ($pre != '<b>') { |
| 190 | $pre = '<i><font color ="'.$color[1].'">'; |
| 191 | $end = '</i></font>'; |
| 192 | } |
| 193 | } |
| 194 | if (!$messageheader && !$header ) { |
| 195 | $mimepart=true; |
| 196 | } else { |
| 197 | $mimepart=false; |
| 198 | } |
| 199 | $pre = ''; |
| 200 | $end = ''; |
| 201 | } |
| 202 | if ( ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) { |
| 203 | $bnd = $reg[1]; |
| 204 | $bndreg = $bnd; |
| 205 | $bndreg = str_replace("\\","\\\\",$bndreg); |
| 206 | $bndreg = str_replace("?","\\?",$bndreg); |
| 207 | $bndreg = str_replace("+","\\+",$bndreg); |
| 208 | $bndreg = str_replace(".","\\.",$bndreg); |
| 209 | $bndreg = str_replace("/","\\/",$bndreg); |
| 210 | $bndreg = str_replace("-","\\-",$bndreg); |
| 211 | $bndreg = str_replace("(","\\(",$bndreg); |
| 212 | $bndreg = str_replace(")","\\)",$bndreg); |
| 213 | |
| 214 | $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg); |
| 215 | $messageheader = false; |
| 216 | $messageheaderstart=false; |
| 217 | $mimepart=false; |
| 218 | if ($entStr=='') { |
| 219 | $entStr='0'; |
| 220 | } else { |
| 221 | $entStr = CalcEntity("$entStr",1); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (($line != '' && $line{0} == '-' || $header) && isset($boundaries[0])) { |
| 226 | $cnt=count($boundaries)-1; |
| 227 | $bnd = $boundaries[$cnt]['bnd']; |
| 228 | $bndreg = $boundaries[$cnt]['bndreg']; |
| 229 | |
| 230 | $regstr = '/^--'."($bndreg)".".*".'/'; |
| 231 | if (preg_match($regstr,$line,$reg) ) { |
| 232 | $bndlen = strlen($reg[1]); |
| 233 | $bndend = false; |
| 234 | if (strlen($line) > ($bndlen + 3)) { |
| 235 | if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-') |
| 236 | $bndend = true; |
| 237 | } |
| 238 | if ($bndend) { |
| 239 | $entStr = CalcEntity("$entStr",-1); |
| 240 | array_pop($boundaries); |
| 241 | $pre .= '<b><font color ="'.$color[2].'">'; |
| 242 | $end .= '</font></b>'; |
| 243 | $header = true; |
| 244 | $mimepart = false; |
| 245 | $bnd_end = true; |
| 246 | $encoding = ''; |
| 247 | } else { |
| 248 | $header = true; |
| 249 | $bnd_end = false; |
| 250 | $entStr = CalcEntity("$entStr",0); |
| 251 | $content_indx++; |
| 252 | $content[$content_indx]=array(); |
| 253 | $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>'; |
| 254 | $pre .= "\n \n".'</div>'."\n \n".'<div class="entheader" id="'. |
| 255 | $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">'; |
| 256 | $end .= '</font></b>'."\n"; |
| 257 | $header = true; |
| 258 | $mimepart = false; |
| 259 | $encoding = ''; |
| 260 | } |
| 261 | } else { |
| 262 | if ($header) { |
| 263 | if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) { |
| 264 | if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') { |
| 265 | $messageheader = true; |
| 266 | } |
| 267 | $content[$content_indx]['type'] = "$reg[2]/$reg[3]"; |
| 268 | $contentset = true; |
| 269 | if ($reg[2] == 'image') { |
| 270 | $entities["$entStr"] = array(); |
| 271 | $entities["$entStr"]['entity'] = $entStr; |
| 272 | $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3]; |
| 273 | } |
| 274 | } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) { |
| 275 | $name = htmlspecialchars($reg[2]); |
| 276 | $content[$content_indx]['name'] = decodeHeader($name); |
| 277 | $nameset = true; |
| 278 | if (isset($entities["$entStr"])) { |
| 279 | $entities["$entStr"]['name'] = urlEncode($reg[2]); |
| 280 | } |
| 281 | } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) { |
| 282 | $encoding = $reg[2]; |
| 283 | if (isset($entities["$entStr"])) { |
| 284 | $entities["$entStr"]['encoding']=$reg[2]; |
| 285 | } |
| 286 | $content[$content_indx]['encoding'] = $encoding; |
| 287 | $mimeentity = ''; |
| 288 | } |
| 289 | |
| 290 | $pre .= '<b><font color='.$color[7].'">'; |
| 291 | $end .= '</font></b>'; |
| 292 | //$mimepart=false; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | /* |
| 297 | if ($mimepart) { |
| 298 | if (isset($entities["$entStr"])) { |
| 299 | if (isset($encoding) && $encoding == 'base64') { |
| 300 | if (!isset( $entities["$entStr"]['content'])) $entities[$entStr]['content'] = ''; |
| 301 | $entities["$entStr"]['content'] .= $line; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | */ |
| 306 | if ($stripHTML) { |
| 307 | $message_body .= $line . "\r\n"; |
| 308 | } else { |
| 309 | $line = htmlspecialchars($line); |
| 310 | if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line); |
| 311 | $message_body .= "$pre"."$line"."$end".'<br />'."\r\n"; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | //$returnValue .= returnTime($start).'<br />'; |
| 316 | $xtra = <<<ECHO |
| 317 | |
| 318 | <style type="text/css"> |
| 319 | |
| 320 | <!-- |
| 321 | .ent_body { |
| 322 | display:inline; |
| 323 | } |
| 324 | |
| 325 | .header { |
| 326 | display:inline; |
| 327 | } |
| 328 | |
| 329 | .entheader { |
| 330 | display:inline; |
| 331 | width:99%; |
| 332 | } |
| 333 | //--> |
| 334 | |
| 335 | </style> |
| 336 | |
| 337 | ECHO; |
| 338 | |
| 339 | if (!$stripHTML) { |
| 340 | ob_start(); |
| 341 | displayHtmlHeader( _("Message Details"), $xtra, FALSE ); |
| 342 | $returnValue .= ob_get_contents(); |
| 343 | ob_end_clean(); |
| 344 | } |
| 345 | |
| 346 | /* body */ |
| 347 | if (!$stripHTML) { |
| 348 | $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n"; |
| 349 | $returnValue .= '<code>'."\n"; |
| 350 | $returnValue .= '<font face="monospace">'."\n"; |
| 351 | $returnValue .= '<br />'."\n"; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | //session_register("entities"); |
| 356 | //$keys = array_keys($entities); |
| 357 | //$start = gettimeofday(); |
| 358 | //foreach ($keys as $key) { |
| 359 | // if (isset($entities[$key])) { |
| 360 | // if ($entities[$key]['encoding'] == 'base64') { |
| 361 | // if (!$stripHTML) { |
| 362 | // $returnValue .= 'img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&name='.$entities[$key]['name'].'"><br />'; |
| 363 | // } |
| 364 | // } |
| 365 | // } |
| 366 | //} |
| 367 | //session_unregister("entities"); |
| 368 | |
| 369 | if (count($content) > 0 && !$stripHTML) { |
| 370 | $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n"; |
| 371 | $returnValue .= '<table border="1" width="98%"><thead>'. |
| 372 | '<tr bgcolor="'.$color[7].'">'. |
| 373 | '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'. |
| 374 | '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'. |
| 375 | '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'. |
| 376 | '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'. |
| 377 | '</tr>'. |
| 378 | '</thead><tbody>'; |
| 379 | for ($i = 0; $i < count($content);$i++) { |
| 380 | $returnValue .= '<tr><td>'; |
| 381 | $returnValue .= $content[$i]['ent'].'</td><td>'; |
| 382 | if (isset($content[$i]['type'])) { |
| 383 | $returnValue .= $content[$i]['type']; |
| 384 | } else $returnValue .= 'TEXT/PLAIN'; |
| 385 | $returnValue .= '</td><td>'; |
| 386 | if (isset($content[$i]['name'])) { |
| 387 | $returnValue .= $content[$i]['name']; |
| 388 | } else $returnValue .= ' '; |
| 389 | $returnValue .= '</td><td>'; |
| 390 | if (isset($content[$i]['encoding'])) { |
| 391 | $returnValue .= $content[$i]['encoding']; |
| 392 | } else $returnValue .= ' '; |
| 393 | $returnValue .= '</td></tr>'."\n"; |
| 394 | } |
| 395 | $returnValue .= '</tbody></table><br />'."\n"; |
| 396 | } |
| 397 | |
| 398 | if (!$stripHTML) { |
| 399 | $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n"; |
| 400 | $returnValue .= '<div><div class="header">'."\n\n"; |
| 401 | } |
| 402 | |
| 403 | $returnValue .= $message_body; |
| 404 | |
| 405 | if (!$stripHTML) |
| 406 | $returnValue .= '</div></div></font></code></body></html>'; |
| 407 | |
| 408 | return $returnValue; |
| 409 | |
| 410 | } |
| 411 | |
| 412 | ?> |