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