]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twitteroauthclient.php
Fix for ticket 2756 - Calls to OAuth endpoints are redirected to the
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitteroauthclient.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Class for doing OAuth calls against Twitter
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Integration
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2009-2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * Class for talking to the Twitter API with OAuth.
36  *
37  * @category Integration
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  *
43  */
44 class TwitterOAuthClient extends OAuthClient
45 {
46     public static $requestTokenURL = 'https://twitter.com/oauth/request_token';
47     public static $authorizeURL    = 'https://twitter.com/oauth/authorize';
48     public static $signinUrl       = 'https://twitter.com/oauth/authenticate';
49     public static $accessTokenURL  = 'https://twitter.com/oauth/access_token';
50
51     /**
52      * Constructor
53      *
54      * @param string $oauth_token        the user's token
55      * @param string $oauth_token_secret the user's token secret
56      *
57      * @return nothing
58      */
59     function __construct($oauth_token = null, $oauth_token_secret = null)
60     {
61         $consumer_key    = common_config('twitter', 'consumer_key');
62         $consumer_secret = common_config('twitter', 'consumer_secret');
63
64         if (empty($consumer_key) && empty($consumer_secret)) {
65             $consumer_key = common_config(
66                 'twitter',
67                 'global_consumer_key'
68             );
69             $consumer_secret = common_config(
70                 'twitter',
71                 'global_consumer_secret'
72             );
73         }
74
75         parent::__construct(
76             $consumer_key,
77             $consumer_secret,
78             $oauth_token,
79             $oauth_token_secret
80         );
81     }
82
83     // XXX: the following two functions are to support the horrible hack
84     // of using the credentils field in Foreign_link to store both
85     // the access token and token secret.  This hack should go away with
86     // 0.9, in which we can make DB changes and add a new column for the
87     // token itself.
88
89     static function packToken($token)
90     {
91         return implode(chr(0), array($token->key, $token->secret));
92     }
93
94     static function unpackToken($str)
95     {
96         $vals = explode(chr(0), $str);
97         return new OAuthToken($vals[0], $vals[1]);
98     }
99
100     static function isPackedToken($str)
101     {
102         if (strpos($str, chr(0)) === false) {
103             return false;
104         } else {
105             return true;
106         }
107     }
108
109     /**
110      * Gets a request token from Twitter
111      *
112      * @return OAuthToken $token the request token
113      */
114     function getRequestToken()
115     {
116         return parent::getRequestToken(
117             self::$requestTokenURL,
118             common_local_url('twitterauthorization')
119         );
120     }
121
122     /**
123      * Builds a link to Twitter's endpoint for authorizing a request token
124      *
125      * @param OAuthToken $request_token token to authorize
126      *
127      * @return the link
128      */
129     function getAuthorizeLink($request_token, $signin = false)
130     {
131         $url = ($signin) ? self::$signinUrl : self::$authorizeURL;
132
133         return parent::getAuthorizeLink($url,
134                                         $request_token,
135                                         common_local_url('twitterauthorization'));
136     }
137
138     /**
139      * Fetches an access token from Twitter
140      *
141      * @param string $verifier 1.0a verifier
142      *
143      * @return OAuthToken $token the access token
144      */
145     function getAccessToken($verifier = null)
146     {
147         return parent::getAccessToken(
148             self::$accessTokenURL,
149             $verifier
150         );
151     }
152
153     /**
154      * Calls Twitter's /account/verify_credentials API method
155      *
156      * @return mixed the Twitter user
157      */
158     function verifyCredentials()
159     {
160         $url          = 'https://twitter.com/account/verify_credentials.json';
161         $response     = $this->oAuthGet($url);
162         $twitter_user = json_decode($response);
163         return $twitter_user;
164     }
165
166     /**
167      * Calls Twitter's /statuses/update API method
168      *
169      * @param string $status  text of the status
170      * @param mixed  $params  optional other parameters to pass to Twitter,
171      *                        as defined. For back-compatibility, if an int
172      *                        is passed we'll consider it a reply-to ID.
173      *
174      * @return mixed the status
175      */
176     function statusesUpdate($status, $params=array())
177     {
178         $url      = 'https://twitter.com/statuses/update.json';
179         if (is_numeric($params)) {
180             $params = array('in_reply_to_status_id' => intval($params));
181         }
182         $params['status'] = $status;
183         // We don't have to pass 'source' as the oauth key is tied to an app.
184
185         $response = $this->oAuthPost($url, $params);
186         $status   = json_decode($response);
187         return $status;
188     }
189
190     /**
191      * Calls Twitter's /statuses/home_timeline API method
192      *
193      * @param int $since_id show statuses after this id
194      * @param int $max_id   show statuses before this id
195      * @param int $cnt      number of statuses to show
196      * @param int $page     page number
197      *
198      * @return mixed an array of statuses
199      */
200     function statusesHomeTimeline($since_id = null, $max_id = null,
201                                   $cnt = null, $page = null)
202     {
203
204         $url    = 'https://twitter.com/statuses/home_timeline.json';
205
206         $params = array('include_entities' => 'true');
207
208         if (!empty($since_id)) {
209             $params['since_id'] = $since_id;
210         }
211         if (!empty($max_id)) {
212             $params['max_id'] = $max_id;
213         }
214         if (!empty($cnt)) {
215             $params['count'] = $cnt;
216         }
217         if (!empty($page)) {
218             $params['page'] = $page;
219         }
220
221         $response = $this->oAuthGet($url, $params);
222         $statuses = json_decode($response);
223         return $statuses;
224     }
225
226     /**
227      * Calls Twitter's /statuses/friends API method
228      *
229      * @param int $id          id of the user whom you wish to see friends of
230      * @param int $user_id     numerical user id
231      * @param int $screen_name screen name
232      * @param int $page        page number
233      *
234      * @return mixed an array of twitter users and their latest status
235      */
236     function statusesFriends($id = null, $user_id = null, $screen_name = null,
237                              $page = null)
238     {
239         $url = "https://twitter.com/statuses/friends.json";
240
241         $params = array();
242
243         if (!empty($id)) {
244             $params['id'] = $id;
245         }
246
247         if (!empty($user_id)) {
248             $params['user_id'] = $user_id;
249         }
250
251         if (!empty($screen_name)) {
252             $params['screen_name'] = $screen_name;
253         }
254
255         if (!empty($page)) {
256             $params['page'] = $page;
257         }
258
259         $response = $this->oAuthGet($url, $params);
260         $friends  = json_decode($response);
261         return $friends;
262     }
263
264     /**
265      * Calls Twitter's /statuses/friends/ids API method
266      *
267      * @param int $id          id of the user whom you wish to see friends of
268      * @param int $user_id     numerical user id
269      * @param int $screen_name screen name
270      * @param int $page        page number
271      *
272      * @return mixed a list of ids, 100 per page
273      */
274     function friendsIds($id = null, $user_id = null, $screen_name = null,
275                          $page = null)
276     {
277         $url = "https://twitter.com/friends/ids.json";
278
279         $params = array();
280
281         if (!empty($id)) {
282             $params['id'] = $id;
283         }
284
285         if (!empty($user_id)) {
286             $params['user_id'] = $user_id;
287         }
288
289         if (!empty($screen_name)) {
290             $params['screen_name'] = $screen_name;
291         }
292
293         if (!empty($page)) {
294             $params['page'] = $page;
295         }
296
297         $response = $this->oAuthGet($url, $params);
298         $ids      = json_decode($response);
299         return $ids;
300     }
301
302     /**
303      * Calls Twitter's /statuses/retweet/id.json API method
304      *
305      * @param int $id id of the notice to retweet
306      *
307      * @return retweeted status
308      */
309
310     function statusesRetweet($id)
311     {
312         $url = "http://api.twitter.com/1/statuses/retweet/$id.json";
313         $response = $this->oAuthPost($url);
314         $status = json_decode($response);
315         return $status;
316     }
317
318     /**
319      * Calls Twitter's /favorites/create API method
320      *
321      * @param int $id ID of the status to favorite
322      *
323      * @return object faved status
324      */
325
326     function favoritesCreate($id)
327     {
328         $url = "http://api.twitter.com/1/favorites/create/$id.json";
329         $response = $this->oAuthPost($url);
330         $status = json_decode($response);
331         return $status;
332     }
333
334     /**
335      * Calls Twitter's /favorites/destroy API method
336      *
337      * @param int $id ID of the status to unfavorite
338      *
339      * @return object unfaved status
340      */
341
342     function favoritesDestroy($id)
343     {
344         $url = "http://api.twitter.com/1/favorites/destroy/$id.json";
345         $response = $this->oAuthPost($url);
346         $status = json_decode($response);
347         return $status;
348     }
349
350     /**
351      * Calls Twitter's /statuses/destroy API method
352      *
353      * @param int $id ID of the status to destroy
354      *
355      * @return object destroyed
356      */
357
358     function statusesDestroy($id)
359     {
360         $url = "http://api.twitter.com/1/statuses/destroy/$id.json";
361         $response = $this->oAuthPost($url);
362         $status = json_decode($response);
363         return $status;
364     }
365 }