removed htmlspecialchars applied in wrong place.
[squirrelmail.git] / plugins / translate / functions.php
index 3847af1cea9c92d93747d5710523607e9126387a..bd7616f2a2e8a66a008b6e3049ddddf1e3ae1749 100644 (file)
@@ -1,10 +1,10 @@
 <?php
+
 /**
  * SquirrelMail translate plugin functions
  *
- * Copyright (c) 2004 The SquirrelMail Project Team
- * Licensed under the GNU GPL. For full terms see the file COPYING.
- *
+ * @copyright &copy; 2004-2006 The SquirrelMail Project Team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package plugins
  * @subpackage translate
@@ -77,6 +77,7 @@ function translate_read_form_function() {
         $translate_promt_enabled, $translate_otenet_enabled;
     global $translate_custom_enabled;
 
+
     if (!$translate_show_read) {
         return;
     }
@@ -91,37 +92,47 @@ function translate_read_form_function() {
 
     $trans_ar = $message->findDisplayEntity(array(), array('text/plain'));
     $body = '';
+    $final_body = '';
     if ($trans_ar[0] != '') {
         for ($i = 0; $i < count($trans_ar); $i++) {
-            $body .= formatBody($imapConnection, $message, $color, $wrap_at, $trans_ar[$i], $passed_id, $mailbox);
+            /* reduced version of formatBody and translateText functions */
+
+            // get message entity information
+            $body_message = getEntity($message, $trans_ar[$i]);
+            // get message body
+            $body = mime_fetch_body ($imapConnection, $passed_id, $trans_ar[$i]);
+            // convert encoded messages
+            $body = decodeBody($body, $body_message->header->encoding);
+
+            /*
+             * if message part is html formated - convert spaces, html line feeds, 
+             * less than and greater than html entities and remove tags
+             */
+            if ($body_message->header->type1 == 'html') {
+                $entity_conv = array('&nbsp;' => ' ',
+                                     '<p>'    => "\n",
+                                     '<P>'    => "\n",
+                                     '<br>'   => "\n",
+                                     '<BR>'   => "\n",
+                                     '<br />' => "\n",
+                                     '<BR />' => "\n",
+                                     '&gt;'   => '>',
+                                     '&lt;'   => '<');
+                $body = strtr($body, $entity_conv);
+                $body = strip_tags($body);
+            }
+            // remove whitespace
+            $body = trim($body);
+            // save processed text and parse other entity
+            $final_body.= charset_decode($body_message->header->getParameter('charset'),$body);
         }
-        $hookResults = do_hook('message_body', $body);
-        $body = $hookResults[1];
-    } else {
-        $body = 'Message can\'t be translated';
-    }
 
-    $new_body = $body;
-    $pos = strpos($new_body,
-        '">'. _("Download this as a file") . '</a></center><br /></small>');
-    if (is_int($pos)) {
-        $new_body = substr($new_body, 0, $pos);
+        // add form if message is not empty
+        if (!empty($final_body)) {
+            $function = 'translate_form_' . $translate_server;
+            $function($final_body);
+        }
     }
-
-    $trans = get_html_translation_table(HTML_ENTITIES);
-    $trans[' '] = '&nbsp;';
-    $trans = array_flip($trans);
-    $new_body = strtr($new_body, $trans);
-
-    $new_body = urldecode($new_body);
-    $new_body = strip_tags($new_body);
-
-    /* I really don't like this next part ... */
-    $new_body = str_replace('"', "''", $new_body);
-    $new_body = strtr($new_body, "\n", ' ');
-
-    $function = 'translate_form_' . $translate_server;
-    $function($new_body);
 }
 
 /**
@@ -232,7 +243,7 @@ function translate_showoption() {
         $translate_dictionary_enabled, $translate_google_enabled,
         $translate_gpltrans_enabled, $translate_intertran_enabled,
         $translate_promt_enabled, $translate_otenet_enabled;
-    global $translate_custom_enabled;
+    global $translate_custom_enabled, $translate_gpltrans_url;
 
     if ($translate_babelfish_enabled) translate_showoption_internal('server','babelfish', 'Babelfish');
     if ($translate_go_enabled) translate_showoption_internal('server','go', 'Go.com');
@@ -423,10 +434,12 @@ function translate_lang_opt($from, $to, $value, $text) {
  * Starts translation box
  *
  * @param string $action url that has to recieve message for translation
+ * @param string $charset (since sm 1.5.1) character set, that should be used 
+ * to submit 8bit information.
  * @access private
  */
-function translate_new_form($action) {
-    global $translate_dir, $translate_new_window, $translate_location;
+function translate_new_form($action,$charset=null) {
+    global $translate_dir, $translate_location;
     global $color, $translate_same_window;
 
     echo '<form action="';
@@ -442,6 +455,9 @@ function translate_new_form($action) {
     if (!$translate_same_window) {
         echo ' target="_blank"';
     }
+    
+    if (! is_null($charset))
+        echo ' accept-charset="'.htmlspecialchars($charset).'"';
 
     echo ">\n";
 
@@ -460,7 +476,7 @@ function translate_new_form($action) {
  * @access private
  */
 function translate_form_babelfish($message) {
-    translate_new_form('http://babelfish.altavista.com/babelfish/tr');
+    translate_new_form('http://babelfish.altavista.com/babelfish/tr','utf-8');
 ?>
     <input type="hidden" name="doit" value="done" />
     <input type="hidden" name="intl" value="1" />
@@ -590,7 +606,7 @@ function translate_form_go($message) {
  * @access private
  */
 function translate_form_intertran($message) {
-    translate_new_form('http://www.tranexp.com:2000/InterTran');
+    translate_new_form('http://intertran.tranexp.com/Translate/result.shtml');
     echo '<input type="hidden" name="topframe" value="yes" />'.
          '<input type="hidden" name="type" value="text" />'.
          '<input type="hidden" name="keyb" value="non" />'.
@@ -672,7 +688,13 @@ function translate_form_intertran($message) {
  * @access private
  */
 function translate_form_gpltrans($message) {
-    translate_new_form('http://www.translator.cx/cgi-bin/gplTrans');
+    global $translate_gpltrans_url;
+
+    // make sure that it is not empty
+    if ($translate_gpltrans_url=='')
+        $translate_gpltrans_url='http://www.translator.cx/cgi-bin/gplTrans';
+
+    translate_new_form($translate_gpltrans_url);
     echo '<select name="language">'.
         translate_lang_opt('', 'nl_NL', 'dutch_dict',      _("Dutch")).
         translate_lang_opt('', 'fr_FR', 'french_dict',     _("French")).
@@ -766,7 +788,7 @@ function translate_form_dictionary($message) {
  * @access private
  */
 function translate_form_otenet($message) {
-    translate_new_form('http://systran.otenet.gr/cgi-bin/systran.cgi');
+    translate_new_form('http://trans.otenet.gr/systran/box','windows-1253');
 ?>
     <input type="hidden" name="doit" value="done" />
     <input type="hidden" name="partner" value="OTEnet-en" />
@@ -826,7 +848,7 @@ function translate_form_otenet($message) {
  * @access private
  */
 function translate_form_promt($message) {
-    translate_new_form('http://www.online-translator.com/text.asp#tr_form');
+    translate_new_form('http://www.online-translator.com/text.asp#tr_form','windows-1251');
     echo '<input type="hidden" name="status" value="translate" />';
     echo '<input type="hidden" name="source" value="'.$message.'" />';
     echo _("Interface language")." : ";
@@ -886,7 +908,7 @@ function translate_form_promt($message) {
  * @access private
  */
 function translate_form_google($message) {
-    translate_new_form('http://www.google.com/translate_t');
+    translate_new_form('http://www.google.com/translate_t','utf-8');
     echo '<input type="hidden" name="ie" value="Unknown" />' .
          '<input type="hidden" name="oe" value="ASCII" />' .
          '<input type="hidden" name="hl" value="en" />' .
@@ -921,4 +943,4 @@ function translate_form_google($message) {
 
     translate_table_end();
 }
-?>
\ No newline at end of file
+?>