<?php

define('IN_PHPMPS', true);
require_once dirname(__FILE__) . '/include/common.php';

chkadmin('config');

$_REQUEST['act'] = $_REQUEST['act'] ? trim($_REQUEST['act']) : 'list' ;

switch($_REQUEST['act'])
{
	case 'list':
		$CFG = '';
		$sql = "select setname,value from {$table}config";
		$res = $db->query($sql);
		while($row=$db->fetchRow($res)) {
			$CFG[$row['setname']] = $row['value'];
		}
		$cityall = explode("_", $CFG['citysid']);
		$info_area = area_options3($cityall[0]);
		$tpl_dir = tpl_dir();
		$tpl2_dir = tpl2_dir($CFG[tplname]);
		$html = substr($html,2);
		$html_sms = explode(",", $html);
		include tpl('set_config');
	break;

	case 'set_config':
		if($_POST['token']!=$CFG['crypt']) show('提交错误');
		$_POST['webname']	    = $_POST['webname'] ? trim($_POST['webname']) : '';
		$_POST['weburl']		= trim($_POST['weburl']);
		$_POST['icp']			= trim($_POST['icp']);
		$_POST['tplname']		= trim($_POST['tplname']);
		$_POST['crypt']		    = trim($_POST['crypt']);
		$_POST['post_check']    = intval($_POST['post_check']);
		$_POST['rewrite']		= intval($_POST['rewrite']);
		$_POST['maxpost']		= intval($_POST['maxpost']);
		$_POST['annouce']		= trim($_POST['annouce']);
		$_POST['description']   = trim($_POST['description']);
		$_POST['keywords']      = trim($_POST['keywords']);
		$_POST['copyright']     = trim($_POST['copyright']);
		$_POST['count']         = trim($_POST['count']);
		$_POST['comment_check'] = trim($_POST['comment_check']);
		$_POST['onlyarea']      = trim($_POST['onlyarea']);
		$_POST['del_m_info']    = intval($_POST['del_m_info']);
		$_POST['del_m_comment'] = intval($_POST['del_m_comment']);
		$_POST['pagesize']      = intval($_POST['pagesize']);
		$_POST['postfile']      = trim($_POST['postfile']);
		$_POST['expired_view']  = intval($_POST['expired_view']);
		$_POST['visitor_post']  = intval($_POST['visitor_post']);
		$_POST['visitor_view']  = intval($_POST['visitor_view']);
		$_POST['visitor_comment'] = intval($_POST['visitor_comment']);
		$_POST['closesystem']   = intval($_POST['closesystem']);
		$_POST['close_tips']    = trim($_POST['close_tips']);
		$_POST['sendmailtype']  = trim($_POST['sendmailtype']);
		$_POST['smtphost']      = trim($_POST['smtphost']);
		$_POST['smtpuser']      = trim($_POST['smtpuser']);
		$_POST['smtppass']      = trim($_POST['smtppass']);
		$_POST['smtpport']      = trim($_POST['smtpport']);
		$_POST['info_top_gold'] = intval($_POST['info_top_gold']);
		$_POST['com_pagesize'] = intval($_POST['com_pagesize']);
		$_POST['tpl2name']		    = trim($_POST['tpl2name']);
		$_POST['email']	= trim($_POST['email']);
		$_POST['phone']	= trim($_POST['phone']);
		$_POST['close_register']	= intval($_POST['close_register']);
		$_POST['reg_check']	= intval($_POST['reg_check']);
		$_POST['com_thumbwidth'] = floatval($_POST['com_thumbwidth']);
		$_POST['com_thumbheight']	= floatval($_POST['com_thumbheight']);
		$_POST['cardname']	 = trim($_POST['cardname']);
		$_POST['lejuan']	 = trim($_POST['lejuan']);
		$_POST['diytitle'] = trim($_POST['diytitle']);
		$_POST['page_num'] = intval($_POST['page_num']);
		$_POST['max_zklist'] = intval($_POST['max_zklist']);
		$_POST['creditnum'] = trim($_POST['creditnum']);
		$_POST['smsnum'] = trim($_POST['smsnum']);
		$_POST['usercreditnum'] = trim($_POST['usercreditnum']);
		$_POST['sms_user'] = trim($_POST['sms_user']);
		$_POST['sms_pass'] = trim($_POST['sms_pass']);
		$_POST['com_money_tea'] = trim($_POST['com_money_tea']);
		$_POST['user_money_tea'] = trim($_POST['user_money_tea']);
		$_POST['citysid'] = trim($_POST['citysid']);
		$_POST['del_m_credit'] = intval($_POST['del_m_credit']);
		$_POST['del_m_tx'] = intval($_POST['del_m_tx']);
		$_POST['del_m_juan'] = intval($_POST['del_m_juan']);
		$_POST['cardmoney'] = trim($_POST['cardmoney']);
		$_POST['sms_alidayu_appkey'] = trim($_POST['sms_alidayu_appkey']);
		$_POST['sms_alidayu_secret'] = trim($_POST['sms_alidayu_secret']);
		$_POST['com_jf_sum']	 = intval($_POST['com_jf_sum']);
		$_POST['com_sms_sum']	 = intval($_POST['com_sms_sum']);
		$_POST['com_shop_card']	 = intval($_POST['com_shop_card']);
		$_POST['com_money_off']   = intval($_POST['com_money_off']);
		$_POST['thumbs'] = trim($_POST['thumbs']);
		$_POST['com_thumbs'] = trim($_POST['com_thumbs']);
		$_POST['sms_ming'] = trim($_POST['sms_ming']);
		$_POST['sms_yan_tplid'] = trim($_POST['sms_yan_tplid']);
		$_POST['sms_msg_tplid'] = trim($_POST['sms_msg_tplid']);
		$_POST['wx_appid'] = trim($_POST['wx_appid']);
		$_POST['wx_appsecret'] = trim($_POST['wx_appsecret']);
		$_POST['wx_token'] = trim($_POST['wx_token']);
		$_POST['wx_redirect_uri'] = trim($_POST['wx_redirect_uri']);
		$_POST['com_jf_num']	 = intval($_POST['com_jf_num']);
		
		if($_POST['weburl']=='') {
			$_POST['weburl'] = get_url();
		}
		unset($_POST['act']);
		unset($_POST['submit']);
		foreach($_POST as $key=>$val) {
			$data = $db->getone("SELECT * FROM {$table}config WHERE setname='$key'");
			if($data) {
				$sql = "UPDATE {$table}config SET value = '$val' WHERE setname = '$key' ";
			} else {
				$sql = "INSERT INTO {$table}config (setname,value) VALUES ('$key','$val') ";
			}
			$res = $db->query($sql);
			$res ? $msg.='' : $msg.='1';
		}
		empty($msg) ? $msg = "修改配制成功": $msg = "修改配制失败";
		admin_log("$msg");
		clear_caches('phpcache');
		$link = "set_config.php";
		show($msg, $link);
	break;
}

//--------function--------
function tpl_dir()
{
	$datadir = opendir(PHPMPS_ROOT . "templates");
	while($file = readdir($datadir)) {
		if($file!='.' && $file!='..' && $file!="index.htm"){
			$files[] = $file;
		}
	}
	closedir($datadir);
	return $files;
}

function tpl2_dir($tpls)
{
	$datadir = opendir(PHPMPS_ROOT . "templates/".$tpls);
	while($file = readdir($datadir)) {
		if($file!='.' && $file!='..' && $file!="index.htm" && $file!="images" && $file!="style"){
			$files[] = $file;
		}
	}
	closedir($datadir);
	return $files;
}
?>