-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclass.googlevoice.php
327 lines (267 loc) · 6.99 KB
/
class.googlevoice.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* Google Voice Library.
*
* LICENSE:
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this work except in compliance with the License.
* You may obtain a copy of the License in the LICENSE file, or at:
* http://www.apache.org/licenses/LICENSE-2.0
*
* @copyright Copyright (c) 2012 Matthew Gates.
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://github.com/Geczy/google-voice-library
* @author Matthew Gates <[email protected]>
* @package Google Voice
*/
namespace Geczy\Voice;
class GoogleVoiceLibrary
{
/**
* API request URLs to Google Voice.
*
* @var array
*
* @access private
*/
private $urls = array(
// Authenticate
'login' => 'https://www.google.com/accounts/ClientLogin',
// Requests
'search' => 'https://www.google.com/voice/b/0/inbox/search/',
'get' => 'https://www.google.com/voice/b/0/request/messages/',
'send' => 'https://www.google.com/voice/b/0/sms/send/',
// Actions
'mark_read' => 'https://www.google.com/voice/b/0/inbox/mark/',
'archive' => 'https://www.google.com/voice/b/0/inbox/archiveMessages/',
'delete' => 'https://www.google.com/voice/b/0/inbox/deleteMessages/',
);
/**
* Username and password to the Google Voice account.
*
* @param string $user
* @param string $pass
*/
public function __construct( $user, $pass )
{
// Start the session.
if ( !isset( $_SESSION ) ) session_start();
// Preform authentication
$this->set_login_auth( $user, $pass );
}
/**
* Retrieve a page using CURL.
*
* @param string $url
* @param array $params (optional)
* @param unknown $post (optional)
* @return string
*/
private function get_page( $url, $params = array(), $post = true )
{
$login_auth = (empty($this->auth)) ? '' : $this->auth;
// GET request rather than POST
if ( !$post )
$url = $url . '?' . http_build_query( $params );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( "Authorization: GoogleLogin {$login_auth}", 'User-Agent: Mozilla/5.0' ) );
if ( $params && $post ) {
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
}
$response = curl_exec( $ch );
curl_close( $ch );
return $response;
}
/**
* Authenticate the Google Voice account.
*
* @param string $user
* @param string $pass
* @return string
*/
private function get_login_auth( $user, $pass )
{
// Auth has already been set.
if ( !empty( $_SESSION['Geczy']['login_auth'] ) )
return $_SESSION['Geczy']['login_auth'];
$params = array(
'accountType' => 'GOOGLE',
'Email' => $user,
'Passwd' => $pass,
'service' => 'grandcentral',
'source' => 'Geczy-Google-Voice-Library',
);
$results = $this->get_page( $this->urls['login'], $params );
$auth = $results ? strstr( trim( $results ), 'Auth=' ) : false;
return $auth;
}
/**
* Set the authentication token to a session.
*
* @param string $user
* @param string $pass
* @return string
*/
private function set_login_auth( $user, $pass )
{
$auth = $this->get_login_auth( $user, $pass );
if ( $auth ) {
$_SESSION['Geczy']['login_auth'] = $auth;
$this->auth = $auth;
}
return $auth;
}
/**
* String some Google Voice requests require.
*
* @return string
*/
public function get_rnrse()
{
if ( !empty( $_SESSION['Geczy']['rnr_se'] ) )
return $_SESSION['Geczy']['rnr_se'];
$result = json_decode( $this->get_page( $this->urls['get'] ) );
$_SESSION['Geczy']['rnr_se'] = $result->r;
return $result->r;
}
/**
* Search for a message.
*
* @param string $query
* @return object
*/
public function search( $query, $page = 1 )
{
$params = array(
'q' => $query,
'page' => 'p' . $page,
);
$result = $this->get_page( $this->urls['search'], $params, false );
$json = simplexml_load_string( $result );
return json_decode( (string) $json->json );
}
/**
* Delete a message.
*
* @param string $id
* @return object
*/
public function delete( $id )
{
$params = array(
'messages' => $id,
'trash' => 1,
'_rnr_se' => $this->get_rnrse(),
);
return json_decode( $this->get_page( $this->urls['delete'], $params ) );
}
/**
* Archive a message.
*
* @param string $id
* @return object
*/
public function archive( $id )
{
$params = array(
'messages' => $id,
'archive' => 1,
'_rnr_se' => $this->get_rnrse(),
);
return json_decode( $this->get_page( $this->urls['archive'], $params ) );
}
/**
* Mark a message as read.
*
* @param string $id
* @return object
*/
public function mark_read( $id )
{
$params = array(
'messages' => $id,
'read' => 1,
'_rnr_se' => $this->get_rnrse(),
);
return json_decode( $this->get_page( $this->urls['mark_read'], $params ) );
}
/**
* Send a text to a number.
*
* @param string $to
* @param string $msg
* @param string $id (optional)
* @return object
*/
public function send_text( $to, $msg, $id = '' )
{
$params = array(
'conversationId' => $id,
'phoneNumber' => $to,
'text' => $msg,
'_rnr_se' => $this->get_rnrse(),
);
return json_decode( $this->get_page( $this->urls['send'], $params ) );
}
/**
* Retrieve the current inbox.
*
* @param array $params (optional)
* @return array
*/
public function get_inbox( $params = array() )
{
$defaults = array(
'history' => false,
'onlyNew' => true,
'page' => 1,
);
$params = array_merge( $defaults, $params );
$json = json_decode( $this->get_page( $this->urls['get'].'?page='.$params['page'] ) );
$results = $this->parse_texts( $json, $params );
return $results;
}
/**
* Helper for formatting the inbox.
*
* @param object $data
* @param array $params
* @return array
*/
private function parse_texts( $data, $params )
{
$contacts = $data->contacts->contactPhoneMap;
$results = array(
'unread' => $data->unreadCounts->sms,
'total' => $data->totalSize,
);
foreach ( $data->messageList as $thread ) {
/* This message is already read, so skip */
if ( $params['onlyNew'] && $thread->isRead )
continue;
/* Extract just the information that's useful. */
$number = $thread->phoneNumber;
$results['texts'][$thread->id] = array(
'from' => $contacts->$number->name,
'number' => $thread->displayNumber,
'date' => $thread->displayStartDateTime,
'text' => $thread->children[count( $thread->children )-1]->message,
);
if ( $params['history'] ) {
foreach ( $thread->children as $child ) {
$results['texts'][$thread->id]['history'][] = array(
'from' => $child->type == 11 ? 'Me' : $results['texts'][$thread->id]['from'],
'time' => $child->displayStartDateTime,
'message' => $child->message,
);
}
}
}
return $results;
}
}