message_details_bottom.php 8.7 KB

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