Replacing deprecated HTML "center" element.
[squirrelmail.git] / plugins / message_details / message_details_bottom.php
index 3d9e2182bacdf9a0ea4247a6d7825f80022e81c3..0852e5ca8c61ff3678081b0822141cfa716b12b0 100644 (file)
@@ -1,14 +1,13 @@
 <?php
+
 /**
  * Message Details plugin - bottom frame with message structure and rfc822 body
  *
  * Plugin to view the RFC822 raw message output and the bodystructure of a message
  *
- * Licensed under the GNU GPL. For full terms see the file COPYING.
- * 
  * @author Marc Groot Koerkamp
- * @copyright Copyright &copy; 2002 Marc Groot Koerkamp, The Netherlands
- * @copyright Copyright &copy; 2004 The SquirrelMail Project Team
+ * @copyright &copy; 2002 Marc Groot Koerkamp, The Netherlands
+ * @copyright &copy; 2002-2006 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package plugins
  */
 
 /** @ignore */
-define('SM_PATH','../../');
+if (!defined('SM_PATH'))
+   define('SM_PATH','../../');
 
 /* SquirrelMail required files. */
 require_once(SM_PATH . 'include/validate.php');
 require_once(SM_PATH . 'functions/imap.php');
 require_once(SM_PATH . 'functions/mime.php');
 
-global $color;
+sqgetGlobalVar('get_message_details', $md_action, SQ_GET);
 
-sqgetGlobalVar('passed_id', $passed_id, SQ_GET);
-sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
+/**
+ * Controls display of 8bit symbols in message source
+ * @global boolean $msgd_8bit_in_hex;
+ */
+global $msgd_8bit_in_hex;
+$msgd_8bit_in_hex=false;
 
-sqgetGlobalVar('username', $username, SQ_SESSION);
-sqgetGlobalVar('key', $key, SQ_COOKIE);
-sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
+if (!empty($md_action))
+{
+    sqgetGlobalVar('passed_id', $passed_id, SQ_GET);
+    sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
+    echo get_message_details($mailbox, $passed_id);
+}
+
+
+// ---------- function definitions ----------
+
+/**
+ * Converts 8bit string to hex
+ *
+ * Replaces 8bit symbols with their hex strings,
+ * encloses them in curly brackets and uses different color.
+ * @param string $string text
+ * @return string
+ * @since 1.5.1
+ */
+function msgd_convert_to_hex($string) {
+     global $color;
+     return preg_replace("/([\200-\377])/e","'<font color=\"$color[2]\">{'.dechex(ord('\\1')).'}</font>'",$string);
+}
 
 /**
  * Calculates id of MIME entity
@@ -52,7 +76,7 @@ function CalcEntity($entString, $direction) {
             if ($pos === false) {
                 $entString++;
                 $result= $entString;
-            } 
+            }
             else {
                 $level = substr($entString,0,$pos);
                 $sublevel = substr($entString,$pos+1);
@@ -69,6 +93,8 @@ function CalcEntity($entString, $direction) {
     return ($result);
 }
 
+
+
 /**
  * Returns time in microseconds between selected and current timestamp
  *
@@ -82,6 +108,27 @@ function returnTime($start) {
  return $timepassed;
 }
 
+
+
+/**
+ * Returns actual message details
+ * @param string $mailbox
+ * @param string $passed_id
+ * @param boolean $stripHTML If TRUE, only plain text is returned,
+ *                           default is FALSE, wherein output contains
+ *                           pretty-HTMLification of message body
+ * @return string The formatted message details
+ * @access public
+ */
+function get_message_details($mailbox, $passed_id, $stripHTML=FALSE) {
+
+global $imapServerAddress, $imapPort, $color,$msgd_8bit_in_hex;
+
+$returnValue = '';
+
+sqgetGlobalVar('username', $username, SQ_SESSION);
+sqgetGlobalVar('key', $key, SQ_COOKIE);
+
 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
 $read = sqimap_mailbox_select($imapConnection, $mailbox);
 $start = gettimeofday();
@@ -116,7 +163,7 @@ for ($i=1; $i < $count; $i++) {
         } else if ($messageheader) {
             if ($header) {
                 $header=false;
-                $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n"; 
+                $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
             }
             $mimepart = -$header;
             $bnd_end = false;
@@ -128,11 +175,11 @@ for ($i=1; $i < $count; $i++) {
         } else {
             if ($header) {
                 $pre = '';
-                $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n"; 
+                $end = "\n \n".'</div>'."\n \n".'<div class="ent_body" id="'.$entStr.'B">'."\n \n";
             }
             $header = false;
             $mimepart=true;
-        }  
+        }
         $contentset = false;
         $nameset = false;
     } else {
@@ -142,7 +189,7 @@ for ($i=1; $i < $count; $i++) {
                 $pre = '<i><font color ="'.$color[1].'">';
                 $end = '</i></font>';
             }
-        }     
+        }
         if (!$messageheader && !$header ) {
             $mimepart=true;
         }  else {
@@ -153,7 +200,7 @@ for ($i=1; $i < $count; $i++) {
     }
     if (  ( $header || $messageheader) && (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) )  {
         $bnd = $reg[1];
-        $bndreg = $bnd;    
+        $bndreg = $bnd;
         $bndreg = str_replace("\\","\\\\",$bndreg);
         $bndreg = str_replace("?","\\?",$bndreg);
         $bndreg = str_replace("+","\\+",$bndreg);
@@ -173,18 +220,18 @@ for ($i=1; $i < $count; $i++) {
             $entStr = CalcEntity("$entStr",1);
         }
     }
-    
+
     if (($line != '' && $line{0} == '-' || $header)  && isset($boundaries[0])) {
         $cnt=count($boundaries)-1;
         $bnd = $boundaries[$cnt]['bnd'];
         $bndreg = $boundaries[$cnt]['bndreg'];
-      
+
         $regstr = '/^--'."($bndreg)".".*".'/';
         if (preg_match($regstr,$line,$reg) ) {
             $bndlen = strlen($reg[1]);
-            $bndend = false;            
+            $bndend = false;
             if (strlen($line) > ($bndlen + 3)) {
-                if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-') 
+                if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
                     $bndend = true;
             }
             if ($bndend) {
@@ -222,7 +269,7 @@ for ($i=1; $i < $count; $i++) {
                         $entities["$entStr"] = array();
                         $entities["$entStr"]['entity'] = $entStr;
                         $entities["$entStr"]['contenttype']=$reg[2].'/'.$reg[3];
-                    }        
+                    }
                    } else if (!$nameset && preg_match("/^.*(name=\s*)\"(.*)\".*/i",$line,$reg)) {
                     $name = htmlspecialchars($reg[2]);
                     $content[$content_indx]['name'] = decodeHeader($name);
@@ -255,13 +302,19 @@ for ($i=1; $i < $count; $i++) {
         }
     }
 */
-    $line = htmlspecialchars($line);
-    $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
+    if ($stripHTML) {
+        $message_body .= $line . "\r\n";
+    } else {
+        $line = htmlspecialchars($line);
+        if ($msgd_8bit_in_hex) $line = msgd_convert_to_hex($line);
+        $message_body .= "$pre"."$line"."$end".'<br />'."\r\n";
+    }
 }
-//echo returnTime($start).'<br />';
+
+//$returnValue .= returnTime($start).'<br />';
 $xtra = <<<ECHO
 
-<style>
+<style type="text/css">
 
 <!--
 .ent_body {
@@ -282,12 +335,20 @@ $xtra = <<<ECHO
 
 ECHO;
 
-displayHtmlHeader( _("Message Details"), $xtra, FALSE );
+if (!$stripHTML) {
+    ob_start();
+    displayHtmlHeader( _("Message Details"), $xtra, FALSE );
+    $returnValue .= ob_get_contents();
+    ob_end_clean();
+}
+
 /* body */
-echo "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
-echo '<code>'."\n";
-echo '<font face="monospace">'."\n";
-echo '<br />'."\n";
+if (!$stripHTML) {
+    $returnValue .= "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\">\n";
+    $returnValue .= '<code>'."\n";
+    $returnValue .= '<font face="monospace">'."\n";
+    $returnValue .= '<br />'."\n";
+}
 
 
 //session_register("entities");
@@ -296,15 +357,17 @@ echo '<br />'."\n";
 //foreach ($keys as $key) {
 //    if (isset($entities[$key])) {
 //    if ($entities[$key]['encoding'] == 'base64') {
-//        echo '<img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&amp;name='.$entities[$key]['name'].'"><br />';
+//        if (!$stripHTML) {
+//            $returnValue .= 'img src="message_viewentity.php?ent='.$entities[$key]['entity'].'&amp;name='.$entities[$key]['name'].'"><br />';
+//        }
 //    }
 //    }
 //}
 //session_unregister("entities");
 
-if (count($content) > 0) {
-    echo '<h2>'._("Bodystructure")."</h2>\n\n";
-    echo '<table border="1" width="98%"><thead>'.
+if (count($content) > 0 && !$stripHTML) {
+    $returnValue .= '<h2>'._("Bodystructure")."</h2>\n\n";
+    $returnValue .= '<table border="1" width="98%"><thead>'.
          '<tr bgcolor="'.$color[7].'">'.
          '<td><b><font color="'.$color[5].'">'._("Entity").'</font></b></td>'.
          '<td><b><font color="'.$color[5].'">'._("Content-Type").'</font></b></td>'.
@@ -313,25 +376,36 @@ if (count($content) > 0) {
          '</tr>'.
          '</thead><tbody>';
     for ($i = 0; $i < count($content);$i++) {
-        echo '<tr><td>';
-        echo $content[$i]['ent'].'</td><td>';
+        $returnValue .= '<tr><td>';
+        $returnValue .= $content[$i]['ent'].'</td><td>';
         if (isset($content[$i]['type'])) {
-            echo $content[$i]['type'];
-        } else echo 'TEXT/PLAIN';
-        echo '</td><td>';
+            $returnValue .= $content[$i]['type'];
+        } else $returnValue .= 'TEXT/PLAIN';
+        $returnValue .= '</td><td>';
         if (isset($content[$i]['name'])) {
-            echo $content[$i]['name'];
-        } else echo '&nbsp;';
-        echo '</td><td>';
+            $returnValue .= $content[$i]['name'];
+        } else $returnValue .= '&nbsp;';
+        $returnValue .= '</td><td>';
         if (isset($content[$i]['encoding'])) {
-            echo $content[$i]['encoding'];
-        } else echo '&nbsp;';
-        echo '</td></tr>'."\n";
+            $returnValue .= $content[$i]['encoding'];
+        } else $returnValue .= '&nbsp;';
+        $returnValue .= '</td></tr>'."\n";
     }
-    echo '</tbody></table><br />'."\n";
+    $returnValue .= '</tbody></table><br />'."\n";
 }
-echo '<h2>'._("RFC822 Message body")."</h2>\n\n";
-echo '<div><div class="header">'."\n\n";
-echo $message_body;
-echo '</div></div></font></code></body></html>';
-?>
+
+if (!$stripHTML) {
+    $returnValue .= '<h2>'._("RFC822 Message body")."</h2>\n\n";
+    $returnValue .= '<div><div class="header">'."\n\n";
+}
+
+$returnValue .= $message_body;
+
+if (!$stripHTML)
+    $returnValue .= '</div></div></font></code></body></html>';
+
+return $returnValue;
+
+}
+
+?>
\ No newline at end of file