授权机制说明请大家参考微博开放平台文档:{@link http://open.weibo.com/wiki/Oauth2}
Author: Elmer Zhang
Example #1
0
 private function reflashToken()
 {
     if (empty($this->m_cfg['username']) || empty($this->m_cfg['password'])) {
         return;
     }
     $this->m_reflash_cookie = tmpDir('reflashsina.cookie');
     if (!file_exists($this->m_reflash_cookie)) {
         touch($this->m_reflash_cookie);
     }
     $loginResult = $this->curlLoginSina($this->m_cfg['username'], $this->m_cfg['password']);
     if (!$loginResult) {
         return $loginResult;
     }
     $callbackUrl = callbackUrl('sina');
     $o = new SaeTOAuthV2($this->m_cfg['key'], $this->m_cfg['secret']);
     $authorizeURL = $o->getAuthorizeURL($callbackUrl);
     $ch = curl_init($authorizeURL);
     $option = array();
     $option[CURLOPT_FOLLOWLOCATION] = 1;
     $option[CURLOPT_RETURNTRANSFER] = 1;
     $option[CURLOPT_COOKIEJAR] = $this->m_reflash_cookie;
     $option[CURLOPT_COOKIEFILE] = $this->m_reflash_cookie;
     $option[CURLOPT_HTTPHEADER] = array('Accept-Language: zh-cn', 'Connection: Keep-Alive', 'Cache-Control: no-cache');
     $option[CURLOPT_USERAGENT] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
     curl_setopt_array($ch, $option);
     curl_exec($ch);
     curl_close($ch);
     unlink($this->m_reflash_cookie);
 }
Example #2
0
 /**
  * Index Page for this controller.
  *
  * Maps to the following URL
  * 		http://example.com/index.php/welcome
  * 	- or -  
  * 		http://example.com/index.php/welcome/index
  * 	- or -
  * Since this controller is set as the default controller in 
  * config/routes.php, it's displayed at http://example.com/
  *
  * So any other public methods not prefixed with an underscore will
  * map to /index.php/welcome/<method_name>
  * @see http://codeigniter.com/user_guide/general/urls.html
  */
 public function index()
 {
     $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
     $code_url = $o->getAuthorizeURL(WB_CALLBACK_URL);
     $data['code_url'] = $code_url;
     $this->load->view('celebritytop/navigate_view', $data);
 }
Example #3
0
 public function weibocallback($code)
 {
     $cfg = K::$system->config->get('connect');
     if (defined('IN_MOBILE')) {
         $mobile = K::$system->config->get('mobile');
         $callback = $mobile['url'] . '/' . K::M('helper/link')->mklink('passport:weibocallback');
     } else {
         $site = K::$system->config->get('site');
         $callback = $site['siteurl'] . '/' . K::M('helper/link')->mklink('passport:weibocallback');
     }
     if (empty($cfg['weibo_is_open'])) {
         $this->err->add('很抱歉网站管理员还未开启微博登录功能', 201);
         return false;
     }
     $keys = array();
     $keys['code'] = $code;
     $keys['redirect_uri'] = $callback;
     $o = new SaeTOAuthV2($cfg['weibo_app_id'], $cfg['weibo_app_key']);
     try {
         $token = $o->getAccessToken('code', $keys);
     } catch (OAuthException $e) {
         $this->err->add($e->getMessage(), 201);
         return false;
     }
     $c = new SaeTClientV2($cfg['weibo_app_id'], $cfg['weibo_app_key'], $token['access_token']);
     $ms = $c->home_timeline();
     // done
     $uid_get = $c->get_uid();
     $uid = $uid_get['uid'];
     $user_message = $c->show_user_by_id($uid);
     //根据ID获取用户等基本信息
     return $this->login($uid, $user_message);
 }
Example #4
0
 function actionReturn()
 {
     $o = new \SaeTOAuthV2($this->app_key, $this->app_secret);
     if ($_REQUEST['code']) {
         $keys = array();
         $keys['code'] = $_REQUEST['code'];
         $keys['redirect_uri'] = $this->url;
         try {
             $token = $o->getAccessToken('code', $keys);
             $access_token = $token['access_token'];
             $c = new \SaeTClientV2($this->app_key, $this->app_secret, $access_token);
             $uid_get = $c->get_uid();
             $uid = $uid_get['uid'];
             $me = $c->show_user_by_id($uid);
             $me['name'] = $me['screen_name'];
             $me['options'] = array('url' => $me['profile_url']);
             $r = $this->member_get_third_set_user($me, $this->oauth_id, $access_token);
             flash('success', __('login success'));
             $this->redirect(return_url());
         } catch (OAuthException $e) {
             flash('error', __('login error'));
             $this->redirect(return_url());
         }
     }
     exit;
 }
Example #5
0
    public static function getWeiboUserInfo()
    {
        if (!self::$_config) {
            self::$_config = (require_once WEIBO_PATH . 'config/config.php');
        }
        $o = new SaeTOAuthV2(self::$_config['WB_AKEY'], self::$_config['WB_SKEY']);
        if (isset($_REQUEST['code'])) {
            $keys = array();
            $keys['code'] = $_REQUEST['code'];
            $keys['redirect_uri'] = self::$_config['WB_CALLBACK_URL'];
            try {
                $token = $o->getAccessToken('code', $keys);
            } catch (OAuthException $e) {
            }
        }
        if ($token) {
            $_SESSION['token'] = $token;
            setcookie('weibojs_' . $o->client_id, http_build_query($token), '/');
            //把新浪微博的用户信息存起来
            self::createWeiboUser();
            //跳回登录前的页面
            self::callbackLast();
            ?>
			<?php 
        } else {
            ?>
			授权失败。
			<?php 
        }
    }
Example #6
0
 /**
  * Index Page for this controller.
  *
  * Maps to the following URL
  * 		http://example.com/index.php/welcome
  * 	- or -  
  * 		http://example.com/index.php/welcome/index
  * 	- or -
  * Since this controller is set as the default controller in 
  * config/routes.php, it's displayed at http://example.com/
  *
  * So any other public methods not prefixed with an underscore will
  * map to /index.php/welcome/<method_name>
  * @see http://codeigniter.com/user_guide/general/urls.html
  */
 public function index()
 {
     $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY, null, null);
     if (isset($_REQUEST['code'])) {
         $keys = array();
         $keys['code'] = $_REQUEST['code'];
         $keys['redirect_uri'] = WB_CALLBACK_URL;
         try {
             $token = $o->getAccessToken('code', $keys);
             $data['token'] = $token;
         } catch (OAuthException $e) {
             echo $e;
         }
     }
     if (isset($token)) {
         $this->session->set_userdata($token);
         //设定Session,将$token写入session
         $this->input->set_cookie('weibojs_' . $o->client_id, http_build_query($token));
         //设定cookie
         $data['flag'] = 'Y';
     } else {
         $data['flag'] = 'N';
     }
     $data['o'] = $o;
     $this->load->view('celebritytop/callback_view', $data);
 }
 /**
  * sina绑定
  */
 function bindSina()
 {
     $code = $this->trimmed('code');
     if (empty($code)) {
         $this->clientError('cannot find sina code, oauth failed', $code);
         exit;
     }
     $keys = array();
     $keys['code'] = $code;
     $keys['redirect_uri'] = WB_CALLBACK_URL;
     try {
         $sinaOauth = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
         $token = $sinaOauth->getAccessToken('code', $keys);
     } catch (OAuthException $e) {
         $this->clientError("oauth failed {$e}", 400);
         exit;
     }
     $url = 'https://api.weibo.com/2/users/show.json?' . http_build_query(array('access_token' => $token['access_token'], 'uid' => $token['uid']));
     $user = json_decode(file_get_contents($url));
     if (array_key_exists("error", $user)) {
         $this->clientError($user, 400);
     }
     $userOption = array('via' => 'weibo', 'uid' => $user->id, 'screen_name' => $user->screen_name, 'name' => $user->name, 'location' => $user->location, 'description' => $user->description, 'image' => $user->profile_image_url, 'access_token' => $token->access_token, 'expire_at' => $token->expires, 'refresh_token' => $token->refresh_token);
     $this->bind_common($user->id, User::PLATFORM_TYPE_SINA, $userOption);
 }
Example #8
0
 function callback()
 {
     $o = new SaeTOAuthV2(c('weibo_akey'), c('weibo_skey'));
     if (isset($_REQUEST['code'])) {
         $keys = array();
         $keys['code'] = $_REQUEST['code'];
         $keys['redirect_uri'] = 'http://' . c('site_domain') . '/?c=weibo&a=callback';
         try {
             $token = $o->getAccessToken('code', $keys);
         } catch (OAuthException $e) {
         }
         $_SESSION['weibo_token'] = $token;
         // get user info
         $c = new SaeTClientV2(c('weibo_akey'), c('weibo_skey'), atoken());
         $info = $c->show_user_by_id(wbuid());
         if (strlen($info['name']) < 1) {
             return info_page('登入失败,请去吃点零食后重试');
         }
         $_SESSION['weibo_uid'] = $info['name'];
         $_SESSION['uname'] = $info['name'];
         $_SESSION['avatar'] = $info['profile_image_url'];
         //print_r( $_SESSION );
         header("Location: /?a=index");
     }
 }
Example #9
0
 /**
  * 授权页
  */
 public function actionCallback()
 {
     // weibo POST
     //从POST过来的signed_request中提取oauth2信息
     if (!empty($_REQUEST["signed_request"])) {
         $o = new SaeTOAuthV2(Yii::app()->params['WB_AKEY'], Yii::app()->params['WB_SKEY']);
         $data = $o->parseSignedRequest($_REQUEST["signed_request"]);
         if ($data == '-2') {
             die('签名错误!');
         } else {
             $_SESSION['oauth2'] = $data;
         }
     }
     //print_r($_SESSION['oauth2']);
     if (empty($_SESSION['oauth2']["user_id"])) {
         //若没有获取到access token,则发起授权请求
         $this->render('auth');
     } else {
         //若已获取到access token,则加载应用信息
         //print_r($_SESSION['oauth2']);
         $c = new SaeTClientV2(Yii::app()->params['WB_AKEY'], Yii::app()->params['WB_SKEY'], $_SESSION['oauth2']['oauth_token'], '');
         Yii::app()->session['api'] = $c;
         $this->redirect('/');
         //setcookie( 'weibojs_'.$o->client_id, http_build_query($_SESSION['oauth2']) );
     }
 }
Example #10
0
 public function sinalogin()
 {
     $loginconfig = FS("Webconfig/loginconfig");
     define("WB_AKEY", $loginconfig['sina']['akey']);
     define("WB_SKEY", $loginconfig['sina']['skey']);
     define("WB_CALLBACK_URL", C('WEB_URL') . __APP__ . '/member/oauth/sinalogin');
     require C("APP_ROOT") . "Lib/Oauth/sina/saetv2.ex.class.php";
     $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
     if (isset($_REQUEST['code'])) {
         $keys = array();
         $keys['code'] = $_REQUEST['code'];
         $keys['redirect_uri'] = WB_CALLBACK_URL;
         try {
             $token = $o->getAccessToken('code', $keys);
         } catch (OAuthException $e) {
         }
     }
     if ($token) {
         $_SESSION['token'] = $token;
     } else {
         exit("出错,请重试");
     }
     $map['openid'] = text($token['uid']);
     //唯一ID
     $map['site'] = 'sina';
     $this->appCk($map, "@sina" . $map['openid'], 'sina');
     //nickname
 }
Example #11
0
 public function index()
 {
     if (isset($this->request->get['message_id']) && $this->request->get['message_id']) {
         if (isset($this->request->get['message_id'])) {
             $message_id = $this->request->get['message_id'];
         } else {
             $message_id = '';
         }
         //判断用户是否登陆
         if ($this->customer->isLogged()) {
             $this->data['logged'] = 1;
         } else {
             $this->data['logged'] = 0;
             $this->data['error_login'] = "";
             $this->data['action'] = $this->url->link('account/login', '', 'SSL');
             $this->data['register'] = $this->url->link('account/register', '', 'SSL');
             $this->data['forgotten'] = $this->url->link('account/forgotten', '', 'SSL');
             $this->data['email'] = '';
             $this->data['password'] = '';
             include_once DIR_SYSTEM . 'weibo/config.php';
             include_once DIR_SYSTEM . 'weibo/saetv2.ex.class.php';
             $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
             $this->data['code_url'] = $o->getAuthorizeURL(WB_CALLBACK_URL);
         }
         $this->load->model('social/social');
         $message_info = $this->model_social_social->getMessageByid($message_id);
         if (isset($this->request->get['page'])) {
             $page = $this->request->get['page'];
             $this->data['page'] = $page;
         } else {
             $page = 1;
             $this->data['page'] = $page;
         }
         $limit = 20;
         $this->data['limit'] = $limit;
         $data = array('message_id' => $message_id, 'start' => ($page - 1) * $limit, 'limit' => $limit);
         $comment_info = $this->model_social_social->getComment($data);
         $comment_total = $this->model_social_social->getTotalComment($message_id);
         //回复的用户id
         $this->data['customer_id'] = $this->customer->getId();
         //回复的脸
         $this->data['face'] = $this->customer->getface();
         if (!$this->data['face']) {
             $this->data['face'] = "uploads/big/0b4a96400b2372d25da769647bfe4059.jpg";
         }
         $this->data['message'] = $message_info;
         $this->data['comment_info_all'] = $comment_info;
         $this->data['comment_total'] = $comment_total;
         $pagination = new Pagination();
         $pagination->total = $comment_total;
         $pagination->page = $page;
         $pagination->limit = $limit;
         $pagination->text = $this->language->get('text_pagination');
         $pagination->url = $this->url->link('social/comment', 'message_id=' . $message_id . '&page={page}', 'SSL');
         $this->data['pagination'] = $pagination->render();
         $this->template = $this->config->get('config_template') . '/template/social/comment_list.tpl';
         $this->children = array('common/footer', 'common/social_right', 'common/header_sns');
         $this->response->setOutput($this->render());
     }
 }
Example #12
0
function WB_callback()
{
    $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
    if (isset($_REQUEST['code'])) {
        $keys = array();
        $keys['code'] = $_REQUEST['code'];
        //                $login_type = getvaluebykey('login_type');
        //                $userid = getvaluebykey('userid');
        //                print_r($userid);exit;
        //                $headpic = getvaluebykey('headpic');
        //                $emailnum  = getvaluebykey('emailnum');
        $keys['redirect_uri'] = WB_CALLBACK_URL;
        //.'?login_type='.$login_type.'_'.$userid.'_'.$headpic.'_'.$emailnum;
        try {
            $token = $o->getAccessToken('code', $keys);
            //print_r($token);
        } catch (OAuthException $e) {
        }
    }
    if ($token) {
        $_SESSION['token'] = $token;
        $c1 = new SaeTClientV2(WB_AKEY, WB_SKEY, $token['access_token']);
        $userinfo = $c1->show_user_by_id($token[uid]);
        setcookie('weibojs_' . $o->client_id, http_build_query($token));
        $cb_arr = array('access_token' => $token['access_token'], 'openid' => $token[uid], 'nick' => $userinfo['name']);
        return $cb_arr;
    }
}
Example #13
0
 function get_info()
 {
     if ($this->dx_auth->is_logged_in()) {
         echo "你已经登陆了";
     } else {
         $this->load->model('m_open');
         session_start();
         require_once APPPATH . 'libraries/weibo/config.php';
         require_once APPPATH . 'libraries/weibo/saetv2.ex.class.php';
         $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
         $o->set_debug(DEBUG_MODE);
         if (isset($_REQUEST['code'])) {
             $keys = array();
             // 验证state
             $state = $_REQUEST['state'];
             if (empty($state) || $state !== $_SESSION['weibo_state']) {
                 echo '非法请求!';
                 exit;
             }
             unset($_SESSION['weibo_state']);
             $keys['code'] = $_REQUEST['code'];
             $keys['redirect_uri'] = WB_CALLBACK_URL;
             try {
                 $token = $o->getAccessToken('code', $keys);
             } catch (OAuthException $e) {
             }
         }
         if ($token) {
             $_SESSION['token'] = $token;
             setcookie('weibojs_' . $o->client_id, http_build_query($token));
             //echo "success";
             $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['token']['access_token']);
             $c->set_debug(DEBUG_MODE);
             $uid_get = $c->get_uid();
             $uid = $uid_get['uid'];
             if (!$uid) {
                 echo "error";
             }
             $user = $c->show_user_by_id($uid);
             //根据ID获取用户等基本信息
             //echo $user['screen_name'];
             if ($this->m_open->is_id($uid)) {
                 $this->m_open->login($uid);
                 //echo "登陆";
                 //echo $uid;
                 //$c->update( "坑爹的api终于调好了");
                 redirect('line');
             } else {
                 $this->m_open->register($user);
                 $this->m_open->create($user);
                 //echo $uid;
                 //echo "注册";
                 redirect('line');
             }
         } else {
             echo "fail";
         }
     }
 }
 /**
  * sinaLogin
  */
 public function sinaLogin()
 {
     $state = md5(rand(5, 10));
     Yii::app()->session->add('sina_state', $state);
     $weiboService = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
     $this->sina_code_url = $weiboService->getAuthorizeURL(WB_CALLBACK_URL, 'code', $state);
     Yii::app()->session->add('back_url', $this->back_url . '?state=' . $state);
 }
Example #15
0
 /**
  *  是否已经获取到了token,未获取则显示获取token的图标,否则显示当前登录账号
  *
  * @access public
  * @param 
  * @return string
  */
 public static function SinaAuth()
 {
     self::getPubFile();
     $sina_auth = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
     $authurl = $sina_auth->getAuthorizeURL(WB_CALLBACK_URL, 'code');
     $img_path = Helper::options()->pluginUrl . '/WeiboSync/weibo.png';
     echo $sina_profile = '<ul class="typecho-option"><li><a href="' . $authurl . '"><img src="' . $img_path . '"></a>&nbsp;&nbsp;<b>点击左边图标获取微博Access_token信息</b></li></ul>';
 }
Example #16
0
 public function run()
 {
     require_once Yii::getPathOfAlias('ext') . "/OAuth/sinawb/saetv2.ex.class.php";
     $config = OAuth::getConf('sinawb');
     $sinawb = new SaeTOAuthV2($config['wb_akey'], $config['wb_skey']);
     $code_url = $sinawb->getAuthorizeURL($config['callback']);
     $this->controller->redirect($code_url);
 }
 public function index()
 {
     import('Vendor.Weibo.saetv2');
     $weibo = new \SaeTOAuthV2(WB_APPKEY, WB_SKEY);
     $code_url = $weibo->getAuthorizeURL(WB_CALLBACK_URL);
     $this->assign('code_url', $code_url);
     $this->display();
 }
Example #18
0
 /**
  * 授权回调地址
  */
 public function callback()
 {
     if (empty($_GET['code'])) {
         throw new Typecho_Exception(_t('无效请求!'));
     }
     //跳转
     if (!class_exists('SaeTOAuthV2')) {
         require_once './saetv2.ex.class.php';
     }
     $saeto_client = new SaeTOAuthV2($this->config->client_id, $this->config->client_secret);
     //取access_token
     $access_token = $saeto_client->getAccessToken('code', array('code' => trim($_GET['code']), 'redirect_uri' => $this->config->callback_url));
     if (empty($access_token) || !is_array($access_token) || empty($access_token['uid'])) {
         throw new Typecho_Exception(_t('获取access_token失败,请返回重新授权!'));
     }
     $table = $this->db->getPrefix() . self::$tableName;
     $query = $this->db->query("SELECT * FROM {$table} WHERE openid='{$access_token['uid']}' AND plateform='sina'");
     $users_oauth = $this->db->fetchRow($query);
     if (!empty($users_oauth['uid'])) {
         //该新浪帐号已经绑定了用户
         if (Typecho_Widget::widget('Widget_User')->hasLogin()) {
             /** 直接返回 */
             $this->response->redirect(Typecho_Widget::widget('Widget_Options')->index);
         } else {
             //让其直接登陆
             $this->setUserLogin($users_oauth['uid']);
             if (!Typecho_Widget::widget('Widget_User')->pass('contributor', true)) {
                 /** 不允许普通用户直接跳转后台 */
                 $this->response->redirect(Typecho_Widget::widget('Widget_Options')->profileUrl);
             } else {
                 $this->response->redirect(Typecho_Widget::widget('Widget_Options')->adminUrl);
             }
         }
         exit;
     }
     //该新浪帐号未绑定过
     /** 如果已经登录 */
     if (Typecho_Widget::widget('Widget_User')->hasLogin()) {
         /** 直接绑定 */
         $cookieUid = Typecho_Cookie::get('__typecho_uid');
         $this->bindOauthUser($cookieUid, $access_token['uid'], 'sina', $access_token['expires_in']);
         $this->response->redirect(Typecho_Widget::widget('Widget_Options')->index);
     } else {
         //取用户信息
         $saetc_client = new SaeTClientV2($this->config->client_id, $this->config->client_secret, $access_token['access_token']);
         $weibo_user = $saetc_client->show_user_by_id($access_token['uid']);
         //创建用户
         $uid = $this->registerFromWeiboUser($weibo_user);
         if (!$uid) {
             throw new Typecho_Exception(_t('创建帐号失败,请联系管理员!'));
         }
         $this->setUserLogin($uid);
         $this->bindOauthUser($uid, $access_token['uid'], 'sina', $access_token['expires_in']);
         $this->response->redirect(Typecho_Widget::widget('Widget_Options')->profileUrl);
     }
     //构造用户帐号
     exit;
 }
Example #19
0
 function __construct($allow_debug = false)
 {
     $this->memcache = new Memcache();
     $this->memcache->connect(MC_HOST, 11211) or die("Could not connect");
     if (!$this->memcache->get(MC_KEY)) {
         $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
         $code_url = $o->getAuthorizeURL(WB_CALLBACK_URL);
         if ($allow_debug) {
             echo '<a href="' . $code_url . '">' . $code_url . '</a><br /><br />';
         }
     }
     $this->c = new SaeTClientV2(WB_AKEY, WB_SKEY, $this->memcache->get(MC_KEY));
     $this->c->set_debug($allow_debug);
 }
Example #20
0
 public function index()
 {
     $temp = $this->session->userdata('access_token');
     if (empty($temp)) {
         $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
         $code_url = $o->getAuthorizeURL(WB_CALLBACK_URL);
         echo "<meta http-equiv=refresh content='0; url={$code_url}'>";
         //跳转到授权页面
     } else {
         $user_info = $this->celtop_model->get_user_day();
         $data['user_info'] = $user_info;
         $this->load->view('celebritytop/homepage_view', $data);
     }
 }
Example #21
0
 public function getUserInfo($request_args)
 {
     is_null($request_args['code']) && exit;
     $oauth = new SaeTOAuthV2($this->setting['app_key'], $this->setting['app_secret']);
     $keys = array('code' => $request_args['code'], 'redirect_uri' => $this->redirect_uri);
     $token = $oauth->getAccessToken('code', $keys);
     $client = new SaeTClientV2($this->setting['app_key'], $this->setting['app_secret'], $token['access_token']);
     $user = $client->show_user_by_id($token['uid']);
     $result['keyid'] = $user['id'];
     $result['keyname'] = $user['name'];
     $result['keyavatar_small'] = $user['profile_image_url'];
     $result['keyavatar_big'] = $user['avatar_large'];
     $result['bind_info'] = $token;
     return $result;
 }
 /**
  *微博回调
  *@author winter
  *@version 2015年11月20日19:52:33
  */
 public function weibocallback()
 {
     include_once './libweibo/config.php';
     include_once './libweibo/saetv2.ex.class.php';
     $o = new \SaeTOAuthV2(WB_AKEY, WB_SKEY);
     if (isset($_REQUEST['code'])) {
         $keys = array();
         $keys['code'] = $_REQUEST['code'];
         $keys['redirect_uri'] = WB_CALLBACK_URL;
         try {
             $token = $o->getAccessToken('code', $keys);
         } catch (OAuthException $e) {
         }
     }
     if ($token) {
         //授权完成
         $_SESSION['token'] = $token;
         setcookie('weibojs_' . $o->client_id, http_build_query($token));
         $c = new \SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['token']['access_token']);
         //$ms  = $c->home_timeline(); // done
         $uid_get = $c->get_uid();
         $uid = $uid_get['uid'];
         $user_message = $c->show_user_by_id($uid);
         //根据ID获取用户等基本信息
         if ($user_message) {
             $user = M('user');
             $list = $user->where("wbuid = '" . $uid . "'  and status = 1")->find();
             //echo $user->getlastsql();die();
             if (!$list) {
                 session('uinfo', $user_message);
                 session('sign', array('name' => '微博账号', 'code' => 2));
                 echo "<script>window.close();window.opener.location.href='" . U('User/threepartlogin') . "'</script>";
                 exit;
             }
             session('userid', $list['id']);
             session('username', $list['nickname']);
             session('userphone', $list['mobile']);
             session('userimg', $list['headpic']);
             echo "<script>window.close();window.opener.location.reload()</script>";
             exit;
         } else {
             $this->error('登陆失败');
         }
     } else {
         //授权失败
         $this->error('登陆失败');
     }
 }
Example #23
0
 public function sinalogin()
 {
     $loginconfig = FS("Webconfig/loginconfig");
     define("WB_AKEY", $loginconfig['sina']['akey']);
     define("WB_SKEY", $loginconfig['sina']['skey']);
     define("WB_CALLBACK_URL", "http://" . $_SERVER['HTTP_HOST'] . __APP__ . '/member/oauth/sinalogin');
     require C("APP_ROOT") . "Lib/Oauth/sina/saetv2.ex.class.php";
     $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
     if (isset($_REQUEST['code'])) {
         $keys = array();
         $keys['code'] = $_REQUEST['code'];
         $keys['redirect_uri'] = WB_CALLBACK_URL;
         try {
             $token = $o->getAccessToken('code', $keys);
         } catch (OAuthException $e) {
         }
     }
     if (isset($_REQUEST['error_code'])) {
         if ($_REQUEST['error_code'] == '21330') {
             $this->redirect(__APP__ . "/member/common/login");
         }
     }
     if ($token) {
         $_SESSION['token'] = $token;
     } else {
         exit("Error happenned, please try again.");
     }
     $sinauser = $o->getUserShow(text($token['uid']));
     // 		$sinauser['name'] = sprintf("%05d",rand(1,99999));
     $map['openid'] = text($token['uid']);
     //唯一ID
     $map['site'] = 'sina';
     $this->appCk($map, $sinauser['name']);
     //nickname
 }
Example #24
0
 public function wbcallback()
 {
     Vendor('Weibo.saetv2#ex#class');
     $o = new \SaeTOAuthV2(C('weibo.wb_akey'), C('weibo.wb_skey'));
     if (isset($_REQUEST['code'])) {
         $keys = array();
         $keys['code'] = $this->_request('code');
         $keys['redirect_uri'] = C('weibo.wb_callback_url');
         try {
             $token = $o->getAccessToken('code', $keys);
         } catch (OAuthException $e) {
         }
     }
     if ($token) {
         // 获取用户信息
         $c = new SaeTClientV2(C('weibo.wb_akey'), C('weibo.wb_skey'), $token['access_token']);
         // $ms  = $c->home_timeline(); // done
         $uid_get = $c->get_uid();
         $uid = $uid_get['uid'];
         $uinfo = $c->show_user_by_id($uid);
         //根据ID获取用户等基本信息
         // 查询是否已经绑定
         $this->user_model = D('user');
         $user = $this->user_model->field('id,email,password')->where(array('wb_token' => $token['access_token'], 'wb_openid' => $uid))->find();
         // echo $this->user_model->getLastSql();
         if ($user['id'] > 0) {
             // 登录用户
             if ($this->user_model->login($user['id'], $user['email'], $user['password'])) {
                 //TODO:跳转到登录前页面
                 $this->success('登录成功!', session('rebackurl'));
             } else {
                 $this->error($model->getError());
             }
         } else {
             session('avatar', $uinfo['avatar_hd']);
             session('openid', $uinfo['id']);
             session('token', $token['access_token']);
             session('uname', $uinfo['screen_name']);
             session('oatype', 'wb_');
             //print_r($uinfo);
             $this->redirect('join');
         }
     } else {
         $this->error('授权失败。');
     }
 }
Example #25
0
 public function callback()
 {
     $appkey = $this->config->get('weibo_login_appkey');
     $appsecret = $this->config->get('weibo_login_appsecret');
     $callback_url = $this->url->link('extension/module/weibo_login/callback', '', true);
     $this->load->language('extension/module/weibo_login');
     $data['text_weibo_login'] = $this->language->get('text_weibo_login');
     include_once DIR_SYSTEM . 'library/weibo/saetv2.ex.class.php';
     $o = new SaeTOAuthV2($appkey, $appsecret);
     if (isset($_REQUEST['code'])) {
         $keys = array();
         $keys['code'] = $_REQUEST['code'];
         $keys['redirect_uri'] = $callback_url;
         try {
             $token = $o->getAccessToken('code', $keys);
         } catch (OAuthException $e) {
         }
     }
     if ($token) {
         //setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );
         $c = new SaeTClientV2($appkey, $appsecret, $token['access_token']);
         $ms = $c->home_timeline();
         $uid_get = $c->get_uid();
         $uid = $uid_get['uid'];
         $user_message = $c->show_user_by_id($uid);
         $this->session->data['weibo_login_access_token'] = $token['access_token'];
         $this->session->data['weibo_login_uid'] = $uid;
         if ($this->customer->login_weibo($this->session->data['weibo_login_access_token'], $this->session->data['weibo_login_uid'])) {
             unset($this->session->data['guest']);
             // Default Shipping Address
             $this->load->model('account/address');
             if ($this->config->get('config_tax_customer') == 'payment') {
                 $this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
             }
             if ($this->config->get('config_tax_customer') == 'shipping') {
                 $this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
             }
             $this->response->redirect($this->url->link('account/account', '', 'SSL'));
         } else {
             $this->session->data['weibo_login_warning'] = sprintf($this->language->get('text_weibo_login_warning'), $this->config->get('config_name'));
             $this->response->redirect($this->url->link('account/login', '', 'SSL'));
         }
     } else {
         echo $this->language->get('text_weibo_fail');
     }
 }
Example #26
0
 function sina_oauth_back()
 {
     require_once APPPATH . 'libraries/sina/saetv2.ex.class.php';
     $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
     $back_url = base_url() . 'index.php?c=oauth&m=sina_oauth_back';
     if (isset($_REQUEST['code'])) {
         $keys = array();
         $keys['code'] = $_REQUEST['code'];
         $keys['redirect_uri'] = $back_url;
         try {
             $token = $o->getAccessToken('code', $keys);
         } catch (OAuthException $e) {
         }
     }
     $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $token['access_token']);
     $ms = $c->home_timeline();
     // done
     $uid_get = $c->get_uid();
     $uid = $uid_get['uid'];
     $user_message = $c->show_user_by_id($uid);
     //根据ID获取用户等基本信息
     if ($user_message['gender'] == 'm') {
         $user_data['tu_gender'] = 0;
     } else {
         $user_data['tu_gender'] = 1;
     }
     $user_data['tu_weibo_nickname'] = $user_message['screen_name'];
     $user_data['tu_source'] = 0;
     $weibo_user_check = $this->tickets->select('users', $user_data);
     if ($weibo_user_check) {
         $final_user = $weibo_user_check[0];
     } else {
         $save_dir = 'api_images';
         $image = $user_message['avatar_large'];
         $image_new_name = $this->generate_code(10) . '.jpg';
         $image = $this->getImage($image, $save_dir, $image_new_name, 1);
         $user_data['tu_portrait'] = $image['save_path'];
         $user_data['tu_created'] = date('Y-m-d H:i:s');
         $result = $this->tickets->insert('users', $user_data);
         $temp_user = $this->tickets->select('users', array('tu_id' => $result));
         $final_user = $temp_user[0];
     }
     setcookie('user_cookie', serialize($final_user), time() + 3600 * 24, '/');
     redirect('/main/index');
 }
 public function index()
 {
     if ($this->config->get('weibo_login_status') !== '1') {
         $this->redirect($this->url->link('account/login', '', 'SSL'));
     }
     $this->load->model('account/weibo_login');
     $weibo_uid = '';
     if ($this->request->server['REQUEST_METHOD'] == 'GET') {
         if (isset($_REQUEST['code'])) {
             require_once DIR_SYSTEM . 'weiboSDK/oauth2.class.php';
             $appkey = $this->config->get('weibo_login_appkey');
             $appsecret = $this->config->get('weibo_login_secret');
             $o = new SaeTOAuthV2($appkey, $appsecret);
             $keys = array();
             $keys['code'] = $_REQUEST['code'];
             $keys['redirect_uri'] = $this->url->link('account/weibo_login');
             try {
                 $token = $o->getAccessToken('code', $keys);
                 $c = new SaeTClientV2($appkey, $appsecret, $token['access_token']);
                 $uid_get = $c->get_uid();
                 $weibo_uid = $uid_get['uid'];
             } catch (OAuthException $e) {
             }
         } else {
             $this->response->redirect($this->url->link('account/login', '', 'SSL'));
         }
     } else {
         // POST
         $weibo_uid = $this->request->post['weibo_uid'];
     }
     if (empty($weibo_uid)) {
         $this->response->redirect($this->url->link('account/login', '', 'SSL'));
     }
     if (!$this->model_account_weibo_login->getUidRecord($weibo_uid)) {
         // New customer
         $this->model_account_weibo_login->addCustomer($weibo_uid);
     }
     if ($this->model_account_weibo_login->getUidRecord($weibo_uid)) {
         unset($this->session->data['guest']);
         $customerInfo = $this->model_account_weibo_login->getCustomerInfoByUid($weibo_uid);
         $this->session->data['customer_id'] = $customerInfo['customer_id'];
         $this->customer->loginThridPart($customerInfo);
         $this->response->redirect($this->url->link('account/account'));
     }
 }
Example #28
0
 function getAccessToken($parms)
 {
     require_once dirname(__FILE__) . "/API/saetv2.ex.class.php";
     $o = new SaeTOAuthV2(ISession::get('apiKey'), ISession::get('apiSecret'));
     $token = '';
     if (isset($_REQUEST['code'])) {
         $keys = array();
         $keys['code'] = $_REQUEST['code'];
         $keys['redirect_uri'] = parent::getReturnUrl();
         try {
             $token = $o->getAccessToken('code', $keys);
         } catch (OAuthException $e) {
         }
     }
     if ($token) {
         ISession::set('token', $token);
         setcookie('weibojs_' . $o->client_id, http_build_query($token));
     }
 }
Example #29
0
    public function loginUser()
    {
        $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
        $code = getQuery('code');
        if (isset($code)) {
            $keys = array();
            $keys['code'] = $code;
            $keys['redirect_uri'] = WB_CALLBACK_URL;
            try {
                $tokenData = $o->getAccessToken('code', $keys);
            } catch (OAuthException $e) {
            }
        }
        if ($tokenData) {
            $token = $tokenData['access_token'];
            $userData = $this->loadData($token);
            $data['uid'] = $this->userID;
            $data['nick'] = $this->userName;
            $data['pictureBig'] = $this->pictureBig;
            $data['pictureSmall'] = $this->pictureSmall;
            $data['access_token'] = $token;
            $data['mobile'] = $this->mobile;
            setMyCookie('weiboAuthToken', $data);
            //setcookie( 'weibojs_'.$o->client_id, http_build_query($tokenData));
            return true;
        } else {
            ?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UGG</title>
</head>

<body>
<script type="text/javascript">
	window.close();
</script>
</body>
</html>
            <?php 
        }
    }
 /**
  * 微博登陆成功回调函数
  * @author Mr.Cong <*****@*****.**>
  */
 public function auth()
 {
     header("Content-type:text/html;charset=utf-8");
     $OAuth = new \SaeTOAuthV2($this->weibo_akey, $this->weibo_skey);
     //获取回调中的code参数,再去获取Token
     if (I('get.code', '') != '') {
         $keys = array('code' => I('get.code'), 'redirect_uri' => C('WB_CALLBACK_URL'));
         $token = $OAuth->getAccessToken('code', $keys);
     }
     //如果有Token的话,则写入Session和Cookie
     if ($token != false) {
         $_SESSION['token'] = $token;
         setcookie('weibojs_' . $OAuth->client_id, http_build_query($token));
     } else {
         $this->show("<script>alert('授权失败');window.location.href='" . C('LOGIN_URL') . "'</script>");
         exit;
     }
     //进入检查用户信息
     $this->checkLogin();
 }