Censoring badwords in Comments or *filtering

asizomos

Joined: 2003-09-27
Posts: 5
Posted: Sat, 2003-09-27 18:15

Hi all gallery users, my site http>//www.asizomos.com/index.php is getting bigger and bigger and now its really hard to be deleting all irritating comments people get in their pictures, most of them are just caused by bad words.
So here is my problem, Is there anyway to filter de input text like if it contains some words the comment wont show up_ or lets say just the bad words wont show up in the comment?
Thanks alot I love this gallery

 
zman
zman's picture

Joined: 2003-07-24
Posts: 196
Posted: Mon, 2003-09-29 21:45

Hey asizomos!

I read your request and got to thinking that I might need something like this too so I did some searching on the net and found some code, changed it a bit, and implemented it for Gallery :)

I've only tested it briefly but it seems to work good. Since people can also use bad words for their name I also have the filter working on the commenter name field.

This may be a downside or a plus, but you will need to input your array of words you consider "bad" and their replacements. I actually consider this a plus because I don't mind words like ass but don't want some of the more obscene curse words in there. The code I'll post below will have words in it that aren't usually considered "bad words" but I don't want to put an entire list of curse words here for all to see... you can change the array to your needs.

You'll need to modify two files: util.php and add_comments.php.

In util.php go to the very end. The very last couple lines in the file should be:

}

?>

We are going to put our new code in util.php here:

}

// NEW CODE HERE

?>

Basically, between the "}" and "?>"

Where I have // NEW CODE HERE above, insert the following:

//**** Bad Word Filter for Comments and Commenter Name
// BadWordFilter
// This function does all the work. If $replace is 1 it will replace all bad words
// with the wildcard replacements.  If $replace is 0 it will not replace anything.
// In either case, it will return 1 if it found bad words or 0 otherwise.
// Be sure to fill the $bads array with the bad words you want filtered.
function BadWordFilter(&$filtered_words, $replace)
{
	//fill this array with the bad words you want to filter and their replacements
	$bads = array (
		array("butt","b***"),
		array("poop","p***"),
		array("crap","c***")
	);

	if($replace==1) {								//we are replacing
		$remember = $filtered_words;
		
		for($i=0;$i<sizeof($bads);$i++) {			//go through each bad word
			$filtered_words = eregi_replace($bads[$i][0],$bads[$i][1],$filtered_words); //replace it
		}

		if($remember!=$filtered_words) return 1; 				//if there are any changes, return 1
		
	} else {										//we are just checking
	
		for($i=0;$i<sizeof($bads);$i++) {			//go through each bad word
			if(eregi($bads[$i][0],$filtered_words)) return 1; //if we find any, return 1
		}	
		
	}
}

// *** End Bad Word Filter for Comments and Commenter Name ***

Now in add_comment.php find the section of code that starts with

if (isset($save)) {

and ends with

dismissAndReload();

and replace all that with this:

if (isset($save)) {
	if ($commenter_name && $comment_text) {
	        $comment_text = removeTags($comment_text);
	        // mod for filtering bad words from comments
	        $replace = 1;  
	        $filtered_words = $comment_text;
	        BadWordFilter($filtered_words,$replace); 
	        $comment_text = $filtered_words;
	        // comments bad word filter complete
	        $commenter_name = removeTags($commenter_name);
	        // mod for filtering bad words from commenter name
	        $replace = 1;
	        $filtered_words = $commenter_name;
	        BadWordFilter($filtered_words,$replace); 
	        $commenter_name = $filtered_words;
	        // commenter name bad word filter complete
		$IPNumber = $HTTP_SERVER_VARS['REMOTE_ADDR'];
		$gallery->album->addComment($index, stripslashes($comment_text), $IPNumber, $commenter_name);
		$gallery->album->save();
		dismissAndReload();

If you decide you later want to allow bad words, all you need to do is change the $replace = 1 code above to $replace = 0 and the comments and username should allow bad words once again (without deleting all the code).

Depending on your version of Gallery, you may or may not have exactly what I typed above to start with. Basically, you should be able to find the IF statement and the dismissAndReload() function and, if your code is a little different, try to figure out what I changed above and add it to your code. If you need help, post your original code and I'll see if I can change it for you for your version of Gallery.

Oh, and my usual disclaimers apply... I'm not a programmer but this seems to work so if anybody sees something I need to change/fix/update, please post here :)

 
ika

Joined: 2003-07-03
Posts: 11
Posted: Tue, 2003-09-30 00:03

But no success.. when i add a comment it gives me a white page.. :\..
check it out..
http://192.117.122.120/g2/gallery/dsfdsf/mossad

 
asizomos

Joined: 2003-09-27
Posts: 5
Posted: Tue, 2003-09-30 17:51

Wow zman!
Thanks alot for the fast response, I implemented it last night and its working great! You just saved me a lot of work
Thank you for your censoring system, this should be added to newer versions I think.
:lol:

 
zman
zman's picture

Joined: 2003-07-24
Posts: 196
Posted: Tue, 2003-09-30 19:37
asizomos wrote:
Wow zman!
Thanks alot for the fast response, I implemented it last night and its working great! You just saved me a lot of work
Thank you for your censoring system, this should be added to newer versions I think.
:lol:

Excellent! But, just to be clear, the majority of the code isn't mine. I found it on the web, changed it a little, and stuck it in the right places for Gallery :)

Glad it's working for ya!

 
robert070612

Joined: 2003-08-05
Posts: 565
Posted: Thu, 2003-12-04 18:21

<Oh, and my usual disclaimers apply... I'm not a programmer but this seems to work so if anybody sees something I need to change/fix/update, please post here...>

Some little turd has jumped out of his pram and is mouthing off on my gallery site... I knew that, one day, I would get one;~/

I'm on 1.4.1-rc4 (AIU this is pretty much the same as the subsequent 1.4.1) and implementing this results in a failure to add the comment. Have now had to revert... any ideas?

best wishes, Robert

 
blw91126

Joined: 2004-11-12
Posts: 81
Posted: Tue, 2004-12-14 19:50

Tried implementing this myself info gallery, however I get the follwing error message when clicking on the "post" comment button.

I'm running version: v1.4.4-pl4

Error: Requested index [-1] out of bounds [254]
Fatal error: Call to a member function on a non-object in /www/b/username/htdocs/gallery/classes/Album.php on line 1494

Any ideas anyone.
Thanks Brett[/b]

 
blw91126

Joined: 2004-11-12
Posts: 81
Posted: Tue, 2004-12-14 23:39

Nevermind, just got it to work.
Thanks

 
Neone

Joined: 2003-12-28
Posts: 54
Posted: Thu, 2005-03-24 00:51

But I can't get it to work :(

Error: Requested index [-1] out of bounds [191]
Fatal error: Call to a member function on a non-object in /home/nofear/public_html/gallery/classes/Album.php on line 1494

Any ideas anyone?

 
loupierrot

Joined: 2006-12-06
Posts: 31
Posted: Wed, 2007-10-24 16:24

Hello,
Can somebody tell my where to find those two files ??
>You'll need to modify two files: util.php and add_comments.php.

Thanks a lot in advance,
Pierre