1: <?php
2: /**
3: * TextSanitizer extension
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package class
15: * @subpackage textsanitizer
16: * @since 2.3.0
17: * @author Taiwen Jiang <phppp@users.sourceforge.net>
18: */
19: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20:
21: /**
22: * Replaces banned words in a string with their replacements or terminate current request
23: *
24: * @param string $text
25: * @return string
26: *
27: */
28: class MytsCensor extends MyTextSanitizerExtension
29: {
30: /**
31: * @param MyTextSanitizer $myts
32: * @param $text
33: *
34: * @return mixed|string
35: */
36: public function load($myts, $text)
37: {
38: static $censorConf;
39: if (!isset($censorConf)) {
40: /** @var XoopsConfigHandler $config_handler */
41: $config_handler = xoops_getHandler('config');
42: $censorConf = $config_handler->getConfigsByCat(XOOPS_CONF_CENSOR);
43: $config = parent::loadConfig(__DIR__);
44: //merge and allow config override
45: $censorConf = array_merge($censorConf, $config);
46: }
47:
48: if (empty($censorConf['censor_enable'])) {
49: return $text;
50: }
51:
52: if (empty($censorConf['censor_words'])) {
53: return $text;
54: }
55:
56: if (empty($censorConf['censor_admin']) && $GLOBALS['xoopsUserIsAdmin']) {
57: return $text;
58: }
59:
60: $replacement = $censorConf['censor_replace'];
61: foreach ($censorConf['censor_words'] as $bad) {
62: $bad = trim($bad);
63: if (!empty($bad)) {
64: if (false === strpos($text, $bad)) {
65: continue;
66: }
67: if (!empty($censorConf['censor_terminate'])) {
68: trigger_error('Censor words found', E_USER_ERROR);
69: $text = '';
70:
71: return $text;
72: }
73: $patterns[] = "/(^|[^0-9a-z_]){$bad}([^0-9a-z_]|$)/siU";
74: $replacements[] = "\\1{$replacement}\\2";
75: $text = preg_replace($patterns, $replacements, $text);
76: }
77: }
78:
79: return $text;
80: }
81: }
82: