]> git.donarmstrong.com Git - roundcube.git/blob - program/lib/enriched.inc
Imported Upstream version 0.1~beta2.2~dfsg
[roundcube.git] / program / lib / enriched.inc
1 <?php
2 /*
3         File:           read_enriched.inc
4         Author:         Ryo Chijiiwa
5         License:        GPL (part of IlohaMail)
6         Purpose:        functions for handling text/enriched messages
7         Reference:      RFC 1523, 1896
8 */
9
10
11 function enriched_convert_newlines($str){
12         //remove single newlines, convert N newlines to N-1
13         
14         $str = str_replace("\r\n","\n",$str);
15         $len = strlen($str);
16         
17         $nl = 0;
18         $out = '';
19         for($i=0;$i<$len;$i++){
20                 $c = $str[$i];
21                 if (ord($c)==10) $nl++;
22                 if ($nl && ord($c)!=10) $nl = 0;
23                 if ($nl!=1) $out.=$c;
24                 else $out.=' ';         
25         }
26         return $out;
27 }
28
29 function enriched_convert_formatting($body){
30         $a=array('<bold>'=>'<b>','</bold>'=>'</b>','<italic>'=>'<i>',
31                         '</italic>'=>'</i>','<fixed>'=>'<tt>','</fixed>'=>'</tt>',
32                         '<smaller>'=>'<font size=-1>','</smaller>'=>'</font>',
33                         '<bigger>'=>'<font size=+1>','</bigger>'=>'</font>',
34                         '<underline>'=>'<span style="text-decoration: underline">',
35                         '</underline>'=>'</span>',
36                         '<flushleft>'=>'<span style="text-align:left">',
37                         '</flushleft>'=>'</span>',
38                         '<flushright>'=>'<span style="text-align:right">',
39                         '</flushright>'=>'</span>',
40                         '<flushboth>'=>'<span style="text-align:justified">',
41                         '</flushboth>'=>'</span>',
42                         '<indent>'=>'<span style="padding-left: 20px">',
43                         '</indent>'=>'</span>',
44                         '<indentright>'=>'<span style="padding-right: 20px">',
45                         '</indentright>'=>'</span>');
46         
47         while(list($find,$replace)=each($a)){
48                 $body = eregi_replace($find,$replace,$body);
49         }
50         return $body;
51 }
52
53 function enriched_font($body){
54         $pattern = '/(.*)\<fontfamily\>\<param\>(.*)\<\/param\>(.*)\<\/fontfamily\>(.*)/ims';
55         while(preg_match($pattern,$body,$a)){
56                 //print_r($a);
57                 if (count($a)!=5) continue;
58                 $body=$a[1].'<span style="font-family: '.$a[2].'">'.$a[3].'</span>'.$a[4];
59         }
60
61         return $body;
62 }
63
64
65 function enriched_color($body){
66         $pattern = '/(.*)\<color\>\<param\>(.*)\<\/param\>(.*)\<\/color\>(.*)/ims';
67         while(preg_match($pattern,$body,$a)){
68                 //print_r($a);
69                 if (count($a)!=5) continue;
70
71                 //extract color (either by name, or ####,####,####)
72                 if (strpos($a[2],',')){
73                         $rgb = explode(',',$a[2]);
74                         $color ='#';
75                         for($i=0;$i<3;$i++) $color.=substr($rgb[$i],0,2); //just take first 2 bytes
76                 }else{
77                         $color = $a[2];
78                 }
79                 
80                 //put it all together
81                 $body = $a[1].'<span style="color: '.$color.'">'.$a[3].'</span>'.$a[4];
82         }
83
84         return $body;
85 }
86
87 function enriched_excerpt($body){
88
89         $pattern = '/(.*)\<excerpt\>(.*)\<\/excerpt\>(.*)/i';
90         while(preg_match($pattern,$body,$a)){
91                 //print_r($a);
92                 if (count($a)!=4) continue;
93                 $quoted = '';
94                 $lines = explode('<br>',$a[2]);
95                 foreach($lines as $n=>$line) $quoted.='&gt;'.$line.'<br>';
96                 $body=$a[1].'<span class="quotes">'.$quoted.'</span>'.$a[3];
97         }
98
99         return $body;
100 }
101
102 function enriched_to_html($body){
103         $body = str_replace('<<','&lt;',$body);
104         $body = enriched_convert_newlines($body);
105         $body = str_replace("\n", '<br>', $body);
106         $body = enriched_convert_formatting($body);
107         $body = enriched_color($body);
108         $body = enriched_font($body);
109         $body = enriched_excerpt($body);
110         //$body = nl2br($body);
111         return $body;
112 }
113
114 ?>