<?php
@set_time_limit(0);
@ignore_user_abort(true);
//setlog('进程被启动');
$fp = fopen('data/runtime.txt','r+');
$time = fread($fp,filesize('data/runtime.txt'));
fclose($fp);
$nowtime = date("Y-m-j H:i:s");
$run = strtotime($nowtime) - strtotime($time);echo $run;
$banfp = fopen('data/runcount.txt','r+');
$count = fread($banfp,filesize('data/runcount.txt'));
fclose($banfp);
if($run < 30){
    die('Not at execution time.');
}
/*if($count >= 5){
    die('The process is being executed.');
}
$count = $count+1;
$banfp = fopen('data/runcount.txt','w+');
fwrite($banfp,$count);
fclose($banfp);*/
$fp = fopen('data/runtime.txt','w+');
fwrite($fp,$time = date("Y-m-j H:i:s"));
fclose($fp);
echo '['.$count.']';
$result = mysql_query("SELECT * FROM {$table}online");
global $lang;
while ($row = mysql_fetch_array($result,true)) {
    $runtime=explode("-",$row['runtime']);
    $h=date("H");
    if($h <= $runtime[0] or $h >= $runtime[1]){
        continue;
    }
    $sid = $row['sid'];
    if ($row['status'] == '1') {
        $mi=uc_get_user($row['username']);
        $mqq = new SET_MQQ($row['qq'], $row['pass']);
        $ptqq = new SET_3GQQ($row['qq']);
        $rand = $mqq->ChangeStat($row['login_status']);
        $return = json_decode($rand, true);
        //print_r($return);
        if ($return[status] == 'ok') {
            $time = date("Y-m-j H:i:s");
            $his =$mqq->GetMsg($row['reply']);
            $getmsg = $time.'%0D%0A'.$his.'%0D%0A'.$row['history'];
            if($his){
                $edit=mysql_query("UPDATE ".IQQOL_SQLTABLE."online
 SET history='$getmsg' 
 WHERE qq = '".$row['qq']."'");
            }
            //sleep(3);
        } else {
            $rand = $mqq->Login();
            $return = json_decode($rand, true);
            if ($return[status] == 'ok') {
                $time = date("Y-m-j H:i:s");
                $his =$mqq->GetMsg($row['reply']);
                $getmsg = $time.'%0D%0A'.$his.'%0D%0A'.$row['history'];
                if($his){
                    $edit=mysql_query("UPDATE ".IQQOL_SQLTABLE."online
 SET history='$getmsg' 
 WHERE qq = '".$row['qq']."'");
                }
            } elseif ($return[status] == 'vc') {
                setlog('QQ【'.$row['qq'].'】执行时遇到验证码。【rand：'.$rand.'】');
                mysql_query("UPDATE ".IQQOL_SQLTABLE."online
 SET status='2' 
 WHERE qq = '".$row['qq']."'");
                $rand = $ptqq->Login();
                //mysql_query("INSERT INTO {$table}log (qq,log,time) VALUES ('{$row['qq']}','{$rand}','".date('Y-m-d')."')");
                if(strpos($rand,"帐号暂时无法登录") or strpos($rand,"sid已过期") or strpos($rand,"马上登录")){
                        $edit=mysql_query("DELETE FROM {$table}online
WHERE qq='".$row['qq']."'");
                        $msg=str_replace("#qq#",$row['qq'],$lang[system][checkqq][delete_error]);
                        $msg=str_replace("#username#",$row['username'],$msg);
                        $msg=str_replace("#br#","%0D%0A",$msg);
                        $head="[{$row['qq']}]推送提醒";
                        mysql_query("INSERT INTO {$table}query (id,user,message,qq,email,noticeqq,head) VALUES (NULL,'{$row['username']}','$msg','{$row['qq']}','{$mi['2']}','{$row['noticeqq']}',$head)");
                        //sleep(3);
                }
                $msg=str_replace("#qq#",$row['qq'],$lang[system][checkqq][offline]);
                $msg=str_replace("#username#",$row['username'],$msg);
                $msg=str_replace("#br#","%0D%0A",$msg);
                $head="[{$row['qq']}]推送提醒";
                mysql_query("INSERT INTO {$table}query (id,user,message,qq,email,noticeqq,head) VALUES (NULL,'{$row['username']}','$msg','{$row['qq']}','{$mi['2']}','{$row['noticeqq']}',$head)");
                //sleep(3);
            } elseif ($return[status] == 'invaid password') {
                setlog('QQ【'.$row['qq'].'】密码被用户更改，服务器主动清理。【rand：'.$rand.'】');
                $edit=mysql_query("DELETE FROM {$table}online
WHERE qq='".$row['qq']."'");
                $msg=str_replace("#qq#",$row['qq'],$lang[system][checkqq][delete_error]);
                $msg=str_replace("#username#",$row['username'],$msg);
                $msg=str_replace("#br#","%0D%0A",$msg);
                $head="[{$row['qq']}]推送提醒";
                mysql_query("INSERT INTO {$table}query (id,user,message,qq,email,noticeqq,head) VALUES (NULL,'{$row['username']}','$msg','{$row['qq']}','{$mi['2']}','{$row['noticeqq']}',$head)");
                //sleep(3);
            } else {
                setlog('QQ【'.$row['qq'].'】执行时遇到未知参数，请提交给心挂Q开发人员。【rand：'.$rand.'】');
                $rand = $ptqq->Login();
                //mysql_query("INSERT INTO {$table}log (qq,log,time) VALUES ('{$row['qq']}','{$rand}','".date('Y-m-d')."')");
                if(strpos($rand,"帐号暂时无法登录") or strpos($rand,"sid已过期") or strpos($rand,"马上登录")){
                        $edit=mysql_query("DELETE FROM {$table}online
WHERE qq='".$row['qq']."'");
                        $msg=str_replace("#qq#",$row['qq'],$lang[system][checkqq][delete_error]);
                        $msg=str_replace("#username#",$row['username'],$msg);
                        $msg=str_replace("#br#","%0D%0A",$msg);
                        $head="[{$row['qq']}]推送提醒";
                        mysql_query("INSERT INTO {$table}query (id,user,message,qq,email,noticeqq,head) VALUES (NULL,'{$row['username']}','$msg','{$row['qq']}','{$mi['2']}','{$row['noticeqq']}',$head)");
                        //sleep(3);
                }
                //sleep(10);
            }
        }
    }
    elseif($row['status'] == '2'){
    $mi=uc_get_user($row['username']);
    //---------------ChangeStat_BUG-------------------
        $mqq = new SET_MQQ($row['qq'], $row['pass']);
        $rand = $mqq->ChangeStat_BUG($row['login_status']);
        $return = json_decode($rand, true);
        if ($return[status] == 'ok') {
                $time = date("Y-m-j H:i:s");
                $his =$mqq->GetMsg($row['reply']);
                $getmsg = $time.'%0D%0A'.$his.'%0D%0A'.$row['history'];
                if($his){
                    $edit=mysql_query("UPDATE ".IQQOL_SQLTABLE."online
 SET history='$getmsg' 
 WHERE qq = '".$row['qq']."'");
                }
        }
    //---------------ChangeStat_BUG-------------------
        else{
            $ptqq = new SET_3GQQ($row['qq']);
            $rand = $ptqq->Login();
            //mysql_query("INSERT INTO {$table}log (qq,log,time) VALUES ('{$row['qq']}','{$rand}','".date('Y-m-d')."')");
            if(strpos($rand,"帐号暂时无法登录") or strpos($rand,"sid已过期") or strpos($rand,"马上登录")){
                    $edit=mysql_query("DELETE FROM {$table}online
WHERE qq='".$row['qq']."'");
                    $msg=str_replace("#qq#",$row['qq'],$lang[system][checkqq][delete_error]);
                    $msg=str_replace("#username#",$row['username'],$msg);
                    $msg=str_replace("#br#","%0D%0A",$msg);
                    $head="[{$row['qq']}]推送提醒";
                    mysql_query("INSERT INTO {$table}query (id,user,message,qq,email,noticeqq,head) VALUES (NULL,'{$row['username']}','$msg','{$row['qq']}','{$mi['2']}','{$row['noticeqq']}',$head)");
                    //sleep(3);
            }
        }
    }
    continue;
}
echo '<br>OK!';
/*$count = $count-1;
$banfp = fopen('data/runcount.txt','w+');
fwrite($banfp,$count);
fclose($banfp);*/
die('<br>All completed!');
?>