<?php if (!defined('PmWiki')) exit();
/*
	The cmslike.php script makes PmWiki behave as a CMS.
	It complements UserAuth by presenting only the actions the current user is
	allowed to perform. It works with a single template, by inserting a dynamic
	actions menu as a variable, and disables the edit links in the text whenever
	editing is not allowed.

	Version 0.1 (development version; works with pmwiki-2.0.beta25 or above)

	Copyright 2005 Didier Lebrun (dl@vaour.net)
	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.

	Requirements:
	* userauth.php MUST be enabled

	Usage:
	* copy cmslike.php in the cookbook directory
	* add the following lines in your local/config.php file and adjust them to your taste:
		#$CmsLikeMenuItems = array(
		#	'print' => "<a href='\$PageUrl?action=print' target='_blank' rel='nofollow'>\$[Printable View]</a>\n",
		#	'history' => "<a href='\$PageUrl?action=diff' rel='nofollow'>\$[Page History]</a>\n",
		#	'edit' => "<a href='\$PageUrl?action=edit' rel='nofollow'>\$[Edit Page]</a>\n",
		#	'upload' => "<a href='\$PageUrl?action=upload' rel='nofollow'>\$[Upload]</a>\n",
		#	'attr' => "<a href='\$PageUrl?action=attr' rel='nofollow'>\$[Attributes]</a>\n") );
		#$CmsLikeMenuSep = ' ';
		#$CmsLikeAltMenuItems = $CmsLikeMenuItems;
		#$CmsLikeAltMenuSep = ' - ';
		include_once('cookbook/cmslike.php'); # the module itself (you need this line at least !)
	* in your template:
		* add <!--function:CmsLikeMakeMenuFmt--> somewhere before the first actions menu
		* put $CmsLikeMenuFmt (and $CmsLikeAltMenuFmt if used) wherever you want to insert the
		dynamic actions menus

	Notes:
	* UserAuth interception is used for calling CmsLikeMakeActionsList when the group is
	known, so as to establish the relevant rights. The conditions reproduce UserAuth ones.
	* <!--function:CmsLikeMakeMenuFmt--> in the template is used to evaluate $CmsLikeMenuItems
	after the variables in it have been defined.
	* You can use $CmsLikeAltMenuItems and $CmsLikeAltMenuSep to make an alternate menu layout, so you
	can have 2 different layouts in the same page (ex: a vertical one and an horizontal one like those
	in PmWiki default template)
*/

define(CMSLIKE_VERSION, '0.1');

# You can override those in your local/config.php
SDV($CmsLikeMenuItems, array(
	'print' => "<a href='\$PageUrl?action=print' target='_blank' rel='nofollow'>\$[Printable View]</a>\n",
	'history' => "<a href='\$PageUrl?action=diff' rel='nofollow'>\$[Page History]</a>\n",
	'edit' => "<a href='\$PageUrl?action=edit' rel='nofollow'>\$[Edit Page]</a>\n",
	'upload' => "<a href='\$PageUrl?action=upload' rel='nofollow'>\$[Upload]</a>\n",
	'attr' => "<a href='\$PageUrl?action=attr' rel='nofollow'>\$[Attributes]</a>\n") );
SDV($CmsLikeMenuSep, ' ');
# Alternate menu layout, so you can have 2 different menu layouts in your page
SDV($CmsLikeAltMenuItems, $CmsLikeMenuItems);
SDV($CmsLikeAltMenuSep, ' - ');

# Intercept AuthFunction calls
$AuthFunction = "CmsLikeAuth";

$CmsLikeActionsList = array('print');
$CmsLikeActionsListDone = 0;

function CmsLikeAuth($pagename,$level,$authprompt) {
	global $CmsLikeActionsListDone;

	# We build the list on the first AuthFunction call only
	if (!$CmsLikeActionsListDone) { CmsLikeMakeActionsList($pagename); $CmsLikeActionsListDone = 1; }

	# Redirect to the real UserAuth function
	return UserAuth($pagename,$level,$authprompt);
}

function CmsLikeMakeActionsList($pagename) {
	global $GuestUsername, $LoggedInUsername, $UserInfoObj, $LinkPageCreateFmt, $CmsLikeActionsList;

	# Disables the create links in the text
	$no_create_link_fmt = "<a class='createlinktext'>\$LinkText</a><a class='createlink'>?</a>";

	if (isset($UserInfoObj)) {
		$username = $_SESSION['username'];
		if ($username=="") $username = $GuestUsername;
		$group = FmtPageName('$Group',$pagename);

		# Auth processing is identical to UserAuth one
		if ( $UserInfoObj->UserHasAbility($username, 'admin') )
			array_push($CmsLikeActionsList, 'history', 'edit', 'upload', 'attr');
		else {
			$userauth_level = CmsLikeUserAuthLevel('edit', $group);
			if ( $UserInfoObj->UserHasAbility($username, $userauth_level)
				||  $UserInfoObj->UserHasAbility($GuestUsername, $userauth_level)
				||  $UserInfoObj->UserHasAbility($LoggedInUsername, $userauth_level)
				||  $UserInfoObj->UserHasAbility($username, 'edit_all') )
				array_push($CmsLikeActionsList, 'history', 'edit');
			else $LinkPageCreateFmt = $no_create_link_fmt;

			$userauth_level = CmsLikeUserAuthLevel('upload', $group);
			if ( $UserInfoObj->UserHasAbility($username, $userauth_level)
				||  $UserInfoObj->UserHasAbility($GuestUsername, $userauth_level)
				||  $UserInfoObj->UserHasAbility($LoggedInUsername, $userauth_level)
				||  $UserInfoObj->UserHasAbility($username, 'upload_all') )
				array_push($CmsLikeActionsList, 'upload');

			$userauth_level = CmsLikeUserAuthLevel('attr', $group);
			if ( $UserInfoObj->UserHasAbility($username, $userauth_level)
				||  $UserInfoObj->UserHasAbility($GuestUsername, $userauth_level)
				||  $UserInfoObj->UserHasAbility($LoggedInUsername, $userauth_level)
				||  $UserInfoObj->UserHasAbility($username, 'attr_all') )
				array_push($CmsLikeActionsList, 'attr');
		}
	}
	else  $LinkPageCreateFmt = $no_create_link_fmt;
}

function CmsLikeUserAuthLevel($level,$group) {
	global $UserInfoObj;

	# Change checked level if group specific abilities exist user info object
	$group_specific_level = $level."_group-".$group;
	if( $UserInfoObj->AnyUserHasAbility($group_specific_level) ) return $group_specific_level;
	else return $level;
}

function CmsLikeMakeMenuFmt() {
	global $CmsLikeActionsList, $CmsLikeMenuItems, $CmsLikeMenuSep, $CmsLikeMenuFmt,
		$CmsLikeAltMenuItems, $CmsLikeAltMenuSep, $CmsLikeAltMenuFmt;

	$items_fmt = array();
	$alt_items_fmt = array();
	foreach($CmsLikeActionsList as $action) {
		array_push($items_fmt, $CmsLikeMenuItems[$action]);
		array_push($alt_items_fmt, $CmsLikeAltMenuItems[$action]);
	}

	$CmsLikeMenuFmt = implode($CmsLikeMenuSep, $items_fmt);
	$CmsLikeAltMenuFmt = implode($CmsLikeAltMenuSep, $alt_items_fmt);
}

?>