]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twitterbasicauthclient.php
2c18c94695619ef2ad37cbd648cc157962e7f796
[quix0rs-gnu-social.git] / plugins / TwitterBridge / 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  * General Exception wrapper for HTTP basic auth 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 BasicAuthException 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 mixed  $params  optional other parameters to pass to Twitter,
81      *                        as defined. For back-compatibility, if an int
82      *                        is passed we'll consider it a reply-to ID.
83      *
84      * @return mixed the status
85      */
86     function statusesUpdate($status, $in_reply_to_status_id = null)
87     {
88         $url      = 'https://twitter.com/statuses/update.json';
89         if (is_numeric($params)) {
90             $params = array('in_reply_to_status_id' => intval($params));
91         }
92         $params['status'] = $status;
93         $params['source'] = common_config('integration', 'source');
94         $response = $this->httpRequest($url, $params);
95         $status   = json_decode($response);
96         return $status;
97     }
98
99     /**
100      * Calls Twitter's /statuses/friends_timeline API method
101      *
102      * @param int $since_id show statuses after this id
103      * @param int $max_id   show statuses before this id
104      * @param int $cnt      number of statuses to show
105      * @param int $page     page number
106      *
107      * @return mixed an array of statuses
108      */
109     function statusesFriendsTimeline($since_id = null, $max_id = null,
110                                      $cnt = null, $page = null)
111     {
112         $url    = 'https://twitter.com/statuses/friends_timeline.json';
113         $params = array('since_id' => $since_id,
114                         'max_id' => $max_id,
115                         'count' => $cnt,
116                         'page' => $page);
117         $qry    = http_build_query($params);
118
119         if (!empty($qry)) {
120             $url .= "?$qry";
121         }
122
123         $response = $this->httpRequest($url);
124         $statuses = json_decode($response);
125         return $statuses;
126     }
127
128     /**
129      * Calls Twitter's /statuses/friends API method
130      *
131      * @param int $id          id of the user whom you wish to see friends of
132      * @param int $user_id     numerical user id
133      * @param int $screen_name screen name
134      * @param int $page        page number
135      *
136      * @return mixed an array of twitter users and their latest status
137      */
138     function statusesFriends($id = null, $user_id = null, $screen_name = null,
139                              $page = null)
140     {
141         $url = "https://twitter.com/statuses/friends.json";
142
143         $params = array('id' => $id,
144                         'user_id' => $user_id,
145                         'screen_name' => $screen_name,
146                         'page' => $page);
147         $qry    = http_build_query($params);
148
149         if (!empty($qry)) {
150             $url .= "?$qry";
151         }
152
153         $response = $this->httpRequest($url);
154         $friends  = json_decode($response);
155         return $friends;
156     }
157
158     /**
159      * Calls Twitter's /statuses/friends/ids API method
160      *
161      * @param int $id          id of the user whom you wish to see friends of
162      * @param int $user_id     numerical user id
163      * @param int $screen_name screen name
164      * @param int $page        page number
165      *
166      * @return mixed a list of ids, 100 per page
167      */
168     function friendsIds($id = null, $user_id = null, $screen_name = null,
169                         $page = null)
170     {
171         $url = "https://twitter.com/friends/ids.json";
172
173         $params = array('id' => $id,
174                         'user_id' => $user_id,
175                         'screen_name' => $screen_name,
176                         'page' => $page);
177         $qry    = http_build_query($params);
178
179         if (!empty($qry)) {
180             $url .= "?$qry";
181         }
182
183         $response = $this->httpRequest($url);
184         $ids      = json_decode($response);
185         return $ids;
186     }
187
188     /**
189      * Make an HTTP request
190      *
191      * @param string $url    Where to make the request
192      * @param array  $params post parameters
193      *
194      * @return mixed the request
195      * @throws BasicAuthException
196      */
197     function httpRequest($url, $params = null, $auth = true)
198     {
199         $request = HTTPClient::start();
200         $request->setConfig(array(
201             'follow_redirects' => true,
202             'connect_timeout' => 120,
203             'timeout' => 120,
204             'ssl_verify_peer' => false,
205             'ssl_verify_host' => false
206         ));
207
208         if ($auth) {
209             $request->setAuth($this->screen_name, $this->password);
210         }
211
212         if (isset($params)) {
213             // Twitter is strict about accepting invalid "Expect" headers
214             $headers = array('Expect:');
215             $response = $request->post($url, $headers, $params);
216         } else {
217             $response = $request->get($url);
218         }
219
220         $code = $response->getStatus();
221
222         if ($code < 200 || $code >= 400) {
223             throw new BasicAuthException($response->getBody(), $code);
224         }
225
226         return $response->getBody();
227     }
228
229 }