Skip to content

Instantly share code, notes, and snippets.

@YJSoft
Last active September 12, 2015 12:44
Show Gist options
  • Save YJSoft/25f06c8a044866e55c0d to your computer and use it in GitHub Desktop.
Save YJSoft/25f06c8a044866e55c0d to your computer and use it in GitHub Desktop.
The github library of the socialxe module
<?php
/**
* @class libraryGithub
* @author YJSoft (http://yjsoft.pe.kr)
* @brief The github library of the socialxe module
*/
const GITHUB_OAUTH2_URI = 'https://github.com/login/oauth/';
const GITHUB_API_URI = 'https://api.github.com/';
class libraryGithub extends socialxeLibrary
{
/**
* @brief 인증 URL 생성
*/
function createAuthUrl($type)
{
$_SESSION['socialxe_auth_redirect_mid'] = Context::get('mid');
$_SESSION['socialxe_auth_redirect'] = Context::get('redirect');
$_SESSION['socialxe_auth_type'] = $type;
$_SESSION['socialxe_auth_state'] = md5(microtime().mt_rand());
$params = array(
'scope' => 'user',
'redirect_uri' => getNotEncodedFullUrl('', 'module', 'socialxe', 'act', 'procSocialxeCallback','service','github'),
'client_id' => $this->config->github_client_id,
'state' => $_SESSION['socialxe_auth_state']
);
return GITHUB_OAUTH2_URI . 'authorize?' . http_build_query($params, '', '&');
}
/**
* @brief 코드인증
*/
function authenticate()
{
$state = Context::get('state');
$code = Context::get('code');
if(!$code || !$_SESSION['socialxe_auth_state'] || $state != $_SESSION['socialxe_auth_state']) return new Object(-1, "msg_invalid_request");
$post_data = array(
'code' => $code,
'client_id' => $this->config->github_client_id,
'client_secret' => $this->config->github_client_secret
);
$token = $this->requestHttp('token', $post_data);
$this->setAccessToken($token['access_token']);
//no refresh avaliable for github
//$this->setRefreshToken('');
unset($_SESSION['socialxe_auth_state']);
return new Object();
}
/**
* @brief 토큰 새로고침
* @notice Github에서는 불가능
*/
function refreshToken()
{
return;
}
/**
* @brief 토큰파기
* @notice 미구현
*/
function revokeToken()
{
return;
/*
$post_data = array(
'sercive_provider' => 'NAVER',
'access_token' => $this->access_token,
'grant_type' => 'delete',
'client_id' => $this->config->github_client_id,
'client_secret' => $this->config->github_client_secret
);
$this->requestHttp('token', $post_data);
*/
}
/**
* @brief 계정 정보 가져오기
*/
function takeAccountInfo()
{
if(!$this->access_token) return;
$request_url = GITHUB_API_URI . 'user';
$profile = $this->requestHttp($request_url, array(), $this->access_token);
return $profile;
}
/**
* @brief 프로필 삽입
*/
function setProfile($profile_info = null)
{
if($profile_info){
$this->profile_info = $profile_info;
$profile = json_decode($profile_info, true);
}else{
$profile = $this->takeAccountInfo();
$this->profile_info = json_encode($profile);
}
if(!$profile) return new Object(-1, "msg_errer_api_connect");
$this->profile_id = $profile['login'];
$this->profile_email = $profile['email'];
$this->profile_name = $profile['login'];
$this->profile_image = $profile['avatar_url'];
if(!$profile['email'] || $profile['email']=='') return new Object(-1, "Github 계정에 Public email 주소가 없습니다.");
$this->profile_url = $profile['html_url'];
//프로필 인증
$this->profile_verified = true;
return new Object();
}
/**
* @brief 프로필 확장
* @notice Github에서는 불가능
*/
function getProfileExtend()
{
return;
/*
$profile = json_decode($this->profile_info, true);
$extend = new stdClass;
$extend->birthday = date('Y').preg_replace('/[^0-9]*?/', '', $profile['birthday']);
$email = explode('@', $profile['email']);
$extend->blog = 'http://blog.naver.com/'.$email[0];
$gender = $profile['gender'];
if($gender == 'M'){
$extend->gender = '남성';
}elseif($gender == 'F'){
$extend->gender = '여성';
}
return $extend;
*/
}
function getProfileImage()
{
return $this->profile_image;
//return preg_replace('/\?sz\=[0-9]+/is', '', $profile_image);
}
function requestHttp($request_url, $post_data = array(), $authorization = null)
{
if($post_data) $method = 'POST';
else $method = 'GET';
if($authorization){
$headers = array(
'Host' => 'api.github.com',
'Pragma' => 'no-cache',
'Accept' => 'application/json',
'Authorization' => sprintf("token %s", $authorization)
);
}
$request_config = array(
'ssl_verify_peer' => false
);
if($request_url == 'token'){
$request_url = GITHUB_OAUTH2_URI . 'access_token';
}
$retry = 0;
while ($retry <= 3) {
$buff = FileHandler::getRemoteResource($request_url, null, 10, $method, 'application/x-www-form-urlencoded', $headers, array(), $post_data, $request_config);
if(!is_null($buff)) break;
$retry++;
}
if(strpos($buff, '<?xml') !== false){
$oXmlParser = new XmlParser();
$xml_obj = $oXmlParser->parse($buff);
$response = $xml_obj->data->response;
$buff = array();
$xml_list = get_object_vars($response);
foreach($xml_list as $key=> $val){
$buff[$key] = $val->body;
}
}elseif(strpos($buff, 'access_token=')!==false){
$buff2 = array();
parse_str($buff,$buff2);
$buff = $buff2;
}else{
$buff = json_decode($buff, true);
}
return $buff;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment