more clever + should close #80
authorx2000habouzit <x2000habouzit>
Fri, 29 Oct 2004 14:59:26 +0000 (14:59 +0000)
committerx2000habouzit <x2000habouzit>
Fri, 29 Oct 2004 14:59:26 +0000 (14:59 +0000)
include/xorg.misc.inc.php

index d7f1235..e989557 100644 (file)
  *  Foundation, Inc.,                                                      *
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************
-        $Id: xorg.misc.inc.php,v 1.7 2004-10-21 13:12:06 x2000habouzit Exp $
+        $Id: xorg.misc.inc.php,v 1.8 2004-10-29 14:59:26 x2000habouzit Exp $
  ***************************************************************************/
 
 function quoted_printable_encode($input, $line_max = 76) {
-    $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
     $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
-    $eol = "\r\n";
-    $linebreak = "=0A";
+    $eol = "\n";
+    $linebreak = "=0D=0A=\n    ";
     $escape = "=";
     $output = "";
 
-    for ($j=0;$j<count($lines);$j++) {
-        $line = $lines[$j];
+    foreach ($lines as $j => $line) {
         $linlen = strlen($line);
         $newline = "";
         for($i = 0; $i < $linlen; $i++) {
-            $c = substr($line, $i, 1);
+            $c = $line{$i};
             $dec = ord($c);
-            if ( ($dec == 32) && ($i == ($linlen - 1)) ) { // convert space at eol only
+            if ( ($dec == 32) && ($i == ($linlen - 1)) ) {
+               // convert space at eol only
                 $c = "=20";
-            } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
-                $h2 = floor($dec/16); $h1 = floor($dec%16);
-                $c = $escape.$hex["$h2"].$hex["$h1"];
+            } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) {
+               // always encode "\t", which is *not* required
+                $c = $escape.strtoupper(sprintf("%02x",$dec));
             }
             if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
-                $output .= $newline.$escape."\n"; // soft line break; " =\r\n" is okay
+                $output .= $newline.$escape.$eol;
                 $newline = "    ";
             }
             $newline .= $c;