message_details_bottom.php 11 KB

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