Fix a bug in text/plain formatter when a message contains two URLs, the
[banana.git] / banana / message.func.inc.php
index 011d534..e32d973 100644 (file)
@@ -88,13 +88,16 @@ function banana_catchFormats($text)
                         'strong' => array('\B\*\b', '\b\*\B'));
     $url = Banana::$msgshow_url;
     preg_match_all("/$url/ui", $text, $urls);
-    $text = str_replace($urls[0], "&&&urls&&&", $text);
+    $urls = $urls[0];
+    $replace = $urls;
+    rsort($replace);
+    $text = str_replace($replace, "&&&urls&&&", $text);
     foreach ($formatting as $mark=>$limit) {
         list($ll, $lr) = $limit;
         $text = preg_replace('/' . $ll . '(\w+?)' . $lr . '/us',
                              "<$mark>\\1</$mark>", $text);
     }
-    return preg_replace('/&&&urls&&&/e', 'array_shift($urls[0])', $text);
+    return preg_replace('/&&&urls&&&/e', 'array_shift($urls)', $text);
 }
 
 /** Build a flowed text from plain text
@@ -140,7 +143,7 @@ function banana__catchMailLink($email)
     $mid = '<' . $email . '>';
     if (isset(Banana::$spool->ids[$mid])) {
         return Banana::$page->makeLink(Array('group' => Banana::$group,
-                                             'artid' => Banana::$spool->ids[$mid],
+                                             'artid' => Banana::$spool->ids[$mid]->id,
                                              'text'  => $email));
     } elseif (strpos($email, '$') !== false) {
         return $email;
@@ -204,7 +207,7 @@ function banana_plainTextToHtml($text, $strict = true)
     $text = banana_catchURLs($text);
     $text = banana_catchQuotes($text, $strict);
     $text = banana_catchSignature($text);
-    return banana_cleanHtml('<pre>' . $text . '</pre>');
+    return '<pre>' . $text . '</pre>';
 }
 
 function banana_wrap($text, $base_level = 0, $strict = true)
@@ -298,15 +301,15 @@ function banana_cleanStyles($tag, $attributes)
     } else {
         $style = '';
     }
-    $attributes = str_replace("\n", ' ', stripslashes($attributes));
-    $attributes = str_replace('= "', '="', $attributes);
+    $attributes = str_replace(array("\n", "\r"), ' ', stripslashes($attributes));
+    $attributes = str_replace(array('= "', '= \''), array('="', '=\''), $attributes);
     foreach ($conv as $att=>$stl) {
         $pattern = '/\b' . preg_quote($att, '/') . '=([\'"])?(.+?)(?(1)\1|(?:$| ))/i';
         if (preg_match($pattern, $attributes, $matches)) {
             $attributes = preg_replace($pattern, '', $attributes);
             $val = $matches[2];
             if ($att == 'cellspacing' && strpos($style, 'border-collapse') === false) {
-                $style .= "border-collapse: separate; border-spacing: $val $val; ";
+                $style = "border-collapse: separate; border-spacing: $val $val; " . $style;
             } elseif ($att == 'cellpadding' && $tag == 'table') {
                 $td_style[0] = "$stl: {$val}px; ";
             } elseif ($att == 'style') {
@@ -314,11 +317,11 @@ function banana_cleanStyles($tag, $attributes)
                 $style .= "$val; ";
             } elseif ($att == 'size') {
                 $val = $size_conv[$val];
-                $style .= "$stl: $val; ";
+                $style = "$stl: $val; " . $style;
             } elseif (is_numeric($val)) {
-                $style .= "$stl: {$val}px; ";
+                $style = "$stl: {$val}px; " . $style;
             } else {
-                $style .= "$stl: $val; ";
+                $style = "$stl: $val; " . $style;
             }
         }
     }
@@ -373,6 +376,7 @@ function banana_cleanHtml($source, $to_xhtml = false)
             }
             tidy_set_encoding('utf8');
             $source = tidy_repair_string($source);
+
         } else { // Tidy 2.0
             $source = tidy_repair_string($source, $tidy_config, 'utf8');
         }
@@ -405,7 +409,7 @@ function banana_cleanHtml($source, $to_xhtml = false)
 
     // Use inlined style instead of old html attributes
     if ($to_xhtml) {
-        $source = preg_replace('/<(\/?\w+)(.*?)(\/?>)/uise', "'<\\1' . banana_cleanStyles('\\1', '\\2') . '\\3'", $source);
+        $source = preg_replace('/<(\/?\w+)(.*?)(\/?>)/muise', "'<\\1' . banana_cleanStyles('\\1', '\\2') . '\\3'", $source);
     }    
     return preg_replace('/<(.*?)>/ie', "'<'.banana_removeEvilAttributes('\\1').'>'", $source);
 }