[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * The MIME_Viewer_enriched class renders out plain text from 4 * enriched content tags, ala RFC 1896. 5 * 6 * By RFC, we must do the minimal conformance measures of: A minimal 7 * text/enriched implementation is one that converts "<<" to "<", 8 * removes everything between a <param> command and the next balancing 9 * </param> removes all other formatting commands (all text enclosed 10 * in angle brackets), and outside of <nofill> environments converts 11 * any series of n CRLFs to n-1 CRLFs, and converts any lone CRLF 12 * pairs to SPACE. 13 * 14 * We don't qualify as we don't currently track the <nofill> 15 * environment, that is we do CRLF conversion even if <nofill> is 16 * specified in the text, but we're close at least. 17 * 18 * $Horde: framework/MIME/MIME/Viewer/enriched.php,v 1.23.10.6 2006/01/01 21:28:25 jan Exp $ 19 * 20 * Copyright 2001-2006 Eric Rostetter <eric.rostetter@physics.utexas.edu> 21 * 22 * See the enclosed file COPYING for license information (LGPL). If you 23 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 24 * 25 * @author Eric Rostetter <eric.rostetter@physics.utexas.edu> 26 * @since Horde 2.1 27 * @package Horde_MIME_Viewer 28 */ 29 class MIME_Viewer_enriched extends MIME_Viewer { 30 31 /** 32 * Render out the currently set contents in HTML format. The 33 * $mime_part class variable has the information to render out, 34 * encapsulated in a MIME_Part object. 35 */ 36 function render() 37 { 38 if (($text = $this->mime_part->getContents()) === false) { 39 return false; 40 } 41 42 if (trim($text) == '') { 43 return $text; 44 } 45 46 // We add space at the beginning and end of the string as it will 47 // make some regular expression checks later much easier (so we 48 // don't have to worry about start/end of line characters) 49 $text = ' ' . $text . ' '; 50 51 // We need to preserve << tags, so map them to ascii 1 or ascii 255 52 // We make the assumption here that there would never be an ascii 53 // 1 in an email, which may not be valid, but seems reasonable... 54 // ascii 255 would work if for some reason you don't like ascii 1 55 // ascii 0 does NOT seem to work for this, though I'm not sure why 56 $text = str_replace('<<', chr(1), $text); 57 58 // Remove any unrecognized tags in the text (via RFC minimal specs) 59 // any tags we just don't want to implement can also be removed here 60 // Note that this will remove any html links, but this is intended 61 $implementedTags = '<param><bold><italic><underline><fixed><excerpt>' . 62 '<smaller><bigger><center><color><fontfamily>' . 63 '<flushleft><flushright><flushboth><paraindent>'; 64 // $unImplementedTags = '<nofill><lang>'; 65 $text = strip_tags($text, $implementedTags); 66 67 // restore the << tags as < tags now... 68 $text = str_replace(chr(1), '<<', $text); 69 // $text = str_replace(chr(255), '<', $text); 70 71 // Get color parameters into a more useable format. 72 $text = preg_replace('/<color><param>([\da-fA-F]+),([\da-fA-F]+),([\da-fA-F]+)<\/param>/Uis', '<color r=\1 g=\2 b=\3>', $text); 73 $text = preg_replace('/<color><param>(red|blue|green|yellow|cyan|magenta|black|white)<\/param>/Uis', '<color n=\1>', $text); 74 75 // Get font family parameters into a more useable format. 76 $text = preg_replace('/<fontfamily><param>(\w+)<\/param>/Uis', '<fontfamily f=\1>', $text); 77 78 // Just remove any remaining parameters -- we won't use 79 // them. Any tags with parameters that we want to implement 80 // will have to come before this Someday we hope to use these 81 // tags (e.g. for <color><param> tags) 82 $text = preg_replace('/<param>.*<\/param>/Uis', '', $text); 83 84 // Single line breaks become spaces, double line breaks are a 85 // real break. This needs to do <nofill> tracking to be 86 // compliant but we don't want to deal with state at this 87 // time, so we fake it some day we should rewrite this to 88 // handle <nofill> correctly. 89 $text = preg_replace('/([^\n])\r\n([^\r])/', '\1 \2', $text); 90 $text = preg_replace('/(\r\n)\r\n/', '\1', $text); 91 92 // We try to protect against bad stuff here. 93 $text = @htmlspecialchars($text, ENT_QUOTES, $this->mime_part->getCharset()); 94 95 // Now convert the known tags to html. Try to remove any tag 96 // parameters to stop people from trying to pull a fast one 97 $text = preg_replace('/(?<!<)<bold.*>(.*)<\/bold>/Uis', '<span style="font-weight: bold">\1</span>', $text); 98 $text = preg_replace('/(?<!<)<italic.*>(.*)<\/italic>/Uis', '<span style="font-style: italic">\1</span>', $text); 99 $text = preg_replace('/(?<!<)<underline.*>(.*)<\/underline>/Uis', '<span style="text-decoration: underline">\1</span>', $text); 100 $text = preg_replace_callback('/(?<!<)<color r=([\da-fA-F]+) g=([\da-fA-F]+) b=([\da-fA-F]+)>(.*)<\/color>/Uis', array($this, 'colorize'), $text); 101 $text = preg_replace('/(?<!<)<color n=(red|blue|green|yellow|cyan|magenta|black|white)>(.*)<\/color>/Uis', '<span style="color: \1">\2</span>', $text); 102 $text = preg_replace('/(?<!<)<fontfamily>(.*)<\/fontfamily>/Uis', '\1', $text); 103 $text = preg_replace('/(?<!<)<fontfamily f=(\w+)>(.*)<\/fontfamily>/Uis', '<span style="font-family: \1">\2</span>', $text); 104 $text = preg_replace('/(?<!<)<smaller.*>/Uis', '<span style="font-size: smaller">', $text); 105 $text = preg_replace('/(?<!<)<\/smaller>/Uis', '</span>', $text); 106 $text = preg_replace('/(?<!<)<bigger.*>/Uis', '<span style="font-size: larger">', $text); 107 $text = preg_replace('/(?<!<)<\/bigger>/Uis', '</span>', $text); 108 $text = preg_replace('/(?<!<)<fixed.*>(.*)<\/fixed>/Uis', '<font face="fixed">\1</font>', $text); 109 $text = preg_replace('/(?<!<)<center.*>(.*)<\/center>/Uis', '<div align="center">\1</div>', $text); 110 $text = preg_replace('/(?<!<)<flushleft.*>(.*)<\/flushleft>/Uis', '<div align="left">\1</div>', $text); 111 $text = preg_replace('/(?<!<)<flushright.*>(.*)<\/flushright>/Uis', '<div align="right">\1</div>', $text); 112 $text = preg_replace('/(?<!<)<flushboth.*>(.*)<\/flushboth>/Uis', '<div align="justify">\1</div>', $text); 113 $text = preg_replace('/(?<!<)<paraindent.*>(.*)<\/paraindent>/Uis', '<blockquote>\1</blockquote>', $text); 114 $text = preg_replace('/(?<!<)<excerpt.*>(.*)<\/excerpt>/Uis', '<blockquote>\1</blockquote>', $text); 115 116 // Replace << with < now (from translated HTML form). 117 $text = str_replace('<<', '<', $text); 118 119 // Now we remove the leading/trailing space we added at the 120 // start. 121 $text = preg_replace('/^ (.*) $/s', '\1', $text); 122 123 // Make URLs clickable. 124 require_once 'Horde/Text/Filter.php'; 125 $text = Text_Filter::filter($text, 'linkurls', array('callback' => 'Horde::externalUrl')); 126 127 // Wordwrap -- note this could impact on our above RFC 128 // compliance *IF* we honored nofill tags (which we don't 129 // yet). 130 $text = str_replace("\t", ' ', $text); 131 $text = str_replace(' ', ' ', $text); 132 $text = str_replace("\n ", "\n ", $text); 133 if ($text[0] == ' ') { 134 $text = ' ' . substr($text, 1); 135 } 136 $text = nl2br($text); 137 $text = '<p class="fixed">' . $text . '</p>'; 138 139 return $text; 140 } 141 142 function colorize($colors) 143 { 144 for ($i = 1; $i < 4; $i++) { 145 $colors[$i] = sprintf('%02X', round(hexdec($colors[$i]) / 255)); 146 } 147 return '<span style="color: #' . $colors[1] . $colors[2] . $colors[3] . '">' . $colors[4] . '</span>'; 148 } 149 150 /** 151 * Return the MIME content type of the rendered content. 152 * 153 * @return string The content type of the output. 154 */ 155 function getType() 156 { 157 return 'text/html; charset=' . NLS::getCharset(); 158 } 159 160 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |