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