blob: e3af61a8a93af91db3abc3e324de3b0366df4541 [file] [log] [blame]
<?php if (!defined('PmWiki')) exit();
/* Copyright 2004 Patrick R. Michaud (
This file is part of 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. See pmwiki.php for full details.
This script provides a URL-approval capability. To enable this
script, add the following line to a configuration file:
The URL prefixes to be allowed are stored as patterns in
$WhiteUrlPatterns. This array can be loaded from config.php, or
from the wiki pages given by the $ApprovedUrlPagesFmt[] array.
Any http: or https: URL that isn't in $WhiteUrlPatterns is rendered
using $UnapprovedLinkFmt.
The script also provides ?action=approveurls and ?action=approvesites,
which scan the current page for any new URLs to be automatically added
the first page of $UrlApprovalPagesFmt.
Finally, the script will block any post containing more than
$UnapprovedLinkCountMax unapproved urls in it. By default this
is set to a very large number, leaving the posting of unapproved
urls wide open, but by setting $UnapprovedLinkCountMax to a smaller
number you can limit the number of unapproved urls that make it into
a page. (Wikispammers seem to like to post long lists of urls, while
more "normal" authors tend to only post a few.)
$LinkFunctions['http:'] = 'LinkHTTP';
$LinkFunctions['https:'] = 'LinkHTTP';
$ApprovedUrlPagesFmt = array('Main.ApprovedUrls');
$UnapprovedLinkFmt =
"\$LinkText<a class='apprlink' href='\$PageUrl?action=approvesites'>$[(approve sites)]</a>";
$HTMLStylesFmt[] = '.apprlink { font-size:smaller; }';
$ApproveUrlPattern =
$WhiteUrlPatterns = array();
$HandleActions['approveurls'] = 'HandleApprove';
$HandleActions['approvesites'] = 'HandleApprove';
SDV($UnapprovedLinkCountMax, 1000000);
array_splice($EditFunctions, array_search('PostPage', $EditFunctions),
0, 'BlockUnapprovedPosts');
function LinkHTTP($pagename,$imap,$path,$title,$txt,$fmt=NULL) {
global $EnableUrlApprovalRequired, $IMap, $WhiteUrlPatterns, $FmtV,
$UnapprovedLinkCount, $UnapprovedLinkFmt;
if (!IsEnabled($EnableUrlApprovalRequired,1))
return LinkIMap($pagename,$imap,$path,$title,$txt,$fmt);
static $havereadpages;
if (!$havereadpages) { ReadApprovedUrls($pagename); $havereadpages=true; }
$p = str_replace(' ','%20',$path);
$url = str_replace('$1',$p,$IMap[$imap]);
foreach((array)$WhiteUrlPatterns as $pat) {
if (preg_match("!^$pat(/|$)!",$url))
return LinkIMap($pagename,$imap,$path,$title,$txt,$fmt);
$FmtV['$LinkText'] = $txt;
return FmtPageName($UnapprovedLinkFmt,$pagename);
function ReadApprovedUrls($pagename) {
global $ApprovedUrlPagesFmt,$ApproveUrlPattern,$WhiteUrlPatterns;
foreach((array)$ApprovedUrlPagesFmt as $p) {
$apage = ReadPage(FmtPageName($p,$pagename));
foreach($match[0] as $a) {
$urlp = preg_quote($a,'!');
if (!in_array($urlp,$WhiteUrlPatterns))
$WhiteUrlPatterns[] = $urlp;
function HandleApprove($pagename) {
global $ApproveUrlPattern,$WhiteUrlPatterns,$ApprovedUrlPagesFmt,$action;
$page = ReadPage($pagename);
$text = preg_replace('/[()]/','',$page['text']);
$addpat = array();
foreach($match[0] as $a) {
foreach((array)$WhiteUrlPatterns as $pat)
if (preg_match("!^$pat(/|$)!",$a)) continue 2;
if ($action=='approvesites')
$addpat[] = $a;
if (count($addpat)>0) {
$aname = FmtPageName($ApprovedUrlPagesFmt[0],$pagename);
$apage = RetrieveAuthPage($aname,'edit');
if (!$apage) Abort("?cannot edit $aname");
$new = $apage;
if (substr($new['text'],-1,1)!="\n") $new['text'].="\n";
foreach($addpat as $pat) $new['text'].=" $pat\n";
$_POST['post'] = 'y';
function BlockUnapprovedPosts($pagename, &$page, &$new) {
global $EnableUrlApprovalRequired, $UnapprovedLinkCount,
$UnapprovedLinkCountMax, $EditMessageFmt, $BlockMessageFmt;
if (!IsEnabled($EnableUrlApprovalRequired, 1)) return;
if ($UnapprovedLinkCount <= $UnapprovedLinkCountMax) return;
$EditMessageFmt .= $BlockMessageFmt;