-
Notifications
You must be signed in to change notification settings - Fork 6
/
plurkOAuth.php
167 lines (154 loc) · 5.53 KB
/
plurkOAuth.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/*
* Cheng-Lung Sung (clsung_AT_gmail.com) http://clsung.tw/
*
* A demo PHP Library supporting Plurk OAuth API
*/
require_once('config.php');
require_once('OAuth.php');
define('PLURK_ACCESS_TOKEN_PATH', "/OAuth/access_token");
define('PLURK_AUTHORIZE_PATH', "/OAuth/authorize");
define('PLURK_REQUEST_TOKEN_PATH', "/OAuth/request_token");
class PlurkOAuth {
public $baseURL = 'https://www.plurk.com';
protected $status;
protected $response;
protected $request_token;
protected $access_token;
protected $verifier;
protected $sign_method;
protected $params;
protected $consumer;
protected $client;
function __construct($consumer_key, $consumer_secret,
$access_token = NULL, $access_secret = NULL) {
$this->consumer = new Consumer($consumer_key, $consumer_secret);
$this->sign_method = new SignatureMethod_HMAC_SHA1();
$this->params = array();
if (!empty($access_token) && !empty($access_secret)) {
$this->authorize($access_token, $access_secret);
}
}
function _get_request_token() {
unset($this->token);
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) == 'cli') { # cli mode
$content = $this->request(PLURK_REQUEST_TOKEN_PATH);
} else { # web mode
$content = $this->request(PLURK_REQUEST_TOKEN_PATH, null, array (
'oauth_callback' => CALLBACK_URL));
}
parse_str($content['content'], $this->request_token);
setcookie('token', $this->request_token['oauth_token']);
setcookie('secret', $this->request_token['oauth_token_secret']);
}
function _redirect_to_authorize() {
printf ('<a href="%s?oauth_token=%s">Get Authorized</a>', $this->baseURL.PLURK_AUTHORIZE_PATH,
$this->request_token['oauth_token']);
}
function _get_verifier() {
printf ("Access the following URL to get authorized: \n");
printf ("%s?oauth_token=%s\n", $this->baseURL.PLURK_AUTHORIZE_PATH,
$this->request_token['oauth_token']);
$handle = fopen ("php://stdin","r");
$yes_no = "n";
while (!strncmp($yes_no, "n", 1)) {
printf("Input the verification number: ");
$this->verifier = trim(fgets($handle));
printf("Are you sure? (y/N) ");
$yes_no = trim(fgets($handle));
if (strncmp($yes_no, "y", 1)) $yes_no = "n";
}
fclose($handle);
}
function _get_access_token() {
$content = $this->request(PLURK_ACCESS_TOKEN_PATH, null, array (
'oauth_token' => $this->request_token['oauth_token'],
'oauth_verifier' => $this->verifier,)
);
parse_str($content['content'], $this->access_token);
if (isset($this->access_token['oauth_token'])) {
// XXX: print_r only for your first convenient,
// you should store in config.php
print_r($this->access_token);
unset($this->token);
$this->token = new Token(
$this->access_token['oauth_token'],
$this->access_token['oauth_token_secret']);
return true;
}
return false;
}
function authorize_with_verifier($verifier = NULL) {
$this->verifier = $verifier;
$this->request_token['oauth_token'] = $_COOKIE['token'];
$this->request_token['oauth_token_secret'] = $_COOKIE['secret'];
$this->token = new Token(
$this->request_token['oauth_token'],
$this->request_token['oauth_token_secret']);
return $this->_get_access_token();
}
function get_access_token() {
return $this->access_token;
}
function authorize($access_token = NULL, $access_secret = NULL) {
$this->access_token['oauth_token'] = $access_token;
if (!empty($access_secret)) {
$this->access_token['oauth_token_secret'] = $access_secret;
return true;
} else {
unset($this->access_token);
$this->_get_request_token();
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) == 'cli') { # cli mode
$this->_get_verifier();
return $this->_get_access_token();
} else { # web mode
$this->_redirect_to_authorize();
return false;
}
}
}
function request($path, $params = null, $content = null) {
if (isset($params))
$params = array_merge ($params, $this->params);
else
$params = $this->params;
if (isset ($this->access_token['oauth_token']) &&
isset ($this->access_token['oauth_token_secret']))
$this->token = new Token(
$this->access_token['oauth_token'],
$this->access_token['oauth_token_secret']);
$client = new Client($this->consumer, $this->token);
$this->status = 0;
$this->response['reason'] = null;
try {
if (isset($content) and is_array($content)) {
$content_params = array();
foreach ($content as $key => $value) {
$content_params[] =
sprintf('%s=%s', rawurlencode($key), rawurlencode($value));
}
$content = implode('&', $content_params);
}
$resp = $client->request(
$this->baseURL.$path, "POST", /*$request->to_header()*/null, $content);
if ($json = json_decode($resp))
$resp = $json;
if (isset($resp->error_text)) {
$this->status = -1;
$this->response['body'] = null;
$this->response['reason'] = $resp->error_text;
} else
$this->response['body'] = $resp;
} catch (PlurkOAuthException $e) {
$this->status = -1;
$this->response['reason'] = $e->getMessage();
}
return array( 'content' => $this->response['body'],
'code' => $this->status,
'reason' => $this->response['reason'],
);
}
};
?>