<?php
define('THIS_SCRIPT', 'cnzz_wss');
include_once 'global.php';
define("DV_CNZZ_RSA_PUBLIC_KEY","YTozOntpOjA7czoxNjoiFe/zmJRcSOEVU0H5Q4kplCI7aToxO3M6MzoiAQABIjtpOjI7czo2OiJwdWJsaWMiO30=");
head();
checklogin();

//插件管理权限=59
if ( strpos($sysinfo['flag'],'59,') === false ){
	show_msg('WSS统计管理','您没有本页面管理权限',0,'','','','');
	exit;
}

PageLoad();

function Pageload(){
	global $db,$dv,$lang,$action,$reaction,$forum_setting;
	switch ($action) {
		case "savereg":
			$wss=dv_base64_decode($_GET['wss']);
			list($site_id,$password)=unserialize($wss);

			if($_GET['encode']=='rsa'){
				include_once INC_PATH.'rsa.php';

				$wss=rsa_decrypt($wss,DV_CNZZ_RSA_PUBLIC_KEY);
			}
			include_once INC_PATH.'BoardCache.class.php';
			$query=$db->query("Select forum_setting From {$dv}setup Limit 1");
			$fs = $db->fetch_array($query);
			$db->free_result($query);
			$iforum_setting = explode('|||', $fs['forum_setting']);
			$tmpforum_setting=explode(",",$iforum_setting[1]);
			$forum_setting[123]=$tmpforum_setting[123]=str_replace(array(",","|"),array("",""),$site_id);
			$forum_setting[124]=$tmpforum_setting[124]=str_replace(array(",","|"),array("",""),$password);
			$forum_setting[125]=$tmpforum_setting[125]=1;
			$iforum_setting[1]=join(",",$tmpforum_setting);
			$sforum_setting=$db->escape_string(join("|||",$iforum_setting));
			$db->query("update {$dv}setup set forum_setting='$sforum_setting'");
			$mycache =& new BoardCache($db);
			$mycache->createForumSetting();
			break;

		case "saveconfig":
			$wss_enabled=intval($_POST['wss_enabled']);
			
			if($wss_enabled!=$forum_setting[123]){
				include_once INC_PATH.'BoardCache.class.php';
				$query=$db->query("Select forum_setting From {$dv}setup Limit 1");
				$fs = $db->fetch_array($query);
				$db->free_result($query);
				$iforum_setting = explode('|||', $fs['forum_setting']);
				$tmpforum_setting=explode(",",$iforum_setting[1]);
				$tmpforum_setting[123]=$forum_setting[123];
				$tmpforum_setting[124]=$forum_setting[124];
				$forum_setting[125]=$tmpforum_setting[125]=$wss_enabled;
				$iforum_setting[1]=join(",",$tmpforum_setting);
				$sforum_setting=$db->escape_string(join("|||",$iforum_setting));
				$db->query("update {$dv}setup set forum_setting='$sforum_setting'");
				$mycache =& new BoardCache($db);
				$mycache->createForumSetting();
			}
			break;
		case "register":

			include_once INC_PATH.'BoardCache.class.php';
			$query=$db->query("Select forum_setting From {$dv}setup Limit 1");
			$fs = $db->fetch_array($query);
			$db->free_result($query);
			$iforum_setting = explode('|||', $fs['forum_setting']);
			$tmpforum_setting=explode(",",$iforum_setting[1]);
			$tmpforum_setting[123]=$forum_setting[123]='';
			$tmpforum_setting[124]=$forum_setting[124]='';
			$forum_setting[125]=$tmpforum_setting[125]='';
			$iforum_setting[1]=join(",",$tmpforum_setting);
			$sforum_setting=$db->escape_string(join("|||",$iforum_setting));
			$db->query("update {$dv}setup set forum_setting='$sforum_setting'");
			$mycache =& new BoardCache($db);
			$mycache->createForumSetting();
			break;
		default:

	}

	if($forum_setting[123]==''){
		template_apply();
	}else{
		template_default();
	}
}

function dv_base64_encode($plain_str){
	return str_replace(array('=','+'),array(',','_'),base64_encode($plain_str));
}
function dv_base64_decode($encode_str){
	return str_replace(array(',','_'),array('=','+'),base64_decode($encode_str));
}
footer();


function template_apply(){
?>

<table width="100%"  border="0" cellspacing="0" cellpadding="0" class="tableborder" style="h">
  <tr>
    <td colspan="2" class="tdtit">
       正在开通论坛统计,请稍候......
    </td>
  </tr>
</table>
<script>
var wss=encode='';
var wssRet=0;
getwss();
function getwss(){
	url="http://p.dvbbs.net/api/getwsscode.php?domain=<?=$_SERVER['HTTP_HOST']?>&encode=";
	var ojbscript = document.createElement("script");
	ojbscript.src =encodeURI(url);
	ojbscript.type = "text/javascript";
	ojbscript.language = "javascript";
	document.getElementsByTagName("head")[0].appendChild(ojbscript);

	if(document.all){
		ojbscript.onreadystatechange = function(){//IE用
			var state = ojbscript.readyState;
			if (state == "loaded" || state == "interactive" || state == "complete") {
				callback();
			}
		};
	} else {
		ojbscript.onload = function() {//FF用
			callback();
		};
	}
}

function callback(){
	$errmsg='';
	switch (wssRet){
		case -1:
		$errmsg='Key编码错误';
		break;
		case -2:
		$errmsg='您的域名长度超过最长限制:64';
		break;
		case -3:
		$errmsg='您的域名包含不支持的字符';
		break;
		case -4:
		$errmsg='CNZZ服务器内部错误(数据库错误)';
		break;
		case -5:
		$errmsg='每个IP注册的域名数最多不能超过10';
		break;
		case -6:
		$errmsg='连接统计服务器失败';
		break;
		default:
		$errmsg='';
		this.location='?action=savereg&encode='+encode+'&wss='+wss;
	}
	if($errmsg!=''){
		alert($errmsg);
		$errmsg='';
		return false;
	}

}
</script>
<? } ?>

<?
function template_default(){
	global $forum_setting;
?>

<table width="95%" border="0" cellspacing="1" cellpadding="0"  align="center" class="tableborder">
<tr> 
<td height="23" align="left" colspan="6" class="tit1">■</td>
</tr>
<tr align="left">
<td height="23" class="forumrow" style="text-align:center;width:15%">
<input type="submit" name="Submit" value="注意事项!" style="color: #FF0000;width:80px;height:50px;border:1px outset;">
</td>
<td height="23" class="forumrow" style="LINE-HEIGHT: 140%">
<li>此功能接口由CNZZ提供,为您的网站提供统计服务</li>
<li>您可以点击<a href="http://wss.cnzz.com/user/companion/cms_login.php?site_id=<?=$forum_setting[123]?>&password=<?=$forum_setting[124]?>" target="_blank">查看统计</a>查看您的网站统计信息</li>
</td>
</tr>
</table>

<table border="0" cellspacing="0" cellpadding="0"  align="center" class="tableborder">
<tr align="center">
<td height="25" align="center" bgcolor="#C9DEFA"  width="20%"><font color="red">WSS统计服务</font></a></td>
<td height="25" align="center" bgcolor="#F1F3F5" width="80%" ></td>
</tr>
</table>

<table width="100%" border="0" cellpadding="0" cellspacing="0" bordercolor="#C4D8ED" class="tableborder">
  <form name="form1" id="frmsetting" method="POST" action="?">
  <input type="hidden" name="action" id="action" value="saveconfig">
  <tr>
    <td colspan="3" class="tdtit">WSS统计服务</td>
  </tr>
  <tr class="forumrow">
   <td width="30%" class="forumrow"> <b>是否启用WSS统计</b></td>
   <td width="20%" class="forumrow">
    <input type="radio" name="wss_enabled" value="1" <?echo ($forum_setting[125]?"checked":"")?>>启用&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="wss_enabled" value="0" <?echo ($forum_setting[125]?"":"checked")?>>关闭&nbsp;
   </td>
 <td width="50%" class="forumrow" align="left"></td>
  </tr>
 
  <tr class="forumrow1">
   <td width="30%" class="forumrow1"> <b>站点ID</b></td>
   <td width="20%" class="forumrow1">
   <input type="text"  value="<?=$forum_setting[123]?>" readonly  />
   </td>
 <td width="50%" class="forumrow1" align="left"></td>
  </tr>
  
   
  <tr class="forumrow">
   <td width="30%" class="forumrow"> <b>登陆密码</b></td>
   <td width="20%" class="forumrow">
   <input type="text"  value="<?=$forum_setting[124]?>" readonly  />
   </td>
 <td width="50%" class="forumrow" align="left"></td>
  </tr>
  
  <tr class="forumrow1">
   <td width="30%" class="forumrow1"> <b>统计代码</b></td>
   <td width="20%" class="forumrow1">
   <textarea id="wsscode" rows="6" cols="80" readonly onclick="copyWssCode()" onmouseover="this.select();"><script src='http://pw.cnzz.com/c.php?id=<?=$forum_setting[123]?>&l=2' language='JavaScript' charset='gb2312'></script></textarea>
   </td>
 <td width="50%" class="forumrow" align="left"></td>
  </tr>

<tr><td colspan="2" class="forumrow1" style="text-align:center">
<input type="button" value="复制代码" class="button" onclick="copyWssCode()">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" value="查看统计" class="button" onclick="window.open('http://wss.cnzz.com/user/companion/cms_login.php?site_id=<?=$forum_setting[123]?>&password=<?=$forum_setting[124]?>','')">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="Submit" value="保存设置" class="button" onclick="document.getElementById('action').value='saveconfig'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="Submit" value="重新注册" class="button" onclick="document.getElementById('action').value='register'">
</td>
<td>&nbsp;</td>
</tr>
</form>
</table>

<script language="javascript">
function copyWssCode(){
	var objTextArea=document.getElementById("wsscode");
	objTextArea.select();
	window.clipboardData.setData("text",objTextArea.value);
	alert("代码已经复制到剪贴板!");
}
</script>
<? } ?>