<?php
require './include/common.inc.php';
sajax_init();
sajax_export("doSub");
sajax_handle_client_request();
//$GLOBALS['sajax_debug_mode'] = 1;

//if(!$_userid) showmessage($LANG['please_login'], '/member/login.php?forward='.urlencode($PHP_URL));
if(!$_userid) header('location:/member/login.php?forward='.urlencode($PHP_URL));
$rs = $db->get_one("SELECT area FROM ".TABLE_MEMBER_INFO." WHERE `userid` = '$_userid'");
$areas = explode(',',$rs['area']);


$servlist = array();
$result = $db->query("SELECT * FROM ".TABLE_GAMESERVER." ORDER BY id");
while($r = $db->fetch_array($result))
{
  foreach($areas as $area)
  {
	  if($r['id'] == $area) $r['ag'] = '1';
  }
	$servlist[] = $r;
}

$head['title'] = "激活游戏";


include template('game', 'actgame');



function doSub($id,$gpass)
{
	global $db,$_userid,$_username;

	if (!$id) return "错误的参数";
	$rs = $db->get_one("SELECT * FROM ".TABLE_GAMESERVER." WHERE id='$id'");
	@extract($rs);
	
/*******************调用SQL类型开始**************/
  if(!$dataattr) return "错误类型:unset data_attrib，请联系管理员！";
	if($dataattr == 'mssql')
	{
		if(!extension_loaded('mssql')) return "错误类型:unloaded mssql，请联系管理员！";
		include HS_ROOT.'/include/db_'.$dataattr.'.class.php';
		
    $mssqlco = @mssql_connect($serverip.':'.$port, $loginname, $loginpass,$dataacc);
		if(!$mssqlco) return "错误类型:connect mssql error，请联系管理员！";
		
		$sqldb = new db_mssql;
		$sqldb->connect($serverip.':'.$port, $loginname, $loginpass, $dataacc);
	}
	elseif($dataattr == 'mysql')
	{
	
    $msyqlco = @mysql_connect($serverip.':'.$port, $loginname, $loginpass,$dataacc);
		if(!$msyqlco) return "错误类型:connect mysql error，请联系管理员！";
		
		$sqldb = new db_mysql;
		$sqldb->connect($serverip.':'.$port, $loginname, $loginpass, $dataacc);

	}
	else
	{
	  return "错误类型:data_attrib error，请联系管理员！";
	}
/*******************调用SQL类型结束**************/	
$spwd = hsmd5($gpass);

$checkuser = $sqldb->get_one("SELECT password,user_priv FROM user WHERE username='$_username'");

if($checkuser)
{
  if ($checkuser['password'] == $spwd && $checkuser['user_priv'] == "1")
  {
    $area = $db->get_one("SELECT area FROM ".TABLE_MEMBER_INFO." WHERE userid='$_userid'");
    if(!$area['area'] || $area['area'] =='')
    {
      $newarea = $id;
    }else{
      $newarea = $area['area'].','.$id;
    }
    $db->query("UPDATE ".TABLE_MEMBER_INFO." SET area='$newarea' WHERE userid='$_userid'");
    if($db->affected_rows())
    {
      return "true";
      exit;
    }else{
      return "错误类型:update area error，请联系管理员！";
    }
  }else{
   $query = "UPDATE user SET password='$spwd',user_priv='1' WHERE username='$_username'";
  }
}
else
{
$query = "INSERT INTO user (username,password,user_priv) VALUES ('$_username','$spwd','1')";
}
$sqldb->query($query);
if($sqldb->affected_rows())
{
$area = $db->get_one("SELECT area FROM ".TABLE_MEMBER_INFO." WHERE userid='$_userid'");
if(!$area['area'] || $area['area'] =='')
{
$newarea = $id;
}else{
$newarea = $area['area'].','.$id;
}
$db->query("UPDATE ".TABLE_MEMBER_INFO." SET area='$newarea' WHERE userid='$_userid'");
if($db->affected_rows())
{
return "true";
exit;
}else{
return "错误类型:update area error，请联系管理员！";
}
}else{
return "错误类型:unknow insert error，请联系管理员！";
}

}

?>