<?php
if (isset($_COOKIE['login_auth'])) {
    list($User_uid, $User_username) = explode("\t", uc_authcode($_COOKIE['login_auth'], 'DECODE'));
    if ($_REQUEST['a'] == 'add' and $_REQUEST['qq'] != '' and $_REQUEST['password'] != '') {
        if ($_GET['type'] == 1 or !isset($_GET['type'])) {
            $qqno = $_REQUEST['qq'];
            $qqpw = $_REQUEST['password'];
            $noticeqq = $_REQUEST['noticeqq'];
            $runtime=$_REQUEST['time1'].'-'.$_REQUEST['time2'];
            if ($qqno == "" or $qqpw == "") {
                $style->assign("state", $lang[system][addqq][noqqorpass]);
                $style->display('main/qqinsert.html');
                exit;
            }
            if($_REQUEST['time1']>=$_REQUEST['time2']){
                $style->assign("state", $lang[system][addqq][timeerror]);
                $style->display('main/qqinsert.html');
                exit;
            }
            if ($noticeqq == "") {
                $style->assign("state", $lang[system][addqq][nonoticeqq]);
                $style->display('main/qqinsert.html');
                exit;
            } elseif (!preg_match("/^[0-9]\d{5,11}$/", $_REQUEST["qq"])) {
                $style->assign("state", $lang[system][addqq][qqerror]);
                $style->display('main/qqinsert.html');
                exit;
            }
            $qqresult = mysql_query("SELECT * FROM {$table}online
WHERE qq='$qqno'");
            $nowqqnum = mysql_num_rows(mysql_query("SELECT * FROM {$table}online
WHERE username='$User_username'"));
            $maxqqnum = mysql_fetch_array(mysql_query("SELECT * FROM {$table}members
WHERE user='$User_username'"));
            $qnum = mysql_num_rows($qqresult);
            if ($qnum != 0) {
                $style->assign("state", $lang[system][addqq][already_have]);
                $style->display('main/qqinsert.html');
                exit();
            }elseif($nowqqnum == $maxqqnum[largest]){
                $style->assign("state", $lang[system][addqq][largest_notallow]);
                $style->display('main/qqinsert.html');
                exit();
            } else {
                $result=qq_login($qqno, $qqpw);
                preg_match_all("/sid=([^&=?]*)/", $result, $regs);
                $sid = $regs[1][0]; //sid
                $find = $qqno;
                $find2 = $lang[system][addqq][passworderror];
                $find3 = $lang[system][addqq][noitsqq];
                $find5 = $lang[system][addqq][banlogin];
                if (strpos($result, $find) == true) {
                    preg_match_all("/<postfield name=\"([^\"]+)\" value=\"([^\"]+)\"\/>/", $result, $qqshuzu);
                    preg_match_all("/[a-zA-z]+:\/\/[^\s\"]*/", $result, $a_yzm);
                    $state = '<img src="' . $a_yzm[0][1] . '" alt="verifycode"/>';
                    $state.= '<form method="post" action="in.php?action=addqq&a=add&type=2">';
                    $state.= $lang[system][addqq][form][pl_input_verifycode];
                    $state.= $lang[system][addqq][form][input_verifycode];
                    $state.= '<input type="hidden" name="auto" value=' . $qqshuzu[2][4] . '>';
                    $state.= '<input type="hidden" name="bid" value=' . $qqshuzu[2][30] . '>';
                    $state.= '<input type="hidden" name="bid_code" value="3GQQ">';
                    $state.= '<input type="hidden" name="noticeqq" value="' . $noticeqq . '">';
                    $state.= '<input type="hidden" name="login_status" value="' . $_REQUEST[login_status] . '">';
                    $state.= '<input type="hidden" name="reply" value="' . $_REQUEST[reply] . '">';
                    $state.= '<input type="hidden" name="runtime" value="' . $runtime . '">';
                    $state.= '<input type="hidden" name="password" value="' . $_REQUEST[password] . '">';
                    $state.= '<input type="hidden" name="extend" value=' . $qqshuzu[2][11] . '>';
                    $state.= '<input type="hidden" name="go_url" value="    http://info.3g.qq.com/g/s?aid=index&from=IorA&sid=00&rand=857442">';
                    $state.= '<input type="hidden" name="hexp" value=' . $qqshuzu[2][3] . '>';
                    $state.= '<input type="hidden" name="hexpwd" value=' . $qqshuzu[2][2] . '>';
                    $state.= '<input type="hidden" name="i_p_w" value="imgType|verify|">';
                    $state.= '<input type="hidden" name="imgType" value=' . $qqshuzu[2][10] . '>';
                    $state.= '<input type="hidden" name="loginTitle" value=' . $qqshuzu[2][22] . '>';
                    $state.= '<input type="hidden" name="loginType" value=' . $qqshuzu[2][9] . '>';
                    $state.= '<input type="hidden" name="login_url" value=' . $qqshuzu[2][14] . '>';
                    $state.= '<input type="hidden" name="modifySKey" value=' . $qqshuzu[2][6] . '>';
                    $state.= '<input type="hidden" name="q_from" value="">';
                    $state.= '<input type="hidden" name="q_status" value=' . $qqshuzu[2][24] . '>';
                    $state.= '<input type="hidden" name="qq" value=' . $qqshuzu[2][16] . '>';
                    $state.= '<input type="hidden" name="r" value=' . $qqshuzu[2][8] . '>';
                    $state.= '<input type="hidden" name="r_sid" value=' . $qqshuzu[2][12] . '>';
                    $state.= '<input type="hidden" name="rip" value=' . $qqshuzu[2][15] . '>';
                    $state.= '<input type="hidden" name="sid" value=' . $qqshuzu[2][19] . '>';
                    $state.= '<input type="hidden" name="toQQchat" value="true">';
                    $state.= '<input type="hidden" name="u_token" value=' . $qqshuzu[2][17] . '>';
                    $state.= $lang[system][addqq][form][input_verifycode_submit];
                    $state.= '</form><br><br><br><br>';
                    $style->assign("state", $state);
                    $style->display('main/qqinsert.html');
                    exit();
                } elseif (strpos($result, $find5) == true) {
                    $state = $find5 . $lang[system][addqq][cantgetsid];
                    $style->assign("state", $state);
                    $style->display('main/qqinsert.html');
                } elseif (strpos($result, $find2) == true) {
                    $state = $find2 . $lang[system][addqq][cantgetsid];
                    $style->assign("state", $state);
                    $style->display('main/qqinsert.html');
                } elseif (strpos($result, $find3) == true) {
                    $state = $find3 . $lang[system][addqq][cantgetsid];
                    $style->assign("state", $state);
                    $style->display('main/qqinsert.html');
                } else if (!isset($qqno)) {
                    $style->assign("state", $lang[system][addqq][return_input_qq]);
                    $style->display('main/qqinsert.html');
                    exit();
                } else if (!isset($sid)) {
                    $style->assign("state", $lang[system][addqq][return_input_qpass]);
                    $style->display('main/qqinsert.html');
                    exit();
                } else {
                    @mysql_query($con, "set names 'utf8'");
                    $qpassword = md5($qqpw);
                    $mqq = new SET_MQQ($qqno, $qpassword);
                    $rand = json_decode($mqq->Login(),true);
                    if ($rand[status] != 'ok') {
                        if ($rand[status] == 'vc') {
                            $filename = 'libs/Cache/'.md5($rand['res']) . '.png';
                            $fp = fopen($filename, 'w');
                            fwrite($fp, $mqq->HexToText($rand['res']));
                            fclose($fp);
                            $state = '<form method="post" action="in.php?action=addqq&a=add&type=3">';
                            $state.= $lang[system][addqq][form][sjpl_input_verifycode];
                            $state.= '<img src="' . $filename . '">';
                            $state.= $lang[system][addqq][form][sjinput_verifycode];
                            $state.= '<input type="hidden" name="qq" value="' . $qqno . '">';
                            $state.= '<input type="hidden" name="sid" value="' . $sid . '">';
                            $state.= '<input type="hidden" name="password" value="' . $qqpw . '">';
                            $state.= '<input type="hidden" name="login_status" value="' . $_REQUEST[login_status] . '">';
                            $state.= '<input type="hidden" name="reply" value="' . $_REQUEST[reply] . '">';
                            $state.= '<input type="hidden" name="noticeqq" value="' . $noticeqq . '">';
                            $state.= '<input type="hidden" name="runtime" value="' . $runtime . '">';
                            $state.= $lang[system][addqq][form][input_verifycode_submit];
                            $state.= '</form"> ';
                        }
                            else {
                                $state = $lang[system][addqq]['false'];
                            }
                        $style->display('main/qqinsert.html');
                        exit();
                    } else {
                        add_qq($qqno, $sid, $qqpw, $_REQUEST[login_status], $_REQUEST[reply], $noticeqq, $runtime);
                        echo mysql_error();
                    }
                }
            }
        } elseif ($_GET['type'] == 2) {
            if (@$_GET['type'] == 2) {
                $noticeqq = $_POST['noticeqq'];
                $auto = $_POST['auto'];
                $bid = $_POST['bid'];
                $bid_code = $_POST['bid_code'];
                $extend = $_POST['extend'];
                $go_url = $_POST['go_url'];
                $hexp = $_POST['hexp'];
                $hexpwd = $_POST['hexpwd'];
                $i_p_w = $_POST['i_p_w'];
                $imgType = $_POST['imgType'];
                $loginTitle = $_POST['loginTitle'];
                $loginType = $_POST['loginType'];
                $login_url = $_POST['login_url'];
                $modifySKey = $_POST['modifySKey'];
                $q_from = $_POST['q_from'];
                $q_status = $_POST['q_status'];
                $qq = $_POST['qq'];
                $r = $_POST['r'];
                $r_sid = $_POST['r_sid'];
                $rip = $_POST['rip'];
                $sid = $_POST['sid'];
                $toQQchat = $_POST['toQQchat'];
                $u_token = $_POST['u_token'];
                $verify = $_POST['verify'];
                if ($verify == "") {
                    echo $lang[system][addqq][form][pl_input_verifycode];
                } else {
                    $cookie = dirname(__FILE__) . '/cookie.txt';
                    $post = array(
                        'auto' => $auto,
                        'bid' => $bid,
                        'bid_code' => '3GQQ',
                        'extend' => $extend,
                        'go_url' => $go_url,
                        'hexp' => $hexp,
                        'hexpwd' => $hexpwd,
                        'i_p_w' => 'imgType|verify|',
                        'imgType' => 'gif',
                        'loginTitle' => '手机腾讯网',
                        'loginType' => $loginType,
                        'login_url' => $login_url,
                        'modifySKey' => $modifySKey,
                        'q_from' => $q_from,
                        'q_status' => $q_status,
                        'qq' => $qq,
                        'r' => $r,
                        'r_sid' => $r_sid,
                        'rip' => $rip,
                        'sid' => $sid,
                        'toQQchat' => $toQQchat,
                        'u_token' => $u_token,
                        'verify' => $verify,
                    );
                    $curl = curl_init('http://pt5.3g.qq.com/psw3gqqLogin?sid=AepMDkt5vrXH64LvijQHTfWd');
                    curl_setopt($curl, CURLOPT_HEADER, 0);
                    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
                    curl_setopt($curl, CURLOPT_POST, 1);
                    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
                    $result = curl_exec($curl);
                    curl_close($curl);
                    $find4 = $lang[system][addqq][passworderror];
                    $find6 = $lang[system][addqq][verifycodeerror];
                    if (strpos($result, $find4) == true) {
                        $style->assign("state", $find4 . $lang[system][addqq][cantgetsid]);
                        $style->display('main/qqinsert.html');
                    } elseif (strpos($result, $find6) == true) {
                        $style->assign("state", $find6 . $lang[system][addqq][cantgetsid]);
                        $style->display('main/qqinsert.html');
                    } else {
                        preg_match_all("/sid=([^&=?]*)/", $result, $regs);
                        $sid = $regs[1][0];
                        $qpassword = md5($_REQUEST[password]);
                        $qqno = $_REQUEST[qq];
                        $mqq = new SET_MQQ($qqno, $qpassword);
                        $rand = json_decode($mqq->Login(),true);
                        if ($rand[status] != 'ok') {
                            if ($rand[status] == 'vc') {
                                $filename = 'libs/Cache/'.md5($rand['res']) . '.png';
                                $fp = fopen($filename, 'w');
                                fwrite($fp, $mqq->HexToText($rand['res']));
                                fclose($fp);
                                $state = '<form method="post" action="in.php?action=addqq&a=add&type=3">';
                                $state.= $lang[system][addqq][form][sjpl_input_verifycode];
                                $state.= '<img src="' . $filename . '">';
                                $state.= $lang[system][addqq][form][sjinput_verifycode];
                                $state.= '<input type="hidden" name="qq" value="' . $qqno . '">';
                                $state.= '<input type="hidden" name="sid" value="' . $sid . '">';
                                $state.= '<input type="hidden" name="password" value="' . $_REQUEST[password]. '">';
                                $state.= '<input type="hidden" name="login_status" value="' . $_REQUEST[login_status] . '">';
                                $state.= '<input type="hidden" name="reply" value="' . $_REQUEST[reply] . '">';
                                $state.= '<input type="hidden" name="noticeqq" value="' . $noticeqq . '">';
                                $state.= '<input type="hidden" name="runtime" value="' . $_REQUEST[runtime] . '">';
                                $state.= $lang[system][addqq][form][input_verifycode_submit];
                                $state.= '</form"> ';
                            }
                            else {
                                $state = $lang[system][addqq]['false'];
                            }
                            $style->display('main/qqinsert.html');
                            exit();
                        } else {
                            add_qq($qqno, $sid, $_REQUEST[password], $_REQUEST[login_status], $_REQUEST[reply], $noticeqq, $_REQUEST[runtime]);
                            echo mysql_error();
                        }
                    }
                }
            }
        } elseif ($_GET['type'] == 3) {
            $qq = $_POST['qq'];
            $sid = $_POST['sid'];
            $password = $_POST['password'];
            $login_status = $_POST['login_status'];
            $reply = $_POST['reply'];
            $noticeqq = $_POST['noticeqq'];
            $runtime = $_POST['runtime'];
            $verifycode = $_POST['verifycode'];
            $mqq = new SET_MQQ($_REQUEST['qq'], md5($_REQUEST['password']));
            $rand = json_decode($mqq->Verifycode($_REQUEST['verifycode']) , true);
            if ($rand[status] != 'ok') {
                if ($rand[status] == 'vc') {
                    $filename = 'libs/Cache/'.md5($rand['res']) . '.png';
                    $fp = fopen($filename, 'w');
                    fwrite($fp, $mqq->HexToText($rand['res']));
                    fclose($fp);
                    $state = '<form method="post" action="in.php?action=addqq&a=add&type=3">';
                    $state.= $lang[system][addqq][form][sjpl_input_verifycode];
                    $state.= '<img src="' . $filename . '">';
                    $state.= $lang[system][addqq][form][sjinput_verifycode];
                    $state.= '<input type="hidden" name="qq" value="' . $qq . '">';
                    $state.= '<input type="hidden" name="sid" value="' . $_POST['sid'] . '">';
                    $state.= '<input type="hidden" name="password" value="' . $_REQUEST[password] . '">';
                    $state.= '<input type="hidden" name="login_status" value="' . $_REQUEST[login_status] . '">';
                    $state.= '<input type="hidden" name="reply" value="' . $_REQUEST[reply] . '">';
                    $state.= '<input type="hidden" name="noticeqq" value="' . $noticeqq . '">';
                    $state.= '<input type="hidden" name="runtime" value="' . $_REQUEST[runtime] . '">';
                    $state.= $lang[system][addqq][form][input_verifycode_submit];
                    $state.= '</form"> ';
                }
                            else {
                                $state = $lang[system][addqq]['false'];
                            }
                $style->display('main/qqinsert.html');
                exit();
            } else {
                add_qq($qq, $_POST['sid'], $password, $_REQUEST[login_status], $_REQUEST[reply], $noticeqq, $_REQUEST[runtime]);
                echo mysql_error();
            }
        }
    } else {
        $state = $lang[system][addqq][pl_input];
        $style->display('main/qqinsert.html');
    }
} else {
    $style->display('login.html');
}
?>