0

Guys I am trying to block users under 10 posts from spamming their links help me to make this code work, I need it to block URL.tld and URL.tld/something

function badwords(&$post)
{
    global $mybb, $db;

    if($mybb->user['postnum'] > 10)
    return;

    $badwords = explode('|', 'http|https|www|com|net|org|co.uk');
    foreach($badwords as $badword)
    {

        if(preg_match("/\b$badword\b/i", $post->data['message'], $match))
        {

            $post->errors['badwords']['error_code'] = "Error message goes here";

        }
    }
    return;

}
  • [this blog post](http://blogs.lse.ac.uk/clt/2008/04/23/a-regular-expression-to-match-any-url/) seems relevant. – Dale Jan 06 '14 at 13:40
  • You can probably adapt these resources to suite your needs http://stackoverflow.com/questions/9151681/find-url-from-string-with-php and http://css-tricks.com/snippets/php/find-urls-in-text-make-links/ – MonkeyZeus Jan 06 '14 at 13:57
  • Please help me I only want to check for URL in posts I am not comparing it to anything please – user3065884 Jan 06 '14 at 14:09

3 Answers3

0

You can use this expression: ^(http|https)?(:)?(/)?(/)?[a-z0-9]+([-.]{1}[a-z0-9]+).[a-z]{2,5}(:[0-9]{1,5})?(/.)?$

If you want to make the ones that are false become true, you just need to make this bit .[a-z]{2,5} optional.

And if you want to ignoreCase you should add i to the end of the regex.

$urls = array(
        'google.com/test', // true
        'http://google', // false
        '://google.com', // true
        ':google.com', // true
        ':/google.com', // true
        'www.google', // false
        'www.google.com' // true
    );

    $regex = '/^(http|https)?(:)?(\/)?(\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/';

    foreach($urls as $url){
        $t = preg_match($regex, $url, $match);
        var_dump($t);
    }

Live example here: example

Jorge Faianca
  • 791
  • 5
  • 11
  • Thanks buddy this one works great I only need to make it work with "domain.tld" or "domain.tld/something" "$regex = "%\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s";" – user3065884 Jan 06 '14 at 14:38
0

You can use this expression:

^(http|https)?(:)?(/)?(/)?[a-z0-9]+([-.]{1}[a-z0-9]+).[a-z]{2,5}(:[0-9]{1,5})?(/.)?$

Using this, all messages will be blocked, since [a-z0-9] +) will block all letters and numbers.

Fabio Antunes
  • 22,251
  • 15
  • 81
  • 96
Niek
  • 1
-1

Matching links can be done by using the following regex pattern:

function badwords(&$post)
{
    global $mybb, $db;

    if($mybb->user['postnum'] > 10)
    return;

    // build the regex pattern that matches URLs
    $regex = "%\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s";

    if(preg_match($regex, $post->data['message'], $match))
    {
        $post->errors['badwords']['error_code'] = "Error message goes here";
    }

    return;
}

This regex pattern can then be used in the preg_match function.

Pakspul
  • 648
  • 5
  • 17
  • I need to add something like this I believe "preg_match("/^[a-zA-Z0-9-.]+(.com|.in|.co|.info|.name)$/", $post->data['message'], $matches);" but I can get it work please help me to fix the code – user3065884 Jan 06 '14 at 13:40
  • Look for preg_match url, there are some results on Google that give regex patterns that match every url type. If there is a match, then you can mark them as bad. – Pakspul Jan 06 '14 at 13:43
  • The problem is I don't know how to incorporate those codes in to mine, please help – user3065884 Jan 06 '14 at 13:47
  • I have changed the answer so you can copy/paste the function in your code. Could you test it and give feedback on the results. – Pakspul Jan 06 '14 at 13:49
  • it shows error messages regarding URL! What about this one "if(preg_match("/\b$badword\b/i", $post->data['message'], $match))" – user3065884 Jan 06 '14 at 13:54
  • I'm not paying attention, i forget to change the preg_match line. This is now become: if(preg_match($regex, $post->data['message'], $match)). The pattern has been incorporated into the function. – Pakspul Jan 06 '14 at 13:57
  • I am very sorry but it doesn't block URL? Could you help me with something more simple but effective like "preg_match" – user3065884 Jan 06 '14 at 14:05
  • Next time state the error message you get, that makes my job easier because I then know where to look. But, positive news, i tested local and the next change should work. – Pakspul Jan 06 '14 at 14:11
  • Holy cow It works thanks very much. One question how to block also extensions like so "domain.ltd" without http:// and www. – user3065884 Jan 06 '14 at 14:19
  • You can change the regex pattern for that. Google: "php regex url without www", and test some of the patterns that are given. – Pakspul Jan 06 '14 at 14:22
  • Could you please help me I just can make it work please. How make it work without www and http – user3065884 Jan 06 '14 at 15:20