message_details_bottom.php 11 KB

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