message_details_bottom.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. * @author Marc Groot Koerkamp
  8. * @copyright &copy; 2002 Marc Groot Koerkamp, The Netherlands
  9. * @copyright &copy; 2002-2006 The SquirrelMail Project Team
  10. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11. * @version $Id$
  12. * @package plugins
  13. * @subpackage message_details
  14. */
  15. /**
  16. * Include the SquirrelMail initialization file.
  17. */
  18. require('../../include/init.php');
  19. require(SM_PATH . 'functions/imap_general.php');
  20. require(SM_PATH . 'functions/imap_messages.php');
  21. require(SM_PATH . 'functions/mime.php');
  22. sqgetGlobalVar('get_message_details', $md_action, SQ_GET);
  23. /**
  24. * Controls display of 8bit symbols in message source
  25. * @global boolean $msgd_8bit_in_hex;
  26. */
  27. global $msgd_8bit_in_hex;
  28. $msgd_8bit_in_hex=false;
  29. if (!empty($md_action)) {
  30. sqgetGlobalVar('passed_id', $passed_id, SQ_GET);
  31. sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
  32. /*
  33. * add third function argument, if you want to see
  34. * message source without formating
  35. */
  36. echo get_message_details($mailbox, $passed_id);
  37. }
  38. // ---------- function definitions ----------
  39. /**
  40. * Converts 8bit string to hex
  41. *
  42. * Replaces 8bit symbols with their hex strings,
  43. * encloses them in curly brackets and uses different color.
  44. * @param string $string text
  45. * @return string
  46. * @since 1.5.1
  47. */
  48. function msgd_convert_to_hex($string) {
  49. global $color;
  50. return preg_replace("/([\200-\377])/e","'<font color=\"$color[2]\">{'.dechex(ord('\\1')).'}</font>'",$string);
  51. }
  52. /**
  53. * Calculates id of MIME entity
  54. * @param string $entString
  55. * @param integer $direction
  56. * @return string
  57. * @access private
  58. */
  59. function CalcEntity($entString, $direction) {
  60. $result = $entString;
  61. if ($direction == -1) {
  62. $pos = strrpos($entString,'.');
  63. $result = substr($entString,0,$pos);
  64. }
  65. switch ($direction) {
  66. case 0:
  67. $pos = strrpos($entString,'.');
  68. if ($pos === false) {
  69. $entString++;
  70. $result= $entString;
  71. }
  72. else {
  73. $level = substr($entString,0,$pos);
  74. $sublevel = substr($entString,$pos+1);
  75. $sublevel++;
  76. $result = "$level".'.'."$sublevel";
  77. }
  78. break;
  79. case 1:
  80. $result = "$entString".".0";
  81. break;
  82. default:
  83. break;
  84. }
  85. return ($result);
  86. }
  87. /**
  88. * Returns actual message details
  89. * @param string $mailbox
  90. * @param string $passed_id
  91. * @param boolean $stripHTML If TRUE, only plain text is returned,
  92. * default is FALSE, wherein output contains
  93. * pretty-HTMLification of message body
  94. * @return string The formatted message details
  95. * @access public
  96. */
  97. function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
  98. global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex, $username;
  99. $returnValue = '';
  100. $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
  101. $read = sqimap_mailbox_select($imapConnection, $mailbox);
  102. $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
  103. $message_body = '';
  104. $header = false;
  105. $mimepart = false;
  106. $bnd_end = false;
  107. $messageheader = true;
  108. $messageheaderstart=false;
  109. $boundaries = array();
  110. $entities = array();
  111. session_unregister("entities");
  112. $pre = '<b>';
  113. $end = '</b>';
  114. $entStr = '';
  115. $bla ='';
  116. $content = array ();
  117. $content_indx = -1;
  118. $contentset = false;
  119. $count=count($body);
  120. $body[$count-1] = substr($body[$count-1], -1);
  121. for ($i=1; $i < $count; $i++) {
  122. $line = rtrim($body[$i]);
  123. if ($line == '') {
  124. $pre = '';
  125. $end = '';
  126. if ($bnd_end) {
  127. $header = true;
  128. $mimepart = false;
  129. } else if ($messageheader) {
  130. if ($header) {
  131. $header=false;
  132. $end = '</div><div class="ent_body" id="'.$entStr.'B">';
  133. }
  134. $mimepart = -$header;
  135. $bnd_end = false;
  136. if ($messageheaderstart) {
  137. $messageheaderstart=false;
  138. }
  139. } else if ($messageheaderstart) {
  140. $messageheader= false;
  141. } else {
  142. if ($header) {
  143. $pre = '';
  144. $end = '</div><div class="ent_body" id="'.$entStr.'B">';
  145. }
  146. $header = false;
  147. $mimepart=true;
  148. }
  149. $contentset = false;
  150. $nameset = false;
  151. } else {
  152. if (!$header && $messageheader) {
  153. $messageheaderstart=true;
  154. if ($pre != '<b>') {
  155. $pre = '<i><font color ="'.$color[1].'">';
  156. $end = '</i></font>';
  157. }
  158. }
  159. if (!$messageheader && !$header ) {
  160. $mimepart=true;
  161. } else {
  162. $mimepart=false;
  163. }
  164. $pre = '';
  165. $end = '';
  166. }
  167. if ( ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
  168. $bnd = $reg[1];
  169. $bndreg = $bnd;
  170. $bndreg = str_replace("\\","\\\\",$bndreg);
  171. $bndreg = str_replace("?","\\?",$bndreg);
  172. $bndreg = str_replace("+","\\+",$bndreg);
  173. $bndreg = str_replace(".","\\.",$bndreg);
  174. $bndreg = str_replace("/","\\/",$bndreg);
  175. $bndreg = str_replace("-","\\-",$bndreg);
  176. $bndreg = str_replace("(","\\(",$bndreg);
  177. $bndreg = str_replace(")","\\)",$bndreg);
  178. $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
  179. $messageheader = false;
  180. $messageheaderstart=false;
  181. $mimepart=false;
  182. if ($entStr=='') {
  183. $entStr='0';
  184. } else {
  185. $entStr = CalcEntity("$entStr",1);
  186. }
  187. }
  188. if (($line != '' && $line{0} == '-' || $header) && isset($boundaries[0])) {
  189. $cnt=count($boundaries)-1;
  190. $bnd = $boundaries[$cnt]['bnd'];
  191. $bndreg = $boundaries[$cnt]['bndreg'];
  192. $regstr = '/^--'."($bndreg)".".*".'/';
  193. if (preg_match($regstr,$line,$reg) ) {
  194. $bndlen = strlen($reg[1]);
  195. $bndend = false;
  196. if (strlen($line) > ($bndlen + 3)) {
  197. if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
  198. $bndend = true;
  199. }
  200. if ($bndend) {
  201. $entStr = CalcEntity("$entStr",-1);
  202. array_pop($boundaries);
  203. $pre .= '<b><font color ="'.$color[2].'">';
  204. $end .= '</font></b>';
  205. $header = true;
  206. $mimepart = false;
  207. $bnd_end = true;
  208. $encoding = '';
  209. } else {
  210. $header = true;
  211. $bnd_end = false;
  212. $entStr = CalcEntity("$entStr",0);
  213. $content_indx++;
  214. $content[$content_indx]=array();
  215. $content[$content_indx]['ent'] = '<a href="#'."$entStr \">$entStr".'</a>';
  216. $pre .= '</div><div class="entheader" id="'.
  217. $entStr.'H"><a name="'."$entStr".'"><b><font color="'.$color[2].'">';
  218. $end .= '</font></b>';
  219. $header = true;
  220. $mimepart = false;
  221. $encoding = '';
  222. }
  223. } else {
  224. if ($header) {
  225. if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
  226. if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
  227. $messageheader = true;
  228. }
  229. $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
  230. $contentset = true;
  231. if ($reg[2] == 'image') {
  232. $entities["$entStr"] = array();
  233. $entities["$entStr"]['entity'] = $entStr;
  234. $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
  235. }
  236. } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
  237. $name = htmlspecialchars($reg[2]);
  238. $content[$content_indx]['name'] = decodeHeader($name);
  239. $nameset = true;
  240. if (isset($entities["$entStr"])) {
  241. $entities["$entStr"]['name'] = urlEncode($reg[2]);
  242. }
  243. } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
  244. $encoding = $reg[2];
  245. if (isset($entities["$entStr"])) {
  246. $entities["$entStr"]['encoding']=$reg[2];
  247. }
  248. $content[$content_indx]['encoding'] = $encoding;
  249. $mimeentity = '';
  250. }
  251. $pre .= '<b><font color='.$color[7].'">';
  252. $end .= '</font></b>';
  253. //$mimepart=false;
  254. }
  255. }
  256. }
  257. if ($stripHTML) {
  258. $message_body .= $line . "\r\n";
  259. } else {
  260. $line = htmlspecialchars($line);
  261. if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
  262. $message_body .= "$pre"."$line"."$end"."\r\n";
  263. }
  264. }
  265. //$returnValue .= returnTime($start).'<br />';
  266. $xtra = <<<ECHO
  267. <style type="text/css">
  268. <!--
  269. .ent_body {
  270. display:inline;
  271. }
  272. .header {
  273. display:inline;
  274. }
  275. .entheader {
  276. display:inline;
  277. width:99%;
  278. }
  279. //-->
  280. </style>
  281. ECHO;
  282. if (!$stripHTML) {
  283. ob_start();
  284. displayHtmlHeader( _("Message Details"), $xtra, FALSE );
  285. $returnValue .= ob_get_contents();
  286. ob_end_clean();
  287. }
  288. /* body */
  289. if (!$stripHTML) {
  290. $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
  291. $returnValue .= '<font face="monospace">'."\n";
  292. $returnValue .= '<br />'."\n";
  293. }
  294. if (count($content) > 0 && !$stripHTML) {
  295. $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
  296. $returnValue .= '<table border="1" width="98%"><thead>'.
  297. '<tr bgcolor="'.$color[7].'">'.
  298. '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
  299. '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
  300. '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
  301. '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
  302. '</tr>'.
  303. '</thead><tbody>';
  304. for ($i = 0; $i < count($content);$i++) {
  305. $returnValue .= '<tr><td>';
  306. $returnValue .= $content[$i]['ent'].'</td><td>';
  307. if (isset($content[$i]['type'])) {
  308. $returnValue .= $content[$i]['type'];
  309. } else $returnValue .= 'TEXT/PLAIN';
  310. $returnValue .= '</td><td>';
  311. if (isset($content[$i]['name'])) {
  312. $returnValue .= $content[$i]['name'];
  313. } else $returnValue .= '&nbsp;';
  314. $returnValue .= '</td><td>';
  315. if (isset($content[$i]['encoding'])) {
  316. $returnValue .= $content[$i]['encoding'];
  317. } else $returnValue .= '&nbsp;';
  318. $returnValue .= '</td></tr>'."\n";
  319. }
  320. $returnValue .= '</tbody></table><br />'."\n";
  321. }
  322. if (!$stripHTML) {
  323. $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
  324. $returnValue .= '<pre><div><div class="header">';
  325. } else {
  326. $returnValue .= '<pre>';
  327. }
  328. $returnValue .= $message_body;
  329. if (!$stripHTML) {
  330. $returnValue .= '</div></div></pre></font></body></html>';
  331. } else {
  332. $returnValue .= '</pre>';
  333. }
  334. return $returnValue;
  335. }
  336. ?>