torrentpier/src/Censor.php
2024-12-10 00:15:46 +07:00

66 lines
1.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* TorrentPier Bull-powered BitTorrent tracker engine
*
* @copyright Copyright (c) 2005-2024 TorrentPier (https://torrentpier.com)
* @link https://github.com/torrentpier/torrentpier for the canonical source repository
* @license https://github.com/torrentpier/torrentpier/blob/master/LICENSE MIT License
*/
namespace TorrentPier;
/**
* Class Censor
* @package TorrentPier
*/
class Censor
{
/**
* Word replacements
*
* @var array
*/
public array $replacements = [];
/**
* All censored words (RegEx)
*
* @var array
*/
public array $words = [];
/**
* Initialize word censor
*/
public function __construct()
{
global $bb_cfg, $datastore;
if (!$bb_cfg['use_word_censor']) {
return;
}
// Get censored words
if (!$censoredWords = $datastore->get('censor')) {
$datastore->update('censor');
$censoredWords = $datastore->get('censor');
}
foreach ($censoredWords as $word) {
$this->words[] = '#(?<![\p{Nd}\p{L}_])(' . str_replace('\*', '[\p{Nd}\p{L}_]*?', preg_quote($word['word'], '#')) . ')(?![\p{Nd}\p{L}_])#iu';
$this->replacements[] = $word['replacement'];
}
}
/**
* Word censor
*
* @param string $word
* @return string
*/
public function censorString(string $word): string
{
return preg_replace($this->words, $this->replacements, $word);
}
}