4 * GForge Text Sanitizer Class
8 * This file is part of GForge.
10 * GForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * GForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with GForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /* Text Sanitizer Class
26 by Daniel Perez (danielperez.arg@gmail.com) - 2005
30 Class TextSanitizer extends Error {
34 * convertExtendedCharsForEmail - Grabs some text with html special characters and converts them to the corresponding character.
36 * @param string The input string
37 * @return string The output string
39 function convertExtendedCharsForEmail($text) {
40 $text = str_replace("´","'",$text); //it´s better to see that char in the email than the html entity
41 $text = str_replace("&","&",$text);
42 $text = str_replace(""",'"',$text);
43 $text = str_replace(" ",' ',$text);
44 $text = str_replace("<",'<',$text);
45 $text = str_replace(">",'>',$text);
46 $text = str_replace("°",'°',$text);
47 $text = str_replace("<br>","\n",$text);
48 $text = str_replace("<br />","\n",$text);
53 * convertNeededTagsForEmail - Grabs some text with html tags and those which are important for display (<br>, <p>) convert accordingly
55 * @param string The input string
56 * @return string The output string
58 function convertNeededTagsForEmail($text) {
59 $text = str_replace("<br>","\n",$text);
60 $text = str_replace("<br />","\n",$text);
61 $text = str_replace("<br/>","\n",$text);
65 function unhtmlentities ($string) {
66 $trans_tbl = get_html_translation_table (HTML_SPECIALCHARS );
67 $trans_tbl = array_flip ($trans_tbl );
68 $res = strtr ($string ,$trans_tbl );
69 $res = str_replace("&quot;",'"',$res);
74 * SanitizeHtml - Grabs some text with all kinds of html code and parses it to make it safe
76 * @param string The HTML Code
77 * @return string The HTML output
79 function SanitizeHtml($input) {
81 $input = htmlspecialchars($input); // first strip all chars
83 $input=str_replace('&','&',$input);
84 $input=str_replace('"','"',$input);
85 $input=str_replace('/>','/>',$input);
86 $input=str_replace('">','">',$input);
87 $input=str_replace('</a>','</a>',$input);
88 $input=str_replace('<strike>','<strike>',$input);
89 $input=str_replace('</strike>','</strike>',$input);
90 $input=str_replace('<sub>','<sub>',$input);
91 $input=str_replace('</sub>','</sub>',$input);
92 $input=str_replace('<span','<span',$input);
93 $input=str_replace('</span>','</span>',$input);
94 $input=str_replace('<font','<font',$input);
95 $input=str_replace('</font>','</font>',$input);
96 $input=str_replace('<hr>','<hr>',$input);
97 $input=str_replace('<hr','<hr',$input);
98 $input=str_replace('<br>','<br>',$input);
99 $input=str_replace('<br />','<br />',$input);
100 $input=str_replace('<tbody>','<tbody>',$input);
101 $input=str_replace('</tbody>','</tbody>',$input);
102 $input=str_replace('<tr>','<tr>',$input);
103 $input=str_replace('</tr>','</tr>',$input);
104 $input=str_replace('<td>','<td>',$input);
105 $input=str_replace('</td>','</td>',$input);
106 $input=str_replace('<td','<td',$input);
107 $input=str_replace('<table>','<table>',$input);
108 $input=str_replace('<table','<table',$input);
109 $input=str_replace('</table>','</table>',$input);
110 $input=str_replace('<div','<div',$input);
111 $input=str_replace('</div>','</div>',$input);
112 $input=str_replace('<u>','<u>',$input);
113 $input=str_replace('</u>','</u>',$input);
114 $input=str_replace('<p>','<p>',$input);
115 $input=str_replace('</p>','</p>',$input);
116 $input=str_replace('<p ','<p ',$input);
117 $input=str_replace('<li>','<li>',$input);
118 $input=str_replace('</li>','</li>',$input);
119 $input=str_replace('<ul>','<ul>',$input);
120 $input=str_replace('</ul>','</ul>',$input);
121 $input=str_replace('<ol>','<ol>',$input);
122 $input=str_replace('</ol>','</ol>',$input);
123 $input=str_replace('<blockquote>','<blockquote>',$input);
124 $input=str_replace('<blockquote','<blockquote',$input);
125 $input=str_replace('</blockquote>','</blockquote>',$input);
126 $input=str_replace('<em>','<em>',$input);
127 $input=str_replace('</em>','</em>',$input);
128 $input=str_replace('<strong>','<strong>',$input);
129 $input=str_replace('</strong>','</strong>',$input);
130 $input=str_replace('<sup>','<sup>',$input);
131 $input=str_replace('</sup>','</sup>',$input);
132 $input=str_replace('<input ','<input ',$input);
133 $input=str_replace('<img ','<img ',$input);
134 $input=str_replace('<textarea ','<textarea ',$input);
135 $input=str_replace('</textarea>','</textarea>',$input);
136 $input=str_replace('<a href','<a href',$input);
137 $input=str_replace('<h1>','<h1>',$input);
138 $input=str_replace('</h1>','</h1>',$input);
139 $input=str_replace('<h2>','<h2>',$input);
140 $input=str_replace('</h2>','</h2>',$input);
141 $input=str_replace('<h3>','<h3>',$input);
142 $input=str_replace('</h3>','</h3>',$input);
143 $input=str_replace('<h4>','<h4>',$input);
144 $input=str_replace('</h4>','</h4>',$input);
145 $input=str_replace('<h5>','<h5>',$input);
146 $input=str_replace('</h5>','</h5>',$input);
147 $input=str_replace('<h6>','<h6>',$input);
148 $input=str_replace('</h6>','</h6>',$input);
149 $input=str_replace('<pre>','<pre>',$input);
150 $input=str_replace('</pre>','</pre>',$input);
151 $input=str_replace('<h1 ','<h1 ',$input);
152 $input=str_replace('<h2 ','<h2 ',$input);
153 $input=str_replace('<h3 ','<h3 ',$input);
154 $input=str_replace('<h4 ','<h4 ',$input);
155 $input=str_replace('<h5 ','<h5 ',$input);
156 $input=str_replace('<h6 ','<h6 ',$input);
164 // c-file-style: "bsd"