]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twitterbasicauthclient.php
d1cf45aec608f0d0f4d9836d008f6c4feb724a95
[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  * Class for talking to the Twitter API with HTTP Basic Auth.
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 TwitterBasicAuthClient
45 {
46     var $screen_name = null;
47     var $password    = null;
48
49     /**
50      * constructor
51      *
52      * @param Foreign_link $flink a Foreign_link storing the
53      *                            Twitter user's password, etc.
54      */
55     function __construct($flink)
56     {
57         $fuser             = $flink->getForeignUser();
58         $this->screen_name = $fuser->nickname;
59         $this->password    = $flink->credentials;
60     }
61
62     /**
63      * Calls Twitter's /statuses/update API method
64      *
65      * @param string $status                text of the status
66      * @param int    $in_reply_to_status_id optional id of the status it's
67      *                                      a reply to
68      *
69      * @return mixed the status
70      */
71     function statusesUpdate($status, $in_reply_to_status_id = null)
72     {
73         $url      = 'https://twitter.com/statuses/update.json';
74         $params   = array('status' => $status,
75                           'source' => common_config('integration', 'source'),
76                           'in_reply_to_status_id' => $in_reply_to_status_id);
77         $response = $this->httpRequest($url, $params);
78         $status   = json_decode($response);
79         return $status;
80     }
81
82     /**
83      * Calls Twitter's /statuses/friends_timeline API method
84      *
85      * @param int $since_id show statuses after this id
86      * @param int $max_id   show statuses before this id
87      * @param int $cnt      number of statuses to show
88      * @param int $page     page number
89      *
90      * @return mixed an array of statuses
91      */
92     function statusesFriendsTimeline($since_id = null, $max_id = null,
93                                      $cnt = null, $page = null)
94     {
95         $url    = 'https://twitter.com/statuses/friends_timeline.json';
96         $params = array('since_id' => $since_id,
97                         'max_id' => $max_id,
98                         'count' => $cnt,
99                         'page' => $page);
100         $qry    = http_build_query($params);
101
102         if (!empty($qry)) {
103             $url .= "?$qry";
104         }
105
106         $response = $this->httpRequest($url);
107         $statuses = json_decode($response);
108         return $statuses;
109     }
110
111     /**
112      * Calls Twitter's /statuses/friends API method
113      *
114      * @param int $id          id of the user whom you wish to see friends of
115      * @param int $user_id     numerical user id
116      * @param int $screen_name screen name
117      * @param int $page        page number
118      *
119      * @return mixed an array of twitter users and their latest status
120      */
121     function statusesFriends($id = null, $user_id = null, $screen_name = null,
122                              $page = null)
123     {
124         $url = "https://twitter.com/statuses/friends.json";
125
126         $params = array('id' => $id,
127                         'user_id' => $user_id,
128                         'screen_name' => $screen_name,
129                         'page' => $page);
130         $qry    = http_build_query($params);
131
132         if (!empty($qry)) {
133             $url .= "?$qry";
134         }
135
136         $response = $this->httpRequest($url);
137         $friends  = json_decode($response);
138         return $friends;
139     }
140
141     /**
142      * Calls Twitter's /statuses/friends/ids API method
143      *
144      * @param int $id          id of the user whom you wish to see friends of
145      * @param int $user_id     numerical user id
146      * @param int $screen_name screen name
147      * @param int $page        page number
148      *
149      * @return mixed a list of ids, 100 per page
150      */
151     function friendsIds($id = null, $user_id = null, $screen_name = null,
152                         $page = null)
153     {
154         $url = "https://twitter.com/friends/ids.json";
155
156         $params = array('id' => $id,
157                         'user_id' => $user_id,
158                         'screen_name' => $screen_name,
159                         'page' => $page);
160         $qry    = http_build_query($params);
161
162         if (!empty($qry)) {
163             $url .= "?$qry";
164         }
165
166         $response = $this->httpRequest($url);
167         $ids      = json_decode($response);
168         return $ids;
169     }
170
171     /**
172      * Make a HTTP request using cURL.
173      *
174      * @param string $url    Where to make the request
175      * @param array  $params post parameters
176      *
177      * @return mixed the request
178      */
179     function httpRequest($url, $params = null, $auth = true)
180     {
181         $request = HTTPClient::start();
182         $request->setConfig(array(
183             'follow_redirects' => true,
184             'connect_timeout' => 120,
185             'timeout' => 120,
186             'ssl_verifypeer' => false,
187         ));
188
189         if ($auth) {
190             $request->setAuth($this->screen_name, $this->password);
191         }
192
193         if (isset($params)) {
194             // Twitter is strict about accepting invalid "Expect" headers
195             $headers = array('Expect:');
196             $response = $request->post($url, $headers, $params);
197         } else {
198             $response = $request->get($url);
199         }
200
201         return $response->getBody();
202     }
203
204 }