-
Notifications
You must be signed in to change notification settings - Fork 6
/
class-wp-codebird.php
356 lines (308 loc) · 9.54 KB
/
class-wp-codebird.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
* An extension of the Codebird class to use Wordpress' HTTP API instead of
* cURL.
*
* @version 1.1.2
*/
class WP_Codebird extends \Codebird\Codebird {
/**
* The current singleton instance
*/
private static $_instance = null;
/**
* The file formats extensions twitter supports
* @var array
*/
protected $_supported_media_files_extensions = array( 'gif', 'jpg', 'jpeg', 'png' );
/**
* Returns singleton class instance
* Always use this method unless you're working with multiple authenticated
* users at once.
*
* This method had to be overloaded because self was used
* in the references instead of get_called_class. So the method was always
* returning instances of Codebird instead of WP_Codebird.
*
* @return Codebird The instance
*/
public static function getInstance() {
if ( self::$_instance == null ) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Overload magic __call() to transparently intercept Exceptions
*
* Most exceptions encountered in production are API timeouts - this will
* transparently handle these Exceptions to prevent fatal errors
*/
public function __call( $function, $arguments ) {
try {
return parent::__call( $function, $arguments );
} catch ( Exception $e ) {
return array();
}
}
/**
* Calls the API using Wordpress' HTTP API.
*
* @since 0.1.0
* @see Codebird::_callApi
*
* @param string $httpmethod The HTTP method to use for making the request
* @param string $method The API method to call
* @param string $method_template The templated API method to call
* @param array optional $params The parameters to send along
* @param bool optional $multipart Whether to use multipart/form-data
*
* @return mixed The API reply, encoded in the set return_format.
*/
protected function _callApi( $httpmethod, $method, $method_template, $params = array(), $multipart = false, $app_only_auth = false ) {
$url = $this->_getEndpoint( $method, $method_template );
$url_with_params = null;
$authorization = null;
$remote_params = array(
'method' => $httpmethod,
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => null,
'cookies' => array(),
'sslverify' => false
);
if ( 'GET' == $httpmethod ) {
$url_with_params = $url;
if ( count( $params ) > 0 ) {
$url_with_params .= '?' . http_build_query( $params );
}
$authorization = $this->_sign( $httpmethod, $url, $params );
$url = $url_with_params;
}
else {
if ( $multipart ) {
$authorization = $this->_sign( $httpmethod, $url, array() );
$params = $this->_buildMultipart( $method_template, $params );
// Add the boundaries
$first_newline = strpos( $params, "\r\n" );
$multipart_boundary = substr( $params, 2, $first_newline - 2 );
$remote_params['headers']['Content-Length'] = strlen( $params );
$remote_params['headers']['Content-Type'] = 'multipart/form-data; boundary=' . $multipart_boundary;
}
else {
$authorization = $this->_sign( $httpmethod, $url, $params );
$params = http_build_query( $params );
}
$remote_params['body'] = $params;
}
if ( $app_only_auth ) {
if ( null == self::$_oauth_consumer_key ) {
throw new Exception( 'To make an app-only auth API request, the consumer key must be set' );
}
// automatically fetch bearer token, if necessary
if ( null == self::$_oauth_bearer_token ) {
$this->oauth2_token();
}
$authorization = 'Authorization: Bearer ' . self::$_oauth_bearer_token;
}
// Codebird::_sign() adds Authorization: to $authorization, but the WP HTTP API needs it separate
$authorization = trim( str_replace( 'Authorization:', '', $authorization ) );
if ( $authorization ) {
$remote_params['headers']['Authorization'] = $authorization;
$remote_params['headers']['Expect'] = '';
}
if ( 'GET' == $httpmethod ) {
$reply = wp_remote_get( $url, $remote_params );
}
else {
$reply = wp_remote_post( $url, $remote_params );
}
if ( isset( $reply ) ) {
if ( is_wp_error( $reply ) ) {
throw new Exception( $reply->get_error_message() );
}
else {
$httpstatus = $reply['response']['code'];
$reply = $this->_parseApiReply( $method_template, $reply );
if ( $this->_return_format == CODEBIRD_RETURNFORMAT_OBJECT ) {
$reply->httpstatus = $httpstatus;
}
else {
$reply['httpstatus'] = $httpstatus;
}
}
}
else {
throw new Exception( 'A reply was never generated. Some has gone horribly awry.' );
}
return $reply;
}
/**
* Gets the OAuth bearer token
*
* Overridden to use the WordPress HTTP API
*
* @return string The OAuth bearer token
*/
public function oauth2_token() {
if ( null == self::$_oauth_consumer_key ) {
throw new Exception( 'To obtain a bearer token, the consumer key must be set.' );
}
$post_fields = array(
'grant_type' => 'client_credentials'
);
$url = self::$_endpoint_oauth . 'oauth2/token';
$headers = array(
'Authorization' => 'Basic ' . base64_encode( self::$_oauth_consumer_key . ':' . self::$_oauth_consumer_secret ),
'Expect' => ''
);
$remote_params = array(
'method' => 'POST',
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => $headers,
'body' => $post_fields,
'cookies' => array(),
'sslverify' => false
);
$reply = wp_remote_post( $url, $remote_params );
$httpstatus = wp_remote_retrieve_response_code( $reply );
$reply = $this->_parseApiReply( 'oauth2/token', $reply );
if ( CODEBIRD_RETURNFORMAT_OBJECT == $this->_return_format ) {
$reply->httpstatus = $httpstatus;
if ( 200 == $httpstatus ) {
self::setBearerToken( $reply->access_token );
}
}
else {
$reply['httpstatus'] = $httpstatus;
if ( 200 == $httpstatus ) {
self::setBearerToken( $reply['access_token'] );
}
}
return $reply;
}
/**
* Parses the API reply to encode it in the set return_format.
*
* @since 0.1.0
* @see Codebird::_parseApiReply
*
* @param string $method The method that has been called
* @param string $reply The actual reply, JSON-encoded or URL-encoded
*
* @return array|object The parsed reply
*/
protected function _parseApiReply( $method, $reply ) {
// split headers and body
$http_response = $reply;
$headers = $http_response['headers'];
$reply = '';
if ( isset( $http_response['body'] ) ) {
$reply = $http_response['body'];
}
$need_array = $this->_return_format == CODEBIRD_RETURNFORMAT_ARRAY;
if ( $reply == '[]' ) {
return $need_array ? array() : new stdClass;
}
$parsed = array();
if ( $method == 'users/profile_image/:screen_name' ) {
// this method returns a 302 redirect, we need to extract the URL
if ( isset( $headers['Location'] ) ) {
$parsed = array( 'profile_image_url_https' => $headers['Location'] );
}
}
elseif ( ! $parsed = json_decode( $reply, $need_array ) ) {
if ( $reply ) {
$reply = explode( '&', $reply );
foreach ( $reply as $element ) {
if ( stristr( $element, '=' ) ) {
list( $key, $value ) = explode( '=', $element );
$parsed[$key] = $value;
}
else {
$parsed['message'] = $element;
}
}
}
}
if ( ! $need_array ) {
$parsed = ( object ) $parsed;
}
return $parsed;
}
/**
* Detect filenames in upload parameters,
* build multipart request from upload params
*
* @param string $method The API method to call
* @param array $params The parameters to send along
*
* @return void
*/
protected function _buildMultipart( $method, $params ) {
// well, files will only work in multipart methods
if ( ! $this->_detectMultipart( $method ) ) {
return;
}
// only check specific parameters
$possible_files = array(
// Tweets
'statuses/update_with_media' => 'media[]',
// Accounts
'account/update_profile_background_image' => 'image',
'account/update_profile_image' => 'image',
'account/update_profile_banner' => 'banner'
);
// method might have files?
if ( ! in_array( $method, array_keys( $possible_files ) ) ) {
return;
}
$possible_files = explode( ' ', $possible_files[$method] );
$multipart_border = '--------------------' . $this->_nonce();
$multipart_request = '';
foreach ( $params as $key => $value ) {
// is it an array?
if ( is_array( $value ) ) {
_doing_it_wrong(
'_buildMultiPart()',
'Using URL-encoded parameters is not supported for uploading media.',
'3.7.1'
);
continue;
}
$multipart_request .=
'--' . $multipart_border . "\r\n"
. 'Content-Disposition: form-data; name="' . $key . '"';
// check for filenames
if ( in_array( $key, $possible_files ) ) {
if (
in_array( strtolower( end( explode( ".", $value ) ) ), $this->_supported_media_files_extensions )
&& file_exists( $value )
&& is_readable( $value )
&& $data = getimagesize( $value )
) {
if ( // is it a supported image format?
in_array( $data[2], $this->_supported_media_files )
) {
// try to read the file
$data = file_get_contents( $value );
if ( strlen( $data ) == 0 ) {
continue;
}
$value = $data;
}
}
}
$multipart_request .=
"\r\n\r\n" . $value . "\r\n";
}
$multipart_request .= '--' . $multipart_border . '--';
return $multipart_request;
}
}