<?php

/**
 * 后台系统配置参数模块 Controller
 * ============================================================================
 * zhuna_php 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhaojianjun $  <zwttmac@qq.com>
 * $Id: config.php 138 2012-01-18 09:02:20Z zhaojianjun $
 * http://un.zhuna.cn；
 */
class configAction extends Action
{

    private $tmp_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('config');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('zhuna_admin');
    }

    /**
     * 资讯类别管理 首页
     */
    public function index()
    {
        //TODO  取出所有资讯
        $rs = $this->tmp_model->getConfigList();
        $this->assign('rs', $rs);
        $this->V('config');
    }

    /**
     * 新增/修改系统配置参数
     */
    public function add()
    {
        $sysid = !empty($_GET['sysid']) ? (int) $_GET['sysid'] : 0;
        if ($sysid > 0) {
            $editinfo = $this->tmp_model->getConfigInfo($sysid);
        }
        //取系统配置参数类型radiobutton数组
        $vartype_ary = UtilityConfig::vartype_ary();
        $vartype_ary_radio = Utility::RadioButton($vartype_ary, 'vartype', $editinfo['type'], 'onclick="typechange(this.value);"');

        $this->assign('vartype_ary_radio', $vartype_ary_radio);
        $this->assign('editinfo', $editinfo);

        $this->V('config');
    }

    /**
     * 新增/修改系统配置参数页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 4) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $sysid = !empty($_POST['sysid']) ? (int) $_POST['sysid'] : 0;
        $nvarname = !empty($_POST['nvarname']) ? trim($this->H($_POST['nvarname'])) : '';
        $nvarvalue = !empty($_POST['nvarvalue']) ? $_POST['nvarvalue'] : '';
        $vartype = !empty($_POST['vartype']) ? $_POST['vartype'] : '';
        $nameaction = !empty($_POST['nameaction']) ? trim($this->H($_POST['nameaction'])) : '';
        $item = !empty($_POST['item']) ? trim($this->H($_POST['item'])) : '';
        $varmsg = !empty($_POST['varmsg']) ? $_POST['varmsg'] : '';
        $help = !empty($_POST['help']) ? trim($this->H($_POST['help'])) : '';
        $order = !empty($_POST['order']) ? $_POST['order'] : '0';

        /* 验证不能为空 */
        if (empty($nvarname)) {
            $this->redirect('变量名称不能为空');
            exit;
        }

        if (!isset($vartype)) {
            $this->redirect('选择变量类型!');
            exit;
        }
        // TODO goon to verify

        $info = array(
            'varname' => $nvarname,
            'value' => $nvarvalue,
            'type' => $vartype,
            'nameaction' => $nameaction,
            'item' => $item,
            'info' => $varmsg,
            'help' => $help,
            'order' => $order
        );
        if ($sysid > 0) {
            $config_page = HttpResponse::getCookie('config_page');
            //update save article
            $where = 'sysid = ' . $sysid . '';
            $rs = $this->tmp_model->addConfig($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改系统配置参数成功', PHP_SELF.'?m=config&page= ' . $config_page . '');
            } else {
                $this->redirect('修改系统配置参数失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addConfig($info);
            if ($rs) {
                $this->redirect('添加系统配置参数成功', PHP_SELF.'?m=config.add');
            } else {
                $this->redirect('添加系统配置参数失败');
            }
        }
    }

    /**
     * 根据服务器类型获取相应的rewrite内容
     */
    public function getServerRewrite()
    {
    	$type = isset($_GET['cfg_servertype']) ? $_GET['cfg_servertype'] : 0;
    	$content = "" ; 
    	if($type == 0){
    	   $content = file_get_contents(TMAC_BASE_PATH . APP_WWW_NAME . DIRECTORY_SEPARATOR .DIRECTORY_SEPARATOR.VARROOT.DIRECTORY_SEPARATOR.'Data'.DIRECTORY_SEPARATOR.'http.conf');
    	}else{
    		$content = file_get_contents(TMAC_BASE_PATH . APP_WWW_NAME . DIRECTORY_SEPARATOR .DIRECTORY_SEPARATOR.VARROOT.DIRECTORY_SEPARATOR.'Data'.DIRECTORY_SEPARATOR.'httpd.ini');
    	}
        echo $content;
        exit;
    }
    
    public function savelist()
    {
        if ($_POST['cfg_rewrite'] == 1) {
            $rewrite_search = $this->tmp_model->getConfigInfo(13);
            $rewrite_search = $rewrite_search['value'];
            $hotel_info_name = $this->tmp_model->getConfigInfo(14);
            $hotel_info_name = $hotel_info_name['value'];

            $htaccess = $_POST['cfg_rewrite_rule'];
            $htaccess_file = WEB_ROOT . '.htaccess';
            $httpd_file = WEB_ROOT . 'httpd.ini';
            $htaccess_search = array('^'.$rewrite_search, '^'.$hotel_info_name);
            $htaccess_replace = array('^' . $_POST['cfg_rewrite_search'], '^' . $_POST['cfg_hotel_info']);
            
            $htaccess = str_replace($htaccess_search, $htaccess_replace, $htaccess);
            //判断用户选择的是iis还是apache
            if($_POST['cfg_servertype'] == 0 ){
                file_put_contents($htaccess_file, htmlspecialchars_decode(stripslashes($htaccess)), LOCK_EX);
            }else{
            	file_put_contents($httpd_file, htmlspecialchars_decode(stripslashes($htaccess)), LOCK_EX);
            }
            $_POST['cfg_rewrite_rule'] = $htaccess;
        }
        foreach ($_POST as $k => $v) {
            $value = $this->H($v);
            $info = array(
                'value' => $value
            );
            $where = "varname = '{$k}'";
            $rs = $this->tmp_model->addConfig($info, 'UPDATE', $where);
        }        
        $configfile = TMAC_BASE_PATH . APP_WWW_NAME . DIRECTORY_SEPARATOR . APPLICATION . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . 'configcache.inc.php';
        $adminconfigfile = TMAC_BASE_PATH . APP_NAME . DIRECTORY_SEPARATOR . APPLICATION . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . 'configcache.inc.php';
            
        $this->tmp_model->ReWriteConfig($configfile);
        $this->tmp_model->ReWriteConfig($adminconfigfile);

        if ($rs) {
            $this->redirect('修改系统配置参数成功', PHP_SELF.'?m=config');
        } else {
            $this->redirect('修改系统配置参数失败');
        }
    }

}