Forums

Unfortunately no one can be told what FluxBB is - you have to see it for yourself.

You are not logged in.

#1 2010-11-15 11:31:48

Visman
Member
From: Siberia
Registered: 2010-07-10
Posts: 1,518
Website

[MOD] Cache for censor

File functions.php, New function censor_words ():

function censor_words($text)
{
    global $db;
    static $search_for, $replace_with;

    // If not already built in a previous call, build an array of censor words and their replacement text
    if (!isset($search_for))
    {
        if (file_exists(FORUM_CACHE_DIR.'cache_censor.php'))
            include FORUM_CACHE_DIR.'cache_censor.php';

        if (!defined('PUN_CENSOR_LOADED'))
        {
            if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
                require PUN_ROOT.'include/cache.php';

            generate_censor_cache();
            require FORUM_CACHE_DIR.'cache_censor.php';
        }
    }

    if (!empty($search_for))
        $text = substr(preg_replace($search_for, $replace_with, ' '.$text.' '), 1, -1);

    return $text;
}

File cache.php, Add new function generate_censor_cache():

//
// Generate the censor cache PHP script - Visman
//
function generate_censor_cache()
{
    global $db;

    $result = $db->query('SELECT search_for, replace_with FROM '.$db->prefix.'censoring') or error('Unable to fetch censor word list', __FILE__, __LINE__, $db->error());
    $num_words = $db->num_rows($result);

    $search_for = $replace_with = array();
    for ($i = 0; $i < $num_words; ++$i)
    {
        list($search_for[$i], $replace_with[$i]) = $db->fetch_row($result);
        $search_for[$i] = '/(?<=\W)('.str_replace('\*', '\w*?', preg_quote($search_for[$i], '/')).')(?=\W)/i';
    }

    // Output config as PHP code
    $fh = @fopen(FORUM_CACHE_DIR.'cache_censor.php', 'wb');
    if (!$fh)
        error('Unable to write censor cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'', __FILE__, __LINE__);

    fwrite($fh, '<?php'."\n\n".'define(\'PUN_CENSOR_LOADED\', 1);'."\n\n".'$search_for = '.var_export($search_for, true).';'."\n\n".'$replace_with = '.var_export($replace_with, true).';'."\n\n".'?>');

    fclose($fh);

    if (function_exists('apc_delete_file'))
        @apc_delete_file(FORUM_CACHE_DIR.'cache_censor.php');
}

File admin_censoring.php, Before lines containing redirect('admin_censoring.php', add the block (3 times):

    // Regenerate the bans cache
    if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
        require PUN_ROOT.'include/cache.php';

    generate_censor_cache();

Offline

#2 2010-11-15 11:35:52

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 6,737
Website

Re: [MOD] Cache for censor

Nice.
Would that be a good idea to include in core in the future?


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#3 2010-11-15 11:39:22

Visman
Member
From: Siberia
Registered: 2010-07-10
Posts: 1,518
Website

Re: [MOD] Cache for censor

It should be made from the very beginning.

Offline

#4 2010-11-15 14:03:48

Smartys
Former Developer
Registered: 2008-04-27
Posts: 3,139
Website

Re: [MOD] Cache for censor

Franz wrote:

Nice.
Would that be a good idea to include in core in the future?

Yes

Offline

#5 2010-11-15 16:54:55

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 6,737
Website

Re: [MOD] Cache for censor

Ok. Nice suggestion. Created a ticket for 1.4.4.


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#6 2010-11-25 09:32:28

Visman
Member
From: Siberia
Registered: 2010-07-10
Posts: 1,518
Website

Re: [MOD] Cache for censor

Change it

        $search_for[$i] = '/(?<=\W)('.str_replace('\*', '\w*?', preg_quote($search_for[$i], '/')).')(?=\W)/i';

On it

        $search_for[$i] = '/(?<=\W)('.str_replace('\*', '\w*?', preg_quote($search_for[$i], '/')).')(?=\W)/iu';

Offline

#7 2010-12-08 22:31:05

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 6,737
Website

Re: [MOD] Cache for censor

Visman wrote:

Change it

        $search_for[$i] = '/(?<=\W)('.str_replace('\*', '\w*?', preg_quote($search_for[$i], '/')).')(?=\W)/i';

On it

        $search_for[$i] = '/(?<=\W)('.str_replace('\*', '\w*?', preg_quote($search_for[$i], '/')).')(?=\W)/iu';

Thanks for the patch.

This is necessary for making it all work with Unicode, right?


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#8 2010-12-09 01:58:47

Visman
Member
From: Siberia
Registered: 2010-07-10
Posts: 1,518
Website

Re: [MOD] Cache for censor

Yes, I have made this change, that censorship correctly worked in Russian.

Offline

Board footer

Powered by FluxBB