XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
module.textsanitizer.php
1<?php
16
17
19{
20 public $censorConf;
21
25 public $mTextFilter = null;
26
31 public $mMakeClickablePostFilter = null;
32
37 public $mXoopsCodePostFilter = null;
38
50 public function __construct()
51 {
52 $this->mMakeClickablePostFilter =new XCube_Delegate();
53 $this->mMakeClickablePostFilter->register('MyTextSanitizer.MakeClickablePostFilter');
54
55 $this->mXoopsCodePostFilter =new XCube_Delegate();
56 $this->mXoopsCodePostFilter->register('MyTextSanitizer.XoopsCodePostFilter');
57
58 $root =& XCube_Root::getSingleton();
59 $this->mTextFilter =& $root->getTextFilter();
60 }
61
70 public static function &sGetInstance()
71 {
72 static $instance;
73 if (!isset($instance)) {
74 $instance = new MyTextSanitizer();
75 }
76 return $instance;
77 }
78
84 public function getSmileys()
85 {
86 return $this->mTextFilter->getSmileys();
87 }
88
95 public function &smiley($text)
96 {
97 $text = $this->mTextFilter->smiley($text);
98 return $text;
99 }
100
107 public function &makeClickable($text)
108 {
109 $text = $this->mTextFilter->makeClickable($text);
110
111 // RaiseEvent : 'MyTextSanitizer.MakeClickablePostFilter'
112 // Delegate may convert output text with quickApplyFilter rule
113 // Args :
114 // 'string' [I/O] : Text to convert;
115 //
116 $this->mMakeClickablePostFilter->call(new XCube_Ref($text));
117 return $text;
118 }
119
128 public function &xoopsCodeDecode($text, $allowimage = 1)
129 {
130 $text = $this->mTextFilter->convertXCode($text, $allowimage);
131
132 // RaiseEvent : 'MyTextSanitizer.XoopsCodePostFilter'
133 // Delegate may convert output text with quickApplyFilter rule
134 // Args :
135 // 'string' [I/O] : Text to convert;
136 // 'allowimage' [I] : xoopsCodeDecode $allowimage parameter
137 //
138 $this->mXoopsCodePostFilter->call(new XCube_Ref($text), $allowimage);
139 return $text;
140 }
141
148 public function _filterImgUrl($matches)
149 {
150 if ($this->checkUrlString($matches[2])) {
151 return $matches[0];
152 }
153
154 return '';
155 }
156
163 public function checkUrlString($text)
164 {
165 // Check control code
166 if (preg_match("/[\\0-\\31]/", $text)) {
167 return false;
168 }
169 // check black pattern(deprecated)
170 return !preg_match('/^(javascript|vbscript|about):/i', $text);
171 }
172
180 public function &nl2Br($text)
181 {
182 $ret = $this->mTextFilter->nl2Br($text);
183 return $ret;
184 }
185
192 public function addSlashes($text)
193 {
194 // Ensure proper UTF-8 encoding before adding slashes
195 if (!mb_check_encoding($text, 'UTF-8')) {
196 $text = mb_convert_encoding($text, 'UTF-8', 'auto');
197 }
198
199 // Use mb_addslashes equivalent for UTF-8 safety
200 if (function_exists('mb_ereg_replace')) {
201 return mb_ereg_replace('([\'\"\\\\])', '\\\\\\1', $text);
202 } else {
203 return addslashes($text);
204 }
205 }
206
207 /*
208 * if magic_quotes_gpc is on, strip back slashes
209 *
210 * @param string $text
211 *
212 * @return string
213 */
214 public function &stripSlashesGPC($text)
215 {
216 // Ensure $text is a string
217 $text = (string)$text;
218
219 // Ensure proper UTF-8 encoding
220 if (!mb_check_encoding($text, 'UTF-8')) {
221 $text = mb_convert_encoding($text, 'UTF-8', 'auto');
222 }
223
224 // Use mb_stripslashes equivalent for UTF-8 safety
225 if (function_exists('mb_ereg_replace')) {
226 $text = mb_ereg_replace('\\\\([\'\"\\\\])', '\\1', (string)$text);
227 }
228
229 return $text;
230 }
231
232 /*
233 * for displaying data in html textbox forms
234 *
235 * @param string $text
236 * @param bool $forEdit (experimental)
237 *
238 * @return string
239 */
240 public function &htmlSpecialChars($text, $forEdit=false)
241 {
242 if (!$forEdit) {
243 $ret = $this->mTextFilter->toShow($text, true);
244 } else {
245 $ret = $this->mTextFilter->toEdit($text);
246 }
247 return $ret;
248 }
249
257 public function &undoHtmlSpecialChars($text)
258 {
259 $ret = preg_replace(['/&gt;/i', '/&lt;/i', '/&quot;/i', '/&#039;/i'], ['>', '<', '"', "'"], $text);
260 return $ret;
261 }
262
275
276 public function _ToShowTarea($text, $html = 0, $smiley = 1, $xcode = 1, $image = 1, $br = 1)
277 {
278 $text = $this->codePreConv($text, $xcode);
279 if (1 !== $html) {
280 $text = $this->htmlSpecialChars($text);
281 }
282 $text = $this->makeClickable($text);
283 if (0 !== $smiley) {
284 $text = $this->smiley($text);
285 }
286 if (0 !== $xcode) {
287 $text = $this->xoopsCodeDecode($text, $image);
288 }
289 if (0 !== $br) {
290 $text = $this->nl2Br($text);
291 }
292 $text = $this->codeConv($text, $xcode, $image);
293 return $text;
294 }
295
307 public function &displayTarea($text, $html = 0, $smiley = 1, $xcode = 1, $image = 1, $br = 1)
308 {
309 $text = $this->mTextFilter->toShowTarea($text, $html, $smiley, $xcode, $image, $br, true);
310 return $text;
311 }
312
324 public function &previewTarea($text, $html = 0, $smiley = 1, $xcode = 1, $image = 1, $br = 1)
325 {
326 $text =& $this->stripSlashesGPC($text);
327 $text = $this->mTextFilter->toPreviewTarea($text, $html, $smiley, $xcode, $image, $br, true);
328 return $text;
329 }
330
339 public function &censorString($text)
340 {
341 if (!isset($this->censorConf)) {
342 $config_handler =& xoops_gethandler('config');
343 $this->censorConf =& $config_handler->getConfigsByCat(XOOPS_CONF_CENSOR);
344 }
345 if (1 == $this->censorConf['censor_enable']) {
346 $replacement = $this->censorConf['censor_replace'];
347 foreach ($this->censorConf['censor_words'] as $bad) {
348 if (!empty($bad)) {
349 $bad = quotemeta($bad);
350 $patterns[] = "/(\s)".$bad . '/siU';
351 $replacements[] = "\\1".$replacement;
352 $patterns[] = '/^' . $bad . '/siU';
353 $replacements[] = $replacement;
354 $patterns[] = "/(\n)".$bad . '/siU';
355 $replacements[] = "\\1".$replacement;
356 $patterns[] = '/]' . $bad . '/siU';
357 $replacements[] = ']' . $replacement;
358 $text = preg_replace($patterns, $replacements, $text);
359 }
360 }
361 }
362 return $text;
363 }
364
371 public function codePreConv($text, $xcode = 1)
372 {
373 if (0 !== $xcode) {
374 $text = $this->mTextFilter->preConvertXCode($text, $xcode);
375 }
376 return $text;
377 }
378
379 public function codeConv($text, $xcode = 1, $image = 1)
380 {
381 if (0 !== $xcode) {
382 $text = $this->mTextFilter->postConvertXCode($text, $xcode);
383 }
384 return $text;
385 }
386
387##################### Deprecated Methods ######################
388
397 public function sanitizeForDisplay($text, $allowhtml = 0, $smiley = 1, $bbcode = 1)
398 {
399 $text = $this->_ToShowTarea($text, $allowhtml, $smiley, $bbcode, 1, 1);
400 return $text;
401 }
402
403 public function sanitizeForPreview($text, $allowhtml = 0, $smiley = 1, $bbcode = 1)
404 {
405 $text = $this->oopsStripSlashesGPC($text);
406 $text = $this->_ToShowTarea($text, $allowhtml, $smiley, $bbcode, 1, 1);
407 return $text;
408 }
409
410 public function makeTboxData4Save($text)
411 {
412 return $this->addSlashes($text);
413 }
414
415 public function makeTboxData4Show($text, $smiley=0)
416 {
417 return $this->mTextFilter->toShow($text, true);
418 }
419
420 public function makeTboxData4Edit($text)
421 {
422 return $this->mTextFilter->toEdit($text);
423 }
424
425 public function makeTboxData4Preview($text, $smiley=0)
426 {
427 $text = $this->stripSlashesGPC($text);
428 $text = $this->mTextFilter->toShow($text, true);
429 return $text;
430 }
431
432 public function makeTboxData4PreviewInForm($text)
433 {
434 $text = $this->stripSlashesGPC($text);
435 return $this->mTextFilter->toEdit($text);
436 }
437
438 public function makeTareaData4Save($text)
439 {
440 return $this->addSlashes($text);
441 }
442
443 public function &makeTareaData4Show($text, $html=1, $smiley=1, $xcode=1)
444 {
445 $ret = $this->displayTarea($text, $html, $smiley, $xcode);
446 return $ret;
447 }
448
449 public function makeTareaData4Edit($text)
450 {
451 return $this->mTextFilter->toEdit($text);
452 }
453
454 public function &makeTareaData4Preview($text, $html=1, $smiley=1, $xcode=1)
455 {
456 $ret = $this->previewTarea($text, $html, $smiley, $xcode);
457 return $ret;
458 }
459
460 public function makeTareaData4PreviewInForm($text)
461 {
462 //if magic_quotes_gpc is on, do stipslashes
463 $text = $this->stripSlashesGPC($text);
464 return $this->mTextFilter->toEdit($text);
465 }
466
467 public function makeTareaData4InsideQuotes($text)
468 {
469 return $this->mTextFilter->toShow($text, true);
470 }
471
472 public function &oopsStripSlashesGPC($text)
473 {
474 $ret = $this->stripSlashesGPC($text);
475 return $ret;
476 }
477
478 public function &oopsStripSlashesRT($text)
479 {
480 if (get_magic_quotes_runtime()) {
481 $text =& stripslashes($text);
482 }
483 return $text;
484 }
485
486 public function &oopsAddSlashes($text)
487 {
488 $ret = $this->addSlashes($text);
489 return $ret;
490 }
491
492 public function &oopsHtmlSpecialChars($text)
493 {
494 $ret = $this->mTextFilter->toShow($text, true);
495 return $ret;
496 }
497
498 public function &oopsNl2Br($text)
499 {
500 $ret = $this->nl2br($text);
501 return $ret;
502 }
503
504 public static function &getInstance()
505 {
506 $ret = self::sGetInstance();
507 return $ret;
508 }
510}
sanitizeForDisplay($text, $allowhtml=0, $smiley=1, $bbcode=1)
& displayTarea($text, $html=0, $smiley=1, $xcode=1, $image=1, $br=1)
_ToShowTarea($text, $html=0, $smiley=1, $xcode=1, $image=1, $br=1)
& xoopsCodeDecode($text, $allowimage=1)
& previewTarea($text, $html=0, $smiley=1, $xcode=1, $image=1, $br=1)
codePreConv($text, $xcode=1)
[Final] Used for the simple mechanism for common delegation in XCube.