<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Qqclass {

//登陆函数
    function qq_login($state, $appid, $scope, $callback) {

        $login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101145266&redirect_uri=http://www.lloonn.cn/oauth/qqcallback/"
//. "&state=" . $state
                . "&scope=get_user_info";

//      echo $login_url;
        header("Location:$login_url");
    }

//登陆返回函数
    function qq_callback($inputs, $appid, $appkey, $callback, $code) {
//debug
//print_r($inputs);
//print_r($qqlogin);
//var_dump($inputs);
//csrf
        $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
                . "client_id=101145266&redirect_uri=http://www.lloonn.cn/oauth/qqcallback/"
                . "&client_secret=a7ff90cb2e5def5c4655f133dcb39334&code=" . $code;

        $response =$this->get_contents($token_url);
        
     //   echo $response;
        if (strpos($response, "callback") !== false) {
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response = substr($response, $lpos + 1, $rpos - $lpos - 1);
            $msg = json_decode($response);
            if (isset($msg->error)) {
//echo "<h3>error:</h3>" . $msg->error;
//echo "<h3>msg  :</h3>" . $msg->error_description;
              //  exit;
            }
        }

        $params = array();
        parse_str($response, $params);

//debug
//print_r($params);
//set access token to session
        return $params["access_token"];
        /* else 
          {
          echo("The state does not match. You may be a victim of CSRF.");
          } */
    }

    function get_openid($inputs, $access_token) {
        $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=" . $access_token;

        $str = $this->get_contents($graph_url);
        print_r($graph_url);
        if (strpos($str, "callback") !== false) {
            $lpos = strpos($str, "(");
            $rpos = strrpos($str, ")");
            $str = substr($str, $lpos + 1, $rpos - $lpos - 1);
        }

        $user = json_decode($str);
        if (isset($user->error)) {
//echo "<h3>error:</h3>" . $user->error;
//echo "<h3>msg  :</h3>" . $user->error_description;
            exit;
        }

//debug
//echo("Hello " . $user->openid);
//set openid to session
        return $user->openid;
//var_dump();
    }

//获取用户信息
    function get_user_info($access_token, $appid, $open_id) {
        $get_user_info = "https://graph.qq.com/user/get_user_info?"
                . "access_token=" . $access_token
                . "&oauth_consumer_key=101145266"
                . "&openid=" . $open_id
                . "&format=json";

        $info = $this->get_contents($get_user_info);
        $arr = json_decode($info, true);

        return $arr;
    }

    /**
     * get_contents
     * 服务器通过get请求获得内容
     * @param string $url       请求的url,拼接后的
     * @return string           请求返回的内容
     */
    function get_contents($url) {

        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_URL, $url);
            $response = curl_exec($ch);
            curl_close($ch);
            return $response;
        } catch (Exception $e) {
            print $e->getMessage();
            exit();
        }
    }

}

/* End of file Qqloginclass.php */