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 2002 Marc Groot Koerkamp, The Netherlands
  9. * @copyright 2002-2025 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, NULL, SQ_TYPE_BIGINT);
  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, $imap_stream_options,
  104. $color,$msgd_8bit_in_hex, $username;
  105. $returnValue = '';
  106. $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0, $imap_stream_options);
  107. $read = sqimap_mailbox_select($imapConnection, $mailbox);
  108. if (!empty($passed_ent_id))
  109. $body = sqimap_run_command($imapConnection, "FETCH $passed_id BODY[$passed_ent_id]",true, $response, $readmessage, TRUE);
  110. else
  111. $body = sqimap_run_command($imapConnection, "FETCH $passed_id RFC822",true, $response, $readmessage, TRUE);
  112. $message_body = '';
  113. $header = false;
  114. $mimepart = false;
  115. $bnd_end = false;
  116. $messageheader = true;
  117. $messageheaderstart=false;
  118. $boundaries = array();
  119. $entities = array();
  120. sqsession_unregister("entities");
  121. $pre = '<b>';
  122. $end = '</b>';
  123. $entStr = '';
  124. $bla ='';
  125. $content = array ();
  126. $content_indx = -1;
  127. $contentset = false;
  128. $count=count($body);
  129. $body[$count-1] = substr($body[$count-1], -1);
  130. for ($i=1; $i < $count; $i++) {
  131. $line = rtrim($body[$i]);
  132. if ($line == '') {
  133. $pre = '';
  134. $end = '';
  135. if ($bnd_end) {
  136. $header = true;
  137. $mimepart = false;
  138. } else if ($messageheader) {
  139. if ($header) {
  140. $header=false;
  141. $end = '</div><div class="ent_body" id="'.$entStr.'B">';
  142. }
  143. $mimepart = -$header;
  144. $bnd_end = false;
  145. if ($messageheaderstart) {
  146. $messageheaderstart=false;
  147. }
  148. } else if ($messageheaderstart) {
  149. $messageheader= false;
  150. } else {
  151. if ($header) {
  152. $pre = '';
  153. $end = '</div><div class="ent_body" id="'.$entStr.'B">';
  154. }
  155. $header = false;
  156. $mimepart=true;
  157. }
  158. $contentset = false;
  159. $nameset = false;
  160. } else {
  161. if (!$header && $messageheader) {
  162. $messageheaderstart=true;
  163. if ($pre != '<b>') {
  164. $pre = '<i><font color ="'.$color[1].'">';
  165. $end = '</i></font>';
  166. }
  167. }
  168. if (!$messageheader && !$header ) {
  169. $mimepart=true;
  170. } else {
  171. $mimepart=false;
  172. }
  173. $pre = '';
  174. $end = '';
  175. }
  176. if ( ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) ) {
  177. $bnd = $reg[1];
  178. $bndreg = $bnd;
  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. $bndreg = str_replace(")","\\)",$bndreg);
  187. $boundaries[] = array( 'bnd' => $bnd, 'bndreg' => $bndreg);
  188. $messageheader = false;
  189. $messageheaderstart=false;
  190. $mimepart=false;
  191. if ($entStr=='') {
  192. $entStr='0';
  193. } else {
  194. $entStr = CalcEntity("$entStr",1);
  195. }
  196. }
  197. if (($line != '' && $line[0] == '-' || $header) && isset($boundaries[0])) {
  198. $cnt=count($boundaries)-1;
  199. $bnd = $boundaries[$cnt]['bnd'];
  200. $bndreg = $boundaries[$cnt]['bndreg'];
  201. $regstr = '/^--'."($bndreg)".".*".'/';
  202. if (preg_match($regstr,$line,$reg) ) {
  203. $bndlen = strlen($reg[1]);
  204. $bndend = false;
  205. if (strlen($line) > ($bndlen + 3)) {
  206. if ($line[$bndlen+2] == '-' && $line[$bndlen+3] == '-')
  207. $bndend = true;
  208. }
  209. if ($bndend) {
  210. $entStr = CalcEntity("$entStr",-1);
  211. array_pop($boundaries);
  212. $pre .= '<b><font color ="'.$color[2].'">';
  213. $end .= '</font></b>';
  214. $header = true;
  215. $mimepart = false;
  216. $bnd_end = true;
  217. $encoding = '';
  218. } else {
  219. $header = true;
  220. $bnd_end = false;
  221. $entStr = CalcEntity("$entStr",0);
  222. $content_indx++;
  223. $content[$content_indx]=array();
  224. $content[$content_indx]['ent'] = '<a href="#' . $entStr . '">' . $entStr . '</a>';
  225. $pre .= '</div><div class="entheader" id="'.
  226. $entStr.'H"><a name="'."$entStr".'"></a><b><font color="'.$color[2].'">';
  227. $end .= '</font></b>';
  228. $header = true;
  229. $mimepart = false;
  230. $encoding = '';
  231. }
  232. } else {
  233. if ($header) {
  234. if (!$contentset && preg_match("/^.*(content-type:)\s*(\w+)\/(\w+).*/i",$line,$reg)) {
  235. if (strtolower($reg[2]) == 'message' && strtolower($reg[3]) == 'rfc822') {
  236. $messageheader = true;
  237. }
  238. $content[$content_indx]['type'] = "$reg[2]/$reg[3]";
  239. $contentset = true;
  240. if ($reg[2] == 'image') {
  241. $entities["$entStr"] = array();
  242. $entities["$entStr"]['entity'] = $entStr;
  243. $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
  244. }
  245. } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
  246. $name = sm_encode_html_special_chars($reg[2]);
  247. $content[$content_indx]['name'] = decodeHeader($name);
  248. $nameset = true;
  249. if (isset($entities["$entStr"])) {
  250. $entities["$entStr"]['name'] = urlEncode($reg[2]);
  251. }
  252. } else if (preg_match("/^.*(content-transfer-encoding:)\s*(\w+-?(\w+)?).*/i",$line,$reg) ) {
  253. $encoding = $reg[2];
  254. if (isset($entities["$entStr"])) {
  255. $entities["$entStr"]['encoding']=$reg[2];
  256. }
  257. $content[$content_indx]['encoding'] = $encoding;
  258. $mimeentity = '';
  259. }
  260. $pre .= '<b><font color='.$color[7].'">';
  261. $end .= '</font></b>';
  262. //$mimepart=false;
  263. }
  264. }
  265. }
  266. if ($stripHTML) {
  267. $message_body .= $line . "\r\n";
  268. } else {
  269. $line = sm_encode_html_special_chars($line);
  270. if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
  271. $message_body .= "$pre"."$line"."$end"."\r\n";
  272. }
  273. }
  274. //$returnValue .= returnTime($start).'<br />';
  275. $xtra = <<<ECHO
  276. <style type="text/css">
  277. <!--
  278. .ent_body {
  279. display:inline;
  280. }
  281. .header {
  282. display:inline;
  283. }
  284. .entheader {
  285. display:inline;
  286. width:99%;
  287. }
  288. -->
  289. </style>
  290. ECHO;
  291. if (!$stripHTML) {
  292. ob_start();
  293. displayHtmlHeader( _("Message Details"), $xtra, FALSE );
  294. $returnValue .= ob_get_contents();
  295. ob_end_clean();
  296. }
  297. /* body */
  298. if (!$stripHTML) {
  299. $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
  300. $returnValue .= '<font face="monospace">'."\n";
  301. $returnValue .= '<br />'."\n";
  302. }
  303. if (count($content) > 0 && !$stripHTML) {
  304. $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
  305. $returnValue .= '<table border="1" width="98%"><thead>'.
  306. '<tr bgcolor="'.$color[7].'">'.
  307. '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
  308. '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
  309. '<td><b><font color="'.$color[5].'">'._("Name").'</font></b></td>'.
  310. '<td><b><font color="'.$color[5].'">'._("Encoding").'</font></b></td>'.
  311. '</tr>'.
  312. '</thead><tbody>';
  313. for ($i = 0; $i < count($content);$i++) {
  314. $returnValue .= '<tr><td>';
  315. $returnValue .= $content[$i]['ent'].'</td><td>';
  316. if (isset($content[$i]['type'])) {
  317. $returnValue .= $content[$i]['type'];
  318. } else $returnValue .= 'TEXT/PLAIN';
  319. $returnValue .= '</td><td>';
  320. if (isset($content[$i]['name'])) {
  321. $returnValue .= $content[$i]['name'];
  322. } else $returnValue .= '&nbsp;';
  323. $returnValue .= '</td><td>';
  324. if (isset($content[$i]['encoding'])) {
  325. $returnValue .= $content[$i]['encoding'];
  326. } else $returnValue .= '&nbsp;';
  327. $returnValue .= '</td></tr>'."\n";
  328. }
  329. $returnValue .= '</tbody></table><br />'."\n";
  330. }
  331. if (!$stripHTML) {
  332. $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
  333. $returnValue .= '<pre><div><div class="header">';
  334. } else {
  335. $returnValue .= '<pre>';
  336. }
  337. $returnValue .= $message_body;
  338. if (!$stripHTML) {
  339. $returnValue .= '</div></div></pre></font></body></html>';
  340. } else {
  341. $returnValue .= '</pre>';
  342. }
  343. return $returnValue;
  344. }