<?php

/*
 *	W-AGORA 4.1
 *	-----------
 *	$Id: shared_user.php,v 1.2 2005/06/05 16:42:46 mdruilhe Exp $
 *
 *	Usage:	This authentication module is intented to allow a site to share the same user database 
 *			with another site (called "shared_site"). Users already registered in the "shared_site"
 *			are automatically registered in the current site during the first login process.
 *			
 *	Installation:
 *	-------------
 *			1. Copy this file into the user/ directory
 *			2. set the variable $shared_site to the site name that holds the users, the site must 
 *			exists prior to create the new site
 *			3. Create a new site and select "shared" in the "User source"
 *
 *	current restrictions:
 *	---------------------
 *			1. the 2 sites sharing the user database must be created in the same SQL database
 *			2. Users created in the current site (ie. which use this module) will not be known
 *			in site1 and therefore should first register in the shared site. This is usually done 
 *			in the register process ( getRegisterURL() ) which register the user in the shared site
 *			rather than in the current site.
 *			3. Updating the user profile in the current site will _NOT_ cause the user profile to be
 *			updated in the shared site and will be reset at the next user login 
 */

// Change to the site that you want to share the users
$shared_site = "";

if (!defined('_GLOBALS')) {
	die('Hacking attempt');
}

if (!defined("_AUTH")) {
	include ("$inc_dir/auth.$ext");
}

class shared_user_source extends wa_Auth {

 function shared_user_source() {
	global $shared_site, $db;

	if (empty($shared_site) ) {
		die("<b>Fatal error</b>: The shared site is not defined, please adjust the variable  <b>\$shared_site</b> in <strong>".__FILE__."</strong>");
	}

	$db->query("select bn_name from agora where bn_name='$shared_site'");
	if (!$db->next_record() ) {
		die("<b>Fatal error</b>: The shared site '<b>$shared_site</b>' does not exists, please adjust the variable  <b>\$shared_site</b> in <strong>".__FILE__."</strong>");
	}

 }

 function getRegisterURL() {
	global $shared_site, $site;

	$site = $shared_site; 
	return;			# use default
 }

 function getForgotPasswordURL() {
	global $shared_site, $site;

	$site = $shared_site; 
    return;			# use default
 }

 function getChangePasswordURL() {
	global $shared_site, $site;

	$site = $shared_site; 
	return;			# use default
 }

 function getEditProfileURL() {
	global $shared_site, $site;

	$site = $shared_site; 
	return;			# use default
 }

 function getLoginURL() {
	return;			# use default
 }

 function getLoginInfo () {
	global $shared_site;

	// Check if the user is already authenticated in the common site
	if (isset($_COOKIE["${shared_site}_auth"]) ) {
		$cookie_auth = (get_magic_quotes_gpc ()) ? stripslashes($_COOKIE["${shared_site}_auth"]) : $_COOKIE["${shared_site}_auth"];
		$pairs = split ('&', $cookie_auth);
		reset($pairs);
		while (list (,$pair) = each ($pairs) ) {
			$pair = split ('=', $pair);
			$var = $pair[0];
			$$var = $pair[1];
		}
		# At this point we should have $uid/$pw available in the current scope
		# return the authentication information
		return array("userid" => $uid, "encryptedpw" => $pw);
	}

	return false;
 }

 function encryptPassword ($password) {
	return md5($password);
 }

 function authenticateUser($site, $userid, $encryptedpw) {
	global $shared_site, $db;
	$this->user = $db->getUser($site, $userid);
	if (!is_array($this->user) ) {
		unset ($this->user);
		$this->user = $db->getUser($shared_site, $userid);
		if (!is_array($this->user) ) {
			return false;
		}
	}

	if ($encryptedpw != $this->user["password"]) {
		return false;
	}

	return true;

 }

}; // end class
