Posted by maher on October 13, 2008 at 7:53pm
Hi all,
Wondering if someone could take a look at the below function and provide feedback/critic.
My site allows guests to post forum questions. Spam is becoming a problem although I have the CAPTCHA module installed. What I’m trying to do is prevent guests from posting links to their questions (forum nodes). To do that, I’m developing a very simple custom module with the below function that Implements the hook_nodeapi().
<?php
function noguestlinks_nodeapi (&$node, $op, $a3 = NULL, $a4 = NULL) {
global $user;
switch ($op) {
case "validate":
//check if anonymous user
if (!$user->uid) {
//the checks are derived from the filter module > <em>filter_url method
if (preg_match("@(http://|https://|ftp://|mailto:|smb://|afp://|file://|gopher://|news://|ssl://|sslv2://|sslv3://|tls://|tcp://|udp://)+@se", $node->body) || preg_match("@(www.[a-zA-Z0-9\@:%</em>+<em>~#?&=.,/;-]</em>[a-zA-Z0-9\@:%_+~#\&=/;-])+@se", $node->body)) {
form_set_error("node", t("You have to be logged in to post links. This is an anti-spam measure."));
}
}
break;
}
}
?>Do you guys see a problem with the above code? Thanks in advance for your help.

Comments
Let's see
D6 or D5?
There are probably filter-access modules that control what filter to use per role. Simply allowing certain roles to use certain filters, maybe with http://drupal.org/project/default_filter. I might also suggest that no markup be allowed for anonymous users.
Otherwise, in my humble opinion, looks OK. Might want to check the node->type first. There could be a better way to do it with hook_form_alter validate but don't know off the top of my head.
--
zzolo
--
zzolo