From 0fa54df638a0b0f514d1bfba3cefb93e38991a35 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sat, 01 Dec 2012 14:02:34 -0500
Subject: [PATCH] enriched.inc -> rcube_enriched

---
 /dev/null                                |  114 -------------------
 program/include/bc.php                   |    6 
 program/steps/mail/compose.inc           |    9 -
 program/steps/mail/func.inc              |    3 
 tests/phpunit.xml                        |    1 
 tests/Framework/Enriched.php             |   74 ++++++++++++
 program/lib/Roundcube/rcube_enriched.php |  147 ++++++++++++++++++++++++
 7 files changed, 231 insertions(+), 123 deletions(-)

diff --git a/program/include/bc.php b/program/include/bc.php
index 5047e0a..12110c0 100644
--- a/program/include/bc.php
+++ b/program/include/bc.php
@@ -399,7 +399,11 @@
     return rcube_utils::get_boolean($str);
 }
 
+function enriched_to_html($data)
+{
+    return rcube_enriched::to_html($data);
+}
+
 class rcube_html_page extends rcmail_html_page
 {
-    
 }
diff --git a/program/lib/Roundcube/rcube_enriched.php b/program/lib/Roundcube/rcube_enriched.php
new file mode 100644
index 0000000..8b64fe0
--- /dev/null
+++ b/program/lib/Roundcube/rcube_enriched.php
@@ -0,0 +1,147 @@
+<?php
+
+/*
+ +-----------------------------------------------------------------------+
+ | program/include/rcube_enriched.php                                    |
+ |                                                                       |
+ | This file is part of the Roundcube Webmail client                     |
+ | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
+ |                                                                       |
+ | Licensed under the GNU General Public License version 3 or            |
+ | any later version with exceptions for skins & plugins.                |
+ | See the README file for a full license statement.                     |
+ |                                                                       |
+ | PURPOSE:                                                              |
+ |   Helper class to convert Enriched to HTML format (RFC 1523, 1896)    |
+ |                                                                       |
+ +-----------------------------------------------------------------------+
+ | Author: Aleksander Machniak <alec@alec.pl>                            |
+ | Author: Ryo Chijiiwa (IlohaMail)                                      |
+ +-----------------------------------------------------------------------+
+*/
+
+
+/**
+ * Class for Enriched to HTML conversion
+ *
+ * @package    Framework
+ * @subpackage Utils
+ */
+class rcube_enriched
+{
+    protected static function convert_newlines($body)
+    {
+        // remove single newlines, convert N newlines to N-1
+        $body = str_replace("\r\n", "\n", $body);
+        $len  = strlen($body);
+        $nl   = 0;
+        $out  = '';
+
+        for ($i=0; $i<$len; $i++) {
+            $c = $body[$i];
+            if (ord($c) == 10)
+                $nl++;
+            if ($nl && ord($c) != 10)
+                $nl = 0;
+            if ($nl != 1)
+                $out .= $c;
+            else
+                $out .= ' ';
+        }
+
+        return $out;
+    }
+
+    protected static function convert_formatting($body)
+    {
+        $replace = array(
+            '<bold>'        => '<b>',            '</bold>'   => '</b>',
+            '<italic>'      => '<i>',            '</italic>' => '</i>',
+            '<fixed>'       => '<tt>',           '</fixed>'  => '</tt>',
+            '<smaller>'     => '<font size=-1>', '</smaller>'=> '</font>',
+            '<bigger>'      => '<font size=+1>', '</bigger>' => '</font>',
+            '<underline>'   => '<span style="text-decoration: underline">', '</underline>'   => '</span>',
+            '<flushleft>'   => '<span style="text-align: left">',           '</flushleft>'   => '</span>',
+            '<flushright>'  => '<span style="text-align: right">',          '</flushright>'  => '</span>',
+            '<flushboth>'   => '<span style="text-align: justified">',      '</flushboth>'   => '</span>',
+            '<indent>'      => '<span style="padding-left: 20px">',         '</indent>'      => '</span>',
+            '<indentright>' => '<span style="padding-right: 20px">',        '</indentright>' => '</span>',
+        );
+
+        return str_ireplace(array_keys($replace), array_values($replace), $body);
+    }
+
+    protected static function convert_font($body)
+    {
+        $pattern = '/(.*)\<fontfamily\>\<param\>(.*)\<\/param\>(.*)\<\/fontfamily\>(.*)/ims';
+
+        while (preg_match($pattern, $body, $a)) {
+            if (count($a) != 5)
+                continue;
+
+            $body = $a[1].'<span style="font-family: '.$a[2].'">'.$a[3].'</span>'.$a[4];
+        }
+
+        return $body;
+    }
+
+    protected static function convert_color($body)
+    {
+        $pattern = '/(.*)\<color\>\<param\>(.*)\<\/param\>(.*)\<\/color\>(.*)/ims';
+
+        while (preg_match($pattern, $body, $a)) {
+            if (count($a) != 5)
+                continue;
+
+            // extract color (either by name, or ####,####,####)
+            if (strpos($a[2],',')) {
+                $rgb   = explode(',',$a[2]);
+                $color = '#';
+                for ($i=0; $i<3; $i++)
+                    $color .= substr($rgb[$i], 0, 2); // just take first 2 bytes
+            }
+            else {
+                $color = $a[2];
+            }
+
+            // put it all together
+            $body = $a[1].'<span style="color: '.$color.'">'.$a[3].'</span>'.$a[4];
+        }
+
+        return $body;
+    }
+
+    protected static function convert_excerpt($body)
+    {
+        $pattern = '/(.*)\<excerpt\>(.*)\<\/excerpt\>(.*)/i';
+
+        while (preg_match($pattern, $body, $a)) {
+            if (count($a) != 4)
+                continue;
+
+            $quoted = '';
+            $lines  = explode('<br>', $a[2]);
+
+            foreach ($lines as $n => $line)
+                $quoted .= '&gt;'.$line.'<br>';
+
+            $body = $a[1].'<span class="quotes">'.$quoted.'</span>'.$a[3];
+        }
+
+        return $body;
+    }
+
+    public static function to_html($body)
+    {
+        $body = str_replace('<<','&lt;',$body);
+        $body = self::convert_newlines($body);
+        $body = str_replace("\n", '<br>', $body);
+        $body = self::convert_formatting($body);
+        $body = self::convert_color($body);
+        $body = self::convert_font($body);
+        $body = self::convert_excerpt($body);
+        //$body = nl2br($body);
+
+        return $body;
+    }
+}
diff --git a/program/lib/enriched.inc b/program/lib/enriched.inc
deleted file mode 100644
index e3abd8c..0000000
--- a/program/lib/enriched.inc
+++ /dev/null
@@ -1,114 +0,0 @@
-<?php
-/*
-	File:		read_enriched.inc
-	Author: 	Ryo Chijiiwa
-	License:	GPL (part of IlohaMail)
-	Purpose: 	functions for handling text/enriched messages
-	Reference: 	RFC 1523, 1896
-*/
-
-
-function enriched_convert_newlines($str){
-	//remove single newlines, convert N newlines to N-1
-	
-	$str = str_replace("\r\n","\n",$str);
-	$len = strlen($str);
-	
-	$nl = 0;
-	$out = '';
-	for($i=0;$i<$len;$i++){
-		$c = $str[$i];
-		if (ord($c)==10) $nl++;
-		if ($nl && ord($c)!=10) $nl = 0;
-		if ($nl!=1) $out.=$c;
-		else $out.=' ';		
-	}
-	return $out;
-}
-
-function enriched_convert_formatting($body){
-	$a=array('<bold>'=>'<b>','</bold>'=>'</b>','<italic>'=>'<i>',
-			'</italic>'=>'</i>','<fixed>'=>'<tt>','</fixed>'=>'</tt>',
-			'<smaller>'=>'<font size=-1>','</smaller>'=>'</font>',
-			'<bigger>'=>'<font size=+1>','</bigger>'=>'</font>',
-			'<underline>'=>'<span style="text-decoration: underline">',
-			'</underline>'=>'</span>',
-			'<flushleft>'=>'<span style="text-align:left">',
-			'</flushleft>'=>'</span>',
-			'<flushright>'=>'<span style="text-align:right">',
-			'</flushright>'=>'</span>',
-			'<flushboth>'=>'<span style="text-align:justified">',
-			'</flushboth>'=>'</span>',
-			'<indent>'=>'<span style="padding-left: 20px">',
-			'</indent>'=>'</span>',
-			'<indentright>'=>'<span style="padding-right: 20px">',
-			'</indentright>'=>'</span>');
-	
-	while(list($find,$replace)=each($a)){
-		$body = preg_replace('#'.$find.'#i', $replace, $body);
-	}
-	return $body;
-}
-
-function enriched_font($body){
-	$pattern = '/(.*)\<fontfamily\>\<param\>(.*)\<\/param\>(.*)\<\/fontfamily\>(.*)/ims';
-	while(preg_match($pattern,$body,$a)){
-		//print_r($a);
-		if (count($a)!=5) continue;
-		$body=$a[1].'<span style="font-family: '.$a[2].'">'.$a[3].'</span>'.$a[4];
-	}
-
-	return $body;
-}
-
-
-function enriched_color($body){
-	$pattern = '/(.*)\<color\>\<param\>(.*)\<\/param\>(.*)\<\/color\>(.*)/ims';
-	while(preg_match($pattern,$body,$a)){
-		//print_r($a);
-		if (count($a)!=5) continue;
-
-		//extract color (either by name, or ####,####,####)
-		if (strpos($a[2],',')){
-			$rgb = explode(',',$a[2]);
-			$color ='#';
-			for($i=0;$i<3;$i++) $color.=substr($rgb[$i],0,2); //just take first 2 bytes
-		}else{
-			$color = $a[2];
-		}
-		
-		//put it all together
-		$body = $a[1].'<span style="color: '.$color.'">'.$a[3].'</span>'.$a[4];
-	}
-
-	return $body;
-}
-
-function enriched_excerpt($body){
-
-	$pattern = '/(.*)\<excerpt\>(.*)\<\/excerpt\>(.*)/i';
-	while(preg_match($pattern,$body,$a)){
-		//print_r($a);
-		if (count($a)!=4) continue;
-		$quoted = '';
-		$lines = explode('<br>',$a[2]);
-		foreach($lines as $n=>$line) $quoted.='&gt;'.$line.'<br>';
-		$body=$a[1].'<span class="quotes">'.$quoted.'</span>'.$a[3];
-	}
-
-	return $body;
-}
-
-function enriched_to_html($body){
-	$body = str_replace('<<','&lt;',$body);
-	$body = enriched_convert_newlines($body);
-	$body = str_replace("\n", '<br>', $body);
-	$body = enriched_convert_formatting($body);
-	$body = enriched_color($body);
-	$body = enriched_font($body);
-	$body = enriched_excerpt($body);
-	//$body = nl2br($body);
-	return $body;
-}
-
-?>
\ No newline at end of file
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index c039e42..96391c8 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -733,8 +733,7 @@
         if ($part->ctype_secondary == 'html') {
         }
         else if ($part->ctype_secondary == 'enriched') {
-            require_once(INSTALL_PATH . 'program/lib/enriched.inc');
-            $body = enriched_to_html($body);
+            $body = rcube_enriched::to_html($body);
         }
         else {
             // try to remove the signature
@@ -750,8 +749,7 @@
     }
     else {
         if ($part->ctype_secondary == 'enriched') {
-            require_once(INSTALL_PATH . 'program/lib/enriched.inc');
-            $body = enriched_to_html($body);
+            $body = rcube_enriched::to_html($body);
             $part->ctype_secondary = 'html';
         }
 
@@ -763,8 +761,7 @@
             $body = $txt->get_text();
         }
         else if ($part->ctype_secondary == 'enriched') {
-            require_once(INSTALL_PATH . 'program/lib/enriched.inc');
-            $body = enriched_to_html($body);
+            $body = rcube_enriched::to_html($body);
         }
         else {
             if ($part->ctype_secondary == 'plain' && $part->ctype_parameters['format'] == 'flowed') {
diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index cb1a5dd..80dac71 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -751,8 +751,7 @@
   }
   // text/enriched
   else if ($data['type'] == 'enriched') {
-    require_once(INSTALL_PATH . 'program/lib/enriched.inc');
-    $body = enriched_to_html($data['body']);
+    $body = rcube_enriched::to_html($data['body']);
     $body = rcmail_wash_html($body, $data, $part->replaces);
     $part->ctype_secondary = 'html';
   }
diff --git a/tests/Framework/Enriched.php b/tests/Framework/Enriched.php
new file mode 100644
index 0000000..26bbc3b
--- /dev/null
+++ b/tests/Framework/Enriched.php
@@ -0,0 +1,74 @@
+<?php
+
+/**
+ * Test class to test rcube_enriched class
+ *
+ * @package Tests
+ */
+class Framework_Enriched extends PHPUnit_Framework_TestCase
+{
+
+    /**
+     * Class constructor
+     */
+    function test_class()
+    {
+        $object = new rcube_enriched();
+
+        $this->assertInstanceOf('rcube_enriched', $object, "Class constructor");
+    }
+
+    /**
+     * Test to_html()
+     */
+    function test_to_html()
+    {
+        $enriched = '<bold><italic>the-text</italic></bold>';
+        $expected = '<b><i>the-text</i></b>';
+        $result   = rcube_enriched::to_html($enriched);
+
+        $this->assertSame($expected, $result);
+    }
+
+    /**
+     * Data for test_formatting()
+     */
+    function data_formatting()
+    {
+        return array(
+            array('<bold>', '<b>'),
+            array('</bold>', '</b>'),
+            array('<italic>', '<i>'),
+            array('</italic>', '</i>'),
+            array('<fixed>', '<tt>'),
+            array('</fixed>', '</tt>'),
+            array('<smaller>', '<font size=-1>'),
+            array('</smaller>', '</font>'),
+            array('<bigger>', '<font size=+1>'),
+            array('</bigger>', '</font>'),
+            array('<underline>', '<span style="text-decoration: underline">'),
+            array('</underline>', '</span>'),
+            array('<flushleft>', '<span style="text-align: left">'),
+            array('</flushleft>', '</span>'),
+            array('<flushright>', '<span style="text-align: right">'),
+            array('</flushright>', '</span>'),
+            array('<flushboth>', '<span style="text-align: justified">'),
+            array('</flushboth>', '</span>'),
+            array('<indent>', '<span style="padding-left: 20px">'),
+            array('</indent>', '</span>'),
+            array('<indentright>', '<span style="padding-right: 20px">'),
+            array('</indentright>', '</span>'),
+        );
+    }
+
+    /**
+     * Test formatting conversion
+     * @dataProvider data_formatting
+     */
+    function test_formatting($enriched, $expected)
+    {
+        $result = rcube_enriched::to_html($enriched);
+
+        $this->assertSame($expected, $result);
+    }
+}
diff --git a/tests/phpunit.xml b/tests/phpunit.xml
index 36ab6d7..c9e229e 100644
--- a/tests/phpunit.xml
+++ b/tests/phpunit.xml
@@ -10,6 +10,7 @@
             <file>Framework/Charset.php</file>
             <file>Framework/ContentFilter.php</file>
             <file>Framework/Csv2vcard.php</file>
+            <file>Framework/Enriched.php</file>
             <file>Framework/Html.php</file>
             <file>Framework/Imap.php</file>
             <file>Framework/ImapGeneric.php</file>

--
Gitblit v1.9.1