block posting with list banned words in message and subject and link in subject

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Zurd
Registered User
Posts: 5
Joined: Tue Jan 02, 2018 3:10 pm

block posting with list banned words in message and subject and link in subject

Post by Zurd »

Following the discussion of viewtopic.php?t=2135443

I modified the script to add more functionality. Actually, I didn't do nothing, it's all ChatGPT :D

Works with phpBB version 3.3.3 and just updated to latest 3.3.14 and it works again.

Modify posting.php, add code below at around 1110 line (or at line 1137 in the latest phpBB 3.3.14 right after

Code: Select all

                if (count($message_parser->warn_msg))
                {
                        $error[] = implode('<br />', $message_parser->warn_msg);
                        $message_parser->warn_msg = array();
                }

Code: Select all

/*** BEGIN CUSTOM CODE
Block posting with banned words being used in both the message and the subject. Also block posting if there is a link into the subject.
To debug, uncomment the error_log line which will output errors into the error_log file. You have to enable this file first however.
https://www.phpbb.com/community/viewtopic.php?t=2135443
***/
// Example spam word list
$aSpam = [
      'word1'=> ''
    , 'word2'=> ''
    , 'word3'=> ''
];

// Regex for detecting URLs (only in the subject)
$urlPattern = '#\bhttps?://[^\s]+#i';

// Combine spam words into a single regex pattern
$wordPattern = '#\b(' . implode('|', array_map('preg_quote', array_keys($aSpam))) . ')\b#i';

// Subject and message to validate
$subject = $request->variable('subject', '', true);
$message = isset($message_parser->message) ? $message_parser->message : '';

// Initialize errors
$error = [];

// Debug: Log subject and message to verify data
// error_log("Subject: " . $subject);
// error_log("Message: " . $message);

// Check for spam words in the subject
if (preg_match($wordPattern, $subject, $matches)) {
    $error[] = "Spam detected due to prohibited word: '{$matches[1]}' in subject.";
}

// Check for spam words in the message
if (preg_match($wordPattern, $message, $matches)) {
    $error[] = "Spam detected due to prohibited word: '{$matches[1]}' in message.";
}

// Check for URLs in the subject only
if (preg_match($urlPattern, $subject)) {
    $error[] = "Spam detected due to URL in subject.";
}

// Debug: Log detected errors
// if (!empty($error)) {
//    error_log("Errors detected: " . implode(' | ', $error));
// }

/*** END CUSTOM CODE
***/
Last edited by thecoalman on Sat Jan 25, 2025 8:32 pm, edited 1 time in total.
Reason: Moved to Custom Coding

Return to “phpBB Custom Coding”