]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitterbasicauthclient.php
Commit upstream updates to php-gettext after the 1.0.7 release (but in 2006! :P)
[quix0rs-gnu-social.git] / lib / twitterbasicauthclient.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  * Exception wrapper for cURL errors
36  *
37  * @category Integration
38  * @package  StatusNet
39  * @author Adrian Lang <mail@adrianlang.de>
40  * @author Brenda Wallace <shiny@cpan.org>
41  * @author Craig Andrews <candrews@integralblue.com>
42  * @author Dan Moore <dan@moore.cx>
43  * @author Evan Prodromou <evan@status.net>
44  * @author mEDI <medi@milaro.net>
45  * @author Sarven Capadisli <csarven@status.net>
46  * @author Zach Copley <zach@status.net> * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  *
49  */
50 class BasicAuthCurlException extends Exception
51 {
52 }
53
54 /**
55  * Class for talking to the Twitter API with HTTP Basic Auth.
56  *
57  * @category Integration
58  * @package  StatusNet
59  * @author   Zach Copley <zach@status.net>
60  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
61  * @link     http://status.net/
62  *
63  */
64 class TwitterBasicAuthClient
65 {
66     var $screen_name = null;
67     var $password    = null;
68
69     /**
70      * constructor
71      *
72      * @param Foreign_link $flink a Foreign_link storing the
73      *                            Twitter user's password, etc.
74      */
75     function __construct($flink)
76     {
77         $fuser             = $flink->getForeignUser();
78         $this->screen_name = $fuser->nickname;
79         $this->password    = $flink->credentials;
80     }
81
82     /**
83      * Calls Twitter's /statuses/update API method
84      *
85      * @param string $status                text of the status
86      * @param int    $in_reply_to_status_id optional id of the status it's
87      *                                      a reply to
88      *
89      * @return mixed the status
90      */
91     function statusesUpdate($status, $in_reply_to_status_id = null)
92     {
93         $url      = 'https://twitter.com/statuses/update.json';
94         $params   = array('status' => $status,
95                           'source' => common_config('integration', 'source'),
96                           'in_reply_to_status_id' => $in_reply_to_status_id);
97         $response = $this->httpRequest($url, $params);
98         $status   = json_decode($response);
99         return $status;
100     }
101
102     /**
103      * Calls Twitter's /statuses/friends_timeline API method
104      *
105      * @param int $since_id show statuses after this id
106      * @param int $max_id   show statuses before this id
107      * @param int $cnt      number of statuses to show
108      * @param int $page     page number
109      *
110      * @return mixed an array of statuses
111      */
112     function statusesFriendsTimeline($since_id = null, $max_id = null,
113                                      $cnt = null, $page = null)
114     {
115         $url    = 'https://twitter.com/statuses/friends_timeline.json';
116         $params = array('since_id' => $since_id,
117                         'max_id' => $max_id,
118                         'count' => $cnt,
119                         'page' => $page);
120         $qry    = http_build_query($params);
121
122         if (!empty($qry)) {
123             $url .= "?$qry";
124         }
125
126         $response = $this->httpRequest($url);
127         $statuses = json_decode($response);
128         return $statuses;
129     }
130
131     /**
132      * Calls Twitter's /statuses/friends API method
133      *
134      * @param int $id          id of the user whom you wish to see friends of
135      * @param int $user_id     numerical user id
136      * @param int $screen_name screen name
137      * @param int $page        page number
138      *
139      * @return mixed an array of twitter users and their latest status
140      */
141     function statusesFriends($id = null, $user_id = null, $screen_name = null,
142                              $page = null)
143     {
144         $url = "https://twitter.com/statuses/friends.json";
145
146         $params = array('id' => $id,
147                         'user_id' => $user_id,
148                         'screen_name' => $screen_name,
149                         'page' => $page);
150         $qry    = http_build_query($params);
151
152         if (!empty($qry)) {
153             $url .= "?$qry";
154         }
155
156         $response = $this->httpRequest($url);
157         $friends  = json_decode($response);
158         return $friends;
159     }
160
161     /**
162      * Calls Twitter's /statuses/friends/ids API method
163      *
164      * @param int $id          id of the user whom you wish to see friends of
165      * @param int $user_id     numerical user id
166      * @param int $screen_name screen name
167      * @param int $page        page number
168      *
169      * @return mixed a list of ids, 100 per page
170      */
171     function friendsIds($id = null, $user_id = null, $screen_name = null,
172                         $page = null)
173     {
174         $url = "https://twitter.com/friends/ids.json";
175
176         $params = array('id' => $id,
177                         'user_id' => $user_id,
178                         'screen_name' => $screen_name,
179                         'page' => $page);
180         $qry    = http_build_query($params);
181
182         if (!empty($qry)) {
183             $url .= "?$qry";
184         }
185
186         $response = $this->httpRequest($url);
187         $ids      = json_decode($response);
188         return $ids;
189     }
190
191     /**
192      * Make a HTTP request using cURL.
193      *
194      * @param string $url    Where to make the request
195      * @param array  $params post parameters
196      *
197      * @return mixed the request
198      */
199     function httpRequest($url, $params = null, $auth = true)
200     {
201         $options = array(
202                          CURLOPT_RETURNTRANSFER => true,
203                          CURLOPT_FAILONERROR    => true,
204                          CURLOPT_HEADER         => false,
205                          CURLOPT_FOLLOWLOCATION => true,
206                          CURLOPT_USERAGENT      => 'StatusNet',
207                          CURLOPT_CONNECTTIMEOUT => 120,
208                          CURLOPT_TIMEOUT        => 120,
209                          CURLOPT_HTTPAUTH       => CURLAUTH_ANY,
210                          CURLOPT_SSL_VERIFYPEER => false,
211
212                          // Twitter is strict about accepting invalid "Expect" headers
213
214                          CURLOPT_HTTPHEADER => array('Expect:')
215                          );
216
217         if (isset($params)) {
218             $options[CURLOPT_POST]       = true;
219             $options[CURLOPT_POSTFIELDS] = $params;
220         }
221
222         if ($auth) {
223             $options[CURLOPT_USERPWD] = $this->screen_name .
224               ':' . $this->password;
225         }
226
227         $ch = curl_init($url);
228         curl_setopt_array($ch, $options);
229         $response = curl_exec($ch);
230
231         if ($response === false) {
232             $msg  = curl_error($ch);
233             $code = curl_errno($ch);
234             throw new BasicAuthCurlException($msg, $code);
235         }
236
237         curl_close($ch);
238
239         return $response;
240     }
241
242 }