<?php
require_once '../include/adminfunction.php';
checkadmin();
function updatesettext($text){
	if($text=="")
		return "";
	$text = htmlspecialchars($text,ENT_QUOTES);
    return $text;
}
$appkey_arr = explode(",",$ArrayAppKey);
$appsecret_arr = explode(",",$ArrayAppSecret);


$action=var_request("action","");
$errmsg = "";
if($action=="update" || $action=="add"){

	
	$pagetext = '$rootroad = "'.$rootroad.'";   //网站所在目录'."\n";
	$pagetext = $pagetext.'$weijingtai="'.$weijingtai.'";  //开启伪静态'."\n";
	$pagetext = $pagetext.'$usernick="'.$usernick.'";  //淘宝昵称'."\n";
	$pagetext = $pagetext.'$userpid="'.$userpid.'";  //阿里妈妈PID'."\n";
	$pagetext = $pagetext.'$userpiddp="'.$userpiddp.'";  //阿里妈妈小PID'."\n";
	$pagetext = $pagetext.'$sitetitle="'.$sitetitle.'";  //网站TITLE'."\n";
	$pagetext = $pagetext.'$sitetitleurl="'.$sitetitleurl.'";  //网站域名'."\n";
	$pagetext = $pagetext.'$sitekey="'.$sitekey.'";  //SEO关键字'."\n";
	$pagetext = $pagetext.'$sitedesc="'.$sitedesc.'";  //SEO网页描述'."\n";
	$pagetext = $pagetext.'$beianxinxi="'.$beianxinxi.'";  //备案信息号'."\n";
	$pagetext = $pagetext.'$webcontact="'.$webcontact.'";  //站长联系方式'."\n";
	$pagetext = $pagetext.'$tongjidaima="'.$tongjidaima.'";  //统计脚本'."\n";
	$pagetext = $pagetext.'$setCacheTime="'.$setCacheTime.'";  //缓存时间'."\n";
	
	//原有的字段
	$pagetext = $pagetext.'$manage_adminname="'.$manage_adminname.'";  //管理员账号'."\n";
	$pagetext = $pagetext.'$manage_adminpass="'.$manage_adminpass.'";  //管理员密码'."\n";
	$pagetext = $pagetext.'$templatefolder="'.$templatefolder.'";  //templatefolder'."\n";
	
	//计算APPKEY的变动
	$ctxtappkey = trim(updatesettext(var_request("txtappkey","")));
	$ctxtappsecre = trim(updatesettext(var_request("txtappsecre","")));
	$cappno = var_request("appno","0");
	$cbutton = var_request("button","0");
	
	if($ctxtappkey=="" || $ctxtappsecre==""){
		echo("<script language=\"JavaScript\">alert(\"输入的内容不全，请重新输入！！\");history.go(-1);</script>");
		exit;
	}
	
	if($action=="add"){
		if($ArrayAppKey==""){
			$ArrayAppKey = $ctxtappkey;
			$ArrayAppSecret = $ctxtappsecre;
		}else{
			$ArrayAppKey = $ArrayAppKey.",".$ctxtappkey;
			$ArrayAppSecret = $ArrayAppSecret.",".$ctxtappsecre;
		}
	}
	
	if($action=="update" && $cbutton=="修改"){
		$appkey_arr[$cappno] = $ctxtappkey;
		$appsecret_arr[$cappno] = $ctxtappsecre;
		$ArrayAppKey = "";
		$ArrayAppSecret = "";
		for($i=0;$i<Count($appkey_arr);$i++){
			if($appkey_arr[$i]!=""){
				$ArrayAppKey=$ArrayAppKey.$appkey_arr[$i].",";
				$ArrayAppSecret=$ArrayAppSecret.$appsecret_arr[$i].",";
			}
		}
	}
	
	if($action=="update" && $cbutton=="删除"){
		$appkey_arr[$cappno] = "";
		$appsecret_arr[$cappno] = "";
		$ArrayAppKey = "";
		$ArrayAppSecret = "";
		for($i=0;$i<Count($appkey_arr);$i++){
			if($appkey_arr[$i]!=""){
				$ArrayAppKey=$ArrayAppKey.$appkey_arr[$i].",";
				$ArrayAppSecret=$ArrayAppSecret.$appsecret_arr[$i].",";
			}
		}
	}
	
	if(strlen($ArrayAppKey)>1 && substr($ArrayAppKey,strlen($ArrayAppKey)-1,1)==","){
		$ArrayAppKey = substr($ArrayAppKey,0,strlen($ArrayAppKey)-1);
		$ArrayAppSecret = substr($ArrayAppSecret,0,strlen($ArrayAppSecret)-1);
	}
	
	
	
	//配置的字段
	
	$pagetext = $pagetext.'$ArrayAppKey="'.$ArrayAppKey.'";  //AppKey'."\n";
	$pagetext = $pagetext.'$ArrayAppSecret="'.$ArrayAppSecret.'";  //AppSecret'."\n";
	
	$pagetext = $pagetext.'define(\'ROOT\',dirname(__FILE__)."/");'."\n";
	//写入到缓存中.
	file_put_contents ( "../data/configdata.php", "<?php \n".$pagetext."\n?>");
	
	redirect_to("manageAPP.php?add=ok");
	exit;
}





?>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>315爱购网</title>
	<link rel='stylesheet' href='images/order.css'>
    <style type="text/css">
<!--
.STYLE2 {color: #FF0000}
-->
    </style>
</head>

<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
<center>
<table width="96%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td height="10" align=center></td>
  </tr>
</table>

<?php if(var_request("add","")=="ok"){?>
<table width="96%" border="0" cellpadding="3" cellspacing="1" bgcolor="#cccccc">
  <tr>
    <td height="30" colspan="2" align="center" bgcolor="#FFFFCC"><a href="manageconfig.php"><span class="STYLE2">修改成功！</span></a> </td>
  </tr>
</table>
<?php }?>
<?php 


if(isset($appkey_arr) && Count($appkey_arr)>0 && Count($appkey_arr)==Count($appsecret_arr)) {
	for($i=0;$i<Count($appkey_arr);$i++){
		if($appkey_arr[$i]!=""){
	?>
<form name="form1" method="post" action="manageAPP.php?action=update" style=" margin:2px;">
<table width="96%" border="0" cellpadding="3" cellspacing="1" bgcolor="#cccccc"><tr bgcolor="#c8ddf4">
    <td width="15%" align="right" bgcolor="#FFFFFF" class="paragraph" title="20039669">AppKey<?php echo $i?></td>
    <td width="15%" bgcolor="#ffffff"><input type="text" name="txtappkey" id="txtrootroad" value="<?php echo $appkey_arr[$i] ?>"></td>
    <td width="9%" bgcolor="#ffffff"><div align="right">AppSecre<?php echo $i?></div></td>
    <td width="21%" bgcolor="#ffffff"><input name="txtappsecre" type="text" id="txtrootroad2" value="<?php echo $appsecret_arr[$i] ?>" size="35"></td>
    <td width="40%" bgcolor="#ffffff"><label>
      <input type="submit" name="button" id="button" value="修改">
      <input type="submit" name="button" id="button2" value="删除">
      <input type="hidden" name="appno" value="<?php echo $i?>">
    </label></td>
</tr>
</table>
</form>
<?php 
		}
	}
}
?>

<form name="form1" method="post" action="manageAPP.php?action=add">
<table width="96%" border="0" cellpadding="3" cellspacing="1" bgcolor="#cccccc"><tr bgcolor="#c8ddf4">
    <td width="15%" align="right" bgcolor="#FFFFFF" class="paragraph" title="20039669">新AppKey</td>
    <td width="15%" bgcolor="#ffffff"><input type="text" name="txtappkey" value=""></td>
    <td width="9%" bgcolor="#ffffff"><div align="right">新AppSecre</div></td>
    <td width="21%" bgcolor="#ffffff"><input name="txtappsecre" type="text" value="" size="35"></td>
    <td width="40%" bgcolor="#ffffff"><label>
      <input type="submit" name="button" id="button" value="添加">
    </label></td>
</tr>
</table>
</form>


<table width="96%" height="91" border="0" cellpadding="0" cellspacing="0">
  <tr> 
    <td height="1" bgcolor="#b2b2b2"></td>
  </tr>
  <tr>
    <td height="90" align=center><span class="STYLE2"><br>
      <br>如何申请App Key 不会申请的 <a href="http://www.cnmysoft.com/help/19.html" target="_blank">点击这里</a> 查看申请步骤</span><br> 
<p>&nbsp;    </p>      </td>
  </tr>
</table>
</center>
</body>
</html>
