<?php
defined('IN_YUNYECMSAdmin') or exit('No permission.');
core::load_admin_class('admin');
class config extends YUNYE_admin
{
	function __construct()
	{
		parent::__construct();
		$this->db = core::load_model('config_model');
	}
	//加载首页
	public function init()
	{
		$parnav = '<li class=\"breadcrumb-item\"><a href=\"' . url_admin('save', "config") . '\" target=\"maincontent\">系统</a></li><li class=\"breadcrumb-item\"><a href=\"' . url_admin('save', 'config') . '\" target=\"maincontent\">系统配置信息</a></li>';
		if (!getroot('users', 'logs')) {
			messagebox(Lan('no_permission'), 'back', "warn");
		}
		require tpl_adm('config');
	}

	public function save()
	{
		if (!getroot('system', 'setting')) {
			messagebox(Lan('no_permission_sysconfig'), 'back', "warn");
		}
		$yyact = "edit";
		$parnav = '<li class=\"breadcrumb-item\"><a href=\"' . url_admin('save', "config") . '\" target=\"maincontent\">系统</a></li><li class=\"breadcrumb-item\"><a href=\"' . url_admin('save', 'config') . '\" target=\"maincontent\">系统配置信息</a></li>';
		$sqlcnt = "select count(*) from `#yunyecms_config`";
		$checkcnt = $this->db->GetCount($sqlcnt);
		if ($checkcnt > 0) {
			$strsql = "select * from `#yunyecms_config` order by id desc limit 0,1";
			$curconfig = $this->db->find($strsql);
			$curconfig['logo'] = dopic($curconfig['logo']);
			$curconfig['icon'] = dopic($curconfig['icon']);
			$curconfig['qrcode'] = dopic($curconfig['qrcode']);
			$curconfig['footlogo'] = dopic($curconfig['footlogo']);
			$curconfig['logo_mem'] = dopic($curconfig['logo_mem']);
			$curconfig['logo_mem_mini'] = dopic($curconfig['logo_mem_mini']);
			$curconfig['logo_mobile'] = dopic($curconfig['logo_mobile']);
			$curconfig['copyright'] = doimgs($curconfig['copyright']);
			if (!empty($curconfig)) {
				$curconfig['mobilecfg'] = unserialize($curconfig['mobilecfg']);
				$curconfig['content'] = unserialize($curconfig['content']);
				$curconfig['email'] = unserialize($curconfig['email']);
				$curconfig['sms'] = unserialize($curconfig['sms']);
				$curconfig['weixin'] = unserialize($curconfig['weixin']);
				$curconfig['alipay'] = unserialize($curconfig['alipay']);
				$curconfig['upload'] = unserialize($curconfig['upload']);
				$rs = $curconfig;
			} else {
				messagebox("配置信息错误！");
			}
		}

		if (isset($_POST["yyact"])) {
			$_POST = ustripslashes($_POST);
			$content = TrimArray($_POST['content']);
			$mobilecfg = TrimArray($_POST['mobilecfg']);
			$email = TrimArray($_POST['email']);
			$sms = TrimArray($_POST['sms']);
			$weixin = TrimArray($_POST['weixin']);
			$alipay = TrimArray($_POST['alipay']);
			$upload = TrimArray($_POST['upload']);
			if (!empty($email)) {
				if (!empty($email["pwd"])) {
					$email["pwd"] = yunyecms_strencode($email["pwd"]);
				} else {
					$strsql = "select * from `#yunyecms_config` order by id desc limit 0,1";
					$curconfig = $this->db->find($strsql);
					$curconfig['email'] = unserialize($curconfig['email']);
					$email["pwd"] = $curconfig['email']['pwd'];
				}
				$email = serialize($email);
			} else {
				$email = '';
			}
			if (!empty($content)) {
				$content = serialize($content);
			} else {
				$content = '';
			}
			if (!empty($mobilecfg)) {
				$mobilecfg = serialize($mobilecfg);
			} else {
				$mobilecfg = '';
			}
			if (!empty($sms)) {
				$sms = serialize($sms);
			} else {
				$sms = '';
			}
			if (!empty($weixin)) {
				$weixin = serialize($weixin);
			} else {
				$weixin = '';
			}
			if (!empty($alipay)) {
				$alipay = serialize($alipay);
			} else {
				$alipay = '';
			}
			if (!empty($upload)) {
				$upload = serialize($upload);
			} else {
				$upload = '';
			}
			$data["isclose"] = usafestr(trim($_POST["isclose"]));
			$data["iskefu"] = usafestr(trim($_POST["iskefu"]));
			$data["ismall"] = usafestr(trim($_POST["ismall"]));
			$data["isquicknav"] = usafestr(trim($_POST["isquicknav"]));
			$data["ismailsub"] = usafestr(trim($_POST["ismailsub"]));
			$data["ismailreg"] = usafestr(trim($_POST["ismailreg"]));
			$data["isfeedbackmail"] = usafestr(trim($_POST["isfeedbackmail"]));
			$data["ishtml"] = usafestr(trim($_POST["ishtml"]));
			$data["verifymode"] = usafestr(trim($_POST["verifymode"]));
			$data["tel"] = usafestr(trim($_POST["tel"]));
			$data["mobile"] = usafestr(trim($_POST["mobile"]));
			$data["mail"] = usafestr(trim($_POST["mail"]));
			$data["qq"] = usafestr(trim($_POST["qq"]));
			$data["weibo"] = usafestr(trim($_POST["weibo"]));
			$data["address"] = usafestr(trim($_POST["address"]));
			$data["zipcode"] = usafestr(trim($_POST["zipcode"]));
			$data["copyright"] = !empty($_POST["copyright"]) ? revimgs(trim($_POST["copyright"])) : "";
			$data["user_agreement"] = $_POST["user_agreement"];
			$data["grantcode"] = usafestr(trim($_POST["grantcode"]));
			$data["logo"] = usafepic(revpic(trim($_POST["logo"])));
			$data["icon"] = usafepic(revpic(trim($_POST["icon"])));
			$data["footlogo"] = revpic(trim($_POST["footlogo"]));
			$data["logo_mem"] = revpic(trim($_POST["logo_mem"]));
			$data["logo_mem_mini"] = revpic(trim($_POST["logo_mem_mini"]));
			$data["logo_mobile"] = revpic(trim($_POST["logo_mobile"]));
			$data["qrcode"] = usafepic(revpic(trim($_POST["qrcode"])));
			$data["sitename"] = usafestr(trim($_POST["sitename"]));
			$data["seotitle"] = usafestr(trim($_POST["seotitle"]));
			$data["seokey"] = usafestr(trim($_POST["seokey"]));
			$data["seodesc"] = usafestr(trim($_POST["seodesc"]));
			$data["email"] = $email;
			$data["content"] = $content;
			$data["mobilecfg"] = $mobilecfg;
			$data["sms"] = $sms;
			$data["weixin"] = $weixin;
			$data["alipay"] = $alipay;
			$data["upload"] = $upload;
			if (is_numeric($_POST["allow_reg"])) $data["allow_reg"] = intval(usafestr(trim($_POST["allow_reg"])));
			//if(is_numeric($_POST["url_route"]))$data["url_route"]=intval(usafestr(trim($_POST["url_route"])));
			if (is_numeric($_POST["url_model"])) $data["url_model"] = intval(usafestr(trim($_POST["url_model"])));
			//同步更新默认语言版配置信息
			$langcfg = getdefaultlang();
			$syndata["sitename"] = $data["sitename"];
			$syndata["seotitle"] = $data["seotitle"];
			$syndata["seokey"] = $data["seokey"];
			$syndata["seodesc"] = $data["seodesc"];
			$syndata["tel"] = $data["tel"];
			$syndata["mobile"] = $data["mobile"];
			$syndata["mail"] = $data["mail"];
			$syndata["qq"] = $data["qq"];
			$syndata["address"] = $data["address"];
			$syndata["zipcode"] = $data["zipcode"];
			$syndata["copyright"] = $data["copyright"];
			$syndata["logo"] = $data["logo"];
			$syndata["footlogo"] = $data["footlogo"];
			$syndata["logo_mem"] = $data["logo_mem"];
			$syndata["logo_mem_mini"] = $data["logo_mem_mini"];
			$syndata["logo_mobile"] = $data["logo_mobile"];
			//$syndata["icon"]=$data["icon"];
			$syndata["qrcode"] = $data["qrcode"];
			if ($_POST["yyact"] == "edit") {
				$id = $_POST["id"];
				if (!$this->check_exist($id)) {
					messagebox("配置信息错误！", url_admin('init'), "warn");
				}
				$retres = $this->db->update($data, "id={$id}", "config");
				$retlang = $this->db->update($syndata, array('id' => $langcfg['id']), "lang");
				if ($retres && $retlang) {
					$doing = "更新系统配置";
					$yyact = "UpdateConfig";
					insert_admlogs($doing, $yyact);
					messagebox("系统配置信息更新成功！", url_admin('save', 'config'), "success");
				} else {
					messagebox("系统配置信息更新失败！", url_admin('save', 'config'), "error");
				}
			}
		}
		require tpl_adm('config');
	}


	public function savetop()
	{
		if (!getroot('system', 'setting')) {
			messagebox(Lan('no_permission_sysconfig'), 'back', "warn");
		}
		if (empty($_POST['isclose'])) {
			$isclose = 2;
		} else {
			$isclose = 1;
		}
		if (empty($_POST['iskefu'])) {
			$iskefu = 2;
		} else {
			$iskefu = 1;
		}
		if (empty($_POST['isfeedbackmail'])) {
			$isfeedbackmail = 2;
		} else {
			$isfeedbackmail = 1;
		}
		if (empty($_POST['ismailreg'])) {
			$ismailreg = 2;
		} else {
			$ismailreg = 1;
		}
		if (empty($_POST['ismailsub'])) {
			$ismailsub = 2;
		} else {
			$ismailsub = 1;
		}
		if (empty($_POST['ishtml'])) {
			$ishtml = 2;
		} else {
			$ishtml = 1;
		}
		if (empty($_POST['ismall'])) {
			$ismall = 2;
		} else {
			$ismall = 1;
		}
		if (empty($_POST['verifymode'])) {
			$verifymode = 2;
		} else {
			$verifymode = 1;
		}
		$data["isclose"] = $isclose;
		$data["iskefu"] = $iskefu;
		$data["ismall"] = $ismall;
		$data["ismailsub"] = $ismailsub;
		$data["ismailreg"] = $ismailreg;
		$data["isfeedbackmail"] = $isfeedbackmail;
		$data["ishtml"] = $ishtml;
		$data["verifymode"] = $verifymode;
		if (empty($_POST['upload']['isthumb'])) {
			$_POST['upload']['isthumb'] = 0;
		} else {
			$_POST['upload']['isthumb'] = 1;
		}
		if (!empty($upload)) {
			$upload = serialize($upload);
		} else {
			$upload = '';
		}
		$upload = TrimArray($_POST['upload']);
		$upload = serialize($_POST['upload']);
		$data["upload"] = $upload;
		if ($_POST["yyact"] == "edit") {
			$id = $_POST["id"];
			if (!$this->check_exist($id)) {
				messagebox("配置信息错误！", url_admin('init'), "warn");
			}
			$retres = $this->db->update($data, "id={$id}", "config");
			if ($retres) {
				$doing = "更新系统配置";
				$yyact = "UpdateConfig";
				insert_admlogs($doing, $yyact);
				messagebox("系统配置信息更新成功！", url_admin('save', 'config'), "success");
			} else {
				messagebox("系统配置信息更新失败！", url_admin('save', 'config'), "error");
			}
		}
	}

	public function check()
	{
		parent::infocheck("lang", $this->db);
	}

	public function nocheck()
	{
		parent::infonocheck("lang", $this->db);
	}

	private function check_exist($id)
	{
		$id = trim($id);
		if (empty($id)) {
			return false;
		} else {
			if (!is_numeric($id)) {
				return false;
			}
			if ($this->db->find("select count(*) as cnt from `#yunyecms_config` where `id`= {$id}")) {
				return true;
			}
		}
	}
}
