XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
textsanitizer.php
1<?php
14
15
17
18 /*
19 * Constructor of this class
20 * Gets allowed html tags from admin config settings
21 * <br> should not be allowed since nl2br will be used
22 * when storing data
23 */
24
25 public static function getInstance() {
26 static $instance;
27 if ( ! isset( $instance ) ) {
28 $instance = new TextSanitizer();
29 }
30
31 return $instance;
32 }
33
34 public function &makeClickable( &$text ) {
35 // Ensure proper UTF-8 encoding
36 if (!mb_check_encoding($text, 'UTF-8')) {
37 $text = mb_convert_encoding($text, 'UTF-8', 'auto');
38 }
39
40 $patterns = [
41 "/([^]_a-z0-9-=\"'\/])([a-z]+?):\/\/([^, \r\n\"\‍(\‍)'<>]+)/iu", // Added 'u' modifier
42 "/([^]_a-z0-9-=\"'\/])www\.([a-z0-9\-]+)\.([^, \r\n\"\‍(\‍)'<>]+)/iu", // Added 'u' modifier
43 "/([^]_a-z0-9-=\"'\/])([a-z0-9\-_.]+?)@([^, \r\n\"\‍(\‍)'<>]+)/iu" // Added 'u' modifier
44 ];
45 $replacements = [
46 "\\1<a href=\"\\2://\\3\" rel=\"external\">\\2://\\3</a>",
47 "\\1<a href=\"https://www.\\2.\\3\" rel=\"external\">www.\\2.\\3</a>",
48 "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>"
49 ];
50 $ret = preg_replace( $patterns, $replacements, $text );
51
52 return $ret;
53 }
54
55 public function &nl2Br( $text ) {
56 // Ensure proper UTF-8 encoding
57 if (!mb_check_encoding($text, 'UTF-8')) {
58 $text = mb_convert_encoding($text, 'UTF-8', 'auto');
59 }
60
61 $ret = preg_replace( "/(\015\012)|(\015)|(\012)/u", '<br>', $text ); // Added 'u' modifier
62
63 return $ret;
64 }
65
66 public function &addSlashes( $text, $force = false ) {
67 // Ensure proper UTF-8 encoding
68 if (!mb_check_encoding($text, 'UTF-8')) {
69 $text = mb_convert_encoding($text, 'UTF-8', 'auto');
70 }
71
72 if ( $force ) {
73 // Use mb_ereg_replace for UTF-8 safety if available
74 if (function_exists('mb_ereg_replace')) {
75 $ret = mb_ereg_replace('([\'\"\\\\])', '\\\\\\1', $text);
76 } else {
77 $ret = addslashes( $text );
78 }
79 return $ret;
80 }
81
82 return $text;
83 }
84
85 /*
86 * if magic_quotes_gpc is on, stirip back slashes
87 */
88 public function &stripSlashesGPC( $text ) {
89 //trigger_error("assume magic_quotes_gpc is off", E_USER_NOTICE);
90 return $text;
91 }
92
93 /*
94 * for displaying data in html textbox forms
95 */
96 public function &htmlSpecialChars( $text ) {
97 // Ensure proper UTF-8 encoding
98 if (!mb_check_encoding($text, 'UTF-8')) {
99 $text = mb_convert_encoding($text, 'UTF-8', 'auto');
100 }
101
102 $text = preg_replace( '/&amp;/i', '&', htmlspecialchars( $text, ENT_QUOTES, 'UTF-8' )); // Added UTF-8 encoding
103
104 return $text;
105 }
106
107 public function &undoHtmlSpecialChars( &$text ) {
108 $ret = preg_replace( [
109 '/&gt;/i',
110 '/&lt;/i',
111 '/&quot;/i',
112 '/&#039;/i'
113 ], [ '>', '<', '"', "'" ], $text );
114
115 return $ret;
116 }
117
118 /*
119 * Filters textarea form data in DB for display
120 */
121 public function &displayText( $text, $html = false ) {
122 if ( ! $html ) {
123 // html not allowed
124 $text =& $this->htmlSpecialChars( $text );
125 }
126 $text =& $this->makeClickable( $text );
127 $text =& $this->nl2Br( $text );
128
129 return $text;
130 }
131
132 /*
133 * Filters textarea form data submitted for preview
134 */
135 public function &previewText( $text, $html = false ) {
136 $text =& $this->stripSlashesGPC( $text );
137
138 return $this->displayText( $text, $html );
139 }
140
141##################### Deprecated Methods ######################
142
143 public function sanitizeForDisplay( $text, $allowhtml = 0, $smiley = 1, $bbcode = 1 ) {
144 $text = 0 === $allowhtml ? $this->htmlSpecialChars( $text ) : $this->makeClickable( $text );
145 if ( 1 === $smiley ) {
146 $text = $this->smiley( $text );
147 }
148 if ( 1 === $bbcode ) {
149 $text = $this->xoopsCodeDecode( $text );
150 }
151 $text = $this->nl2Br( $text );
152
153 return $text;
154 }
155
156 public function sanitizeForPreview( $text, $allowhtml = 0, $smiley = 1, $bbcode = 1 ) {
157 $text = $this->oopsStripSlashesGPC( $text );
158 $text = 0 === $allowhtml ? $this->htmlSpecialChars( $text ) : $this->makeClickable( $text );
159 if ( 1 === $smiley ) {
160 $text = $this->smiley( $text );
161 }
162 if ( 1 === $bbcode ) {
163 $text = $this->xoopsCodeDecode( $text );
164 }
165 $text = $this->nl2Br( $text );
166
167 return $text;
168 }
169
170 public function makeTboxData4Save( $text ) {
171 //$text = $this->undoHtmlSpecialChars($text);
172 return $this->addSlashes( $text );
173 }
174
175 public function makeTboxData4Show( $text, $smiley = 0 ) {
176 $text = $this->htmlSpecialChars( $text );
177
178 return $text;
179 }
180
181 public function makeTboxData4Edit( $text ) {
182 return $this->htmlSpecialChars( $text );
183 }
184
185 public function makeTboxData4Preview( $text, $smiley = 0 ) {
186 $text = $this->stripSlashesGPC( $text );
187 $text = $this->htmlSpecialChars( $text );
188
189 return $text;
190 }
191
192 public function makeTboxData4PreviewInForm( $text ) {
193 $text = $this->stripSlashesGPC( $text );
194
195 return $this->htmlSpecialChars( $text );
196 }
197
198 public function makeTareaData4Save( $text ) {
199 return $this->addSlashes( $text );
200 }
201
202 public function &makeTareaData4Show( &$text, $html = 1, $smiley = 1, $xcode = 1 ) {
203 return $this->displayTarea( $text, $html, $smiley, $xcode );
204 }
205
206 public function makeTareaData4Edit( $text ) {
207 return htmlSpecialChars( $text, ENT_QUOTES );
208 }
209
210 public function &makeTareaData4Preview( &$text, $html = 1, $smiley = 1, $xcode = 1 ) {
211 return $this->previewTarea( $text, $html, $smiley, $xcode );
212 }
213
214 public function makeTareaData4PreviewInForm( $text ) {
215 return htmlSpecialChars( $text, ENT_QUOTES );
216 }
217
218 public function makeTareaData4InsideQuotes( $text ) {
219 return $this->htmlSpecialChars( $text );
220 }
221
222 public function &oopsStripSlashesGPC( $text ) {
223 return $this->stripSlashesGPC( $text );
224 }
225
226 public function &oopsStripSlashesRT( $text ) {
227 //trigger_error("assume magic_quotes_gpc is off", E_USER_NOTICE);
228 return $text;
229 }
230
231 public function &oopsAddSlashes( $text ) {
232 return $this->addSlashes( $text );
233 }
234
235 public function &oopsHtmlSpecialChars( $text ) {
236 return $this->htmlSpecialChars( $text );
237 }
238
239 public function &oopsNl2Br( $text ) {
240 return $this->nl2br( $text );
241 }
242}