-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmr-oauth.php
133 lines (112 loc) · 3.37 KB
/
mr-oauth.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
<?php
require_once 'config';
require_once 'OAuth.php';
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();
$scope = "https://www.google.com/reader/api";
$consumer = new OAuthConsumer($consumer_key, $consumer_secret, null);
function parseTokenSecret($result)
{
$arr = array();
$param_pairs = explode('&', $result);
foreach ($param_pairs as $param_pair)
{
if (trim($param_pair) == '')
continue;
list($key, $value) = explode('=', $param_pair);
$arr[$key] = urldecode($value);
}
return $arr;
}
function requestOAuth($url, $post_data=null, $fetching=false)
{
global $sig_method, $consumer_key, $consumer_secret, $scope, $consumer;
if ($fetching)
{
$access_token = fetchConfig('access_token');
$access_token_secret = fetchConfig('access_token_secret');
}
else
{
$access_token = $_SESSION['access_token'];
$access_token_secret = $_SESSION['access_token_secret'];
}
$token = new OAuthConsumer($access_token, $access_token_secret);
if ($post_data === null)
$request = OAuthRequest::from_consumer_and_token(
$consumer, $token, 'GET', $url, array());
else
$request = OAuthRequest::from_consumer_and_token(
$consumer, $token, 'POST', $url, $post_data);
$request->sign_request($sig_method, $consumer, $token);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($request->to_header()));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($post_data !== null)
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($http_code, $result);
}
function oauthError($http_code, $result)
{
echo <<<_END
OAuth error ($http_code): $result<br />
You can try to refresh this page,
or <a href="mr-logout.php">logout</a> and login again.
_END;
exit;
}
function getActionToken()
{
global $scope;
if (isset($_SESSION['action_token']) && time() < $_SESSION['action_token_time'] + 60 * 25)
return $_SESSION['action_token'];
list($http_code, $result) = requestOAuth("$scope/0/token");
if ($http_code == 200)
{
$_SESSION['action_token'] = $result;
$_SESSION['action_token_time'] = time();
return $result;
}
else
oauthError($http_code, $result);
}
function markItem($id, $stream, $action)
{
global $scope;
$post_data = array(
'async' => 'true',
'a' => $action,
'i' => $id,
's' => $stream,
'T' => getActionToken());
list($http_code, $result) = requestOAuth("$scope/0/edit-tag", $post_data);
if ($http_code != 200)
oauthError($http_code, $result);
}
function markStar($id, $stream)
{
markItem($id, $stream, 'user/-/state/com.google/starred');
}
function markRead($id, $stream)
{
markItem($id, $stream, 'user/-/state/com.google/read');
}
function markLater($id, $stream)
{
markItem($id, $stream, 'user/-/label/later');
}
function getItem($id, $fetching=false)
{
global $scope;
$url = "$scope/0/stream/items/contents?i=$id";
list($http_code, $result) = requestOAuth($url, null, $fetching);
if ($http_code == 200)
return json_decode($result, true);
else
oauthError($http_code, $result);
}
?>