<?php

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

class Oauth extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('myclass');
        $this->load->model('user_m');
        $this->load->library('qqclass');
    }

    /**
     * 登陆页面
     */
    public function index() {
        $this->load->view('qqlogin');
    }

    /**
     * 点击QQ登陆页面操作
     */
    public function qqlogin() {
        $qq_state = md5(uniqid(rand(), TRUE)); //CSRF protection
        $this->qqclass->qq_login($qq_state, $this->config->item("qq_appid"), $this->config->item("qq_scope"), site_url($this->config->item("qq_callback"))); //用户点击qq登录按钮调用此函数	
    }

    /**
     * QQ登陆返回到本网站
     */
    public function qqcallback() {
        $this->load->helper('form');
        $inputs = 1;
        $code = $this->input->get('code');
        $access_token = $this->qqclass->qq_callback($inputs, $this->config->item("qq_appid"), $this->config->item("qq_appkey"), site_url($this->config->item("qq_callback")), $code); //QQ登录成功后的回调地址,主要保存access token
        $open_id = $this->qqclass->get_openid($inputs, $access_token); //获取用户标示id
        //获取用户基本资料
        echo $access_token;
        echo $open_id;
        $arr['user'] = $this->qqclass->get_user_info($access_token, $this->config->item("qq_appid"), $open_id);
        print_r($arr['user']);
        $arr['user']['username'] = $arr['user']['nickname'];
        $arr['user']['openid'] = $open_id;

        if ($user) {
            $this->session->set_userdata(array('uid' => $user['uid'], 'username' => $user['username'], 'password' => $user['password'], 'group_type' => $user['group_type'], 'gid' => $user['gid']));
            redirect('/');
        }
        if (empty($open_id)) {
            $this->myclass->notice('alert("QQ一键登录授权失败，请采用普通方式注册和登录！");"";');
        } else {
            $qq_userid = $open_id;
            $this->session->set_userdata('qq_userid', $qq_userid);
            $sql = "select * from users where qq_userid = '" . $qq_userid . "'";
            $user = $this->db->query($sql)->row();
            if ($user) {
                $this->session->set_userdata('current_user', $user);
                redirect('/');
            } else {
                redirect('/user/register');
            }
            $this->load->view('qqcallback', $arr);
        }
    }

    private function check_username($username) {
        $check_username = $this->db->select('username')->from('users')->where('username', $username)->get()->row_array();
        if ($check_username) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('username', '昵称', 'callback_check_username');
            $this->form_validation->set_message('check_username', 'The %s 已经存在');
            return true;
        } else {
            return false;
        }
    }

    private function validate_qqlogin_form() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[3]|max_length[50]|valid_email');
        $this->form_validation->set_rules('username', '昵称', 'trim|required|min_length[2]|max_length[20]|xss_clean|callback_check_username');
        $this->form_validation->set_message('required', "%s 不能为空！");
        $this->form_validation->set_message('min_length', "%s 最小长度不少于 %s 个字符！");
        $this->form_validation->set_message('max_length', "%s 最大长度不多于 %s 个字符！");
        $this->form_validation->set_message('valid_email', "邮箱格式不对");
        $this->form_validation->set_message('alpha_dash', "邮箱格式不对");
        if ($this->form_validation->run() == FALSE) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

}

/* End of file oauth.php */
/* Location: ./application/controllers/oauth.php */