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