<?php if (!defined('PmWiki')) exit();
/*
+----------------------------------------------------------------------+
| bridge module for integrating SMF Forum with PmWiki
+----------------------------------------------------------------------+
| Copyright 2008 Hans Bracker
| This program is free software; you can redistribute it and/or modify
| it under the terms of the GNU General Public License, Version 2, as
| published by the Free Software Foundation.
| http://www.gnu.org/copyleft/gpl.html
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
| GNU General Public License for more details.
+----------------------------------------------------------------------+
*/
$RecipeInfo['Auth-SMF']['Version'] = '2008-06-27';

# Save POST input array before SSI.php calls cleanRequest() which adds slashes
$savePost = $_POST;
# load SSI.php 
require_once("$SMFDir/SSI.php");
# Restore POST input array
$_POST = $savePost;

# check if SMF $scripturl is set 
# Note $ScriptUrl is url for pmwiki, $scripturl is url for smf/index.php !!!
if (!isset($scripturl)) echo "<b style='color:red'>Configuration Error:</b>
										<br /> Please add to the SMF file 'Settings.php':
										<br />\$scripturl = \$boardurl . '/index.php';<br />";

# SMF logout calls PmWiki ?action=logout
$_SESSION['logout_url'] = $ScriptUrl."?action=logout"; # after SMF logout PmWiki logout is triggered

# PmWiki ?action=logout, calls SMF  logout
if ($action=='logout') {
	$_SESSION['logout_url'] = $ScriptUrl."?n=".$pagename; # return to current page after logout
	Redirect($pagename, $scripturl.'?action=logout;sesc='.$sc);
	HandleLogoutA($pagename);
	exit;
}

# SMF auth form called by PmWiki ?action=login
# use Site.SMFAuthForm if exists, else SMF ssi_login form
$InputTags['smf_auth_form'] = array(
  ':html' => "<form action='\$scripturl?action=login2' method='post'
    name='authform' accept-charset='".$context['character_set']."'>\$PostVars");
if (PageExists('$SiteGroup.SMFAuthForm'))
	$AuthPromptFmt = array(&$PageStartFmt, 'page:$SiteGroup.SMFAuthForm',
	  "<script language='javascript' type='text/javascript'><!--
	    try { document.authform.authid.focus(); }
	    catch(e) { document.authform.user.focus(); } //--></script>",
	  &$PageEndFmt);
else  
	$AuthPromptFmt = array(&$PageStartFmt, ssiFunction('login'), &$PageEndFmt);

# Set PmWiki author name as user name used by SMF (realName, not memberName login Id). 
# Author name will be set to 'Guest' if the
// user is not signed in.
if ($user_info['username'])
	$Author = $user_settings['realName'];
else 
	$Author = 'Guest';

// Populate variables for AuthUser. Only if the login succeeded, or the user
// will appear to AuthUser as an authenticated user named "anonymous".
if ($context['user']['is_logged'] == 1) {
	$AuthId = $user_settings['memberName'];

	# 1 = SMF Administrator => PmWiki @admin
	if ($user_settings['ID_GROUP'] == 1) {
		$AuthList ['@admin'] = 1;
	}
	# 2 = SMF Global Moderator => PmWiki @editor
	if ($user_settings['ID_GROUP'] == 2) { 
		$AuthList ['@editor'] = 1;
	}	
}
else $AuthId = 'guest';

# add page variables for logged in user's email address and real name ( user name is {$AuthId} )
$FmtPV['$UserEmail'] = 'NoCache($GLOBALS["user_settings"]["emailAddress"])';
$FmtPV['$UserName'] = 'NoCache($GLOBALS["user_settings"]["realName"])';


# Debug:
#echo "<pre>"; print_r($context); echo "</pre>";
#echo "<pre>"; print_r($user_settings); echo "</pre>";

// Include AuthUser for authorization
require_once ("$FarmD/scripts/authuser.php");

Markup('ssi_function','directives',
  '/\\(:smf-(.*?)\\s*:\\)/',
  "mu_ssiFunction");
function mu_ssiFunction($m) {
  return Keep(ssiFunction($m[1]));
}
function ssiFunction($args) {
	global $ScriptUrl; 
	$args = ParseArgs($args, '(?>(\\w+)=)');
	$fn = array_shift($args['']);
	$arg1 = array_shift($args['']);
	$arg2 = array_shift($args['']);
	$fn = 'ssi_'.$fn;
		# call ssi function (from SMF module SSI.php)
		if (function_exists($fn) ) {
			# use output buffer to catch echos
			ob_start();
			if (isset($arg1) && isset($arg2))	$fn( $arg1, $arg2);
			else if (isset($arg1))	$fn( $arg1);
			else $fn();
			$out = ob_get_contents();
			ob_end_clean();
			return $out;
		}		
		else
			return '<b>ERROR!</b> The ssi function <b>'.$fn.'</b> does not exist!';
}