<?php
include ('admin_global.php');

$cmd = $_GET["cmd"];
if($cmd=="list"){
    $args=$_POST;
    $t_sql="select id from user where";

    $sql="select * from user where";
    $username=$args[username];
    if($username==null || $username==""){
        $sql.=" 1=1 ";
        $t_sql.=" 1=1 ";
    }else{
        $sql.=" title ='$username' ";
        $t_sql.=" title ='$username' ";
    }

    $total=mysql_num_rows($db->query($t_sql));  //计算结果集总条数用于分页

    if($args[limit]!=""){
        if($args[start]!="" || $args[start]!=0){
         $start=$args[start];
        }else{
            $start=0;
        }
         $sql.="limit ".$start.",$args[limit]";
    }
    $query=$db->query($sql);
    while ($row=$db->fetch_array($query)) {
        $i=0;
        $userrole=($row[role]==0)?"普通用户":"管理员";
        $data.="{id:".$row[id].",password:'".$row[password]."',username:\"".iconv("GBK", "utf-8", $row[username])."\",email:'$row[email]',roleId:'$row[role]',userrole:'$userrole'}";
        if($i<  (count($row)-1)){
           $data.=",";
        }
        $i++;
    }
    $str="{rowCount:".$total.",result:[".$data."]}";
    echo $str;
}

if($cmd=="save"){
    $args=$_POST;
    $username=iconv("utf-8", "gbk", $args[username]);
    $sql="insert into user values(null,'$username','".  md5($args[password])."','$args[email]',$args[roleId])";
    $query=$db->query($sql);
    if($query){
        echo "{success:true,info:'新增成功'}";
    }else{
        echo "{failure:true,info:'新增失败'}";
    }
}

if($cmd=="update"){
    $args=$_POST;
    $name=iconv("utf-8", "gbk", $args[username]);
    $pw=  md5($args[password]);
    if($pw!=null && $pw!=""){
        $editPw=",password='$pw'";
    }
    $sql="update user set username='$name' $editPw ,email='$args[email]',role=$args[roleId] where id=$args[id]";
    $query=$db->query($sql);
    if($query){
        echo "{success:true,info:'修改成功'}";
    }else{
        echo "{failure:true,info:'修改失败'}";
    }
}

if($cmd=="remove"){
    $sql="DELETE newsinfo,user from newsinfo LEFT JOIN user ON newsinfo.userid=user.id WHERE user.id=$_POST[id]";
    $query=$db->query($sql);
    if($query){
        echo "{success:true,info:'删除成功'}";
    }else{
        echo "{failure:true,info:'删除失败'}";
    }
}

if($cmd=="login"){
    if(!empty($_POST[username])&& !empty($_POST[password])){
     $db->Get_user_login($_POST[username],$_POST[password]);
 }
}

if($cmd=="register"){
    $args=$_POST;
    $username=iconv("utf-8", "gbk", $args[userName]);
    $sql_check="select id from user where username='$username' or email='$args[email]'";
    $count_check=mysql_num_rows($db->query($sql_check));
    if(!$count_check){
        $sql="insert into user values(null,'$username','".  md5($args[pass])."','$args[email]',0)";
        $query=$db->query($sql);
        $uid=$db->insert_id();
        if($query){
            $_SESSION[uid] = $uid;
            $_SESSION[uname] = $username;
            $_SESSION[email] = $args[email];
            $_SESSION[shell] = md5($username . $args[pass] . "TKBK");
            $_SESSION[ontime] = mktime();
            echo "{success:true,info:'注册成功'}";
        }else{
            echo "{failure:true,info:'注册失败'}";
        }
    }else{
            echo "{failure:true,info:'注册失败!用户名或email重复！'}";
    }
}

if($cmd=="getUserId"){
    if($uid!=null && $uid!=""){
        echo $uname;
    }else{
        echo -1;
    }
}

if($cmd=='logout')$db->Get_user_out();

?>
