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


# Copyright
# =========
# 
# Copyright (c) 2006 Criss Ittermann. This software is released under GPL
# under the same terms as PmWiki.
#
# Description
# ===========
#
# vbulletin.php translates vbulletin user groups to PmWiki user groups.
#
# Requirements
# =====
# Requires adodb be installed & included (see instructions)
#
# Usage
# =====
#
# To use this for user authentication, the following steps must be taken.
# 

# Additions for config.php
/* 

include_once("cookbook/adodb/adodb.inc.php");
// not optional - database info
$VB_DBase = array (
	'dbtype' => 'mysql',
	'host' => 'localhost',
	'db' => 'database',
	'user' => 'user',
	'password' => 'password');	

// not optional
$VB_TablePrefix = "prefix_";
// each of the following is optional
$VB_UserGID = array (
	'#' => '@group');

$VB_MemberGID = array (
	'#' => '@group');

$VB_DisplayGID = array (
	'#' => '@group');
// not optional - includes the script
include_once('cookbook/vbulletin.php');
*/
#
# Release History
# ===============
#
# v.0.1.0 2006-10-09 - Private release
# v.0.1.1 2006-10-10 - Fixed Member group retrieval
# v.0.1.2 2006-10-11 - Add salt retrieval
/*
*/

$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
VB2pmwiki();


function VB2pmwiki() {
	global $VB_UserGID, $VB_MemberGID, $VB_DisplayGID, $VB_DBase, $AuthUser, $MessagesFmt;
	// if no settings it will skip running
	if(is_array($VB_UserGID) || is_array($VB_MemberGID) || is_array($VB_DisplayGID) ) {
	
		// set up ADODB
		$db = NewADOConnection($VB_DBase['dbtype']);
		$db->Connect($VB_DBase['host'], $VB_DBase['user'], $VB_DBase['password'], $VB_DBase['db']);
	
		// seed an array for queries
		$queries = array();
	
		// form queries for each field case
		if(is_array($VB_UserGID)) {
			$queries = VB_MakeQueries("usergroupid", $VB_UserGID);
		}
		if(is_array($VB_MemberGID)) {
			$queries = array_merge($queries, VB_MakeQueries("membergroupids", $VB_MemberGID, 1));
		}
		if(is_array($VB_DisplayGID)) {
			$queries = array_merge($queries, VB_MakeQueries("displaygroupid", $VB_DisplayGID));
		}
		
		//$MessagesFmt[] = "<pre>" . print_r($queries, true) . "</pre>";			

		// grab results for each query
		foreach ($queries as $query ){
			$result = $db->Execute($query);
			if ($result === false) die("failed");  
			while (!$result->EOF) {
				$auth_array[] = $result->fields;
				$result->MoveNext();
			} 
		}
		// create the PmWiki AuthUser syntax
		foreach($auth_array as $pairing) {
			foreach($pairing as $key=>$value) $AuthUser[$key][] = $value;
		}
		//$MessagesFmt[] = "<pre>" . print_r($AuthUser, true) . "</pre>";			
	} else {
		$MessagesFmt[] = "There are problems with the wiki settings.  Please notify the administrator. (vbulletin)";
		return false;
	}
}


function VB_MakeQueries($field, $array, $multi=0) {
	global $VB_TablePrefix;
	foreach($array as $key=>$value){
		if((! is_int($key)) || (! preg_match('/^@[A-Za-z0-9]*$/', $value))) {exit("There is an error in your vbulletin.php settings in config.php.");}
		$result = $field . "=" . $key;
		if ($multi == 1)  $result = "$field = '$key' OR $field LIKE '%,$key' OR $field LIKE '%,$key,%' OR $field LIKE '$key,%'";
		$results[] = "SELECT username as '" . $value . "' FROM " . $VB_TablePrefix . "user WHERE " .  $result;
	}
	return $results;
}


function VB_GetSalt($username) {
	global $VB_DBase;
	$query = "Select salt from user where username like '$username'";
	// set up ADODB
	$db = NewADOConnection($VB_DBase['dbtype']);
	$db->Connect($VB_DBase['host'], $VB_DBase['user'], $VB_DBase['password'], $VB_DBase['db']);
	$result = $db->Execute($query);
	if ($result === false) die("failed");  
	return $result->fields['salt'];
}