Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update

  • Loading branch information...
commit f56855797858e630e868ab13a234cc2229860505 1 parent 64e549b
@windsdeng authored
View
96 OauthLogin.php
@@ -0,0 +1,96 @@
+<?php
+/**
+ * oauth login for yii
+ *
+ * @author windsdeng@gmail.com http://www.dlf5.com
+ * @copyright Copyright &copy; 2010 dlf5.com
+ */
+
+Yii::import('ext.oauthLogin.qq.qqConnect',true);
+Yii::import('ext.oauthLogin.sina.sinaWeibo',true);
+
+class oauthLogin extends CWidget
+{
+ /***** widget options *****/
+
+ /******* widget public vars *******/
+ public $baseUrl = null;
+
+ public $cssFile = array(
+ '/css/oauth_login_yii.css',
+ );
+
+ public $data = array();
+
+ /**
+ *
+ * @var small_login and medium_login big_login
+ */
+ public $itemView = 'small_login';
+
+ public $sina_code_url = null;
+
+ public $qq_code_url = null;
+
+ public $back_url = null;
+
+
+ /**
+ * Initialize the widget
+ */
+ public function init()
+ {
+ parent::init();
+ //Publish assets
+ $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
+ $this->baseUrl = Yii::app()->getAssetManager()->publish($dir);
+
+ //Register the widget css files
+ $cs=Yii::app()->clientScript;
+ foreach($this->cssFile as $css) {
+
+ $oauthCssFile = $this->baseUrl . $css;
+ $cs->registerCssFile($oauthCssFile);
+ }
+
+ $this->sinaLogin();
+ $this->qqLogin();
+ }
+
+
+ /**
+ * 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);
+ }
+
+ /**
+ * qqLogin
+ */
+ public function qqLogin()
+ {
+ $state = md5(rand(5, 10));
+ Yii::app()->session->add('qq_state',$state);
+ $qqService = new qqConnectAuthV2(QQ_APPID,QQ_APPKEY);
+ $this->qq_code_url = $qqService->getAuthorizeURL(QQ_CALLBACK_URL,'code',$state);
+ Yii::app()->session->add('back_url',$this->back_url.'?state='.$state);
+ }
+
+
+ /**
+ * Run the widget
+ */
+ public function run()
+ {
+ parent::run();
+ $this->getViewFile($this->itemView);
+ $this->render($this->itemView,array('data',$this->data));
+ }
+
+}
View
16 config/main.php
@@ -0,0 +1,16 @@
+<?php
+/**
+ * @var 新浪配置
+ */
+define( "WB_AKEY" , '********' );
+define( "WB_SKEY" , '****************************************' );
+define( "WB_CALLBACK_URL" , 'http://demo.dlf5.net/site/wblogin');
+
+/**
+ * @var QQ配置
+ */
+
+define( "QQ_APPID" , '********' );
+define( "QQ_APPKEY" , '****************************************' );
+define( "QQ_CALLBACK_URL" ,'http://demo.dlf5.net/site/qqlogin');
+
View
365 qq/qqConnect.php
@@ -0,0 +1,365 @@
+<?php
+Yii::import('ext.oauthLogin.config.main',true);
+/**
+ * QQ互联 OAuth 认证类(OAuth2)
+ * @author WindsDeng <windsdeng@gmail.com>
+ * @qq 620088997
+ */
+
+class qqConnectAuthV2 {
+
+ /**
+ * @ignore
+ */
+ public $client_id;
+ /**
+ * @ignore
+ */
+ public $client_secret;
+ /**
+ * @ignore
+ */
+ public $access_token;
+ /**
+ * @ignore
+ */
+ public $refresh_token;
+ /**
+ * Contains the last HTTP status code returned.
+ *
+ * @ignore
+ */
+ public $http_code;
+ /**
+ * Contains the last API call.
+ *
+ * @ignore
+ */
+ public $url;
+ /**
+ * Set up the API root URL.
+ *
+ * @ignore
+ */
+ public $host = "https://graph.qq.com/";
+ /**
+ * Set timeout default.
+ *
+ * @ignore
+ */
+ public $timeout = 30;
+ /**
+ * Set connect timeout.
+ *
+ * @ignore
+ */
+ public $connecttimeout = 30;
+ /**
+ * Verify SSL Cert.
+ *
+ * @ignore
+ */
+ public $ssl_verifypeer = FALSE;
+ /**
+ * Respons format.
+ *
+ * @ignore
+ */
+ public $format = 'json';
+ /**
+ * Decode returned json data.
+ *
+ * @ignore
+ */
+ public $decode_json = TRUE;
+ /**
+ * Contains the last HTTP headers returned.
+ *
+ * @ignore
+ */
+ public $http_info;
+ /**
+ * Set the useragnet.
+ *
+ * @ignore
+ */
+ public $useragent = 'QQ T OAuth2 v1.0';
+
+ /**
+ * print the debug info
+ *
+ * @ignore
+ */
+ public $debug = FALSE;
+
+ /**
+ * boundary of multipart
+ * @ignore
+ */
+ public static $boundary = '';
+
+ /**
+ * Set API URLS
+ */
+ /**
+ * @ignore
+ */
+ function accessTokenURL(){ return 'https://graph.qq.com/oauth2.0/token'; }
+
+ /**
+ * @ignore
+ */
+ function authorizeURL(){ return 'https://graph.qq.com/oauth2.0/authorize'; }
+
+ /**
+ * construct qqOAuth object
+ */
+ function __construct($client_id, $client_secret, $access_token = NULL, $refresh_token = NULL) {
+ $this->client_id = $client_id;
+ $this->client_secret = $client_secret;
+ $this->access_token = $access_token;
+ $this->refresh_token = $refresh_token;
+ }
+
+
+
+ function getAuthorizeURL($url, $response_type = 'code', $state = NULL, $display = NULL ) {
+ $params = array();
+ $params['client_id'] = $this->client_id;
+ $params['redirect_uri'] = $url;
+ $params['response_type'] = $response_type;
+ $params['state'] = $state;
+ $params['display'] = $display;
+ return $this->authorizeURL() . "?" . http_build_query($params);
+ }
+
+ function getAccessToken($type = 'code', $keys) {
+ $params = array();
+ $params['client_id'] = $this->client_id;
+ $params['client_secret'] = $this->client_secret;
+ if ( $type === 'token' ) {
+ $params['grant_type'] = 'refresh_token';
+ $params['refresh_token'] = $keys['refresh_token'];
+ } elseif ( $type === 'code' ) {
+ $params['grant_type'] = 'authorization_code';
+ $params['code'] = $keys['code'];
+ $params['state'] = $keys['state'];
+ $params['redirect_uri'] = $keys['redirect_uri'];
+ } elseif ( $type === 'password' ) {
+ $params['grant_type'] = 'password';
+ $params['username'] = $keys['username'];
+ $params['password'] = $keys['password'];
+ } else {
+ throw new CHttpException("wrong auth type");
+ }
+
+ $response = $this->oAuthRequest($this->accessTokenURL(), 'GET', $params);
+ $token = array();
+ parse_str($response,$token);
+ $getOpenID = $this->getOpenID($token);
+ $token['openid'] = $getOpenID['openid'];
+
+ if ( is_array($token) && !isset($token['error']) ) {
+ $this->access_token = $token['access_token'];
+ } else {
+ throw new CHttpException("get access token failed." . $token['error']);
+ }
+
+ return $token;
+ }
+
+
+ /**
+ * Format and sign an OAuth / API request
+ *
+ * @return string
+ * @ignore
+ */
+ function oAuthRequest($url, $method, $parameters, $multi = false) {
+
+ if (strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0) {
+ $url = "{$this->host}{$url}";
+ }
+
+ switch ($method) {
+ case 'GET':
+ $url = $url . '?' . http_build_query($parameters);
+ return $this->http($url, 'GET');
+ default:
+ $headers = array();
+ if (!$multi && (is_array($parameters) || is_object($parameters)) ) {
+ $body = http_build_query($parameters);
+ } else {
+ $body = self::build_http_query_multi($parameters);
+ $headers[] = "Content-Type: multipart/form-data; boundary=" . self::$boundary;
+ }
+
+ return $this->http($url, $method, $body, $headers);
+ }
+ }
+
+ /**
+ * Make an HTTP request
+ *
+ * @return string API results
+ * @ignore
+ */
+ function http($url, $method, $postfields = NULL, $headers = array()) {
+ $this->http_info = array();
+ if(!function_exists('curl_init')) {
+ echo 'CURL 不可用';
+ }
+ $ci = curl_init();
+ /* Curl settings */
+ curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+ curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
+ curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
+ curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
+ curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
+ curl_setopt($ci, CURLOPT_ENCODING, "");
+ curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
+ curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
+ curl_setopt($ci, CURLOPT_HEADER, FALSE);
+
+ switch ($method) {
+ case 'POST':
+ curl_setopt($ci, CURLOPT_POST, TRUE);
+ if (!empty($postfields)) {
+ curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
+ $this->postdata = $postfields;
+ }
+ break;
+ case 'DELETE':
+ curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
+ if (!empty($postfields)) {
+ $url = "{$url}?{$postfields}";
+ }
+
+ }
+
+ if ( isset($this->access_token) && $this->access_token )
+ $headers[] = "Authorization: OAuth2 ".$this->access_token;
+
+ $headers[] = "API-RemoteIP: " . $_SERVER['REMOTE_ADDR'];
+ curl_setopt($ci, CURLOPT_URL, $url );
+ curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
+ curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
+
+ $response = curl_exec($ci);
+ $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
+ $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
+ $this->url = $url;
+
+ if ($this->debug) {
+ echo "=====post data======\r\n";
+ var_dump($postfields);
+
+ echo '=====info====='."\r\n";
+ print_r( curl_getinfo($ci) );
+
+ echo '=====$response====='."\r\n";
+ print_r( $response );
+ }
+ curl_close ($ci);
+ return $response;
+ }
+
+
+ /**
+ * Get the header info to store.
+ *
+ * @return int
+ * @ignore
+ */
+ function getHeader($ch, $header) {
+ $i = strpos($header, ':');
+ if (!empty($i)) {
+ $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
+ $value = trim(substr($header, $i + 2));
+ $this->http_header[$key] = $value;
+ }
+ return strlen($header);
+ }
+
+ /**
+ * @ignore
+ */
+ public static function build_http_query_multi($params) {
+ if (!$params) return '';
+
+ uksort($params, 'strcmp');
+
+ $pairs = array();
+
+ self::$boundary = $boundary = uniqid('------------------');
+ $MPboundary = '--'.$boundary;
+ $endMPboundary = $MPboundary. '--';
+ $multipartbody = '';
+
+ foreach ($params as $parameter => $value) {
+
+ if( in_array($parameter, array('pic', 'image')) && $value{0} == '@' ) {
+ $url = ltrim( $value, '@' );
+ $content = file_get_contents( $url );
+ $array = explode( '?', basename( $url ) );
+ $filename = $array[0];
+
+ $multipartbody .= $MPboundary . "\r\n";
+ $multipartbody .= 'Content-Disposition: form-data; name="' . $parameter . '"; filename="' . $filename . '"'. "\r\n";
+ $multipartbody .= "Content-Type: image/unknown\r\n\r\n";
+ $multipartbody .= $content. "\r\n";
+ } else {
+ $multipartbody .= $MPboundary . "\r\n";
+ $multipartbody .= 'content-disposition: form-data; name="' . $parameter . "\"\r\n\r\n";
+ $multipartbody .= $value."\r\n";
+ }
+
+ }
+
+ $multipartbody .= $endMPboundary;
+ return $multipartbody;
+ }
+
+ /**
+ * GET wrappwer for oAuthRequest.
+ *
+ * @return mixed
+ */
+ function get($url, $parameters = array()) {
+ $response = $this->oAuthRequest($url, 'GET', $parameters);
+ if ($this->format === 'json' && $this->decode_json) {
+ return json_decode($response, true);
+ }
+ return $response;
+ }
+
+ /**
+ * POST wreapper for oAuthRequest.
+ *
+ * @return mixed
+ */
+ function post($url, $parameters = array(), $multi = false) {
+ $response = $this->oAuthRequest($url, 'POST', $parameters, $multi );
+ if ($this->format === 'json' && $this->decode_json) {
+ return json_decode($response, true);
+ }
+ return $response;
+ }
+
+ function getOpenID($token)
+ {
+ $response = $this->oAuthRequest('oauth2.0/me', 'GET', $token);
+ $aTemp = array();
+ preg_match('/callback\(\s+(.*?)\s+\)/i', $response,$aTemp);
+ return json_decode($aTemp[1],true);
+ }
+
+ function getUserInfo($params)
+ {
+ $token['access_token'] = $params['access_token'];
+ $token['oauth_consumer_key'] = QQ_APPID;
+ $token['openid'] = $params['openid'];
+ return $this->get('user/get_user_info',$token);
+ }
+
+}
View
3,237 sina/sinaWeibo.php
3,237 additions, 0 deletions not shown
View
6 views/big_login.php
@@ -0,0 +1,6 @@
+<div class="auth-login-yii">
+ <p>
+ <a class="auth-big-yii" href="<?php echo $this->sina_code_url ?>" title="新浪微博"><img src="<?php echo $this->baseUrl;?>/images/sina_logo.png" alt="新浪微博">新浪微博</a>
+ <a class="auth-big-yii" href="<?php echo $this->qq_code_url ?>" title="腾讯互联"><img src="<?php echo $this->baseUrl;?>/images/qq_logo.png" alt="腾讯互联">腾讯互联</a>
+ </p>
+</div>
View
6 views/medium_login.php
@@ -0,0 +1,6 @@
+<div class="auth-login-yii">
+ <p>
+ <a class="auth-medium-yii" href="<?php echo $this->sina_code_url ?>" title="新浪微博"><img src="<?php echo $this->baseUrl;?>/images/sina_logo.png" alt="新浪微博">新浪微博</a>
+ <a class="auth-medium-yii" href="<?php echo $this->qq_code_url ?>" title="腾讯互联"><img src="<?php echo $this->baseUrl;?>/images/qq_logo.png" alt="腾讯互联">腾讯互联</a>
+ </p>
+</div>
View
6 views/small_login.php
@@ -0,0 +1,6 @@
+<div class="auth-login-yii">
+ <p>
+ <a class="auth-small-yii" href="<?php echo $this->sina_code_url ?>" title="新浪微博"><img src="<?php echo $this->baseUrl;?>/images/sina_logo.png" alt="新浪微博"></a>
+ <a class="auth-small-yii" href="<?php echo $this->qq_code_url ?>" title="腾讯互联"><img src="<?php echo $this->baseUrl;?>/images/qq_logo.png" alt="腾讯互联"></a>
+ </p>
+</div>
Please sign in to comment.
Something went wrong with that request. Please try again.