¤ thing + #294
authorPierre Habouzit (MadCoder <pierre.habouzit@m4x.org>
Tue, 24 May 2005 07:13:23 +0000 (07:13 +0000)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Fri, 4 Jan 2008 23:34:25 +0000 (00:34 +0100)
git-archimport-id: opensource@polytechnique.org--2005/banana--mainline--1.0--patch-18

Changelog
banana/misc.inc.php
banana/post.inc.php
banana/spool.inc.php
banana/utf8.php [new file with mode: 0644]

index f06f42f..b950355 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -1,4 +1,12 @@
 ================================================================================
+VERSION 1.1
+
+Tue, 24 May 2005                                       Pierre Habouzit <pierre.habouzit@m4x.org>
+
+       * Fix one problem with $this->ids beeing empty in Spool.
+       * Fix encoding issues since iconv truncates strings sometimes.
+
+================================================================================
 VERSION 1.0
 
 Fri, 07 Jan 2005                                       Pierre Habouzit <pierre.habouzit@m4x.org>
index a72e6cf..ea88ccd 100644 (file)
 
 function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); }
 
+function to_html($str, $charset) {
+    require_once 'banana/utf8.php';
+    return utf8entities(htmlentities(iconv($charset, 'utf8', $str), ENT_NOQUOTES, 'UTF-8'));
+}
+
 /********************************************************************************
  *  HEADER STUFF
  */
index 340d218..60f2986 100644 (file)
@@ -44,7 +44,8 @@ class BananaPost
         }
 
         if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers['content-type'], $matches)) {
-            $this->body = iconv($matches[1], 'iso-8859-15', $this->body);
+            require_once 'banana/misc.inc.php';
+            $this->body = to_html($this->body, $matches[1]);
         }
     }
 
index 1f9777d..f199342 100644 (file)
@@ -202,6 +202,7 @@ class BananaSpool
         if (empty($since)) { return; }
 
         if (is_array($newpostsids = $banana->nntp->newnews($since, $this->group))) {
+            if (!is_array($this->ids)) { $this->ids = array(); }
             $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
             foreach ($newpostsids as $mid) {
                 $this->overview[$this->ids[$mid]]->isread     = false;
diff --git a/banana/utf8.php b/banana/utf8.php
new file mode 100644 (file)
index 0000000..6a9fb51
--- /dev/null
@@ -0,0 +1,88 @@
+<?php\r
+\r
+/**\r
+ * Transforme une chaine encodée en UTF-8, et la convertit\r
+ * en entitiées unicode &#xxx; pour que ça s'affiche correctement\r
+ * dans les navigateurs, sans forcément tenir compte du meta\r
+ * content-type charset...\r
+ * @param String $source la chaine en UTF-8\r
+ * @return String les entitées\r
+ * @access public\r
+ * @see http://www.php.net/utf8_decode\r
+ */\r
+function utf8entities($source)\r
+{\r
+   // array used to figure what number to decrement from character order value \r
+   // according to number of characters used to map unicode to ascii by utf-8\r
+   $decrement[4] = 240;\r
+   $decrement[3] = 224;\r
+   $decrement[2] = 192;\r
+   $decrement[1] = 0;\r
+   \r
+   // the number of bits to shift each charNum by\r
+   $shift[1][0] = 0;\r
+   $shift[2][0] = 6;\r
+   $shift[2][1] = 0;\r
+   $shift[3][0] = 12;\r
+   $shift[3][1] = 6;\r
+   $shift[3][2] = 0;\r
+   $shift[4][0] = 18;\r
+   $shift[4][1] = 12;\r
+   $shift[4][2] = 6;\r
+   $shift[4][3] = 0;\r
+   \r
+   $pos = 0;\r
+   $len = strlen($source);\r
+   $encodedString = '';\r
+   while ($pos < $len)\r
+   {\r
+      $charPos = $source{$pos};\r
+      $asciiPos = ord($charPos);\r
+      if ($asciiPos < 128)\r
+      {\r
+         $encodedString .= $charPos;\r
+         $pos++;\r
+         continue;\r
+      }\r
+      \r
+      $i=1;\r
+      if (($asciiPos >= 240) && ($asciiPos <= 255)) // 4 chars representing one unicode character\r
+         $i=4;\r
+      else if (($asciiPos >= 224) && ($asciiPos <= 239)) // 3 chars representing one unicode character\r
+         $i=3;\r
+      else if (($asciiPos >= 192) && ($asciiPos <= 223)) // 2 chars representing one unicode character\r
+         $i=2;\r
+      else // 1 char (lower ascii)\r
+         $i=1;\r
+      $thisLetter = substr($source, $pos, $i);\r
+      $pos += $i;\r
+      \r
+      // process the string representing the letter to a unicode entity\r
+      $thisLen = strlen($thisLetter);\r
+      $thisPos = 0;\r
+      $decimalCode = 0;\r
+      while ($thisPos < $thisLen)\r
+      {\r
+         $thisCharOrd = ord(substr($thisLetter, $thisPos, 1));\r
+         if ($thisPos == 0)\r
+         {\r
+            $charNum = intval($thisCharOrd - $decrement[$thisLen]);\r
+            $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);\r
+         }\r
+         else\r
+         {\r
+            $charNum = intval($thisCharOrd - 128);\r
+            $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);\r
+         }\r
+         \r
+         $thisPos++;\r
+      }\r
+      \r
+      $encodedLetter = '&#'. str_pad($decimalCode, ($thisLen==1)?3:5, '0', STR_PAD_LEFT).';';\r
+      $encodedString .= $encodedLetter;\r
+   }\r
+   \r
+   return $encodedString;\r
+}\r
+\r
+?>\r