<?php
require('common.php');
require(APP_PATH.'module/option.php');

$fileurl = 'option.php';
$tempfile = 'option.html';
$table = $DB->table('options');

$option = $_GET['opt'] ? $_GET['opt'] : $_POST['opt'];
if (!isset($option)) $option = 'basic';
$fileurl .= '?opt='.$option;

if (in_array($option, array('basic', 'misc', 'mail', 'rewrite'))) {
	switch ($option) {
		case 'basic' :
			$pagetitle = '基本设置';
			break;
		case 'misc' :
			$pagetitle = '选项设置';
			break;
		case 'mail' :
			$pagetitle = '邮件设置';
			break;
		case 'rewrite' :
			$pagetitle = '伪静态设置';
			break;
		default :
			$pagetitle = '基本设置';
			break;
	}
	
	$configs = get_options();
	$configs = stripslashes_deep($configs);
	$configs['site_root'] = str_replace('\\', '/', dirname($site_root));
	
	$smarty->assign('pagetitle', $pagetitle);
	$smarty->assign('option', $option);
	$smarty->assign('cfg', $configs);
	unset($configs);
	
	if ($action == 'update') {
		foreach ($_POST['cfg'] as $cname => $cval) {
			if ($cname == 'data_update_cycle' && $cval <= 0) $cval = 3;
			
			$udata = array('option_value' => $cval);
			$uwhere = array('option_name' => $cname);
			$idata = array('option_name' => $cname, 'option_value' => $cval);
			
			$DB->fetch_one("SELECT option_name FROM $table WHERE option_name = '$cname'") ? $DB->update($table, $udata, $uwhere) : $DB->insert($table, $idata);
		}
		update_cache('options');
		
		msgbox('更新系统配置成功！', $fileurl);
	}
}

smarty_output($tempfile);
?>