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