Author: zhujianting(zhujianting@baidu.com)
Ejemplo n.º 1
0
 /**
  * Get an instance of BaiduOAuth2 class.
  * 
  * @return BaiduOAuth2
  */
 public function getBaiduOAuth2Service()
 {
     if (!$this->oauth2) {
         $this->oauth2 = new BaiduOAuth2($this->clientId, $this->clientSecret);
         $this->oauth2->setRedirectUri($this->redirectUri);
     }
     return $this->oauth2;
 }
Ejemplo n.º 2
0
 /**
  * Constructor
  * 
  * @param string $clientId Client_id of the baidu thirdparty app or access_key of the developer.
  * @param string $accessToken Access token for api call.
  */
 public function __construct($clientId = '', $accessToken = '')
 {
     if ($accessToken == '') {
         $auth = new BaiduOAuth2();
         $token = $auth->getAccessTokenByAuthorizationCode($_GET['code']);
         $this->accessToken = $token['access_token'];
     } else {
         $this->accessToken = $accessToken;
     }
     if ($clientId == '') {
         $this->clientId = C('BaiduOpenid.clientId');
     } else {
         $this->clientId = $clientId;
     }
 }
Ejemplo n.º 3
0
 public function authorize()
 {
     global $_G, $_GET;
     if (empty($_G['uid'])) {
         dsetcookie('_refer', rawurlencode(BASESCRIPT . '?mod=connect&op=oauth&bz=baiduPCS'));
         showmessage('to_login', '', array(), array('showmsg' => true, 'login' => 1));
     }
     require_once DZZ_ROOT . './core/api/BaiduPCS/BaiduOAuth2.php';
     $cloud = DB::fetch_first("select `key` , `secret` from " . DB::table('connect') . " where bz='baiduPCS'");
     $auth = new BaiduOAuth2($cloud['key'], $cloud['secret']);
     $auth->setRedirectUri($_G['siteurl'] . 'index.php?mod=connect&op=oauth&bz=baiduPCS');
     if ($_GET['code'] && (($state = authcode($_GET['state'], 'DECODE')) == $cloud['key'] || $state == 'in_admin_' . $cloud['key']) && ($token = $auth->getAccessTokenByAuthorizationCode($_GET['code']))) {
         $token['refreshtime'] = TIMESTAMP;
         $token['uid'] = strpos($state, 'in_admin_') === 0 ? 0 : $_G['uid'];
         if ($token['access_token'] && ($userinfo = $auth->getLoggedInUser($token['access_token']))) {
             $token['cuid'] = $userinfo['uid'];
             $token['cusername'] = $userinfo['uname'];
             $token['portrait'] = $userinfo['portrait'];
         }
         if ($token['cuid']) {
             if ($id = DB::result_first("select id from " . DB::table(self::T) . " where uid='{$token[uid]}' and cuid='{$token[cuid]}' and bz='baiduPCS'")) {
                 DB::update(self::T, $token, "id ='{$id}'");
             } else {
                 $token['bz'] = 'baiduPCS';
                 $token['dateline'] = TIMESTAMP;
                 $id = DB::insert(self::T, $token, 1);
             }
             if (strpos($state, 'in_admin_') === 0) {
                 //插入企业盘空间库(local_storage);
                 $setarr = array('name' => '百度网盘:' . $token['cusername'], 'bz' => 'baiduPCS', 'isdefault' => 0, 'dname' => self::T, 'did' => $id, 'dateline' => TIMESTAMP);
                 if (!DB::result_first("select COUNT(*) from %t where did=%d and dname=%s", array('local_storage', $id, self::T))) {
                     C::t('local_storage')->insert($setarr);
                 }
             }
         }
         if (strpos($state, 'in_admin_') === 0) {
             $returnurl = 'admin.php?mod=cloud&op=space';
         } else {
             $returnurl = 'index.php?mod=connect';
         }
         header('Location: ' . $returnurl);
         //include template('oauth');
         exit;
     }
     $clientid = $cloud['key'];
     $state = authcode(defined('IN_ADMIN') ? 'in_admin_' . $clientid : $clientid, 'ENCODE');
     $authorizeurl = $auth->getAuthorizeUrl('code', 'basic netdisk', $state);
     //exit($authorizeurl);
     header('Location: ' . $authorizeurl);
 }
Ejemplo n.º 4
0
<?php

require_once './loginsdk/BaiduApiClient.php';
require_once './loginsdk/BaiduOAuth2.php';
require_once './loginsdk/BaiduUtils.php';
require_once './inc/lightapp_login_api.inc.php';
//回调页地址
$redirectUri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$code = $_GET['code'];
//echo $code;
//echo $redirectUri;
$oauth = new BaiduOAuth2($lightapp_api_key, $ligthapp_secret_key);
$oauth->setRedirectUri($redirectUri);
$tokenArr = $oauth->getAccessTokenByAuthorizationCode($code);
if (is_array($tokenArr)) {
    // 换取token成功
    $accessToken = $tokenArr['access_token'];
    $expires_in = $tokenArr['expires_in'];
    // 获取用户信息
    $client = new BaiduApiClient($lightapp_api_key, $accessToken);
    $infoArr = $client->api('/rest/2.0/passport/users/getInfo', array('fields' => 'userid,username,portrait'));
    if (is_array($infoArr)) {
        // 获取用户信息成功
        // 在这里将百度账号与应用自身的账号系统做联合登录处理,建议采取将百度账号暗绑到自身账号体系上
        // 然后将联合登录后生成的用户session的相关信息通过cookie返回到前端页面上
        // 为方便处理,这里将access_token和百度用户uid直接当session信息塞入cookie
        setcookie('bd_access_token', $accessToken, strtotime('2030-1-1 12:00:00'), '/');
        setcookie('bd_username', $infoArr['username'], strtotime('2030-1-1 12:00:00'), '/');
        setcookie('bd_uid', $infoArr['userid'], strtotime('2030-1-1 12:00:00'), '/');
        setcookie('bd_portrait', $infoArr['portrait'], strtotime('2030-1-1 12:00:00'), '/');
    }
 * 计划任务脚本 每周获取检查百度网盘的token,过期时间小于1周的全部刷新
 * @copyright   Leyun internet Technology(Shanghai)Co.,Ltd
 * @license     http://www.dzzoffice.com/licenses/license.txt
 * @package     DzzOffice
 * @link        http://www.dzzoffice.com
 * @author      zyx(zyx@dzz.cc)
 */
if (!defined('IN_DZZ')) {
    exit('Access Denied');
}
set_time_limit(0);
$week = 7 * 24 * 60 * 60;
require_once DZZ_ROOT . './core/api/BaiduPCS/BaiduOAuth2.php';
if (!($cloud = DB::fetch_first("select `key` , `secret` from " . DB::table('connect') . " where bz='baiduPCS'"))) {
    exit;
}
if (!isset($cloud['key']) || !isset($cloud['secret'])) {
    exit;
}
$auth = new BaiduOAuth2($cloud['key'], $cloud['secret']);
$list = DB::fetch_all("select * from " . DB::table('connect_pan') . " where bz='baiduPCS' and cuid!='1731268900'");
foreach ($list as $value) {
    if ($value['refreshtime'] + $value['expires_in'] - TIMESTAMP < $week) {
        if ($token = $auth->getAccessTokenByRefreshToken($value['refresh_token'], $value['scope'])) {
            $token['refreshtime'] = TIMESTAMP;
            if ($token['access_token']) {
                C::t('connect_pan')->update($value['id'], $token);
            }
        }
    }
}