[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package s9e\TextFormatter 5 * @copyright Copyright (c) 2010-2022 The s9e authors 6 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 7 */ 8 namespace s9e\TextFormatter\Plugins\Censor; 9 10 class Helper 11 { 12 /** 13 * @var string Regexp matching whitelisted words 14 */ 15 public $allowed; 16 17 /** 18 * @var string Name of attribute used for the replacement 19 */ 20 public $attrName = 'with'; 21 22 /** 23 * @var string Default string used to replace censored words 24 */ 25 public $defaultReplacement = '****'; 26 27 /** 28 * @var string Regexp matching blacklisted words in plain text 29 */ 30 public $regexp = '/(?!)/'; 31 32 /** 33 * @var string Regexp matching blacklisted words in HTML 34 */ 35 public $regexpHtml = '/(?!)/'; 36 37 /** 38 * @var array Array of [regexp => replacement] 39 */ 40 public $replacements = []; 41 42 /** 43 * @var string Name of the tag used to mark censored words 44 */ 45 public $tagName = 'CENSOR'; 46 47 /** 48 * Constructor 49 * 50 * @param array $config Helper's config 51 */ 52 public function __construct(array $config) 53 { 54 foreach ($config as $k => $v) 55 { 56 $this->$k = $v; 57 } 58 } 59 60 /** 61 * Censor text nodes inside of HTML code 62 * 63 * NOTE: will only recognize attributes that are enclosed in double quotes 64 * 65 * @param string $html Original HTML 66 * @param bool $censorAttributes Whether to censor the content of attributes 67 * @return string Censored HTML 68 */ 69 public function censorHtml($html, $censorAttributes = false) 70 { 71 $attributesExpr = ''; 72 if ($censorAttributes) 73 { 74 $attributesExpr = '|[^<">]*+(?="(?> [-\\w]+="[^"]*+")*+\\/?>)'; 75 } 76 77 // Modify the original regexp so that it only matches text nodes and optionally attribute 78 // values 79 $delim = $this->regexpHtml[0]; 80 $pos = strrpos($this->regexpHtml, $delim); 81 $regexp = $delim 82 . '(?<!&|&#)' 83 . substr($this->regexpHtml, 1, $pos - 1) 84 . '(?=[^<>]*+(?=<|$)' . $attributesExpr . ')' 85 . substr($this->regexpHtml, $pos); 86 87 return preg_replace_callback( 88 $regexp, 89 function ($m) 90 { 91 return htmlspecialchars($this->getReplacement(html_entity_decode($m[0], ENT_QUOTES, 'UTF-8')), ENT_QUOTES); 92 }, 93 $html 94 ); 95 } 96 97 /** 98 * Censor given plain text 99 * 100 * @param string $text Original text 101 * @return string Censored text 102 */ 103 public function censorText($text) 104 { 105 return preg_replace_callback( 106 $this->regexp, 107 function ($m) 108 { 109 return $this->getReplacement($m[0]); 110 }, 111 $text 112 ); 113 } 114 115 /** 116 * Test whether given word is censored 117 * 118 * @param string $word 119 * @return bool 120 */ 121 public function isCensored($word) 122 { 123 return (preg_match($this->regexp, $word) && !$this->isAllowed($word)); 124 } 125 126 /** 127 * Get the replacement for given word 128 * 129 * @param string $word Original word 130 * @return string Replacement if the word is censored, or the original word otherwise 131 */ 132 protected function getReplacement($word) 133 { 134 if ($this->isAllowed($word)) 135 { 136 return $word; 137 } 138 139 foreach ($this->replacements as list($regexp, $replacement)) 140 { 141 if (preg_match($regexp, $word)) 142 { 143 return $replacement; 144 } 145 } 146 147 return $this->defaultReplacement; 148 } 149 150 /** 151 * Test whether given word is allowed (whitelisted) 152 * 153 * @param string $word 154 * @return bool 155 */ 156 protected function isAllowed($word) 157 { 158 return (isset($this->allowed) && preg_match($this->allowed, $word)); 159 } 160 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |