<?php if (!defined('PmWiki')) exit();

/**	=== Bloge-UrlApprove ===
 *	Copyright 2009 Eemeli Aro <eemeli@gmail.com>
 *
 *	Approve links one at a time
 *
 *	Developed and tested using PmWiki 2.2.x
 *
 *	To install, add the following to your configuration file:

		include_once("$FarmD/cookbook/bloge-urlapprove.php");

 *	This is a part of the Bloge bundle of recipes, but may be used by itself.
 *	For more information, please see the online documentation at
 *		http://www.pmwiki.org/wiki/Cookbook/Bloge-UrlApprove and at
 *		http://www.pmwiki.org/wiki/Cookbook/Bloge
 *
 *	This program is free software; 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.
 */

$RecipeInfo['Bloge-UrlApprove']['Version'] = '2009-08-10';

SDV($UnapprovedLinkFmt, empty($AuthId)
	? '$LinkText'
	: "<u>\$LinkText</u>
<a class='apprlink' rel='nofollow' href='\$LinkUrl' title='$[Unapproved link]'>($[test])</a>
<a class='apprlink' rel='nofollow' href='{\$PageUrl}?action=approvelink&link=\$LinkUrl' title='$[Approve link]'>($[url ok])</a>
<a class='apprlink' rel='nofollow' href='{\$PageUrl}?action=approvelinksite&link=\$LinkUrl' title='$[Approve site]'>($[site ok])</a>");

include_once("$FarmD/scripts/urlapprove.php");

$FmtPV['$LinksToApprove'] = '$GLOBALS["UnapprovedLinkCount"]';

SDV($HandleActions['approvelink'], 'HandleApproveOneLink');
SDV($HandleAuth['approvelink'], 'edit');

SDV($HandleActions['approvelinksite'], $HandleActions['approvelink']);
SDV($HandleAuth['approvelinksite'], $HandleAuth['approvelink']);

function HandleApproveOneLink($pagename, $auth='edit') {
	global $WhiteUrlPatterns, $ApprovedUrlPagesFmt, $action;

	if (empty($_GET['link'])) Abort('HandleApproveOneLink: no link defined');
	$link = $_GET['link'];

	if ($action=='approvelinksite') $link = preg_replace("!^([^:]+://[^/]+).*$!", '$1', $link);

	ReadApprovedUrls($pagename);
	foreach( (array)$WhiteUrlPatterns as $pat )
		if (preg_match("!^$pat(/|$)!i", $link)) Abort('HandleApproveOneLink: link already approved?');

	Lock(2);
	$aname = FmtPageName($ApprovedUrlPagesFmt[0], $pagename);
	$apage = RetrieveAuthPage($aname, $auth);
	if (!$apage) Abort("?cannot edit $aname");

	$new = $apage;
	if (substr($new['text'],-1,1) != "\n") $new['text'] .= "\n";
	$new['text'] .= "$link\n";
	$_POST['post'] = 'y';
	PostPage($aname,$apage,$new);

	Redirect($pagename);
}