<?php
/**
* FILE_NAME : setsystem.php
*
* @copyright Copyright (c) www.shuangyulin.com
* @author 双鱼林设计
* @package 系统信息设置
* @subpackage
* @version QQ:287307421或254540457
*/

define('IN_WEB', TRUE);

require_once( str_replace('\\', '/', dirname(__FILE__)). '/include/global.php');

/* act操作项的初始化 */
if (empty($_REQUEST['act'])) {
    $_REQUEST['act'] = 'show';
} else {
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

/*------------------------------------------------------ */
//-- 显示信息
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'show')
{
    $sql = $query = '';
    $sql = "SELECT * FROM {$dbprefix}settings WHERE variable!=''";
    $query = $DB->query($sql);
    while ($setting = $DB->fetch_array($query))
    {
    	$settings[$setting['variable']] = htmlspecialchars($setting['value']);
    }
    
    $smarty->assign("sitename",$settings['sitename']);
    $smarty->assign("format_type",$settings['format_type']);
//    $smarty->assign("siteurl",$settings['siteurl']);
    $smarty->assign("allow_guest_down",$settings['allow_guest_down']);
    $smarty->assign("article_num",$settings['article_num']);
    $smarty->assign("article_title_len",$settings['article_title_len']);
//	$smarty->assign("settings",$settings);
    $smarty->assign("css_root",$css_root);
    $smarty->display("setsystem.html");
}

/*------------------------------------------------------ */
//-- 更新信息
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'setok')
{
	$sql = $query = '';
    $sql = "SELECT * FROM {$dbprefix}settings WHERE variable!=''";
    $query = $DB->query($sql);
    while ($setting = $DB->fetch_array($query)) {
    	$old_settings[$setting['variable']] = htmlspecialchars($setting['value']);
    }
    
	/* 取值 */
	$settings = $_REQUEST['settings'];
	
	$sitename = $settings['sitename'];
	$format_type = isset($settings['format_type']) ? $settings['format_type'] : '';
//    $enable_gzip = $settings['enable_gzip'];
    $allow_guest_down = $settings['allow_guest_down'];
    $article_num = $settings['article_num'];
    $article_title_len = $settings['article_title_len'];
    
    $edit = 0;
    
    /*--  更新网站名  --*/
    if ($old_settings['sitename'] != $sitename) {
    	$sql = "UPDATE {$dbprefix}settings SET value = '".$sitename."' WHERE variable = 'sitename'";
    	$query = $DB->query($sql);
    	
    	$edit = 1;
    	if (!$query) {
    		showmsg("网站名更新失败","setsystem.php?act=show");
    	}
    }
    
    /*--  更新时间显示格式  --*/
    if ($old_settings['format_type'] != $format_type) {
    	$sql = "UPDATE {$dbprefix}settings SET value = '".$format_type."' WHERE variable = 'format_type'";
    	$query = $DB->query($sql);
    	
    	$edit = 1;
    	if (!$query) {
    		showmsg("时间格式更新失败","setsystem.php?act=show");
    	}
    }
    
    /*--  更新下载权限  --*/
    if ($old_settings['allow_guest_down'] != $allow_guest_down) {
    	$sql = "UPDATE {$dbprefix}settings SET value = '".$allow_guest_down."' WHERE variable = 'allow_guest_down'";
    	$query = $DB->query($sql);
    	
    	$edit = 1;
    	if (!$query) {
    		showmsg("下载权限更新失败","setsystem.php?act=show");
    	}
    }
    
    /*--  更新单页文章数  --*/
    if ($old_settings['article_num'] != $article_num) {
    	$sql = "UPDATE {$dbprefix}settings SET value = '".$article_num."' WHERE variable = 'article_num'";
    	$query = $DB->query($sql);
    	
    	$edit = 1;
    	if (!$query) {
    		showmsg("单页文章数更新失败","setsystem.php?act=show");
    	}
    }
    
    /*--  更新文章标题字数  --*/
    if ($old_settings['article_title_len'] != $article_title_len) {
    	$sql = "UPDATE {$dbprefix}settings SET value = '".$article_title_len."' WHERE variable = 'article_title_len'";
    	$query = $DB->query($sql);
    	
    	$edit = 1;
    	if (!$query) {
    		showmsg("文章标题字数更新失败","setsystem.php?act=show");
    	}
    }
    
    if ($edit == 1) {
    	/*--  引入缓存文件，更新缓存  --*/
    	require_once(WEB_ROOT.'include/cache.php');
		recache();
		
		showmsg("数据更新成功","setsystem.php?act=show");
    } else {
    	showmsg("数据不需要更新","setsystem.php?act=show");
    }
}
?>