/
Viadeo.php
131 lines (111 loc) · 3.53 KB
/
Viadeo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/*!
* HybridAuth
* http://hybridauth.sourceforge.net | https://github.com/hybridauth/hybridauth
* (c) 2009-2011 HybridAuth authors | hybridauth.sourceforge.net/licenses.html
*/
/**
* Hybrid_Providers_Viadeo
*/
class Hybrid_Providers_Viadeo extends Hybrid_Provider_Model
{
/**
* IDp wrappers initializer
*/
function initialize()
{
if ( ! $this->config["keys"]["id"] || ! $this->config["keys"]["secret"] ){
throw new Exception( "Your application id and secret are required in order to connect to {$this->providerId}.", 4 );
}
require_once Hybrid_Auth::$config["path_libraries"] . "Viadeo/ViadeoAPI.php";
if( $this->token( "access_token" ) ){
$this->api = new ViadeoAPI( $this->token( "access_token" ) );
}
else{
$this->api = new ViadeoAPI();
}
$this->api->init(array(
'store' => true,
'client_id' => $this->config["keys"]["id"],
'client_secret' => $this->config["keys"]["secret"]
));
}
/**
* begin login step
*/
function loginBegin()
{
try {
$this->api->setRedirectURI( $this->endpoint );
$url = $this->api->getAuthorizationURL();
Hybrid_Auth::redirect( $url );
}
catch ( ViadeoException $e ){
throw new Exception( "Authentication failed! An error occured during {$this->providerId} authentication.", 5 );
}
}
/**
* finish login step
*/
function loginFinish()
{
try {
$this->api->setRedirectURI( $this->endpoint );
$this->api->setAccessTokenFromCode();
}
catch ( ViadeoException $e ){
throw new Exception( "Authentication failed! An error occured during {$this->providerId} authentication", 5 );
}
if ( ! $this->api->isAuthenticated() )
{
throw new Exception( "Authentication failed! An error occured during {$this->providerId} authentication", 5 );
}
// Store tokens
$this->token( "access_token", $this->api->getAccessToken() );
// set user as logged in
$this->setUserConnected();
}
/**
* logout
*/
function logout()
{
$this->api->disconnect();
parent::logout();
}
/**
* load the user profile from the IDp api client
*/
function getUserProfile()
{
try{
$data = $this->api->get("/me")->execute();
}
catch( ViadeoAPIException $e ){
throw new Exception( "User profile request failed! {$this->providerId} returned an error while requesting the user profile. $e.", 6 );
}
if ( ! is_object( $data ) )
{
throw new Exception( "User profile request failed! {$this->providerId} api returned an invalid response.", 6 );
}
$this->user->profile->identifier = @ $data->id;
$this->user->profile->displayName = @ $data->name;
$this->user->profile->firstName = @ $data->first_name;
$this->user->profile->lastName = @ $data->last_name;
$this->user->profile->profileURL = @ $data->link;
$this->user->profile->description = @ $data->headline;
$this->user->profile->photoURL = @ $data->picture_large;
$this->user->profile->gender = @ $data->gender;
if( $this->user->profile->gender == "F" ){
$this->user->profile->gender = "female";
}
elseif( $this->user->profile->gender == "M" ){
$this->user->profile->gender = "male";
}
$this->user->profile->country = @ $data->location->country;
$this->user->profile->region = @ $data->location->area;
$this->user->profile->city = @ $data->location->city;
$this->user->profile->zip = @ $data->location->zipcode;
return $this->user->profile;
}
}