<?php
/**
 * Copyright (C) 2009  Honghao Geng (genghonghao@gmail.com)
 * $Id: system.php 541 2010-01-24 03:25:25Z genghonghao $
 */

define('IN_ADMIN', TRUE);

require_once dirname(__FILE__) . '/includes/init.php';
require_once dirname(__FILE__) . '/includes/global_set_form.php';
require_once GEN_ROOT . '/languages/' . $_CONF['lang'] . '/admin/system.lang.php';
$image = new image();

// 验证用户是否登录
if (false === verify_user()) {;
    header("Location: ../login.php");
}

$tpl->assign('lang', $lang);

$action = isset($_REQUEST['act']) ? $_REQUEST['act'] : 'global_set';

// 获得当前页面的完整URL
$current_page_url = $base_url . '/' . $current_file . '?' . get_uri_param_string();

$tpl->assign('current_page_url', $current_page_url);
$tpl->assign('base_root_url', $base_root_url);

// 全局设置
if ($action == 'global_set') {
    if (isset($_REQUEST['text_submit_conf'])) {
        $return_status = array();
        foreach($_POST as $key=>$item) {
        // 提交按钮
            if ($key == 'text_submit_conf') {
                continue;
            }
            // 上传ICP证书文件
            if ($_FILES['icp_cert_file'] && !isset($up_icp_flag)) {
                if($_FILES['icp_cert_file']['error'] == 0  &&
                    $_FILES['icp_cert_file']['size'] < 4148  &&
                    $_FILES['icp_cert_file']['type'] == 'application/octet-stream' &&
                    $_FILES['icp_cert_file']['name'] == 'bazs.cert') {
                    if (!move_uploaded_file($_FILES['icp_cert_file']['tmp_name'], GEN_ROOT . 'cert/bazs.cert')) {
                        $return_status[] = 'icp_cert_file';
                    }
                }
                $up_icp_flag = true;
            }
            $select_sql = "SELECT conf_name FROM " . $db->get_table('TB_CONF') . " WHERE conf_name='$key'";
            $res = $db->fetch_row($db->query($select_sql));
            if (isset($res[0])) {
                $sql = "UPDATE " . $db->get_table('TB_CONF') . " SET `conf_name`='{$key}', `conf_value` = '{$item}', `is_disable` = '1' WHERE conf_name='$key'";
                $query = $db->query($sql, '', 1);
            } else {
                $sql = "INSERT INTO " . $db->get_table('TB_CONF') . " VALUES('{$key}', '{$item}', '1')";
                $query = $db->query($sql, '', 1);
            }
            if (false === $query) {
                $return_status[] = $key;
            }
        }

        $height = intval($_REQUEST['screenheight']);
        if (count($return_status) == 0) {
            redirect('Success', $lang['edit_success_text'], $current_page_url, 5, $height);
        } else {
            $failures = "";
            foreach($return_status AS $val) {
                $failures .= $lang['label_'.$val];
            }
            redirect('Failure',sprintf($lang['update_failure_text'],$failures), $current_page_url5, $height);
        }
    }

    // 表单数组
    $tpl->assign('form_items', $form_items);

    // 把配置数据取出来放到模板中去
    $conf_data  = $db->result("SELECT conf_name, conf_value FROM " . $db->get_table('TB_CONF'));
    foreach($conf_data AS $item) {
        $data_items['data_' . $item['conf_name']] = $item['conf_value'];
    }
    $tpl->assign('data_items', $data_items);

    // 检察是否已经上传了ICP证书文件
    if (file_exists(GEN_ROOT . 'cert/bazs.cert')) {
        $tpl->assign('is_up_file', $lang['notice_already']);
    } else {
        $tpl->assign('is_up_file', $lang['notice_non']);
    }

    // 所在位置
    $location_link = array("<a href='system.php'>" . $lang['3_system_setup']['name'] . "</a>",
        "<a href='system.php?act=$action'>" . $lang['3_system_setup'][$action] . "</a>"
    );
    // 相关操作
    $location_other = array("<a class='realbutton' href='system.php?act=nav_set'>" . $lang['3_system_setup']['nav_set'] . "</a>",
        "<a class='realbutton' href='system.php?act=basic_set'>" . $lang['3_system_setup']['basic_set'] . "</a>"
    );

    // 时区
    $timezone_identifiers = DateTimeZone::listIdentifiers();
    $tpl->assign('timezone_all', $timezone_identifiers);

    $tpl->assign('location_link', $location_link);
    $tpl->assign('location_other', $location_other);
    $tpl->display('global_set.html');
}

// 基本设置,博客设置，显示设置
elseif ($action == 'basic_set' || $action == 'blog_set' || $action == 'view_set' ) {

    if ($action == 'basic_set') {
        $set_type = 'basic';
    } elseif ($action == 'blog_set') {
        $set_type = 'blog';
    } elseif ($action == 'view_set') {
        $set_type = 'view';
    }

    if (isset($_REQUEST['text_submit_conf'])) {
        $return_status = array();
        foreach($_POST as $key=>$item) {
        // 提交按钮
            if ($key == 'text_submit_conf') {
                continue;
            }
            // 上传ICP证书文件
            if ($_FILES['icp_cert_file'] && !isset($up_icp_flag)) {
                if($_FILES['icp_cert_file']['error'] == 0  &&
                    $_FILES['icp_cert_file']['size'] < 4148  &&
                    $_FILES['icp_cert_file']['type'] == 'application/octet-stream' &&
                    $_FILES['icp_cert_file']['name'] == 'bazs.cert') {
                    if (!move_uploaded_file($_FILES['icp_cert_file']['tmp_name'], GEN_ROOT . 'cert/bazs.cert')) {
                        $return_status[] = 'icp_cert_file';
                    }
                }
                $up_icp_flag = true;
            }
            $select_sql = "SELECT conf_name FROM " . $db->get_table('TB_CONF') . " WHERE conf_name='$key'";
            $res = $db->fetch_row($db->query($select_sql));
            if (isset($res[0])) {
                $sql = "UPDATE " . $db->get_table('TB_CONF') . " SET `conf_name`='{$key}', `conf_value` = '{$item}', `is_disable` = '1' WHERE conf_name='$key'";
                $query = $db->query($sql, '', 1);
            } else {
                $sql = "INSERT INTO " . $db->get_table('TB_CONF') . " VALUES('{$key}', '{$item}', '1')";
                $query = $db->query($sql, '', 1);
            }
            if (false === $query) {
                $return_status[] = $key;
            }
        }

        if (count($return_status) == 0) {
            redirect('Success', $lang['edit_success_text'], $current_page_url);
        } else {
            $failures = "";
            foreach($return_status AS $val) {
                $failures .= $lang['label_'.$val];
            }
            redirect('Failure',sprintf($lang['update_failure_text'],$failures), $current_page_url);
        }
    }

    // 表单数组
    foreach($form_items AS $type=>$iname) {
        if ($set_type != $type) {
            unset($form_items[$type]);
        }
    }
    $tpl->assign('form_items', $form_items);

    // 把配置数据取出来放到模板中去
    $conf_data  = $db->result("SELECT conf_name, conf_value FROM " . $db->get_table('TB_CONF'));
    foreach($conf_data AS $item) {
        $data_items['data_' . $item['conf_name']] = $item['conf_value'];
    }
    $tpl->assign('data_items', $data_items);

    // 检察是否已经上传了ICP证书文件
    if (file_exists(GEN_ROOT . 'cert/bazs.cert')) {
        $tpl->assign('is_up_file', $lang['notice_already']);
    } else {
        $tpl->assign('is_up_file', $lang['notice_non']);
    }

    // 所在位置
    $location_link = array("<a href='system.php'>" . $lang['3_system_setup']['name'] . "</a>",
        "<a href='system.php?act=$action'>" . $lang['3_system_setup'][$action] . "</a>"
    );
    // 相关操作
    $location_other = array("<a class='realbutton' href='system.php?act=nav_set'>" . $lang['3_system_setup']['nav_set'] . "</a>",
        "<a class='realbutton' href='system.php?act=basic_set'>" . $lang['3_system_setup']['basic_set'] . "</a>"
    );

    // 时区
    $timezone_identifiers = DateTimeZone::listIdentifiers();
    $tpl->assign('timezone_all', $timezone_identifiers);

    $tpl->assign('location_link', $location_link);
    $tpl->assign('location_other', $location_other);
    $tpl->display('basic_set.html');
}

// 导航设置
elseif ($action == 'nav_set') {
    // 删除导航
    if (isset($_REQUEST['mod']) && $_REQUEST['mod'] == 'del') {      
        $navid = $_REQUEST['navid'];
        $current_page_url = str_replace("&mod=del&navid={$navid}", '', $current_page_url);
        $query = $db->query("DELETE FROM " . $db->get_table(TB_NAVIGATION) . " WHERE nav_id='$navid'", '', 1);
        if ($query  === false) {
             redirect('Failure', $lang['notice_delete_failure'], $current_page_url);
        } else {
             redirect('Success', $lang['notice_delete_success'], $current_page_url);
        }
    }
    // 提交表单
    if (isset($_REQUEST['sub_nav']) && !empty($_REQUEST['sub_nav'])) {
        $nav_name = isset($_REQUEST['text_nav_name']) ? trim($_REQUEST['text_nav_name'])    : NULL;
        $nav_url  = isset($_REQUEST['text_nav_url'])  ? trim($_REQUEST['text_nav_url'])     : NULL;
        $order    = isset($_REQUEST['text_display_order'])  ? intval($_REQUEST['text_display_order'])   : 0;
        $unlock   = isset($_REQUEST['text_unlock_type'])  ? intval($_REQUEST['text_unlock_type']) : 1;
        $target   = isset($_REQUEST['text_target_type'])  ? intval($_REQUEST['text_target_type']) : 1;

        if (!empty($nav_name) && !empty($nav_url)) {
            $query = $db->query("INSERT INTO " . $db->get_table(TB_NAVIGATION) .
                "(`nav_id`, `nav_name`, `nav_url`, `display_order`, `is_unlock`, `target_type`)
                    VALUES(NULL, '$nav_name', '$nav_url', '$order', '$unlock', '$target')");

            if (false !== $query) {
                redirect('Success', $lang['insert_success_text'], $current_page_url);
            } else {
                redirect('Failure', $lang['insert_db_failure'], $current_page_url);
            }
        } else {
            redirect('Failure', $lang['notice_nav_null'], $current_page_url);
        }
    }

    $nav_list = $db->result("SELECT * FROM " . $db->get_table(TB_NAVIGATION) . " ORDER BY display_order ASC");

    // 所在位置
    $location_link = array("<a href='system.php'>" . $lang['3_system_setup']['name'] . "</a>",
        "<a href='system.php?act=$action'>" . $lang['3_system_setup']['nav_set'] . "</a>"
    );
    // 相关操作
    $location_other = array("<a class='realbutton' href='system.php?act=global_set'>" . $lang['3_system_setup']['global_set'] . "</a>",
        "<a class='realbutton' href='system.php?act=basic_set'>" . $lang['3_system_setup']['basic_set'] . "</a>"
    );

    $tpl->assign('location_link', $location_link);
    $tpl->assign('location_other', $location_other);
    $tpl->assign('nav_list', $nav_list);
    $tpl->display('nav_set.html');
}

// 模板设置
elseif ($action == 'template_set') {
// 使用新模板
    if (isset($_REQUEST['tpl_name']) && !empty($_REQUEST['tpl_name'])) {
        $tpl_name = strtolower(trim($_REQUEST['tpl_name']));
        $query = $db->query("UPDATE " . $db->get_table(TB_CONF) . " SET conf_value='$tpl_name' WHERE conf_name='tpl_name'", '', 1);

        $current_page_url = 'system.php?act=template_set';
        if (false !== $query) {
        // 更新缓存
            $cache->set('conf');
            $cache-> destroy();
            redirect('Success', $lang['notice_new_tpl_use_success'], $current_page_url);
        } else {
            redirect('Failure', $lang['notice_new_tpl_use_failure'], $current_page_url);
        }
    }
    // 取数模板数据
    $tpl_list = get_tpl_list();
    $tpl_info = array();
    $cur_info = array();
    foreach($tpl_list AS $item) {
        $info = get_tpl_info($item);
        if (strtolower($info['template_name']) == strtolower($_CONF['tpl_name'])) {
            $cur_info = $info;
        } else {
            $tpl_info[] = $info;
        }
    }

    // 所在位置
    $location_link = array("<a href='system.php'>" . $lang['3_system_setup']['name'] . "</a>",
        "<a href='system.php?act=$action'>" . $lang['3_system_setup'][$action] . "</a>"
    );
    // 相关操作
    $location_other = array("<a class='realbutton' href='system.php?act=global_set'>" . $lang['3_system_setup']['global_set'] . "</a>",
        "<a class='realbutton' href='system.php?act=basic_set'>" . $lang['3_system_setup']['basic_set'] . "</a>"
    );

    $tpl->assign('location_link', $location_link);
    $tpl->assign('location_other', $location_other);
    $tpl->assign('cur_tpl', $cur_info);
    $tpl->assign('tpls', $tpl_info);
    $tpl->display('template_set.html');
}
?>
