]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitteroauthclient.php
change LACONICA to STATUSNET
[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 2008 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')) {
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     /**
85      * Builds a link to Twitter's endpoint for authorizing a request token
86      *
87      * @param OAuthToken $request_token token to authorize
88      *
89      * @return the link
90      */
91     function getAuthorizeLink($request_token)
92     {
93         return parent::getAuthorizeLink(self::$authorizeURL,
94                                         $request_token,
95                                         common_local_url('twitterauthorization'));
96     }
97
98     /**
99      * Calls Twitter's /account/verify_credentials API method
100      *
101      * @return mixed the Twitter user
102      */
103     function verifyCredentials()
104     {
105         $url          = 'https://twitter.com/account/verify_credentials.json';
106         $response     = $this->oAuthGet($url);
107         $twitter_user = json_decode($response);
108         return $twitter_user;
109     }
110
111     /**
112      * Calls Twitter's /stutuses/update API method
113      *
114      * @param string $status                text of the status
115      * @param int    $in_reply_to_status_id optional id of the status it's
116      *                                      a reply to
117      *
118      * @return mixed the status
119      */
120     function statusesUpdate($status, $in_reply_to_status_id = null)
121     {
122         $url      = 'https://twitter.com/statuses/update.json';
123         $params   = array('status' => $status,
124             'in_reply_to_status_id' => $in_reply_to_status_id);
125         $response = $this->oAuthPost($url, $params);
126         $status   = json_decode($response);
127         return $status;
128     }
129
130     /**
131      * Calls Twitter's /stutuses/friends_timeline API method
132      *
133      * @param int $since_id show statuses after this id
134      * @param int $max_id   show statuses before this id
135      * @param int $cnt      number of statuses to show
136      * @param int $page     page number
137      *
138      * @return mixed an array of statuses
139      */
140     function statusesFriendsTimeline($since_id = null, $max_id = null,
141                                      $cnt = null, $page = null)
142     {
143
144         $url    = 'https://twitter.com/statuses/friends_timeline.json';
145         $params = array('since_id' => $since_id,
146                         'max_id' => $max_id,
147                         'count' => $cnt,
148                         'page' => $page);
149         $qry    = http_build_query($params);
150
151         if (!empty($qry)) {
152             $url .= "?$qry";
153         }
154
155         $response = $this->oAuthGet($url);
156         $statuses = json_decode($response);
157         return $statuses;
158     }
159
160     /**
161      * Calls Twitter's /stutuses/friends API method
162      *
163      * @param int $id          id of the user whom you wish to see friends of
164      * @param int $user_id     numerical user id
165      * @param int $screen_name screen name
166      * @param int $page        page number
167      *
168      * @return mixed an array of twitter users and their latest status
169      */
170     function statusesFriends($id = null, $user_id = null, $screen_name = null,
171                              $page = null)
172     {
173         $url = "https://twitter.com/statuses/friends.json";
174
175         $params = array('id' => $id,
176                         'user_id' => $user_id,
177                         'screen_name' => $screen_name,
178                         'page' => $page);
179         $qry    = http_build_query($params);
180
181         if (!empty($qry)) {
182             $url .= "?$qry";
183         }
184
185         $response = $this->oAuthGet($url);
186         $friends  = json_decode($response);
187         return $friends;
188     }
189
190     /**
191      * Calls Twitter's /stutuses/friends/ids API method
192      *
193      * @param int $id          id of the user whom you wish to see friends of
194      * @param int $user_id     numerical user id
195      * @param int $screen_name screen name
196      * @param int $page        page number
197      *
198      * @return mixed a list of ids, 100 per page
199      */
200     function friendsIds($id = null, $user_id = null, $screen_name = null,
201                          $page = null)
202     {
203         $url = "https://twitter.com/friends/ids.json";
204
205         $params = array('id' => $id,
206                         'user_id' => $user_id,
207                         'screen_name' => $screen_name,
208                         'page' => $page);
209         $qry    = http_build_query($params);
210
211         if (!empty($qry)) {
212             $url .= "?$qry";
213         }
214
215         $response = $this->oAuthGet($url);
216         $ids      = json_decode($response);
217         return $ids;
218     }
219
220 }