<?php 
/*-------------------------------------------
通行站反向整合接口远程调用文件

本接口不需要导入第三方程序的用户和密码信息，系统会自动判断和生成

本文件仅作为网关供远程调用
请使用或参考 pp_dederemote_interface.asp 提供的接口函数进行整合编程
-----------------------------------------*/
require_once(dirname(__FILE__)."/../../include/config_base.php");
header("Content-Type: text/html; charset=gb2312");

if($cfg_pp_isopen = 0){
	echo "系统没开启通行证功能，禁止远程调用！";
	exit();
}

$cfg_ndsql = 0;

if(empty($rmdata)){
	echo "没接收到任何远程数据！";
	exit();
}

$keys = Array('userid','signstr','action');

foreach($keys as $v) $$v = '';

//解码GET字符串
$datas = explode('&',$rmdata);
foreach($datas as $ky){
	$nkys = explode('=',$ky);
	if(in_array($nkys[0],$keys) && isset($nkys[1])) ${$nkys[0]} = urldecode($nkys[1]);
}

$ntime = time();

if($action!='exit'){
  //验证证书
  if($userid==''||!TestStringSafe($userid)){
	  echo "用户ID为空或存在非法字符串！".$oldrmdata;
	  exit();
  }
  if(strlen($userid)>24){
	  echo "用户ID长度不能超过24位！";
	  exit();
  }
  $testSign = substr(md5($userid.$cfg_cookie_encode),0,24);
  if($testSign!=$signstr){
	  echo "证书验证失败！";
	  exit();
  }
}

//注解里的function仅方便UltraEdit索引，并无其它意义
/*--------------------------------
会员注册
function __UserReg()
---------------------------------*/
if($action=='reg'){
	Z_OpenSql();
	$row = $cfg_ndsql->GetOne("Select ID,pwd From #@__member where userid = '$userid' ");
	//如果已经存在用户名，检测密码是否正确，如果密码正确，则返回登录信息，否则不允许注册
	if(is_array($row)){
		 echo "用户ID：{$userid} 已存在，请使用其它用户名！";
		 Z_CloseSql();
		 exit();
	}
	$userpwd = chr(mt_rand(ord('A'),ord('Z'))).chr(mt_rand(ord('a'),ord('z'))).chr(mt_rand(ord('A'),ord('Z'))).chr(mt_rand(ord('A'),ord('Z'))).chr(mt_rand(ord('a'),ord('z'))).mt_rand(1000,9999).chr(mt_rand(ord('A'),ord('Z')));
	$userpwd = GetEncodePwd($userpwd);
	$loginip = Z_GetIP();
	$inQuery = "
 	 INSERT INTO #@__member(userid,pwd,uname,sex,birthday,membertype,money,
 	 weight,height,job,province,city,myinfo,tel,oicq,email,homepage,
 	 jointime,joinip,logintime,loginip,showaddr,address) 
   VALUES ('$userid','$userpwd','$userid','','0000-00-00','10','0',
   '0','0','','0','0','','','','','','$ntime','$loginip','$ntime','$loginip','0','');";
   $cfg_ndsql->ExecuteNoneQuery($inQuery);
   $row = $cfg_ndsql->GetOne("Select ID From #@__member where userid like '$userid' ");
	 if(!is_array($row)){
		 Z_CloseSql();
		 echo "系统出错，无法完成注册，请联系管理员！";
		 exit();
	 }
	 $ID = $row['ID'];
	 Z_CloseSql();
	 $backString = $ID;
	 echo 'OK!'.$backString;
	 exit();
}
/*--------------------------------
会员登录
function __UserLogin()
---------------------------------*/
else if($action=='login'){
	Z_OpenSql();
	$row = $cfg_ndsql->GetOne("Select ID,pwd From #@__member where userid like '$userid' ");
	if(!is_array($row)){
		 $userpwd = chr(mt_rand(ord('A'),ord('Z'))).chr(mt_rand(ord('a'),ord('z'))).chr(mt_rand(ord('A'),ord('Z'))).chr(mt_rand(ord('A'),ord('Z'))).chr(mt_rand(ord('a'),ord('z'))).mt_rand(1000,9999).chr(mt_rand(ord('A'),ord('Z')));
	   $userpwd = GetEncodePwd($userpwd);
	   $loginip = Z_GetIP();
		 $inQuery = "
 	   INSERT INTO #@__member(userid,pwd,uname,sex,birthday,membertype,money,
 	   weight,height,job,province,city,myinfo,tel,oicq,email,homepage,
 	   jointime,joinip,logintime,loginip,showaddr,address) 
     VALUES ('$userid','$userpwd','$userid','','0000-00-00','10','0',
     '0','0','','0','0','','','','','','$ntime','$loginip','$ntime','$loginip','0','');";
     $cfg_ndsql->ExecuteNoneQuery($inQuery);
     $row = $cfg_ndsql->GetOne("Select ID,pwd From #@__member where userid like '$userid' ");
	}
	$ID = $row['ID'];
	$loginip = Z_GetIP();
	$cfg_ndsql->ExecuteNoneQuery("update #@__member set logintime='$ntime',loginip='$loginip' where ID='$ID' ");
	Z_CloseSql();
	$backString = $ID;
	echo 'OK!'.$backString;
	exit();
}
/*--------------------------------
退出系统
function __UserExit()
---------------------------------*/
else if($action=='exit'){
	$backString = "0";
	echo 'OK!'.$backString;
	exit();
}
/*--------------------------------
无法识别远程动作
function __ActionError()
---------------------------------*/
else{
	echo "无法识别你的动作！";
	exit();
}

//其它功能如函数
function Z_OpenSql(){
	global $cfg_ndsql;
	if(!$cfg_ndsql) $cfg_ndsql = new DedeSql(false);
}
function Z_CloseSql(){
	global $cfg_ndsql;
	if($cfg_ndsql) $cfg_ndsql->Close();
}
function Z_GetIP(){
	if(!empty($_SERVER["HTTP_CLIENT_IP"])) $cip = $_SERVER["HTTP_CLIENT_IP"];
	else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	else if(!empty($_SERVER["REMOTE_ADDR"])) $cip = $_SERVER["REMOTE_ADDR"];
	else $cip = "无法获取！";
	return $cip;
}
?>