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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
<?php
/**
* @author Dimmduh
* @email dimmduh@gmail.com
*/
class API{
private $service;
private $auth;
private $token;
private $source = 'GoogleReaderAPIClass-0.1';
private $client = 'scroll';
private $account_type = 'HOSTED_OR_GOOGLE';
private $clientlogin_url = 'https://www.google.com/accounts/ClientLogin';
private $reader_api_url = 'http://www.google.com/reader/api/0/';
private $session_var_auth_name = 'google_auth';
public function __construct( $email, $password, $service = 'reader' ){
if (isset( $service ) ){
$this -> service = $service;
}
/* if ( isset($_SESSION[ $this -> session_var_auth_name ] ) ){
$this -> auth = $_SESSION[ $this -> session_var_auth_name ];
echo "Loading";
} else { */
$this -> clientLogin( $email, $password );
$this -> get_token();
/* } */
}
private function request( $url, $type = 'get', $headers = false, $fields = false, $cookie = false){
$curl = curl_init();
if ( $fields ){
if ($type == 'get'){
$url .= '?'.http_build_query( $fields );
} else {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($fields) );
}
}
if ( $headers ){
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
if ( $cookie ){
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
}
curl_setopt($curl, CURLOPT_URL, $url);
if (strpos($url, 'https://') !== false){
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
$response = array();
$response['text'] = curl_exec($curl);
$response['info'] = curl_getinfo( $curl);
$response['code'] = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
$response['body'] = substr( $response['text'], $response['info']['header_size'] );
curl_close( $curl );
return $response;
}
private function request2google( $url, $type = 'get', $headers = false, $fields = false ){
if ( $this -> auth ){
$headers[] = 'Content-type: application/x-www-form-urlencoded';
$headers[] = 'Authorization: GoogleLogin auth='.$this -> auth;
if ( strpos( $url, 'http://') === false && strpos( $url, 'https://' ) === false ){
$url = $this -> reader_api_url.$url;
}
$response = $this -> request( $url, $type, $headers, $fields);
if ( $response['code'] == 200 ){
if ( isset( $fields['output'] ) ){
switch ($fields['output']){
case 'xml':
return (new SimpleXMLElement( $response['body'] ) );
break;
case 'json':
default:
return json_decode( $response['body'] );
break;
}
} else {
return $response['body'];
}
} else {
Throw new AutentificationException('Auth error: server response '.$response['code'] );
}
} else {
Throw new AutentificationException('Auth error: not finded Auth token');
}
}
public function get_tag_list( $output = 'json' ){
return $this -> request2google('tag/list', "get", false, array(
'output' => $output,
'ck' => time(),
'client' => $this -> client,
));
}
public function get_subscription_list( $output = 'json' ){
return $this -> request2google('subscription/list', "get", false, array(
'output' => $output,
'ck' => time(),
'client' => $this -> client,
));
}
public function get_preference_list( $output = 'json' ){
return $this -> request2google('preference/list', "get", false, array(
'output' => $output,
'ck' => time(),
'client' => $this -> client,
));
}
public function get_unread_count( $output = 'json' ){
return $this -> request2google('unread-count', "get", false, array(
'all' => true,
'output' => $output,
'ck' => time(),
'client' => $this -> client,
));
}
public function get_user_info( $output = 'json' ){
return $this -> request2google('user-info', "get", false, array(
'output' => $output,
'ck' => time(),
'client' => $this -> client,
));
}
private function get_token(){
$this -> token = $this -> request2google('token');
}
//get contents functions
/*
r - order
r = n - new items
r = o - old items
r = a - auto sort
*/
private function get_content( $content_url = '', $number = 20, $order = 'n', $exclude_target = '', $start_time = '', $continuation = ''){
$fields = array(
'ck' => time(),
'client' => $this -> client,
'n' => $number,
'r' => $order,
'output' => 'json',
);
if ( !empty($exclude_target) ){$fields['xt'] = $exclude_target;}
if ( !empty($start_time) ){$fields['ot'] = $start_time;}
if ( !empty($continuation) ){$fields['c'] = $continuation;}
return $this -> request2google('stream/contents/'.Utils::urlencode( $content_url ), 'get', false, $fields);
}
public function get_content_feed( $feed_url = '', $number = 20, $order = 'n', $exclude_target = '', $start_time = '', $continuation = ''){
return $this -> get_content( $feed_url, $number, $order, $exclude_target, $start_time, $continuation );
}
public function get_content_by_label( $label = '', $number = 20, $order = 'n', $exclude_target = '', $start_time = '', $continuation = ''){
return $this -> get_content( (strpos($label, '/') === false?'user/-/label/':'').$label, $number, $order, $exclude_target, $start_time, $continuation );
}
public function get_content_by_state( $state = '', $number = 20, $order = 'n', $exclude_target = '', $start_time = '', $continuation = ''){
return $this -> get_content( (strpos($state, '/') === false?'user/-/state/com.google/':'').$state, $number, $order, $exclude_target, $start_time, $continuation );
}
public function get_unread( $number = 20, $order = 'n' ){
return $this ->get_content_by_state('reading-list', $number, $order, 'user/-/state/com.google/read');
}
public function get_starred($number = 20, $order = 'n'){
return $this ->get_content_by_state('starred', $number, $order);
}
/*
Edit functions
*/
private function edit_do( $api_function , $post_fields ){
$post_fields['T'] = $this -> token;
if ( $this -> request2google( $api_function, "post", false, $post_fields ) == "OK"){
return true;
} else {
return false;
}
}
/* public function edit_subscription(
s return $this -> edit_do( 'subscription/edit',
} */
public function set_state( $itemId, $state = 'read'){
$post_fields = array(
"i" => $itemId,
"a" => 'user/-/state/com.google/'.$state,
);
//print_r( $post_fields );
return $this ->edit_do('edit-tag?client='.$this -> client, $post_fields);
}
private function clientLogin( $email, $password ){
$response = $this -> request( $this -> clientlogin_url, 'post', false, array(
"accountType" => $this -> account_type,
"Email" => $email,
"Passwd" => $password,
"service" => $this -> service,
"source" => $this -> source,
));
if ( $response['code'] == 200) {
preg_match("/Auth=([a-z0-9_\-]+)/i", $response['body'], $matches_auth);
if ($matches_auth[1]){
$this -> auth = $matches_auth[1];
$_SESSION[ $this -> session_var_auth_name ] = $this -> auth;
return true;
} else {
Throw new AutentificationException('Auth error: not finded Auth token in response');
}
} else {
Throw new AutentificationException('Auth error: server response '.$response['code'] );
}
}
}
//Exceptions
class AutentificationException extends Exception {}
|