1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32: if(get_magic_quotes_runtime()) set_magic_quotes_runtime(0);
33:
34: class TextSanitizer
35: {
36:
37: 38: 39: 40: 41: 42:
43: function TextSanitizer()
44: {
45:
46: }
47:
48: function &getInstance()
49: {
50: static $instance;
51: if (!isset($instance)) {
52: $instance = new TextSanitizer();
53: }
54: return $instance;
55: }
56:
57: function &makeClickable(&$text)
58: {
59: $patterns = array("/([^]_a-z0-9-=\"'\/])([a-z]+?):\/\/([^, \r\n\"\(\)'<>]+)/i", "/([^]_a-z0-9-=\"'\/])www\.([a-z0-9\-]+)\.([^, \r\n\"\(\)'<>]+)/i", "/([^]_a-z0-9-=\"'\/])([a-z0-9\-_.]+?)@([^, \r\n\"\(\)'<>]+)/i");
60: $replacements = array("\\1<a href=\"\\2://\\3\" rel=\"external\">\\2://\\3</a>", "\\1<a href=\"http://www.\\2.\\3\" rel=\"external\">www.\\2.\\3</a>", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>");
61: $ret = preg_replace($patterns, $replacements, $text);
62: return $ret;
63: }
64:
65: function &nl2Br($text)
66: {
67: $ret = preg_replace("/(\015\012)|(\015)|(\012)/","<br />",$text);
68: return $ret;
69: }
70:
71: function &addSlashes($text, $force=false)
72: {
73: if ($force) {
74: $ret = addslashes($text);
75: return $ret;
76: }
77: if (!get_magic_quotes_gpc()) {
78: $text = addslashes($text);
79: }
80: return $text;
81: }
82:
83: 84: 85:
86: function &stripSlashesGPC($text)
87: {
88: if (get_magic_quotes_gpc()) {
89: $text = stripslashes($text);
90: }
91: return $text;
92: }
93:
94: 95: 96:
97: function &htmlSpecialChars($text)
98: {
99: $text = preg_replace("/&/i", '&', htmlspecialchars($text, ENT_QUOTES));
100: return $text;
101: }
102:
103: function &undoHtmlSpecialChars(&$text)
104: {
105: $ret = preg_replace(array("/>/i", "/</i", "/"/i", "/'/i"), array(">", "<", "\"", "'"), $text);
106: return $ret;
107: }
108:
109: 110: 111:
112: function &displayText($text, $html=false)
113: {
114: if (! $html) {
115:
116: $text =& $this->htmlSpecialChars($text);
117: }
118: $text =& $this->makeClickable($text);
119: $text =& $this->nl2Br($text);
120: return $text;
121: }
122:
123: 124: 125:
126: function &previewText($text, $html=false)
127: {
128: $text =& $this->stripSlashesGPC($text);
129: return $this->displayText($text, $html);
130: }
131:
132:
133:
134: function sanitizeForDisplay($text, $allowhtml = 0, $smiley = 1, $bbcode = 1)
135: {
136: if ( $allowhtml == 0 ) {
137: $text = $this->htmlSpecialChars($text);
138: } else {
139:
140:
141:
142: $text = $this->makeClickable($text);
143: }
144: if ( $smiley == 1 ) {
145: $text = $this->smiley($text);
146: }
147: if ( $bbcode == 1 ) {
148: $text = $this->xoopsCodeDecode($text);
149: }
150: $text = $this->nl2Br($text);
151: return $text;
152: }
153:
154: function sanitizeForPreview($text, $allowhtml = 0, $smiley = 1, $bbcode = 1)
155: {
156: $text = $this->oopsStripSlashesGPC($text);
157: if ( $allowhtml == 0 ) {
158: $text = $this->htmlSpecialChars($text);
159: } else {
160:
161:
162:
163: $text = $this->makeClickable($text);
164: }
165: if ( $smiley == 1 ) {
166: $text = $this->smiley($text);
167: }
168: if ( $bbcode == 1 ) {
169: $text = $this->xoopsCodeDecode($text);
170: }
171: $text = $this->nl2Br($text);
172: return $text;
173: }
174:
175: function makeTboxData4Save($text)
176: {
177:
178: return $this->addSlashes($text);
179: }
180:
181: function makeTboxData4Show($text, $smiley=0)
182: {
183: $text = $this->htmlSpecialChars($text);
184: return $text;
185: }
186:
187: function makeTboxData4Edit($text)
188: {
189: return $this->htmlSpecialChars($text);
190: }
191:
192: function makeTboxData4Preview($text, $smiley=0)
193: {
194: $text = $this->stripSlashesGPC($text);
195: $text = $this->htmlSpecialChars($text);
196: return $text;
197: }
198:
199: function makeTboxData4PreviewInForm($text)
200: {
201: $text = $this->stripSlashesGPC($text);
202: return $this->htmlSpecialChars($text);
203: }
204:
205: function makeTareaData4Save($text)
206: {
207: return $this->addSlashes($text);
208: }
209:
210: function &makeTareaData4Show(&$text, $html=1, $smiley=1, $xcode=1)
211: {
212: return $this->displayTarea($text, $html, $smiley, $xcode);
213: }
214:
215: function makeTareaData4Edit($text)
216: {
217: return htmlSpecialChars($text, ENT_QUOTES);
218: }
219:
220: function &makeTareaData4Preview(&$text, $html=1, $smiley=1, $xcode=1)
221: {
222: return $this->previewTarea($text, $html, $smiley, $xcode);
223: }
224:
225: function makeTareaData4PreviewInForm($text)
226: {
227:
228: $text = $this->stripSlashesGPC($text);
229: return htmlSpecialChars($text, ENT_QUOTES);
230: }
231:
232: function makeTareaData4InsideQuotes($text)
233: {
234: return $this->htmlSpecialChars($text);
235: }
236:
237: function &oopsStripSlashesGPC($text)
238: {
239: return $this->stripSlashesGPC($text);
240: }
241:
242: function &oopsStripSlashesRT($text)
243: {
244: if (get_magic_quotes_runtime()) {
245: $text =& stripslashes($text);
246: }
247: return $text;
248: }
249:
250: function &oopsAddSlashes($text)
251: {
252: return $this->addSlashes($text);
253: }
254:
255: function &oopsHtmlSpecialChars($text)
256: {
257: return $this->htmlSpecialChars($text);
258: }
259:
260: function &oopsNl2Br($text)
261: {
262: return $this->nl2br($text);
263: }
264: }
265: ?>