]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twitteroauthclient.php
Merge branch 'master' into testing
[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 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         parent::__construct($consumer_key, $consumer_secret,
65                             $oauth_token, $oauth_token_secret);
66     }
67
68     // XXX: the following two functions are to support the horrible hack
69     // of using the credentils field in Foreign_link to store both
70     // the access token and token secret.  This hack should go away with
71     // 0.9, in which we can make DB changes and add a new column for the
72     // token itself.
73
74     static function packToken($token)
75     {
76         return implode(chr(0), array($token->key, $token->secret));
77     }
78
79     static function unpackToken($str)
80     {
81         $vals = explode(chr(0), $str);
82         return new OAuthToken($vals[0], $vals[1]);
83     }
84
85     static function isPackedToken($str)
86     {
87         if (strpos($str, chr(0)) === false) {
88             return false;
89         } else {
90             return true;
91         }
92     }
93
94     /**
95      * Gets a request token from Twitter
96      *
97      * @return OAuthToken $token the request token
98      */
99     function getRequestToken()
100     {
101         return parent::getRequestToken(
102             self::$requestTokenURL,
103             common_local_url('twitterauthorization')
104         );
105     }
106
107     /**
108      * Builds a link to Twitter's endpoint for authorizing a request token
109      *
110      * @param OAuthToken $request_token token to authorize
111      *
112      * @return the link
113      */
114     function getAuthorizeLink($request_token, $signin = false)
115     {
116         $url = ($signin) ? self::$signinUrl : self::$authorizeURL;
117
118         return parent::getAuthorizeLink($url,
119                                         $request_token,
120                                         common_local_url('twitterauthorization'));
121     }
122
123     /**
124      * Fetches an access token from Twitter
125      *
126      * @param string $verifier 1.0a verifier
127      *
128      * @return OAuthToken $token the access token
129      */
130     function getAccessToken($verifier = null)
131     {
132         return parent::getAccessToken(
133             self::$accessTokenURL,
134             $verifier
135         );
136     }
137
138     /**
139      * Calls Twitter's /account/verify_credentials API method
140      *
141      * @return mixed the Twitter user
142      */
143     function verifyCredentials()
144     {
145         $url          = 'https://twitter.com/account/verify_credentials.json';
146         $response     = $this->oAuthGet($url);
147         $twitter_user = json_decode($response);
148         return $twitter_user;
149     }
150
151     /**
152      * Calls Twitter's /statuses/update API method
153      *
154      * @param string $status                text of the status
155      * @param int    $in_reply_to_status_id optional id of the status it's
156      *                                      a reply to
157      *
158      * @return mixed the status
159      */
160     function statusesUpdate($status, $in_reply_to_status_id = null)
161     {
162         $url      = 'https://twitter.com/statuses/update.json';
163         $params   = array('status' => $status,
164             'in_reply_to_status_id' => $in_reply_to_status_id);
165         $response = $this->oAuthPost($url, $params);
166         $status   = json_decode($response);
167         return $status;
168     }
169
170     /**
171      * Calls Twitter's /statuses/friends_timeline API method
172      *
173      * @param int $since_id show statuses after this id
174      * @param int $max_id   show statuses before this id
175      * @param int $cnt      number of statuses to show
176      * @param int $page     page number
177      *
178      * @return mixed an array of statuses
179      */
180     function statusesFriendsTimeline($since_id = null, $max_id = null,
181                                      $cnt = null, $page = null)
182     {
183
184         $url    = 'https://twitter.com/statuses/friends_timeline.json';
185         $params = array('since_id' => $since_id,
186                         'max_id' => $max_id,
187                         'count' => $cnt,
188                         'page' => $page);
189         $qry    = http_build_query($params);
190
191         if (!empty($qry)) {
192             $url .= "?$qry";
193         }
194
195         $response = $this->oAuthGet($url);
196         $statuses = json_decode($response);
197         return $statuses;
198     }
199
200     /**
201      * Calls Twitter's /statuses/friends API method
202      *
203      * @param int $id          id of the user whom you wish to see friends of
204      * @param int $user_id     numerical user id
205      * @param int $screen_name screen name
206      * @param int $page        page number
207      *
208      * @return mixed an array of twitter users and their latest status
209      */
210     function statusesFriends($id = null, $user_id = null, $screen_name = null,
211                              $page = null)
212     {
213         $url = "https://twitter.com/statuses/friends.json";
214
215         $params = array('id' => $id,
216                         'user_id' => $user_id,
217                         'screen_name' => $screen_name,
218                         'page' => $page);
219         $qry    = http_build_query($params);
220
221         if (!empty($qry)) {
222             $url .= "?$qry";
223         }
224
225         $response = $this->oAuthGet($url);
226         $friends  = json_decode($response);
227         return $friends;
228     }
229
230     /**
231      * Calls Twitter's /statuses/friends/ids API method
232      *
233      * @param int $id          id of the user whom you wish to see friends of
234      * @param int $user_id     numerical user id
235      * @param int $screen_name screen name
236      * @param int $page        page number
237      *
238      * @return mixed a list of ids, 100 per page
239      */
240     function friendsIds($id = null, $user_id = null, $screen_name = null,
241                          $page = null)
242     {
243         $url = "https://twitter.com/friends/ids.json";
244
245         $params = array('id' => $id,
246                         'user_id' => $user_id,
247                         'screen_name' => $screen_name,
248                         'page' => $page);
249         $qry    = http_build_query($params);
250
251         if (!empty($qry)) {
252             $url .= "?$qry";
253         }
254
255         $response = $this->oAuthGet($url);
256         $ids      = json_decode($response);
257         return $ids;
258     }
259
260 }