<?php

namespace core_next;

class QQ
{
    static $appid;
    static $appkey;
    static $login_page_url;
    static $auth_url;

    static function init()
    {
        global $core_next_set;
        self::$appid = $core_next_set['other_login_qq_appid'];
        self::$appkey = $core_next_set['other_login_qq_appkey'];
        self::$login_page_url = $_GET['login_page_url'] ?? home_url();
        self::$auth_url = $core_next_set['other_login_auth_url'];
    }

    static function getAuthorizationCode()
    {
        session_start();
        $base_url = 'https://graph.qq.com/oauth2.0/authorize';
        $_SESSION['state'] = md5(uniqid(rand(), TRUE));
        $params = ['type' => 'login_return', 'auth_type' => 'qq', 'login_page_url' => self::$login_page_url];
        $redirect_uri = self::$auth_url . '?' . http_build_query($params);
        $params = array(
            "response_type" => "code",
            "client_id" => self::$appid,
            "redirect_uri" => $redirect_uri,
            "state" => $_SESSION['state'],
            "scope" => 'get_user_info'
        );
        $url = $base_url . '?' . http_build_query($params);
        // $url = Tools::buildUrl($base_url, $params);
        header("Location:$url");
    }

    static function loginReturn()
    {
        session_start();
        $code = $_GET['code'];
        $state = $_GET['state'];
        if ($state !== $_SESSION['state']) {
            die('登录失败，非法提交');
        }
        $open_id = self::getOpenID($code);
        //$open_id = '111';
        if ($open_id !== false && $open_id !== null) {
            $user_query = new \WP_User_Query(array('meta_key' => 'corepress_thirdparty_qq', 'meta_value' => $open_id));
            $users = $user_query->get_results();
            if (empty($users)) {
                $_SESSION['core_next_bind_type'] = 'qq';
                $_SESSION['core_next_open_id'] = $open_id;
                get_template_part('template/views/other-login');
            } else {
                wp_set_current_user($users[0]->ID, $users[0]->user_login);
                wp_set_auth_cookie($users[0]->ID, true);
                $login_page_url = $_GET['login_page_url'] ?? get_bloginfo('url');
                header("Location:" . $login_page_url);
            }
        } else {
            die('登录失败');
        }
    }

    static function getOpenID($code)
    {
        $base_url = 'https://graph.qq.com/oauth2.0/token';
        $params = ['type' => 'login_return', 'auth_type' => 'qq'];
        $redirect_uri = self::$auth_url . '?' . http_build_query($params);
        $params = array(
            "grant_type" => "authorization_code",
            "client_id" => self::$appid,
            "client_secret" => self::$appkey,
            "code" => $code,
            "redirect_uri" => $redirect_uri,
            "need_openid" => 1,
            'fmt' => 'json'
        );
        $url = $base_url . '?' . http_build_query($params);
        $result = wp_remote_get($url);
        $response = wp_remote_retrieve_body($result);
        $response = json_decode($response);
        if ($response) {
            return $response->openid;
        }
        return false;
    }
}