<?php

/* ========================================================================
 * $Id: login.qq.php 2808 2017-07-21 09:50:16Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
#名称：QQ登录
#标识：login.qq

class onezphp_login_qq extends onezphp{
  function __construct(){
    global $G;
    
  }
  function mobile_callback(){
    global $G;
    $G['is_mobile']=1;
    $ptoken=onez()->gp('ptoken');
    $this->link($ptoken,false);
  }
  function link($ptoken,$proxy=false,$goto=false){
    global $G;
    $sitetoken=onez()->gp('sitetoken');
    if($sitetoken){
      $G['this']=onez($sitetoken);
      if(defined('IS_ONEZ_WWW')){
        $G['this']->data_alias=$G['this']->cname;
      }
    }
    $_login_qq=(int)onez()->gp('_login_qq');
    if($_login_qq){
      ob_clean();
      require_once(dirname(__FILE__).'/lib/qqConnectAPI.php');
      $qc = new QC();
      $token=$qc->qq_callback();
      $openid=$qc->get_openid();
      $QC = new QC($token,$openid);
      $arr = $QC->get_user_info();
      $r=onez('login.third')->init(array(
        'ptoken'=>$ptoken,
        'third_name'=>'qq',
        'third_token'=>$openid,
        'nickname'=>$arr['nickname'],
        'avatar'=>$arr['figureurl_qq_2'],
        'avatar'=>$arr['figureurl_2'],//空间
        'sex'=>$arr['gender'],
        'prov'=>$arr['province'],
        'city'=>$arr['city'],
      ));
      if($G['is_mobile']){
        onez($ptoken)->third_login_callback($r,$G['login.third'],onez()->cururl(false,array('_login_qq','code','goto')));
        onez()->location('?_m=m');
      }
      onez()->location(onez($ptoken)->third_login_callback($r,$G['login.third'],onez()->cururl(false,array('_login_qq','code','goto'))));
      exit();
    }
    $link='?_p='.$this->token.'&_m=login&sitetoken='.$G['this']->token;
    if(!$goto){
      $goto=onez()->homepage().onez()->cururl();
      if(strpos($goto,'?')===false){
        $goto.='?';
      }
      $goto.='&ptoken='.$ptoken.'&sitetoken='.$G['this']->token;
    }
    if($proxy!==false){
      $link=$proxy.$link.'&goto='.urlencode($goto);
    }else{
      $link.='&goto='.urlencode($goto);
    }
    return $link;
  }
  function login(){
    require_once(dirname(__FILE__).'/lib/qqConnectAPI.php');
    $qc = new QC();
    $code=onez()->gp('code');
    if($code){
      /*
      $token=$qc->qq_callback();
      $openid=$qc->get_openid();
      
      $QC = new QC($token,$openid);
      $arr = $QC->get_user_info();
      */
      $goto=onez('scookie')->call('goto');
      if($goto){
        #onez('scookie')->call('goto','');
        if(strpos($goto,'&_m=m')!==false){
          $goto=str_replace('&_m=m','',$goto);
          $goto.='&_p='.$this->token.'&_m=mobile_callback';
        }
        onez()->location($goto.'&_login_qq=1&code='.$code);
      }
    }else{
      $goto=onez()->gp('goto');
      if($goto){
        onez('scookie')->call('goto',$goto);
      }
      $qc->qq_login();
    }
  }
}