-
Notifications
You must be signed in to change notification settings - Fork 8
/
OpenPhotoOAuth.php
138 lines (128 loc) · 4.06 KB
/
OpenPhotoOAuth.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
132
133
134
135
136
137
138
<?php
if(!class_exists('OAuthSimple'))
require 'OAuthSimple.php';
class OpenPhotoOAuth
{
protected $client;
protected $host;
protected $consumerKey;
protected $consumerSecret;
protected $token;
protected $tokenSecret;
protected $version = '1';
protected $protocol = 'http';
protected $requestTokenPath= '/v%d/oauth/token/request';
protected $accessTokenPath = '/v%d/oauth/token/access';
protected $authorizePath = '/v%d/oauth/authorize';
public function __construct($host, $consumerKey = null, $consumerSecret = null, $token = null, $tokenSecret = null)
{
$this->host = $host;
$this->consumerKey = $consumerKey;
$this->consumerSecret = $consumerSecret;
$this->token = $token;
$this->tokenSecret = $tokenSecret;
}
public function get($endpoint, $params = null)
{
$curlEndpoint = $endpoint;
if(!empty($params))
$curlEndpoint .= sprintf('?%s', http_build_query($params));
$ch = curl_init($this->constructEndpoint($curlEndpoint, true, $params));
if(!empty($this->consumerKey))
{
$client = new OAuthSimple($this->consumerKey, $this->consumerSecret);
$request = $client->sign(
array(
'action' => 'GET',
'path' => $this->constructEndpoint($endpoint),
'version' => '1.0a',
'parameters' => $params,
'signatures' =>
array(
'consumer_key' => $this->consumerKey,
'consumer_secret' => $this->consumerSecret,
'access_token' => $this->token,
'access_secret' => $this->tokenSecret
)
)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: {$request['header']}"));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$resp = curl_exec($ch);
curl_close($ch);
return $resp;
}
public function getAccessToken($params = null)
{
$client = new OAuthSimple($this->consumerKey, $this->consumerSecret);
}
public function getAuthorizeUrl($token, $params = null)
{
$client = new OAuthSimple($this->consumerKey, $this->consumerSecret);
$request = $client->sign(
array(
'path' => '',
'parameters' => $params,
'version' => '1.0a',
'signatures' => array(
'consumer_key' => $this->consumerKey,
'consumer_secret' => $this->consumerSecret
)
)
);
return $request['signed_url'];
}
public function getRequestToken($params = null)
{
}
public function post($endpoint, $params = null)
{
$ch = curl_init($this->constructEndpoint($endpoint, true));
if(!empty($this->consumerKey))
{
$client = new OAuthSimple($this->consumerKey, $this->consumerSecret);
$request = $client->sign(
array(
'action' => 'POST',
'path' => $this->constructEndpoint($endpoint),
'version' => '1.0a',
'parameters' => $params,
'signatures' =>
array(
'consumer_key' => $this->consumerKey,
'consumer_secret' => $this->consumerSecret,
'access_token' => $this->token,
'access_secret' => $this->tokenSecret
)
)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: {$request['header']}"));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
if(!empty($params))
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$resp = curl_exec($ch);
curl_close($ch);
return $resp;
}
public function useSSL($bool)
{
$this->protocol = $bool ? 'https' : 'http';
}
private function constructEndpoint($endpoint, $includeConsumerKey = false)
{
if($includeConsumerKey)
{
if(stristr($endpoint, '?') === false)
return sprintf('%s://%s%s?oauth_consumer_key=%s', $this->protocol, $this->host, $endpoint, $this->consumerKey);
else
return sprintf('%s://%s%s&oauth_consumer_key=%s', $this->protocol, $this->host, $endpoint, $this->consumerKey);
}
else
{
return sprintf('%s://%s%s', $this->protocol, $this->host, $endpoint);
}
}
}