MyBB Community Forums

Full Version: Word Replacement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm working on a plugin which replaces occurrences of a word or expression with the same word, but with extra MyCode around it.

The main problems I'm having are:

- Nothing seems to happen when a thread is replied to using the Quick Reply
- Only one word/expression is changed when it does work (using Full Edit or non-quick Reply)

I'm not a fantastic PHP scripter, so there might be a few rookie mistakes.

The code is below, and attached.

<?php
if(!defined("IN_MYBB"))
{
	die("Direct initialisation of this file is not allowed.");
}

$plugins->add_hook("datahandler_post_update", "autoban_run");
$plugins->add_hook("datahandler_post_insert_post", "autoban_run");
$plugins->add_hook("datahandler_post_insert_thread_post", "autoban_run");
$plugins->add_hook("datahandler_post_update_thread_post", "autoban_run");
$plugins->add_hook("datahandler_post_update", "autoban_run");

function autoban_info()
{
	return array(
		"name"			=> "Uber's Autoban",
		"description"	=> "Automatically bans members for posting certain words.",
		"website"		=> "google.com",
		"author"		=> "UberMensch",
		"authorsite"	=> "google.com",
		"version"		=> "0.1",
		"compatibility" => "1*",
	);
}

function autoban_activate()
{
}

function autoban_deactivate()
{
}

function autoban_run($post)
{
	$banwords = "test,test2,line of test";
	$callback = 0;
	
	if( isset( $post->post_update_data['message']) )
	{
		$callback = 1;
	}
	$words = split(",", $banwords);
	$message;
	for( $i = 0; $i <= count($words)-1; $i++)
	{
		if($callback == 1)
		{
			$message = str_replace( $words[$i], "[highlight][u]" . $words[$i] . "[/u][/highlight]", $post->post_update_data['message']);
		} else {
                	$message = str_replace( $words[$i], "[highlight][u]" . $words[$i] . "[/u][/highlight]", $post->post_insert_data['message']);
		}
	}

	if( $callback = 1 )
	{
		$post->post_update_data['message'] = $message . "\r\n\r\n\r\n[highlight]USER WAS AUTOBANNED FOR THIS POST[/highlight]";
	} else {
		$post->post_insert_data['message'] = $message . "\r\n\r\n\r\n[highlight]USER WAS AUTOBANNED FOR THIS POST[/highlight]";
	}

}

?>

As you can see by the code, it's eventually going to be automatically banning/warning users for saying certain words, saving moderators a bit of time Wink
Hi,

Here's a few things I'll point out:
$plugins->add_hook("datahandler_post_update", "autoban_run");
$plugins->add_hook("datahandler_post_insert_post", "autoban_run");
$plugins->add_hook("datahandler_post_insert_thread_post", "autoban_run");
$plugins->add_hook("datahandler_post_update_thread_post", "autoban_run");
$plugins->add_hook("datahandler_post_update", "autoban_run");
The first and last call to add_hook does the same thing. From memory, I believe MyBB will automatically disregard the 2nd one, but it might be a good idea to remove one of them.

	$message;
You've got a line with just that - it doesn't actually do anything unfortunately...

	for( $i = 0; $i <= count($words)-1; $i++)
	{
		if($callback == 1)
		{
			$message = str_replace( $words[$i], "[highlight][u]" . $words[$i] . "[/u][/highlight]", $post->post_update_data['message']);
		} else {
                	$message = str_replace( $words[$i], "[highlight][u]" . $words[$i] . "[/u][/highlight]", $post->post_insert_data['message']);
		}
	}
You might want to examine your logic there. You're setting $message to be a replacement of stuff in the $post object. Whilst this will work for one iteration of the loop, you should notice that further iterations of the for loop will cause $message to be overwritten with previous values of whatever is in the $post object.
Easiest solution would be to replace $message with the $post things, ie:
	for( $i = 0; $i <= count($words)-1; $i++)
	{
		if($callback == 1)
		{
			$post->post_update_data['message'] = str_replace( $words[$i], "[highlight][u]" . $words[$i] . "[/u][/highlight]", $post->post_update_data['message']);
		} else {
                	$post->post_insert_data['message'] = str_replace( $words[$i], "[highlight][u]" . $words[$i] . "[/u][/highlight]", $post->post_insert_data['message']);
		}
	}
(don't forget to remove the code below that as well)


I can't quite see why it wouldn't work for quick reply (haven't tested anything), but try the above and see if it helps.
Thanks, I'll play around with that when I get home from work Wink
Almost everything seems to be working fine, the only part which is giving issues is using quick reply. The words are highlighted, but no message is appended.

Thanks for your help so far Wink

All I need to do now is work out how to warn in a plugin.