<?php
/********************************************************************
 *                     DIRCMS整合PHPWIND7.5单点登录通行证
 *                      ------------------------------
 *   begin                : 2010-4-29
 *   copyright            : (C) 2009 DirCMS
 *   email                : uuturn@126.com
 *
 *   $Id: passport_server_phpwind.php,v 1.0.0 2010-4-29 10:44:01 M Exp $
********************************************************************/
if(!$forward)$forward=HTTP_REFERER?HTTP_REFERER:SITE_URL;
$passport_key=$DIRCMS['passport_key'];
$jumpurl=$DIRCMS['passport_url'];
if(substr($jumpurl, -1, 1) != '/') $jumpurl .= '/';
$action=in_array($action,array('login','register','logout'))?$action:'login';

$userdb=array();
if(!$userid) $userid = $member->get_userid($username);
$r=$member->get($userid,'id,username,email,amount,point');
$userdb['uid']= $r['id'];
$userdb['username']=$r['username'];
$userdb['password']=$password;
$userdb['email']=$r['email'];
$userdb['money']=$r['amount'];
$userdb['credit']=$r['point'];
$userdb['time']=TIME;
$userdb['cktime']=$cookietime>0?(TIME + $cookietime):0;

$userdb_encode = '';
foreach($userdb as $key=>$val)
{
	$userdb_encode .= $userdb_encode ? "&$key=$val" : "$key=$val";
}

$db_hash = $passport_key;
$userdb_encode = str_replace('=','',StrCode($userdb_encode));

if($action=='login' || $action=='register')
{
	if($action == 'register' || $action == 'login')
	{
		$forward = $DIRCMS['site_url'];
	}
	$verify = md5("login$userdb_encode$forward$passport_key");
	$url = $jumpurl."passport_client.php?action=login&userdb=".rawurlencode($userdb_encode)."&forward=".rawurlencode($forward)."&verify=".rawurlencode($verify);
}
elseif($action=='logout')
{
	$verify = md5("quit$userdb_encode$forward$passport_key");
    $url = $jumpurl."passport_client.php?action=quit&userdb=".rawurlencode($userdb_encode)."&forward=".rawurlencode($forward)."&verify=".rawurlencode($verify);
}

function StrCode($string,$action='ENCODE')
{
	$key	= substr(md5($_SERVER["HTTP_USER_AGENT"].$GLOBALS['db_hash']),8,18);
	$string	= $action == 'ENCODE' ? $string : base64_decode($string);
	$len	= strlen($key);
	$code	= '';
	for($i=0; $i<strlen($string); $i++)
	{
		$k		= $i % $len;
		$code  .= $string[$i] ^ $key[$k];
	}
	$code = $action == 'DECODE' ? $code : base64_encode($code);
	return $code;
}
?>