<?php
!defined('P_W') && exit('Forbidden');

$updatecache = false;
$query = $db->query("SELECT db_name,db_value FROM pw_config WHERE db_name='db_siteid' OR db_name='db_siteownerid' OR db_name='db_sitehash'");
while ($rt = $db->fetch_array($query)) {
	if (($rt['db_name']=='db_siteid' && $rt['db_value']!=$db_siteid) || ($rt['db_name']=='db_siteownerid' && $rt['db_value']!=$db_siteownerid) || ($rt['db_name']=='db_sitehash' && $rt['db_value']!=$db_sitehash)) {
		${$rt['db_name']} = preg_replace('/[^\d\w\_]/is','',$rt['db_value']);
		$updatecache = true;
	}
}
$db->free_result($query);
if (!$db_siteid) {
	$db_siteid = generatestr(32);
	$db->update("REPLACE INTO pw_config SET db_name='db_siteid',db_value=".pwEscape($db_siteid,false));

	$db_siteownerid = generatestr(32);
	$db->update("REPLACE INTO pw_config SET db_name='db_siteownerid',db_value=".pwEscape($db_siteownerid,false));

	$db_sitehash = '10'.SitStrCode(md5($db_siteid.$db_siteownerid),md5($db_siteownerid.$db_siteid));
	$db->update("REPLACE INTO pw_config SET db_name='db_sitehash',db_value=".pwEscape($db_sitehash,false));
	$updatecache = true;
}
$updatecache && updatecache_c();

if (in_array($action,array('login','register'))) {
	$partner = md5($db_siteid.$db_siteownerid);
	$verify = md5($db_sitehash.$partner.$action);
}

!$action && $action='info';
${'cls_'.$action} = 'class="two"';
if ($action=='modify') {
	if ($_POST['step']==2) {
		InitGP(array('siteid','siteownerid','sitehash'));
		if (!$siteid || !$siteownerid || !$sitehash) {
			adminmsg('adcode_error');
		}
		if ($sitehash != '10'.SitStrCode(md5($siteid.$siteownerid),md5($siteownerid.$siteid))) {
			adminmsg('adcode_error');
		}
		$db->update("REPLACE INTO pw_config SET db_name='db_siteid',db_value=".pwEscape($siteid));
		$db->update("REPLACE INTO pw_config SET db_name='db_siteownerid',db_value=".pwEscape($siteownerid));
		$db->update("REPLACE INTO pw_config SET db_name='db_sitehash',db_value=".pwEscape($sitehash));
		updatecache_c();
		adminmsg('operate_success');
	} elseif ($_GET['step']==3) {
		$siteid = generatestr(32);
		$siteownerid = generatestr(32);
		$sitehash = '10'.SitStrCode(md5($siteid.$siteownerid),md5($siteownerid.$siteid));
	}
} elseif ($action == 'login') {
	ObHeader("http://www.phpwind.com/pw_app.php?action=$action&sitehash=$db_sitehash&verify=$verify&host=".rawurlencode($pwServer['HTTP_HOST']));
} elseif ($action == 'register') {
	InitGP('app');
	require_once(R_P.'require/posthost.php');
	if ($app == 1) {
		$data = PostHost("http://www.phpwind.com/pw_app.php?","action=$action&sitehash=$db_sitehash&verify=$verify&","POST");
	} else {
		$data = PostHost("http://www.phpwind.com/pw_app.php?","action=$action&sitehash=$db_sitehash&siteid=$db_siteid&siteownerid=$db_siteownerid&host=".rawurlencode($pwServer['HTTP_HOST'])."&verify=$verify&","POST");
	}
	$backdata = substr($data,strpos($data,'$backdata=')+10);
	if (strpos($backdata,'Not Found') !== false) {
		adminmsg('The Interface is not exist');
	}
	if ($backdata && is_numeric($backdata)) {
		$db->pw_update(
			"SELECT db_name FROM pw_config WHERE db_name='db_appuid'",
			"UPDATE pw_config SET db_value=".pwEscape($backdata)."WHERE db_name='db_appuid'",
			"INSERT INTO pw_config SET db_name='db_appuid',db_value=".pwEscape($backdata)
		);
		updatecache_c();
		$verify = md5($db_sitehash.$partner.'login');
		ObHeader("http://www.phpwind.com/pw_app.php?action=login&sitehash=$db_sitehash&verify=$verify&host=".rawurlencode($pwServer['HTTP_HOST']));
	} else {
		adminmsg($backdata);
	}
} elseif (!in_array($action,array('key','info','reg'))) {
	ObHeader($basename);
}
include PrintEot('code');exit;
function generatestr($len) {
	mt_srand((double)microtime()*1000000);
    $keychars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWYXZ";
	$maxlen = strlen($keychars)-1;
	$str = '';
	for ($i=0;$i<$len;$i++){
		$str .= $keychars[mt_rand(0,$maxlen)];
	}
	return substr(md5($str.microtime().$GLOBALS['HTTP_HOST'].$GLOBALS['pwServer']["HTTP_USER_AGENT"].$GLOBALS['db_hash']),0,$len);
}
function SitStrCode($string,$key,$action='ENCODE'){
	$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 : str_replace('=','',base64_encode($code));
	return $code;
}
?>