<?php
/**
 * Copyright (C) 2009  Honghao Geng (genghonghao@gmail.com)
 * $Id: user.php 307 2009-11-06 09:35:41Z genghonghao $
 */

define('IN_ADMIN', TRUE);

require_once dirname(__FILE__) . '/includes/init.php';
require_once GEN_ROOT . '/languages/' . $_CONF['lang'] . '/admin/user.lang.php';

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

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

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

// 获得当前页面的完整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 == 'edit_user') {

    // 修改用户资料
    if (isset($_REQUEST['text_submit_user'])) {
        $userid = intval($_REQUEST['text_userid']);
        $nickname = trim($_REQUEST['text_nickname']);
        $old_pwd   = trim($_REQUEST['text_old_passwd']);
        $new_pwd   = trim($_REQUEST['text_new_passwd']);
        $repeat_pwd  = trim($_REQUEST['text_repeat_passwd']);
        $cur_time = time();

        if (!empty($nickname) && !empty($old_pwd) && !empty($new_pwd) && !empty($repeat_pwd)) {
            $userinfo = $db->fetch_row($db->query("SELECT admin_id FROM " . $db->get_table(TB_ADMIN) . " WHERE admin_id='$userid' AND password='" . MD5($old_pwd). "'"));
            if (!isset($userinfo[0]) || empty($userinfo[0])) {
                redirect('Failure', $lang['edit_failure_old_passwd_text'], $current_page_url);
            }
            if ($new_pwd != $repeat_pwd && strlen($new_passwd)  < 6) {
                redirect('Failure', $lang['edit_failure_passwd_text'], $current_page_url);
            }
            $update_status = $db->query("UPDATE " . $db->get_table(TB_ADMIN) . " SET nickname='$nickname', password='" .MD5($new_pwd) . "', last_edit_ip='$client_ip', last_edit_time='$cur_time' WHERE admin_id='$userid'", '', 1);
            if (false === $update_status) {
                redirect('Failure', $lang['edit_failure_user'], $current_page_url);
            } else {
                redirect('success', $lang['edit_success_user'], $current_page_url);
            }
        } else {
            redirect('Failure', $lang['edit_failure_text'], $current_page_url);
        }
    }
    $res = $db->fetch_row($db->query("SELECT username, nickname,admin_id FROM " . $db->get_table(TB_ADMIN) ." WHERE username='{$_SESSION['user']['username']}' AND admin_id='{$_SESSION['user']['userid']}'"));
    $tpl->assign('username', $res[0]);
    $tpl->assign('nickname', $res[1]);
    $tpl->assign('userid',    $res[2]);
    
    // 所在位置
    $location_link = array("<a href='user.php'>" . $lang['7_user_manage']['name'] . "</a>",
        "<a href='user.php?act=$action'>" . $lang['7_user_manage'][$action] . "</a>"
    );
    // 相关操作
    $location_other = array("<a class='realbutton' href='user.php?act=list'>" . $lang['7_user_manage']['user_list'] . "</a>",
        "<a class='realbutton' href='user.php?act=add'>" . $lang['7_user_manage']['add_user'] . "</a>"
    );

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

// 添加用户
if ($action == 'add_user') {

    // 添加用户
    if (isset($_REQUEST['text_submit_user'])) {
        $username = trim($_REQUEST['text_username']);
        $nickname = trim($_REQUEST['text_nickname']);
        $user_pwd   = trim($_REQUEST['text_passwd']);
        $repeat_pwd  = trim($_REQUEST['text_repeat_passwd']);
        $cur_time = time();
        $pwd = MD5($user_pwd);

        if (!empty($nickname) && !empty($username) && !empty($user_pwd) && !empty($repeat_pwd)) {
            $userinfo = $db->fetch_row($db->query("SELECT admin_id FROM " . $db->get_table(TB_ADMIN) . " WHERE username='$username'"));
            if (isset($userinfo[0]) || !empty($userinfo[0])) {
                redirect('Failure', $lang['failure_login_user_y'], $current_page_url);
            }
            if ($user_pwd != $repeat_pwd && strlen($user_passwd)  < 6) {
                redirect('Failure', $lang['edit_failure_passwd_text'], $current_page_url);
            }
            $insert_status = $db->query("INSERT INTO " . $db->get_table(TB_ADMIN) . " VALUES(NULL, '$pwd', '$username', '$nickname', '$client_ip', '$cur_time', '', 0, '$client_ip', '$cur_time')", '', 1);
            if (false === $insert_status) {
                redirect('Failure', $lang['failure_user_add'], $current_page_url);
            } else {
                redirect('success', $lang['success_user_add'], $current_page_url);
            }
        } else {
            redirect('Failure', $lang['edit_failure_text'], $current_page_url);
        }
    }

    // 所在位置
    $location_link = array("<a href='user.php'>" . $lang['7_user_manage']['name'] . "</a>",
        "<a href='user.php?act=$action'>" . $lang['7_user_manage'][$action] . "</a>"
    );
    // 相关操作
    $location_other = array("<a class='realbutton' href='user.php?act=list'>" . $lang['7_user_manage']['user_list'] . "</a>",
        "<a class='realbutton' href='user.php?act=add'>" . $lang['7_user_manage']['add_user'] . "</a>"
    );

    $tpl->assign('location_link', $location_link);
    $tpl->assign('location_other', $location_other);
    $tpl->display('add_user.html');
}
// 用户列表
if ($action == 'user_list') {

    $user_list = $db->result("SELECT * FROM " . $db->get_table(TB_ADMIN));

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

    // 所在位置
    $location_link = array("<a href='user.php'>" . $lang['7_user_manage']['name'] . "</a>",
        "<a href='user.php?act=$action'>" . $lang['7_user_manage'][$action] . "</a>"
    );
    // 相关操作
    $location_other = array("<a class='realbutton' href='user.php?act=list'>" . $lang['7_user_manage']['user_list'] . "</a>",
        "<a class='realbutton' href='user.php?act=add'>" . $lang['7_user_manage']['add_user'] . "</a>"
    );

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