BenBE's humble thoughts Thoughts the world doesn't need yet …

13.05.2010

SquirrelMail: Making it work without preg_replace and /e

Filed under: Server — Schlagwörter: , , , , , , , — BenBE @ 15:02:47

Ich hatte ja bereits vor längerer Zeit angemerkt, dass SquirrelMail mit konfiguriertem Suhosin-Patch nicht richtig läuft. Und da seit einem Jahr, wo der Bugreport nun Upstream bekannt ist, immer noch nichts passiert ist, gibt es hier für alle geplagten nun ein Diff-File zum Ersparen der ganzen Tipparbeit (Ja, der Patch ist grundlegend REINES Copy&Paste). Zumal ja durchaus schon die Grundlagen für eine Lösung ohne /e vorhanden sind.

Aber gut, heute einmal ein Post von mir ohne große Worte und nur dem zugehörigen Patch:

Im Verzeichnis decode anzuwenden:

--- iso_8859_1.php      2010-01-05 16:59:48.000000000 +0100
+++ iso_8859_1.php      2010-05-13 14:05:40.000000000 +0200
@@ -23,12 +23,11 @@
     if (! sq_is8bit($string,'iso-8859-1'))
         return $string;
 
-    $string = preg_replace("/([\201-\237])/e","'&#' . ord('\\1') . ';'",$string);
+    $string = preg_replace_callback("/([\201-\377])/",'charset_decode_iso_8859_1_helper',$string);
 
-    /* I don't want to use 0xA0 (\240) in any ranges. RH73 may dislike it */
-    $string = str_replace("\240", ' ', $string);
-
-    $string = preg_replace("/([\241-\377])/e","'&#' . ord('\\1') . ';'",$string);
     return $string;
 }
 
+function charset_decode_iso_8859_1_helper ($m) {
+    return '&#' . ord($m[1]) . ';';
+}
--- us_ascii.php        2010-01-05 16:59:48.000000000 +0100
+++ us_ascii.php        2010-05-13 14:00:36.000000000 +0200
@@ -26,11 +26,7 @@
     if (! sq_is8bit($string,'us-ascii'))
         return $string;
 
-    $string = preg_replace("/([\201-\237])/e","'?'",$string);
+    $string = preg_replace("/([\201-\377])/","?",$string);
 
-    /* I don't want to use 0xA0 (\240) in any ranges. RH73 may dislike it */
-    $string = str_replace("\240", '?', $string);
-
-    $string = preg_replace("/([\241-\377])/e","'?'",$string);
     return $string;
 }

Zuzüglich meinem Patch aus dem vorhergehenden Beitrag zum Thema.

Und für das Verzeichnis encode anzuwenden:

--- cp1251.php  2010-01-05 16:59:48.000000000 +0100
+++ cp1251.php  2010-05-13 14:17:58.000000000 +0200
@@ -19,10 +19,10 @@
  * @return string cp1251 encoded text
  */
 function charset_encode_cp1251 ($string) {
-   // don't run encoding function, if there is no encoded characters
-   if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
+    // don't run encoding function, if there is no encoded characters
+    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetocp1251('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/","unicodetocp1251",$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetocp1251(hexdec('\\1'))",$string);
 
     return $string;
@@ -41,6 +41,10 @@
  */
 function unicodetocp1251($var) {
 
+    if(is_array($var)) {
+        $var=$var[1];
+    }
+
     $cp1251chars=array('160' => "\xA0",
                        '164' => "\xA4",
                        '166' => "\xA6",
--- cp1255.php  2010-01-05 16:59:48.000000000 +0100
+++ cp1255.php  2010-05-13 14:17:49.000000000 +0200
@@ -19,10 +19,10 @@
  * @return string cp1255 encoded text
  */
 function charset_encode_cp1255 ($string) {
-   // don't run encoding function, if there is no encoded characters
-   if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
+    // don't run encoding function, if there is no encoded characters
+    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetocp1255('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/","unicodetocp1255",$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetocp1255(hexdec('\\1'))",$string);
 
     return $string;
@@ -41,6 +41,10 @@
  */
 function unicodetocp1255($var) {
 
+    if(is_array($var)) {
+        $var=$var[1];
+    }
+
     $cp1255chars=array('160' => "\xA0",
                        '161' => "\xA1",
                        '162' => "\xA2",
--- cp1256.php  2010-01-05 16:59:48.000000000 +0100
+++ cp1256.php  2010-05-13 14:17:40.000000000 +0200
@@ -19,10 +19,10 @@
  * @return string cp1256 encoded text
  */
 function charset_encode_cp1256 ($string) {
-   // don't run encoding function, if there is no encoded characters
-   if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
+    // don't run encoding function, if there is no encoded characters
+    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetocp1256('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/","unicodetocp1256",$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetocp1256(hexdec('\\1'))",$string);
 
     return $string;
@@ -41,6 +41,10 @@
  */
 function unicodetocp1256($var) {
 
+    if(is_array($var)) {
+        $var=$var[1];
+    }
+
     $cp1256chars=array('160' => "\xA0",
                        '162' => "\xA2",
                        '163' => "\xA3",
--- iso_8859_1.php      2010-01-05 16:59:48.000000000 +0100
+++ iso_8859_1.php      2010-05-13 14:17:08.000000000 +0200
@@ -19,10 +19,10 @@
  * @return string iso-8859-1 encoded text
  */
 function charset_encode_iso_8859_1 ($string) {
-   // don't run encoding function, if there is no encoded characters
-   if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
+    // don't run encoding function, if there is no encoded characters
+    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetoiso88591('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/","unicodetoiso88591",$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetoiso88591(hexdec('\\1'))",$string);
 
     return $string;
@@ -41,6 +41,10 @@
  */
 function unicodetoiso88591($var) {
 
+    if(is_array($var)) {
+        $var=$var[1];
+    }
+
     if ($var < 256) {
         $ret = chr ($var);
     } else {
--- iso_8859_15.php     2010-01-05 16:59:48.000000000 +0100
+++ iso_8859_15.php     2010-05-13 14:16:54.000000000 +0200
@@ -19,10 +19,10 @@
  * @return string iso-8859-15 encoded text
  */
 function charset_encode_iso_8859_15 ($string) {
-   // don't run encoding function, if there is no encoded characters
-   if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
+    // don't run encoding function, if there is no encoded characters
+    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetoiso885915('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/","unicodetoiso885915",$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetoiso885915(hexdec('\\1'))",$string);
 
     return $string;
@@ -41,6 +41,10 @@
  */
 function unicodetoiso885915($var) {
 
+    if(is_array($var)) {
+        $var=$var[1];
+    }
+
     $iso885915chars=array('160' => "\xA0",
                           '161' => "\xA1",
                           '162' => "\xA2",
--- iso_8859_2.php      2010-01-05 16:59:48.000000000 +0100
+++ iso_8859_2.php      2010-05-13 14:17:21.000000000 +0200
@@ -19,10 +19,10 @@
  * @return string iso-8859-2 encoded text
  */
 function charset_encode_iso_8859_2 ($string) {
-   // don't run encoding function, if there is no encoded characters
-   if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
+    // don't run encoding function, if there is no encoded characters
+    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetoiso88592('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/","unicodetoiso88592",$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetoiso88592(hexdec('\\1'))",$string);
 
     return $string;
@@ -41,6 +41,10 @@
  */
 function unicodetoiso88592($var) {
 
+    if(is_array($var)) {
+        $var=$var[1];
+    }
+
     $iso88592chars=array('160' => "\xA0",
                         '164' => "\xA4",
                         '167' => "\xA7",
--- iso_8859_7.php      2010-01-05 16:59:48.000000000 +0100
+++ iso_8859_7.php      2010-05-13 14:18:39.000000000 +0200
@@ -19,10 +19,10 @@
  * @return string iso-8859-7 encoded text
  */
 function charset_encode_iso_8859_7 ($string) {
-   // don't run encoding function, if there is no encoded characters
-   if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
+    // don't run encoding function, if there is no encoded characters
+    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetoiso88597('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/","unicodetoiso88597",$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetoiso88597(hexdec('\\1'))",$string);
 
     return $string;
@@ -41,6 +41,10 @@
  */
 function unicodetoiso88597($var) {
 
+    if(is_array($var)) {
+        $var=$var[1];
+    }
+
     $iso88597chars=array('160' => "\xA0",
                          '163' => "\xA3",
                          '166' => "\xA6",
--- iso_8859_9.php      2010-01-05 16:59:48.000000000 +0100
+++ iso_8859_9.php      2010-05-13 14:19:15.000000000 +0200
@@ -19,10 +19,10 @@
  * @return string iso-8859-9 encoded text
  */
 function charset_encode_iso_8859_9 ($string) {
-   // don't run encoding function, if there is no encoded characters
-   if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
+    // don't run encoding function, if there is no encoded characters
+    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetoiso88599('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/","unicodetoiso88599",$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetoiso88599(hexdec('\\1'))",$string);
 
     return $string;
@@ -41,6 +41,10 @@
  */
 function unicodetoiso88599($var) {
 
+    if(is_array($var)) {
+        $var=$var[1];
+    }
+
     $iso88599chars=array('160' => "\xA0",
                          '161' => "\xA1",
                          '162' => "\xA2",
--- koi8_r.php  2010-01-05 16:59:48.000000000 +0100
+++ koi8_r.php  2010-05-13 14:20:19.000000000 +0200
@@ -19,10 +19,10 @@
  * @return string koi8-r encoded text
  */
 function charset_encode_koi8_r ($string) {
-   // don't run encoding function, if there is no encoded characters
-   if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
+    // don't run encoding function, if there is no encoded characters
+    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetokoi8r('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/","unicodetokoi8r",$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetokoi8r(hexdec('\\1'))",$string);
 
     return $string;
@@ -41,6 +41,10 @@
  */
 function unicodetokoi8r($var) {
 
+    if(is_array($var)) {
+        $var=$var[1];
+    }
+
     $koi8rchars=array('160' => "\x9A",
                       '169' => "\xBF",
                       '176' => "\x9C",
--- koi8_u.php  2010-01-05 16:59:48.000000000 +0100
+++ koi8_u.php  2010-05-13 14:20:59.000000000 +0200
@@ -19,10 +19,10 @@
  * @return string koi8-u encoded text
  */
 function charset_encode_koi8_u ($string) {
-   // don't run encoding function, if there is no encoded characters
-   if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
+    // don't run encoding function, if there is no encoded characters
+    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetokoi8u('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/","unicodetokoi8u",$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetokoi8u(hexdec('\\1'))",$string);
 
     return $string;
@@ -41,6 +41,10 @@
  */
 function unicodetokoi8u($var) {
 
+    if(is_array($var)) {
+        $var=$var[1];
+    }
+
     $koi8uchars=array('160' => "\x9A",
                       '169' => "\xBF",
                       '176' => "\x9C",
--- tis_620.php 2010-01-05 16:59:48.000000000 +0100
+++ tis_620.php 2010-05-13 14:21:33.000000000 +0200
@@ -19,10 +19,10 @@
  * @return string tis-620 encoded text
  */
 function charset_encode_tis_620 ($string) {
-   // don't run encoding function, if there is no encoded characters
-   if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
+    // don't run encoding function, if there is no encoded characters
+    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetotis620('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/","unicodetotis620",$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetotis620(hexdec('\\1'))",$string);
 
     return $string;
@@ -41,6 +41,10 @@
  */
 function unicodetotis620($var) {
 
+    if(is_array($var)) {
+        $var=$var[1];
+    }
+
     $tis620chars=array('3585' => "\xA1",
                        '3586' => "\xA2",
                        '3587' => "\xA3",
--- us_ascii.php        2010-01-05 16:59:48.000000000 +0100
+++ us_ascii.php        2010-05-13 14:22:17.000000000 +0200
@@ -19,10 +19,10 @@
  * @return string us-ascii encoded text
  */
 function charset_encode_us_ascii ($string) {
-   // don't run encoding function, if there is no encoded characters
-   if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
+    // don't run encoding function, if there is no encoded characters
+    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetousascii('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/","unicodetousascii",$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetousascii(hexdec('\\1'))",$string);
 
     return $string;
@@ -41,6 +41,10 @@
  */
 function unicodetousascii($var) {
 
+    if(is_array($var)) {
+        $var=$var[1];
+    }
+
     if ($var < 128) {
         $ret = chr ($var);
     } else {
--- utf_8.php   2010-01-05 16:58:30.000000000 +0100
+++ utf_8.php   2010-05-12 20:20:33.000000000 +0200
@@ -26,7 +26,7 @@
    // don't run encoding function, if there is no encoded characters
    if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
 
-    $string=preg_replace("/&#([0-9]+);/e","unicodetoutf8('\\1')",$string);
+    $string=preg_replace_callback("/&#([0-9]+);/",'unicodetoutf8',$string);
     // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetoutf8(hexdec('\\1'))",$string);
 
     return $string;
@@ -44,9 +44,6 @@
  * @return string utf8 character
  */
 function unicodetoutf8($var) {
 
+    if(is_array($var)) {
+        $var = $var[1];
+    }
+
     if ($var < 128) {
         $ret = chr ($var);

Bei Fragen, oder falls sich ein Fehler eingeschlichen haben sollte, bitte kurz in den Kommentaren melden, dann korrigier ich die Patches.

Flattr this!

Keine Kommentare »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress