1
0
mirror of https://github.com/e107inc/e107.git synced 2025-02-22 15:55:39 +01:00

128 lines
3.7 KiB
PHP
Raw Normal View History

2015-01-23 21:14:28 -08:00
<?php
/*!
* HybridAuth
* http://hybridauth.sourceforge.net | https://github.com/hybridauth/hybridauth
* (c) 2009-2011 HybridAuth authors | hybridauth.sourceforge.net/licenses.html
*/
/**
2016-02-28 12:03:55 -08:00
* Hybrid_Providers_GitHub
2015-01-23 21:14:28 -08:00
*/
class Hybrid_Providers_GitHub extends Hybrid_Provider_Model_OAuth2
2016-02-28 12:03:55 -08:00
{
// default permissions
2015-01-23 21:14:28 -08:00
// (no scope) => public read-only access (includes public user profile info, public repo info, and gists).
public $scope = "";
/**
2016-02-28 12:03:55 -08:00
* IDp wrappers initializer
2015-01-23 21:14:28 -08:00
*/
2016-02-28 12:03:55 -08:00
function initialize()
2015-01-23 21:14:28 -08:00
{
parent::initialize();
// Provider api end-points
$this->api->api_base_url = "https://api.github.com/";
$this->api->authorize_url = "https://github.com/login/oauth/authorize";
$this->api->token_url = "https://github.com/login/oauth/access_token";
}
/**
* load the user profile from the IDp api client
*/
function getUserProfile()
{
2016-02-28 12:03:55 -08:00
$data = $this->api->api( "user" );
2015-01-23 21:14:28 -08:00
if ( ! isset( $data->id ) ){
throw new Exception( "User profile request failed! {$this->providerId} returned an invalid response.", 6 );
}
2016-02-28 12:03:55 -08:00
$this->user->profile->identifier = @ $data->id;
2015-01-23 21:14:28 -08:00
$this->user->profile->displayName = @ $data->name;
$this->user->profile->description = @ $data->bio;
$this->user->profile->photoURL = @ $data->avatar_url;
2016-02-28 12:03:55 -08:00
$this->user->profile->profileURL = @ $data->html_url;
2015-01-23 21:14:28 -08:00
$this->user->profile->email = @ $data->email;
$this->user->profile->webSiteURL = @ $data->blog;
$this->user->profile->region = @ $data->location;
if( empty($this->user->profile->displayName) ){
$this->user->profile->displayName = @ $data->login;
}
// request user emails from github api
if( empty($data->email) ){
try{
$emails = $this->api->api("user/emails");
// fail gracefully, and let apps collect the email if not present
if (is_array($emails)) {
foreach ($emails as $email) {
if ($email instanceof stdClass
2016-02-28 12:03:55 -08:00
&& property_exists($email, 'primary')
2015-01-23 21:14:28 -08:00
&& true === $email->primary
2016-02-28 12:03:55 -08:00
&& property_exists($email, 'email')
2015-01-23 21:14:28 -08:00
) {
$this->user->profile->email = $email->email;
2017-10-30 12:46:53 -07:00
// record whether the email address is verified
if (property_exists($email, 'verified')
&& true === $email->verified
) {
$this->user->profile->emailVerified = $email->email;
}
2015-01-23 21:14:28 -08:00
break;
}
}
}
}
catch( GithubApiException $e ){
throw new Exception( "User email request failed! {$this->providerId} returned an error: $e", 6 );
}
}
return $this->user->profile;
}
2016-02-28 12:03:55 -08:00
/**
*
*/
function getUserContacts() {
// refresh tokens if needed
$this->refreshToken();
//
$response = array();
$contacts = array();
try {
$response = $this->api->api( "user/followers" );
2017-10-30 12:46:53 -07:00
} catch (Exception $e) {
2016-02-28 12:03:55 -08:00
throw new Exception("User contacts request failed! {$this->providerId} returned an error: $e");
}
//
if ( isset( $response ) ) {
foreach ($response as $contact) {
try {
$contactInfo = $this->api->api( "users/".$contact->login );
2017-10-30 12:46:53 -07:00
} catch (Exception $e) {
2016-02-28 12:03:55 -08:00
throw new Exception("Contact info request failed for user {$contact->login}! {$this->providerId} returned an error: $e");
}
//
$uc = new Hybrid_User_Contact();
//
$uc->identifier = $contact->id;
$uc->profileURL = @$contact->html_url;
$uc->webSiteURL = @$contact->blog;
$uc->photoURL = @$contact->avatar_url;
$uc->displayName = ( isset( $contactInfo->name )?( $contactInfo->name ):( $contact->login ) );
//$uc->description = ;
$uc->email = @$contactInfo->email;
//
$contacts[] = $uc;
}
}
return $contacts;
}
2015-01-23 21:14:28 -08:00
}