1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Core\Text\Sanitizer\Extensions;
13:
14: use Xoops\Core\Text\Sanitizer;
15: use Xoops\Core\Text\Sanitizer\FilterAbstract;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class Censor extends FilterAbstract
29: {
30: 31: 32:
33: protected static $defaultConfiguration = [
34: 'enabled' => true,
35: 'censor_terminate' => false,
36: 'censor_admin' => true,
37: 'censor_words' => ['shit', 'piss', 'fuck', 'cunt', 'cocksucker', 'motherfucker', 'tits'],
38: 'censor_replace' => '%#$@!',
39: ];
40:
41: 42: 43: 44: 45: 46: 47:
48: public function applyFilter($text)
49: {
50: $xoops = \Xoops::getInstance();
51:
52: $enabled = (bool) $xoops->getConfig('censor_enable');
53:
54: $censorWords = (array) $xoops->getConfig('censor_words');
55: $censorWords = empty($censorWords) ? $this->config['censor_words'] : $censorWords;
56:
57: $censorReplace = $xoops->getConfig('censor_replace');
58: $censorReplace = empty($censorReplace) ? $this->config['censor_replace'] : $censorReplace;
59:
60: if ($enabled === false
61: || empty($censorWords)
62: || ((false === $this->config['censor_admin']) && $xoops->userIsAdmin)
63: ) {
64: return $text;
65: }
66:
67: $patterns = [];
68: $replacements = [];
69:
70: foreach ($censorWords as $bad) {
71: $bad = trim($bad);
72: if (!empty($bad)) {
73: if (false === stripos($text, $bad)) {
74: continue;
75: }
76: if ((bool) $this->config['censor_terminate']) {
77: trigger_error("Censor words found", E_USER_ERROR);
78: return '';
79: }
80: $patterns[] = "/(^|[^0-9a-z_]){$bad}([^0-9a-z_]|$)/siU";
81: $replacements[] = "\\1{$censorReplace}\\2";
82: }
83: }
84:
85: $text = preg_replace($patterns, $replacements, $text);
86:
87: return $text;
88: }
89: }
90: