3 File: read_enriched.inc
5 License: GPL (part of IlohaMail)
6 Purpose: functions for handling text/enriched messages
7 Reference: RFC 1523, 1896
11 function enriched_convert_newlines($str){
12 //remove single newlines, convert N newlines to N-1
14 $str = str_replace("\r\n","\n",$str);
19 for($i=0;$i<$len;$i++){
21 if (ord($c)==10) $nl++;
22 if ($nl && ord($c)!=10) $nl = 0;
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>');
47 while(list($find,$replace)=each($a)){
48 $body = preg_replace('#'.$find.'#i', $replace, $body);
53 function enriched_font($body){
54 $pattern = '/(.*)\<fontfamily\>\<param\>(.*)\<\/param\>(.*)\<\/fontfamily\>(.*)/ims';
55 while(preg_match($pattern,$body,$a)){
57 if (count($a)!=5) continue;
58 $body=$a[1].'<span style="font-family: '.$a[2].'">'.$a[3].'</span>'.$a[4];
65 function enriched_color($body){
66 $pattern = '/(.*)\<color\>\<param\>(.*)\<\/param\>(.*)\<\/color\>(.*)/ims';
67 while(preg_match($pattern,$body,$a)){
69 if (count($a)!=5) continue;
71 //extract color (either by name, or ####,####,####)
72 if (strpos($a[2],',')){
73 $rgb = explode(',',$a[2]);
75 for($i=0;$i<3;$i++) $color.=substr($rgb[$i],0,2); //just take first 2 bytes
81 $body = $a[1].'<span style="color: '.$color.'">'.$a[3].'</span>'.$a[4];
87 function enriched_excerpt($body){
89 $pattern = '/(.*)\<excerpt\>(.*)\<\/excerpt\>(.*)/i';
90 while(preg_match($pattern,$body,$a)){
92 if (count($a)!=4) continue;
94 $lines = explode('<br>',$a[2]);
95 foreach($lines as $n=>$line) $quoted.='>'.$line.'<br>';
96 $body=$a[1].'<span class="quotes">'.$quoted.'</span>'.$a[3];
102 function enriched_to_html($body){
103 $body = str_replace('<<','<',$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);