<?php

/**
 * 网站的相关设置
 */
class SettingAction extends CommonAction {

    public function index() {
        $this->getChild();
        $setting = M('setting');
        $settingInfo = $setting->select();
        $set = array();
        foreach ($settingInfo as $val) {
            $set[$val['name']] = $val['data'];
        }
        $this->assign('set', $set);
        $this->display();
    }

    public function edit() {
        $setting = M('setting');
        foreach ($_POST['setting'] as $key => $val) {
            $setting->where("name='" . $key . "'")->delete();
            $data['name'] = $key;
            $data['data'] = $val;
            if (!$setting->add($data)) {
                $this->ajaxReturn('', '数据写入错误！', 0);
            }
            //$setting->where("name='" . $key . "'")->save(array('data' => $val));
        }
        $this->ajaxReturn('', '修改成功!', 1);
    }

    //扩展设置
    public function extend() {
        $this->getChild();
        if (is_file('./data/bbsConfig.php')) {
            $msg = array();
            $msg['bind_bbs'] = C('BIND_BBS') ? 1 : 0;
            $msg['bbs_key'] = C('AUTH_KEY');
            $msg['bbs_url'] = C('BBS_URL');
            $msg['bbs_cookie'] = C('BBS_COOKIE');
			$msg['cookiedomain'] = C('cookiedomain');
			$msg['cookiepath'] = C('cookiepath');
            $this->assign('msg', $msg);
        }
        if (is_file('./data/config.inc.php')) {
            $this->assign('bbsConfig', stripslashes(file_get_contents('./data/config.inc.php')));
        }
        $this->display();
    }

    //配置保存
    public function configSave() {
        $config = "<?php \t\n return array(\t\n 'BIND_BBS'=>" . $_POST['bind_bbs'] .
                ", \t\n 'AUTH_KEY'=>'" . $_POST['auth_key'] .
                "', \t\n 'BBS_URL'=>'" . $_POST['bbs_url'] .
                "', \t\n 'BBS_COOKIE'=>'" . $_POST['bbs_cookie'] .
			    "', \t\n 'cookiedomain'=>'" . $_POST['cookiedomain'] .
			    "', \t\n 'cookiepath'=>'" . $_POST['cookiepath'] .
                "', \t\n); \t\n?>";
        $handle = fopen('./data/bbsConfig.php', 'w');
        fwrite($handle, $config);
        fclose($handle);

        $handle = fopen('./data/config.inc.php', 'w');
        fwrite($handle, stripslashes($_POST['bbs_config']));
        fclose($handle);

        if (!is_writable('data/bbsConfig.php') || !is_writable('data/config.inc.php'))
            $this->ajaxReturn('', 'bbsConfig.php 或 config.inc.php文件不可以写，请修改权限', 0);

        unlink('./admin/Runtime/~app.php');
        unlink('./Home/Runtime/~app.php');
        $this->ajaxReturn('', '', 1);
    }

    //获取网站设置子列表
    private function getChild() {
        $map = array();
        $map['pid'] = C('DOMAIN_SET_ID');
        $menu = M('menu');
        $settingList = $menu->field('name, path')->where($map)->select();
        $this->assign('settingList', $settingList);
    }

}

?>
