aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/libraries/send2kindle/api.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/3rdparty/libraries/send2kindle/api.php')
-rw-r--r--inc/3rdparty/libraries/send2kindle/api.php234
1 files changed, 234 insertions, 0 deletions
diff --git a/inc/3rdparty/libraries/send2kindle/api.php b/inc/3rdparty/libraries/send2kindle/api.php
new file mode 100644
index 00000000..564c171c
--- /dev/null
+++ b/inc/3rdparty/libraries/send2kindle/api.php
@@ -0,0 +1,234 @@
1<?php
2/**
3* @author Dimmduh
4* @email dimmduh@gmail.com
5*/
6class API{
7
8 private $service;
9 private $auth;
10 private $token;
11 private $source = 'GoogleReaderAPIClass-0.1';
12 private $client = 'scroll';
13 private $account_type = 'HOSTED_OR_GOOGLE';
14 private $clientlogin_url = 'https://www.google.com/accounts/ClientLogin';
15 private $reader_api_url = 'http://www.google.com/reader/api/0/';
16 private $session_var_auth_name = 'google_auth';
17
18
19 public function __construct( $email, $password, $service = 'reader' ){
20 if (isset( $service ) ){
21 $this -> service = $service;
22 }
23 /* if ( isset($_SESSION[ $this -> session_var_auth_name ] ) ){
24 $this -> auth = $_SESSION[ $this -> session_var_auth_name ];
25 echo "Loading";
26 } else { */
27 $this -> clientLogin( $email, $password );
28 $this -> get_token();
29 /* } */
30 }
31
32
33 private function request( $url, $type = 'get', $headers = false, $fields = false, $cookie = false){
34
35 $curl = curl_init();
36
37 if ( $fields ){
38 if ($type == 'get'){
39 $url .= '?'.http_build_query( $fields );
40 } else {
41 curl_setopt($curl, CURLOPT_POST, true);
42 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($fields) );
43 }
44 }
45 if ( $headers ){
46 curl_setopt($curl, CURLOPT_HEADER, true);
47 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
48 }
49 if ( $cookie ){
50 curl_setopt($curl, CURLOPT_COOKIE, $cookie);
51 }
52
53 curl_setopt($curl, CURLOPT_URL, $url);
54 if (strpos($url, 'https://') !== false){
55 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
56 }
57 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
58 curl_setopt($curl, CURLINFO_HEADER_OUT, true);
59
60 $response = array();
61 $response['text'] = curl_exec($curl);
62 $response['info'] = curl_getinfo( $curl);
63 $response['code'] = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
64 $response['body'] = substr( $response['text'], $response['info']['header_size'] );
65
66 curl_close( $curl );
67 return $response;
68 }
69
70 private function request2google( $url, $type = 'get', $headers = false, $fields = false ){
71 if ( $this -> auth ){
72 $headers[] = 'Content-type: application/x-www-form-urlencoded';
73 $headers[] = 'Authorization: GoogleLogin auth='.$this -> auth;
74
75 if ( strpos( $url, 'http://') === false && strpos( $url, 'https://' ) === false ){
76 $url = $this -> reader_api_url.$url;
77 }
78
79 $response = $this -> request( $url, $type, $headers, $fields);
80 if ( $response['code'] == 200 ){
81 if ( isset( $fields['output'] ) ){
82 switch ($fields['output']){
83 case 'xml':
84 return (new SimpleXMLElement( $response['body'] ) );
85 break;
86 case 'json':
87 default:
88 return json_decode( $response['body'] );
89 break;
90 }
91 } else {
92 return $response['body'];
93 }
94 } else {
95 Throw new AutentificationException('Auth error: server response '.$response['code'] );
96 }
97
98 } else {
99 Throw new AutentificationException('Auth error: not finded Auth token');
100 }
101 }
102
103 public function get_tag_list( $output = 'json' ){
104 return $this -> request2google('tag/list', "get", false, array(
105 'output' => $output,
106 'ck' => time(),
107 'client' => $this -> client,
108 ));
109 }
110 public function get_subscription_list( $output = 'json' ){
111 return $this -> request2google('subscription/list', "get", false, array(
112 'output' => $output,
113 'ck' => time(),
114 'client' => $this -> client,
115 ));
116 }
117 public function get_preference_list( $output = 'json' ){
118 return $this -> request2google('preference/list', "get", false, array(
119 'output' => $output,
120 'ck' => time(),
121 'client' => $this -> client,
122 ));
123 }
124 public function get_unread_count( $output = 'json' ){
125 return $this -> request2google('unread-count', "get", false, array(
126 'all' => true,
127 'output' => $output,
128 'ck' => time(),
129 'client' => $this -> client,
130 ));
131 }
132 public function get_user_info( $output = 'json' ){
133 return $this -> request2google('user-info', "get", false, array(
134 'output' => $output,
135 'ck' => time(),
136 'client' => $this -> client,
137 ));
138 }
139 private function get_token(){
140 $this -> token = $this -> request2google('token');
141 }
142
143 //get contents functions
144 /*
145 r - order
146 r = n - new items
147 r = o - old items
148 r = a - auto sort
149
150 */
151 private function get_content( $content_url = '', $number = 20, $order = 'n', $exclude_target = '', $start_time = '', $continuation = ''){
152 $fields = array(
153 'ck' => time(),
154 'client' => $this -> client,
155 'n' => $number,
156 'r' => $order,
157 'output' => 'json',
158 );
159 if ( !empty($exclude_target) ){$fields['xt'] = $exclude_target;}
160 if ( !empty($start_time) ){$fields['ot'] = $start_time;}
161 if ( !empty($continuation) ){$fields['c'] = $continuation;}
162
163 return $this -> request2google('stream/contents/'.Utils::urlencode( $content_url ), 'get', false, $fields);
164 }
165
166 public function get_content_feed( $feed_url = '', $number = 20, $order = 'n', $exclude_target = '', $start_time = '', $continuation = ''){
167 return $this -> get_content( $feed_url, $number, $order, $exclude_target, $start_time, $continuation );
168 }
169 public function get_content_by_label( $label = '', $number = 20, $order = 'n', $exclude_target = '', $start_time = '', $continuation = ''){
170 return $this -> get_content( (strpos($label, '/') === false?'user/-/label/':'').$label, $number, $order, $exclude_target, $start_time, $continuation );
171 }
172 public function get_content_by_state( $state = '', $number = 20, $order = 'n', $exclude_target = '', $start_time = '', $continuation = ''){
173 return $this -> get_content( (strpos($state, '/') === false?'user/-/state/com.google/':'').$state, $number, $order, $exclude_target, $start_time, $continuation );
174 }
175
176 public function get_unread( $number = 20, $order = 'n' ){
177 return $this ->get_content_by_state('reading-list', $number, $order, 'user/-/state/com.google/read');
178 }
179
180 public function get_starred($number = 20, $order = 'n'){
181 return $this ->get_content_by_state('starred', $number, $order);
182 }
183
184 /*
185 Edit functions
186 */
187 private function edit_do( $api_function , $post_fields ){
188 $post_fields['T'] = $this -> token;
189 if ( $this -> request2google( $api_function, "post", false, $post_fields ) == "OK"){
190 return true;
191 } else {
192 return false;
193 }
194 }
195
196 /* public function edit_subscription(
197 s return $this -> edit_do( 'subscription/edit',
198 } */
199 public function set_state( $itemId, $state = 'read'){
200 $post_fields = array(
201 "i" => $itemId,
202 "a" => 'user/-/state/com.google/'.$state,
203 );
204 //print_r( $post_fields );
205 return $this ->edit_do('edit-tag?client='.$this -> client, $post_fields);
206 }
207
208 private function clientLogin( $email, $password ){
209
210 $response = $this -> request( $this -> clientlogin_url, 'post', false, array(
211 "accountType" => $this -> account_type,
212 "Email" => $email,
213 "Passwd" => $password,
214 "service" => $this -> service,
215 "source" => $this -> source,
216 ));
217
218 if ( $response['code'] == 200) {
219 preg_match("/Auth=([a-z0-9_\-]+)/i", $response['body'], $matches_auth);
220 if ($matches_auth[1]){
221 $this -> auth = $matches_auth[1];
222 $_SESSION[ $this -> session_var_auth_name ] = $this -> auth;
223 return true;
224 } else {
225 Throw new AutentificationException('Auth error: not finded Auth token in response');
226 }
227 } else {
228 Throw new AutentificationException('Auth error: server response '.$response['code'] );
229 }
230 }
231}
232
233//Exceptions
234class AutentificationException extends Exception {} \ No newline at end of file