<?php
// +----------------------------------------------------------------------
// | 终端接口文件 [ 天方夜谭快速开发框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014~2019 http://www.eirds.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 天方夜谭 <17404785@qq.com>
// +----------------------------------------------------------------------

//引入核心文件
require 'terminal/common.php';

//定义编码
header('Content-Type:application/json; charset=utf-8');

//添加QQ
if (GET('action')=='addqq'){
    $count = GET('count');
    $only = GET('only');
    $qq = GET('qq');
    $pwd = GET('pwd');
    $skey =GET('skey');
    $p_skey =GET('p_skey');
    $superkey =GET('superkey');
    $qqcokie = "skey={$skey};p_skey={$p_skey};superkey={$superkey};";
    if (empty($qq) || empty($pwd) || empty($skey) || empty($p_skey) || empty($superkey)){
        $arr = array('state'=>'no','msg'=>'缺少参数！');
    }else{
        $pwd = authcode($pwd,'ENCODE','qq17404785');
        $qqcokie = authcode($qqcokie,'ENCODE','qq17404785');
        if($info = $TFYT_DB->get_row("select qid from ".PREFIX."qq where qq='{$qq}' and `only`='{$only}' limit 1")){
            if($TFYT_DB->query("update ".PREFIX."qq set qq='{$qq}',pwd='{$pwd}',cookie='{$qqcokie}',status=0,gxtime='".date('Y-m-d h:i:s')."' where qid='{$info->qid}' and `only`='{$only}'")){
                $arr = array('state'=>'ok','msg'=>'QQ更新成功！');
            }else{
                $arr = array('state'=>'no','msg'=>'QQ登录成功，但更新数据库失败！');
            }
        }else{
            if (get_count('qq',"only='{$only}'")>=$count){
                $arr = array('state'=>'ok','msg'=>"对不起，你最大允许添加{$count}个QQ！");
            }else{
                if ($TFYT_DB->query("insert into ".PREFIX."qq (`only`,`qq`,`pwd`,`cookie`,`addtime`)values('{$only}','{$qq}','{$pwd}','{$qqcokie}','".date('Y-m-d h:i:s')."')")){
                    $arr = array('state'=>'ok','msg'=>'QQ添加成功！');
                }else{
                    $arr = array('state'=>'no','msg'=>'QQ登录成功，但添加数据库失败！');
                }
            }
        }
    }
    exit(json_encode($arr));
}elseif (GET('action')=='count'){ //获取数量
    $only = GET('only');
    $arr = array('state'=>'ok','msg'=>get_count('qq',"only='{$only}'"));
    exit(json_encode($arr));
}elseif (GET('action')=='list'){ //获取QQ列表
    $list = $TFYT_DB->get_results("select qid,qq,status,addtime,gxtime from ".PREFIX."qq where `only`='".GET('only')."' order by qid desc");
    exit(json_encode($list));
}elseif (GET('action')=='del'){ //删除QQ
    $only = GET('only');
    $qid = GET('qid');
    if (empty($only) || empty($qid)){
        $arr = array('state'=>'no','msg'=>'缺少参数！');
    }else{
        if ($TFYT_DB->query("delete from ".PREFIX."qq where `only`='{$only}' and qid='{$qid}'")){
            $arr = array('state'=>'ok','msg'=>'删除挂机成功！');
        }else{
            $arr = array('state'=>'no','msg'=>'删除挂机失败，请稍后再试！');
        }
    }
    exit(json_encode($arr));
}elseif (GET('action')=='info'){    //获取挂机信息
    $only = GET('only');
    $qid = GET('qid');
    if (empty($only) || empty($qid)){
        $arr = array('state'=>'no','msg'=>'缺少参数！');
    }else{
        $arr = array('state'=>'ok','msg'=>[$TFYT_DB->get_row("select * from ".PREFIX."qq where qid='{$qid}' and `only`='{$only}' limit 1")]);
    }
    exit(json_encode($arr));
}elseif (GET('action')=='website'){
    $arr = array('zannet'=>$mysql['DB_ZANNET'],'netnum'=>$mysql['DB_NETNUM']);
    exit(json_encode($arr));
}elseif (GET('action')=='netnum'){ //获取服务器挂机数量
    $mold = GET('mold');
    $i = GET('i');
    $arr = array('netnum'=>get_count('qq',"{$mold}='{$i}'",'qid'));
    exit(json_encode($arr));
}elseif (GET('action')=='config'){  //挂机功能配置
    $only = GET('only');
    $qid = GET('qid');
    if ($only && $qid && $info = $TFYT_DB->get_row("select * from ".PREFIX."qq where qid='{$qid}' and `only`='{$only}' limit 1")){
        if (GET('config')=='zan'){  //说说秒赞
            $zan = GET('zan');
            $net = GET('net');
            if (!$zan && !$net){
                $arr = array('state'=>'no','msg'=>'请选择1个合适的服务器！');
            }elseif ($net && $info->zannet!=$net && get_count('qq',"zannet='{$net}'")>=$mysql['DB_NETNUM']){
                $arr = array('state'=>'no','msg'=>"{$net}号服务器已满，请换一个服务器！");
            }else{
                $TFYT_DB->query("update ".PREFIX."qq set zan='{$zan}',zannet='{$net}' where qid='{$info->qid}' and `only`='{$only}'");
                $arr = array('state'=>'ok','msg'=>"说说秒赞配置成功！");
            }
        }elseif (GET('config')=='shuo'){    //自动说说
            $is=is_numeric(GET('is'))?GET('is'):'0';
            $net=is_numeric(GET('net'))?GET('net'):'0';
            $con=GET('content');
            $url=GET('shuopic');
            $phone=GET('shuophone');;
            $rate=GET('rate');
            if (!$is && $net){
                $arr = array('state'=>'no','msg'=>'请选择1个合适的服务器！');
            }elseif ($net && $info->shuonet!=$net && get_count('qq',"shuonet='{$net}'")>=$mysql['DB_NETNUM']){
                $arr = array('state'=>'no','msg'=>"{$net}号服务器已满，请换一个服务器！");
            }else{
                $TFYT_DB->query("update ".PREFIX."qq set isshuo='{$is}',shuoshuo='{$con}',shuopic='{$url}',shuophone='{$phone}',shuorate='{$rate}',shuonet='{$net}' where qid='{$info->qid}' and `only`='{$only}'");
                $arr = array('state'=>'ok','msg'=>"自动说说配置成功！".$rate);
            }
        }elseif (GET('config')=='delshuo'){ //自动删说说
            $delshuo = GET('delshuo');
            $net = GET('net');
            if (!$delshuo && !$net){
                $arr = array('state'=>'no','msg'=>'请选择1个合适的服务器！');
            }elseif ($net && $info->delshuonet!=$net && get_count('qq',"delshuonet='{$net}'")>=$mysql['DB_NETNUM']){
                $arr = array('state'=>'no','msg'=>"{$net}号服务器已满，请换一个服务器！");
            }else{
                $TFYT_DB->query("update ".PREFIX."qq set delshuo='{$delshuo}',delshuonet='{$net}' where qid='{$info->qid}' and `only`='{$only}'");
                $arr = array('state'=>'ok','msg'=>"自动删说说配置成功！");
            }
        }elseif (GET('config')=='delly'){   //自动删留言
            $delly = GET('delly');
            $net = GET('net');
            if (!$delly && !$net){
                $arr = array('state'=>'no','msg'=>'请选择1个合适的服务器！');
            }elseif ($net && $info->dellynet!=$net && get_count('qq',"dellynet='{$net}'")>=$mysql['DB_NETNUM']){
                $arr = array('state'=>'no','msg'=>"{$net}号服务器已满，请换一个服务器！");
            }else{
                $TFYT_DB->query("update ".PREFIX."qq set delly='{$delly}',dellynet='{$net}' where qid='{$info->qid}' and `only`='{$only}'");
                $arr = array('state'=>'ok','msg'=>"自动删留言配置成功！");
            }
        }elseif (GET('config')=='ly'){  //自动互刷留言
            $ly = GET('ly');
            $net = GET('net');
            if (!$ly && !$net){
                $arr = array('state'=>'no','msg'=>'请选择1个合适的服务器！');
            }elseif ($net && $info->lynet!=$net && get_count('qq',"lynet='{$net}'")>=$mysql['DB_NETNUM']){
                $arr = array('state'=>'no','msg'=>"{$net}号服务器已满，请换一个服务器！");
            }else{
                $TFYT_DB->query("update ".PREFIX."qq set ly='{$ly}',lynet='{$net}' where qid='{$info->qid}' and `only`='{$only}'");
                $arr = array('state'=>'ok','msg'=>"互刷留言配置成功！");
            }
        }elseif (GET('config')=='zyzan'){   //自动互刷主页赞
            $zyzan = GET('zyzan');
            $net = GET('net');
            if (!$zyzan && !$net){
                $arr = array('state'=>'no','msg'=>'请选择1个合适的服务器！');
            }elseif ($net && $info->zyzannet!=$net && get_count('qq',"zyzannet='{$net}'")>=$mysql['DB_NETNUM']){
                $arr = array('state'=>'no','msg'=>"{$net}号服务器已满，请换一个服务器！");
            }else{
                $TFYT_DB->query("update ".PREFIX."qq set zyzan='{$zyzan}',zyzannet='{$net}' where qid='{$info->qid}' and `only`='{$only}'");
                $arr = array('state'=>'ok','msg'=>"互刷主页赞配置成功！");
            }
        }elseif (GET('config')=='visitor'){   //自动互刷访客
            $visitor = GET('visitor');
            $net = GET('net');
            if (!$visitor && !$net){
                $arr = array('state'=>'no','msg'=>'请选择1个合适的服务器！');
            }elseif ($net && $info->visitornet!=$net && get_count('qq',"visitornet='{$net}'")>=$mysql['DB_NETNUM']){
                $arr = array('state'=>'no','msg'=>"{$net}号服务器已满，请换一个服务器！");
            }else{
                $TFYT_DB->query("update ".PREFIX."qq set visitor='{$visitor}',visitornet='{$net}' where qid='{$info->qid}' and `only`='{$only}'");
                $arr = array('state'=>'ok','msg'=>"互刷访客配置成功！");
            }
        }elseif (GET('config')=='qd'){  //每日签到
            $qd = GET('qd');
            $qdcon = GET('qdcon');
            $net = GET('net');
            if (!$qd && !$net){
                $arr = array('state'=>'no','msg'=>'请选择1个合适的服务器！');
            }elseif ($net && $info->qdnet!=$net && get_count('qq',"qdnet='{$net}'")>=$mysql['DB_NETNUM']){
                $arr = array('state'=>'no','msg'=>"{$net}号服务器已满，请换一个服务器！");
            }else{
                $TFYT_DB->query("update ".PREFIX."qq set qd='{$qd}',qdnet='{$net}',qdcon='{$qdcon}' where qid='{$info->qid}' and `only`='{$only}'");
                $arr = array('state'=>'ok','msg'=>"每日签到配置成功！");
            }
        }elseif (GET('config')=='setnick'){ //修改昵称
            $only = GET('only');
            $qid = GET('qid');
            $nick = GET('nick');
            if (empty($only) || empty($qid) || empty($nick)){
                $arr = array('state'=>'no','msg'=>'所有项不能为空！');
            }else{
                $qzone = new qzone($info->qq,authcode($info->cookie, 'DECODE', 'qq17404785'));
                $qzone->setnick($nick);
                if ($qzone->skeyzt){
                    $TFYT_DB->query("update ".PREFIX."qq set status='1' where qid='{$info->qid}'");
                    foreach ($qzone->msg as $result){
                        $arr = array('state'=>'no','msg'=>$result);
                    }
                }else{
                    foreach($qzone->msg as $result){
                        $arr = array('state'=>'ok','msg'=>$result);
                    }
                }
            }
        }elseif (GET('config')=='getqqcare'){   //获取关心我的+我关心的+数量
            $only = GET('only');
            $qid = GET('qid');
            if (empty($only) || empty($qid)){
                $arr = array('state'=>'no','msg'=>'所有项不能为空！');
            }else{
                $qzone = new qzone($info->qq,authcode($info->cookie, 'DECODE', 'qq17404785'));
                $arr = array('state'=>'ok','msg'=>'获取成功！','list1'=>$qzone->me_care()['data']['items_list'],'list2'=>$qzone->care_me()['data']['items_list'],'used_count'=>$qzone->fans_count()['data']['used_count'][0]['used_count'],'fans_count'=>$qzone->fans_count()['data']['fans_count'][0]['fans_count']);
            }
        }elseif (GET('config')=='getisknow'){   //获取我可能认识的人
            if (empty($only) || empty($qid)){
                $arr = array('state'=>'no','msg'=>'所有项不能为空！');
            }else{
                $qzone = new qzone($info->qq,authcode($info->cookie, 'DECODE', 'qq17404785'));
                $arr = array('state'=>'ok','msg'=>'获取成功！','list'=>$qzone->is_know()['data']['users']);
            }
        }
    }else{
        $arr = array('state'=>'no','msg'=>'QQ不存在！');
    }
    exit(json_encode($arr));
}elseif (GET('action')=='alllist'){
    $list = $TFYT_DB->get_results("select qid,qq,status,addtime,gxtime,`only` from ".PREFIX."qq order by qid desc");
    exit(json_encode($list));
}