Firefox PHP

Anti-spam: Restrict new users from posting hyperlinks / images

Posted by sheik 
Anti-spam: Restrict new users from posting hyperlinks / images
October 12, 2012 10:28AM
(Apologies if this has been requested before, I have been away from these forums for over a year)

Has anybody written a module to restrict new users from posting hyperlinks?
This would make the job of a spammer harder.

Ideally, the module would be configurable and flexible enough to support things like the following:
  • Maximum number of hyperlinks per post for unregistered users
  • Maximum number of hyperlinks per post for registered users younger than x days
  • Maximum number of hyperlinks per post for registered users
  • Maximum number of images per post for unregistered users
  • Maximum number of images per post for registered users younger than x days
  • Maximum number of images per post for registered users
  • No restrictions for users older than y days

I haven't written a Phorum module for a few years, but would be willing to write this one if somebody gave me some pointers.

Thank you,

/\dam

--
My notable Phorum sites:
Movie Deaths Database - "review comments" system mostly powered by Phorum
Learn Chinese! - integrated forum quiz
Re: Anti-spam: Restrict new users from posting hyperlinks / images
October 13, 2012 05:58AM
I did something like that a number of years ago (for the links). You need to modify the bbcode module
function bbcode_url_handler($content, $args) (in the api.php program in the mod subdirectory)

You just need to add some conditional code toward the end of the function and modify what gets returned.
Re: Anti-spam: Restrict new users from posting hyperlinks / images
October 13, 2012 02:27PM
I'd rather keep it a clean module rather than hacking core routines.
My inclination would be to use a hook before bbcode gets involved, to strip out anything starting with "http ://", "ftp ://" etc based on the user's registration date and the module's own, internal logic.
Ideally I'd then use another hook to display a message to the user explaining what has happened to their post and why.

This should actually be a fairly straightforward module to write. I'd just appreciate some guidance on the hooks to use, and also how to pull back the current user's registration date.

Some admins might also like to base the rules upon how many posts a user has made, so that would be good to know how to extract too.

Cheers,

/\dam

--
My notable Phorum sites:
Movie Deaths Database - "review comments" system mostly powered by Phorum
Learn Chinese! - integrated forum quiz




Edited 1 time(s). Last edit at 10/15/2012 12:29PM by sheik.
Re: Anti-spam: Restrict new users from posting hyperlinks / images
October 13, 2012 07:11PM
Well, since BBCode is a module, you can create a new mod and activate that rather than the "standard" BBCode module.
Re: Anti-spam: Restrict new users from posting hyperlinks / images
October 13, 2012 07:36PM
Yes, but then you have to redo your changes if you want to benefit from updates to the original codebase.
I agree your suggestion is quicker and easier - I just think it's cleaner to keep it as a separate mod.

/\dam

--
My notable Phorum sites:
Movie Deaths Database - "review comments" system mostly powered by Phorum
Learn Chinese! - integrated forum quiz
Re: Anti-spam: Restrict new users from posting hyperlinks / images
October 14, 2012 05:07AM
have a look at the posting hooks, for example the check_post hook [www.phorum.org] .
But you will have to do your own parsing for links there.


Thomas Seifert
Re: Anti-spam: Restrict new users from posting hyperlinks / images
October 15, 2012 12:57AM
Thanks Thomas, I have a proof-of-concept module working that restricts unregistered users only.

I am detecting unregistered users by:
$tsregistered = $PHORUM["user"]["date_added"];
if (!$tsregistered){ // user is UNREGISTERED
Is there a better way?

Additionally, I could use some advice on how to target hyperlinks.
As far as I know, there are three ways to post web links in Phorum and spammers certainly use all of these on my forums:

1: Enter a straightforward http:// address - Phorum auto-parses this to make it clickable
2: Use the bbcode tag (note, you do not need to prefix with http:// for this to work, bbcode will make *anything* clickable and let the browser worry about what to do with it)
3: Just type a www.foo.com address - it won't be made clickable but users will see it.

I am counting instances of (1) using:
preg_match_all('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $message["body"], $matches);
(I swiped this off the web, preliminary testing suggests it seems to work)

instances of (2) using:
preg_match_all('/\[url/', $message["body"], $matches);
(I'm just counting opening bbcode url tags, I don't care about closing ones)

and instances of (3) using:
preg_match_all('/\\www\.(.*?)\./i', $message["body"], $matches);
(this should look for any consecutive string starting with "www." (case-insensitive) and containing at least one other ".")

Now, the problem with the above is that my *total* count of links will often be higher than it should be.
For example, http://www.this.test.link is technically only one link but my regexs above will count it as 2 http links, 1 bbcode link and 2 www links, giving a total of 5 hyperlinks instead of one.

After this gets solved "mailto" links would need to be detected too.

It is late here, so I'm going to sleep on the problem - feedback would be appreciated.

/\dam

--
My notable Phorum sites:
Movie Deaths Database - "review comments" system mostly powered by Phorum
Learn Chinese! - integrated forum quiz
Re: Anti-spam: Restrict new users from posting hyperlinks / images
October 15, 2012 07:01AM
OK, one possible algorithm would involve doing multiple calls to preg_replace.
eg:
- match all instances of pairs of bbcode url open and close tags. Delete all of these strings and make a note of how many matches there were.
- use the output from the above bulk replace to search for strings that start with "http ://" - again count and delete them
- use the output from the above bulk replace to search for strings that start with "www.*." - count them
- add up the three totals from the above, and it *should* give an accurate link count for my previous example.

I think this would work but I'd need to code and test it.
It would use up slightly more memory and CPU than my previous (incorrect) algorithm as the message body would be copied in memory and more intensive regexes would be performed on it.

Thoughts?

/\dam

--
My notable Phorum sites:
Movie Deaths Database - "review comments" system mostly powered by Phorum
Learn Chinese! - integrated forum quiz




Edited 1 time(s). Last edit at 10/15/2012 12:40PM by sheik.
Re: Anti-spam: Restrict new users from posting hyperlinks / images
October 15, 2012 10:15AM
The above algorithm in code:
// first count and remove all bbcode [ url ] tags and everything between them
$parsed_message = preg_replace('@[url(.*?)[/url\]@', '', $message["body"],-1,$num_bbcodelinks);
		
// now count and remove all remaining http*://*. strings to catch http and https links
$parsed_message = preg_replace('/http(.*?)\:\/\/(.*?)\./i', '', $parsed_message, -1, $num_hyperlinks);
		
// now count and remove all remaining www.* strings
$parsed_message = preg_replace('/www(.*?)\./i', '', $parsed_message, -1, $num_wwwlinks);
		
// count all mailto.* strings
preg_match_all('@[email(.*?)[/email\]@', $parsed_message, $matches);
$num_mailtolinks = count($matches[0]);
		
$total_num_links = $num_hyperlinks + $num_bbcodelinks + $num_wwwlinks + $num_mailtolinks;

Initial tests seem to work OK.

/\dam

--
My notable Phorum sites:
Movie Deaths Database - "review comments" system mostly powered by Phorum
Learn Chinese! - integrated forum quiz
Re: Anti-spam: Restrict new users from posting hyperlinks / images
October 15, 2012 12:28PM
Initial admin screen, note I have added a "maximum message length" for different users. If this has already been implemented elsewhere I will remove it.



I am going to run this in the wild for a few days and if there are no problems I will release it.

/\dam

--
My notable Phorum sites:
Movie Deaths Database - "review comments" system mostly powered by Phorum
Learn Chinese! - integrated forum quiz
Sorry, only registered users may post in this forum.

Click here to login