socialAuthOAuth10.class.php
3.17 KB
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
<?php
/**
* socialAuthOAuth10 Class
*
* @author Roman
* @version 1.0.20111010
*/
abstract class socialAuthOAuth10
{
///////////////////////////////////////////////////////////////////////////
protected $settings = false;
protected $oauth = false;
///////////////////////////////////////////////////////////////////////////
public function init()
{
$this->oauth = new OAuth(
$this->settings['consumer_key'],
$this->settings['consumer_secret'],
( isset($this->settings['signature_method']) ? $this->settings['signature_method'] : OAUTH_SIG_METHOD_HMACSHA1 ),
OAUTH_AUTH_TYPE_URI
);
if( !isset($_GET['oauth_verifier']) || strlen(trim($_GET['oauth_verifier']))<=30 )
{
$this->requestToken();
$this->authorize();
die();
}
else
{
if( isset($_SESSION['oauth']['oauth_token']) && isset($_GET['oauth_token']) && trim($_GET['oauth_token'])==trim($_SESSION['oauth']['oauth_token']) )
{
$this->accessToken();
$data = $this->fetchProfileInfo();
return $this->getProfileInfo( $data );
}
else
{
throw new kException( 'Invalid OAuth token.' );
}
}
return false;
}
///////////////////////////////////////////////////////////////////////////
protected function requestToken()
{
$request_token_info = $this->oauth->getRequestToken( $this->settings['request_token_uri'] );
$_SESSION['oauth'] = $request_token_info;
}
///////////////////////////////////////////////////////////////////////////
protected function authorize()
{
header( 'Location: '.$this->settings['authorize_uri'].'?oauth_token='.$_SESSION['oauth']['oauth_token'] );
die();
}
///////////////////////////////////////////////////////////////////////////
protected function accessToken()
{
$this->oauth->setToken( $_SESSION['oauth']['oauth_token'], $_SESSION['oauth']['oauth_token_secret'] );
$access_token_info = $this->oauth->getAccessToken(
$this->settings['access_token_uri'],
$_SESSION['oauth']['oauth_token'],
trim($_GET['oauth_verifier'])
);
$_SESSION['oauth'] = $access_token_info;
$this->oauth->setToken( $_SESSION['oauth']['oauth_token'], $_SESSION['oauth']['oauth_token_secret'] );
}
///////////////////////////////////////////////////////////////////////////
protected function fetchProfileInfo()
{
$data = $this->oauth->fetch( $this->settings['profile_uri'], array(), 'GET', array('Connection'=>'close') );
return json_decode($this->oauth->getLastResponse(), true);
}
///////////////////////////////////////////////////////////////////////////
abstract protected function getProfileInfo();
///////////////////////////////////////////////////////////////////////////
}