<?php if (!defined('PmWiki')) exit();
/*  Copyright 2004 Patrick R. Michaud (pmichaud@pobox.com)
    This file is blocklist.php for PmWiki, you can redistribute it 
    and/or modify it under the terms of the GNU General Public License 
    as published by the Free Software Foundation; either version 2 of 
    the License, or (at your option) any later version.  

    This script adds blocklisting capabilities to PmWiki.  By default,
    it searches through Main.Blocklist (or whatever pages are specified
    by the $BlocklistPages variable) looking for strings of the form
    "block:something" to be excluded from any posting to the site.
    For example, if Main.Blocklist contains "block:spam.com", then
    any posting to "spam.com" will be disallowed.

    In addition, the script scans the Main.Blocklist page for 
    IP addresses of the form "a.b.c.d" or "a.b.c.*", and blocks 
    any postings coming from a host matching the IP address or
    address range.

    To use the script, simply place it in the cookbook/ directory
    and add the following line to local/config.php:

        include_once('cookbook/blocklist.php');

    The script also sets the variable $Blocklisted so that the
    administrator can take actions other than simply blocking the
    post.  
*/

# This script only applies to editing, so 
# if we're not editing just return.
if ($action != 'edit') return;

SDV($BlocklistPages, '$SiteGroup.Blocklist');
SDV($BlocklistMessageFmt, "<h3 class='wikimessage'>$[This post has been blocked by the administrator]</h3>");

$Blocklisted = 0;
foreach((array)$BlocklistPages as $b) {
  $pn = FmtPageName($b,$pagename);
  $page = ReadPage($pn);
  if (!$page) continue;
  if (preg_match_all('/\\d+\\.\\d+\\.\\d+\\.(\\d+|(?=\\*))/', 
          $page['text'], $match)) {
    foreach($match[0] as $m) {
      if ($m == $_SERVER['REMOTE_ADDR']) $Blocklisted++;
      if (substr($m,-1,1)=='.' && 
          strncmp($_SERVER['REMOTE_ADDR'],$m,strlen($m))==0) $Blocklisted++;
    }
  }
  if ($pagename == $pn) return;
  if (preg_match_all('/block:(\\S+)/', $page['text'], $match)) {
    foreach($match[1] as $e) {
      if (stristr(@$_POST['text'],$e) !== FALSE) $Blocklisted++;
    }
  }
}

# okay, if the posting is blocked, then disallow the post and provide
# a message
if ($Blocklisted) { 
  unset($_POST['post']); $EnablePost = 0;
  $MessagesFmt[] = $BlocklistMessageFmt;
}

?>