message_details_bottom.php 13 KB

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