Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bigfa committed Feb 10, 2016
0 parents commit c525a60
Show file tree
Hide file tree
Showing 13 changed files with 721 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.log
.htaccess
sitemap.xml
sitemap.xml.gz
wp-config.php
wp-content/advanced-cache.php
wp-content/backup-db/
wp-content/backups/
wp-content/blogs.dir/
wp-content/cache/
wp-content/upgrade/
wp-content/uploads/
wp-content/wp-cache-config.php
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2016 bigfa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Wordpress 原生Oauth2.0登录

不依赖任何框架的WordPress 社会化登录功能

## 微博登录


[微博登录](http://fatesinger.com/74619)

## QQ互联登录

[QQ互联登录](http://fatesinger.com/75041)

[豆瓣登录](http://fatesinger.com/75041)

[微信登录](http://fatesinger.com/75463)

[Github登录](http://fatesinger.com/77059)

[Instagram登录](http://wpista.com/themes/instagram-oauth.html)

[微信公众号登录](https://fatesinger.com/77241)
121 changes: 121 additions & 0 deletions auth-qq.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

define('QQ_APPID','');//appkey
define('QQ_APPSECRET','');//appsecret
require( dirname(__FILE__) . '/wp-load.php' );

class core_qq_login {
protected function __construct() {
$this->add_actions();
register_activation_hook($this->my_plugin_basename(), array( $this, 'ga_activation_hook' ) );
}

public function qq_login_redirect($redirect_to, $request_from, $user) {
if ($user && !is_wp_error($user)) {
$final_redirect = $this->getFinalRedirect();
if ($final_redirect !== '') {
return $final_redirect;
}
}
return $redirect_to;
}

protected function add_actions() {

add_filter('authenticate', array($this, 'ga_authenticate'), 5, 3);

add_filter('login_redirect', array($this, 'ga_login_redirect'), 5, 3 );
add_action('init', array($this, 'ga_init'), 1);

}
}


function fa_qq_oauth_redirect(){
echo '<script>if( window.opener ) {window.opener.location.reload();
window.close();
}else{
window.location.href = "'.home_url().'";
}</script>';
}


function qq_oauth(){

$code = $_GET['code'];
$token_url = "https://graph.qq.com/oauth2.0/token?client_id=" . QQ_APPID . "&client_secret=" . QQ_APPSECRET . "&grant_type=authorization_code&redirect_uri=".urlencode (home_url())."&code=".$code;
$response = wp_remote_get( $token_url );
if (is_wp_error($response)) {
die($response->get_error_message());
}
$response = $response['body'];
if (strpos ( $response, "callback" ) !== false) {
wp_redirect(home_url());
}
$params = array ();
parse_str ( $response, $params );
$qq_access_token = $params ["access_token"];
$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=" . $qq_access_token;
$str = wp_remote_get( $graph_url );
$str = $str['body'];
if (strpos ( $str, "callback" ) !== false) {
$lpos = strpos ( $str, "(" );
$rpos = strrpos ( $str, ")" );
$str = substr ( $str, $lpos + 1, $rpos - $lpos - 1 );
}
$user = json_decode ( $str,true );
if (isset ( $user->error )) {
echo "<h3>错误代码:</h3>" . $user->error;
echo "<h3>信息 :</h3>" . $user->error_description;
exit ();
}
$qq_openid = $user['openid'];
if(!$qq_openid){
wp_redirect(home_url());
exit;
}
$get_user_info = "https://graph.qq.com/user/get_user_info?" . "access_token=" . $qq_access_token . "&oauth_consumer_key=" . QQ_APPID . "&openid=" . $qq_openid . "&format=json";
$data = wp_remote_get( $get_user_info );
$data = $data['body'];
$data = json_decode($data , true);
$username = $data['nickname'];
$avatar = $data['figureurl_2'];

if(is_user_logged_in()){
$this_user = wp_get_current_user();
update_user_meta($this_user->ID ,"qq_openid",$qq_openid);
update_user_meta($this_user->ID ,"qq_avatar",$avatar);
fa_qq_oauth_redirect();
} else {
$user_qq = get_users(array("meta_key "=>"qq_openid","meta_value"=>$qq_openid));
if(is_wp_error($user_qq) || !count($user_qq)){
$login_name = wp_create_nonce($qq_openid);
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
$userdata=array(
'user_login' => $login_name,
'display_name' => $username,
'user_pass' => $random_password,
'nick_name' => $username
);
$user_id = wp_insert_user( $userdata );
wp_signon(array("user_login"=>$login_name,"user_password"=>$random_password),false);
update_user_meta($user_id ,"qq_openid",$qq_openid);
update_user_meta($user_id ,"qq_avatar",$avatar);
fa_qq_oauth_redirect();
} else {
wp_set_auth_cookie($user_qq[0]->ID);
update_user_meta($user_qq[0]->ID ,"qq_avatar",$avatar);
fa_qq_oauth_redirect();
}
}
}

if (isset($_GET ['state']) && isset($_GET ['code'])) qq_oauth();

function qq_oauth_url(){

$url = "https://graph.qq.com/oauth2.0/authorize?client_id=" . QQ_APPID . "&state=" . md5 ( uniqid ( rand (), true ) ) . "&response_type=code&redirect_uri=" . urlencode (home_url('/auth-qq.php'));
return $url;
}

echo qq_oauth_url();
76 changes: 76 additions & 0 deletions douban.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

define('DB_APPID','');//appkey
define('DB_APPSECRET','');//appsecret

function db_ouath_redirect(){
echo '<script>if( window.opener ) {window.opener.location.reload();
window.close();
}else{
window.location.href = "'.home_url().'";
}</script>';
}

function douban_oauth(){
$code = $_GET['code'];
$url = "https://www.douban.com/service/auth2/token";
$data = array('client_id' => WB_APPID,
'client_secret' => WB_APPSECRET,
'grant_type' => 'authorization_code',
'redirect_uri' => home_url(),
'code' => $code);
$response = wp_remote_post( $url, array(
'method' => 'POST',
'body' => $data,
)
);
$output = json_decode($response['body'],true);
$token = $output['access_token'];
$douban_id = $output['douban_user_id'];
if(empty($douban_id)){
wp_die('服务器响应错误。');
}
if(is_user_logged_in()){
$this_user = wp_get_current_user();
update_user_meta($this_user->ID ,"douban_id",$douban_id);
db_ouath_redirect();
}else{
$user_douban = get_users(array("meta_key "=>"douban_id","meta_value"=>$douban_uid));
if(is_wp_error($user_douban) || !count($user_douban)){
$get_user_info = "http://api.douban.com/labs/bubbler/user/".$douban_id;
$datas = wp_remote_get( $get_user_info );
$str = json_decode($datas['body'] , true);
$username = $str['title'];
$login_name = wp_create_nonce($douban_id);
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
$userdata=array(
'user_login' => $login_name,
'display_name' => $username,
'user_email' => $str['uid'] .'@fatesinger.com',
'user_pass' => $random_password,
'nick_name' => $username
);
$user_id = wp_insert_user( $userdata );
wp_signon(array("user_login"=>$login_name,"user_password"=>$random_password),false);
update_user_meta($user_id ,"douban_id",$douban_id);
db_ouath_redirect();

}else{
wp_set_auth_cookie($user_douban[0]->ID);
db_ouath_redirect();
}
}
}

function social_oauth_douban(){
if (isset($_GET['code']) && isset($_GET['type']) && $_GET['type'] == 'douban'){
douban_oauth();
}
}
add_action('init','social_oauth_douban');


function douban_oauth_url(){
$url = 'https://www.douban.com/service/auth2/auth?client_id=' . DB_APPID . '&scope=shuo_basic_r,shuo_basic_w,douban_basic_common&response_type=code&redirect_uri=' . urlencode (home_url('/?type=douban');
return $url;
}
54 changes: 54 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/*
公共跳转函数
*/
function fa_auth_redirect(){
echo '<script>if( window.opener ) {
window.opener.location.reload();
window.close();
} else {
window.location.href = "'.home_url().'";
}</script>';
}

/*
Gravatar 头像 Hook, 包含微信、微博、QQ头像。
*/
function fa_avatar_hook( $avatar, $id_or_email, $size, $default, $alt ) {
$user = false;

if ( is_numeric( $id_or_email ) ) {

$id = (int) $id_or_email;
$user = get_user_by( 'id' , $id );

} elseif ( is_object( $id_or_email ) ) {

if ( ! empty( $id_or_email->user_id ) ) {
$id = (int) $id_or_email->user_id;
$user = get_user_by( 'id' , $id );
}

} else {
$user = get_user_by( 'email', $id_or_email );
}

if ( $user && is_object( $user ) ) {
if( get_user_meta($user->data->ID,'weixin_avatar',true) ){
$avatar = get_user_meta($user->data->ID,'weixin_avatar',true);
$avatar = str_replace('http','https',$avatar);
$avatar = "<img alt='{$alt}' src='{$avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
} else if( get_user_meta($user->data->ID,'sina_avatar',true) ){
$avatar = get_user_meta($user->data->ID,'sina_avatar',true);
$avatar = "<img alt='{$alt}' src='{$avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
}//根据你的存储头像的key来写
}

return $avatar;
}
add_filter('get_avatar', 'fa_avatar_hook' , 1 , 5);

82 changes: 82 additions & 0 deletions github.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
define('GITHUB_APPID','');//appkey
define('GITHUB_APPSECRET','');//appsecret

function github_ouath_redirect(){
echo '<script>if( window.opener ) {window.opener.location.reload();
window.close();
}else{
window.location.href = "'.home_url().'";
}</script>';
}

function github_oauth(){
$code = $_GET['code'];
$url = "https://github.com/login/oauth/access_token";
$data = array('client_id' => GITHUB_APPID,
'client_secret' => GITHUB_APPSECRET,
'grant_type' => 'authorization_code',
'redirect_uri' => home_url(),
'code' => $code,
'state' => $_GET['state']);
$response = wp_remote_post( $url, array(
'method' => 'POST',
'headers' => array('Accept' => 'application/json'),
'body' => $data,
)
);
$output = json_decode($response['body'],true);
$token = $output['access_token'];
if(!$token) wp_die('授权失败');
$get_user_info = "https://api.github.com/user?access_token=".$token;
$datas = wp_remote_get( $get_user_info );
$str = json_decode($datas['body'] , true);
$github_id = $str['id'];
$email = $str['email'];
$name = $str['name'];
if(!$github_id){
wp_redirect(home_url('/?3'.$douban_id));
exit;
}
if(is_user_logged_in()){
$this_user = wp_get_current_user();
update_user_meta($this_user->ID ,"github_id",$github_id);
github_ouath_redirect();
}else{
$user_github = get_users(array("meta_key "=>"github_id","meta_value"=>$github_id));
if(is_wp_error($user_github) || !count($user_github)){
$username = $str['title'];
$login_name = wp_create_nonce($github_id);
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
$userdata=array(
'user_login' => $login_name,
'display_name' => $name,
'user_email' => $email,
'user_pass' => $random_password,
'nick_name' => $name
);
$user_id = wp_insert_user( $userdata );
wp_signon(array("user_login"=>$login_name,"user_password"=>$random_password),false);
update_user_meta($user_id ,"github_id",$github_id);
github_ouath_redirect();

}else{
wp_set_auth_cookie($user_github[0]->ID);
github_ouath_redirect();
}
}
}

function social_oauth_github(){
if (isset($_GET['code']) && isset($_GET['type']) && $_GET['type'] == 'github'){
github_oauth();
}
}
add_action('init','social_oauth_github');


function github_oauth_url(){
$url = 'https://github.com/login/oauth/authorize?client_id=' . GITHUB_APPID . '&scope=user&state=123&response_type=code&redirect_uri=' . urlencode (home_url('/?type=github') );
return $url;
}

Loading

0 comments on commit c525a60

Please sign in to comment.