Aleksander Machniak
2012-12-07 996af3bfd9bfcac84396790a9a215d177b17c79e
commit | author | age
0fa54d 1 <?php
AM 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_enriched.php                                    |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
9  |                                                                       |
10  | Licensed under the GNU General Public License version 3 or            |
11  | any later version with exceptions for skins & plugins.                |
12  | See the README file for a full license statement.                     |
13  |                                                                       |
14  | PURPOSE:                                                              |
15  |   Helper class to convert Enriched to HTML format (RFC 1523, 1896)    |
16  |                                                                       |
17  +-----------------------------------------------------------------------+
18  | Author: Aleksander Machniak <alec@alec.pl>                            |
19  | Author: Ryo Chijiiwa (IlohaMail)                                      |
20  +-----------------------------------------------------------------------+
21 */
22
23
24 /**
25  * Class for Enriched to HTML conversion
26  *
27  * @package    Framework
28  * @subpackage Utils
29  */
30 class rcube_enriched
31 {
32     protected static function convert_newlines($body)
33     {
34         // remove single newlines, convert N newlines to N-1
35         $body = str_replace("\r\n", "\n", $body);
36         $len  = strlen($body);
37         $nl   = 0;
38         $out  = '';
39
40         for ($i=0; $i<$len; $i++) {
41             $c = $body[$i];
42             if (ord($c) == 10)
43                 $nl++;
44             if ($nl && ord($c) != 10)
45                 $nl = 0;
46             if ($nl != 1)
47                 $out .= $c;
48             else
49                 $out .= ' ';
50         }
51
52         return $out;
53     }
54
55     protected static function convert_formatting($body)
56     {
57         $replace = array(
58             '<bold>'        => '<b>',            '</bold>'   => '</b>',
59             '<italic>'      => '<i>',            '</italic>' => '</i>',
60             '<fixed>'       => '<tt>',           '</fixed>'  => '</tt>',
61             '<smaller>'     => '<font size=-1>', '</smaller>'=> '</font>',
62             '<bigger>'      => '<font size=+1>', '</bigger>' => '</font>',
63             '<underline>'   => '<span style="text-decoration: underline">', '</underline>'   => '</span>',
64             '<flushleft>'   => '<span style="text-align: left">',           '</flushleft>'   => '</span>',
65             '<flushright>'  => '<span style="text-align: right">',          '</flushright>'  => '</span>',
66             '<flushboth>'   => '<span style="text-align: justified">',      '</flushboth>'   => '</span>',
67             '<indent>'      => '<span style="padding-left: 20px">',         '</indent>'      => '</span>',
68             '<indentright>' => '<span style="padding-right: 20px">',        '</indentright>' => '</span>',
69         );
70
71         return str_ireplace(array_keys($replace), array_values($replace), $body);
72     }
73
74     protected static function convert_font($body)
75     {
76         $pattern = '/(.*)\<fontfamily\>\<param\>(.*)\<\/param\>(.*)\<\/fontfamily\>(.*)/ims';
77
78         while (preg_match($pattern, $body, $a)) {
79             if (count($a) != 5)
80                 continue;
81
82             $body = $a[1].'<span style="font-family: '.$a[2].'">'.$a[3].'</span>'.$a[4];
83         }
84
85         return $body;
86     }
87
88     protected static function convert_color($body)
89     {
90         $pattern = '/(.*)\<color\>\<param\>(.*)\<\/param\>(.*)\<\/color\>(.*)/ims';
91
92         while (preg_match($pattern, $body, $a)) {
93             if (count($a) != 5)
94                 continue;
95
96             // extract color (either by name, or ####,####,####)
97             if (strpos($a[2],',')) {
98                 $rgb   = explode(',',$a[2]);
99                 $color = '#';
100                 for ($i=0; $i<3; $i++)
101                     $color .= substr($rgb[$i], 0, 2); // just take first 2 bytes
102             }
103             else {
104                 $color = $a[2];
105             }
106
107             // put it all together
108             $body = $a[1].'<span style="color: '.$color.'">'.$a[3].'</span>'.$a[4];
109         }
110
111         return $body;
112     }
113
114     protected static function convert_excerpt($body)
115     {
116         $pattern = '/(.*)\<excerpt\>(.*)\<\/excerpt\>(.*)/i';
117
118         while (preg_match($pattern, $body, $a)) {
119             if (count($a) != 4)
120                 continue;
121
122             $quoted = '';
123             $lines  = explode('<br>', $a[2]);
124
125             foreach ($lines as $n => $line)
126                 $quoted .= '&gt;'.$line.'<br>';
127
128             $body = $a[1].'<span class="quotes">'.$quoted.'</span>'.$a[3];
129         }
130
131         return $body;
132     }
133
134     public static function to_html($body)
135     {
136         $body = str_replace('<<','&lt;',$body);
137         $body = self::convert_newlines($body);
138         $body = str_replace("\n", '<br>', $body);
139         $body = self::convert_formatting($body);
140         $body = self::convert_color($body);
141         $body = self::convert_font($body);
142         $body = self::convert_excerpt($body);
143         //$body = nl2br($body);
144
145         return $body;
146     }
147 }