]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitter.php
Add an optional theme parameter to theme functions
[quix0rs-gnu-social.git] / lib / twitter.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 function get_twitter_data($uri, $screen_name, $password)
23 {
24
25     $options = array(
26             CURLOPT_USERPWD => sprintf("%s:%s", $screen_name, $password),
27             CURLOPT_RETURNTRANSFER    => true,
28             CURLOPT_FAILONERROR        => true,
29             CURLOPT_HEADER            => false,
30             CURLOPT_FOLLOWLOCATION    => true,
31             # CURLOPT_USERAGENT        => "identi.ca",
32             CURLOPT_CONNECTTIMEOUT    => 120,
33             CURLOPT_TIMEOUT            => 120,
34             # Twitter is strict about accepting invalid "Expect" headers
35             CURLOPT_HTTPHEADER => array('Expect:')
36     );
37
38
39     $ch = curl_init($uri);
40     curl_setopt_array($ch, $options);
41     $data = curl_exec($ch);
42     $errmsg = curl_error($ch);
43
44     if ($errmsg) {
45         common_debug("Twitter bridge - cURL error: $errmsg - trying to load: $uri with user $twit_user.",
46             __FILE__);
47     }
48
49     curl_close($ch);
50
51     return $data;
52 }
53
54 function twitter_user_info($screen_name, $password)
55 {
56
57     $uri = "http://twitter.com/users/show/$screen_name.json";
58     $data = get_twitter_data($uri, $screen_name, $password);
59
60     if (!$data) {
61         return false;
62     }
63
64     $twit_user = json_decode($data);
65
66     if (!$twit_user) {
67         return false;
68     }
69
70     return $twit_user;
71 }
72
73 function update_twitter_user($fuser, $twitter_id, $screen_name)
74 {
75
76     $original = clone($fuser);
77     $fuser->nickname = $screen_name;
78     $fuser->uri = 'http://twitter.com/' . $screen_name;
79     $result = $fuser->updateKeys($original);
80
81     if (!$result) {
82         common_log_db_error($fuser, 'UPDATE', __FILE__);
83         return false;
84     }
85
86     return true;
87 }
88
89 function add_twitter_user($twitter_id, $screen_name)
90 {
91
92     // Otherwise, create a new Twitter user
93     $fuser = DB_DataObject::factory('foreign_user');
94
95     $fuser->nickname = $screen_name;
96     $fuser->uri = 'http://twitter.com/' . $screen_name;
97     $fuser->id = $twitter_id;
98     $fuser->service = 1; // Twitter
99     $fuser->created = common_sql_now();
100     $result = $fuser->insert();
101
102     if (!$result) {
103         common_debug("Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name.");
104         common_log_db_error($fuser, 'INSERT', __FILE__);
105         return false;
106     }
107
108     common_debug("Twitter bridge - Added new Twitter user: $screen_name ($twitter_id).");
109
110     return true;
111 }
112
113 // Creates or Updates a Twitter user
114 function save_twitter_user($twitter_id, $screen_name)
115 {
116
117     // Check to see whether the Twitter user is already in the system,
118     // and update its screen name and uri if so.
119     $fuser = Foreign_user::getForeignUser($twitter_id, 1);
120
121     if ($fuser) {
122
123         // Only update if Twitter screen name has changed
124         if ($fuser->nickname != $screen_name) {
125
126             common_debug('Twitter bridge - Updated nickname (and URI) for Twitter user ' .
127                 "$fuser->id to $screen_name, was $fuser->nickname");
128
129             return update_twitter_user($fuser, $twitter_id, $screen_name);
130         }
131
132     } else {
133         return add_twitter_user($twitter_id, $screen_name);
134     }
135
136     return true;
137 }
138
139 function retreive_twitter_friends($twitter_id, $screen_name, $password)
140 {
141
142     $uri = "http://twitter.com/statuses/friends/$twitter_id.json?page=";
143     $twitter_user = twitter_user_info($screen_name, $password);
144
145     // Calculate how many pages to get...
146     $pages = ceil($twitter_user->friends_count / 100);
147
148     if ($pages == 0) {
149         common_debug("Twitter bridge - Twitter user $screen_name has no friends! Lame.");
150     }
151
152     $friends = array();
153
154     for ($i = 1; $i <= $pages; $i++) {
155
156         $data = get_twitter_data($uri . $i, $screen_name, $password);
157
158         if (!$data) {
159             return null;
160         }
161
162         $more_friends = json_decode($data);
163
164         if (!$more_friends) {
165             return null;
166         }
167
168          $friends = array_merge($friends, $more_friends);
169     }
170
171     return $friends;
172 }
173
174 function save_twitter_friends($user, $twitter_id, $screen_name, $password)
175 {
176
177     $friends = retreive_twitter_friends($twitter_id, $screen_name, $password);
178
179     if (is_null($friends)) {
180         common_debug("Twitter bridge - Couldn't get friends data from Twitter.");
181         return false;
182     }
183
184     foreach ($friends as $friend) {
185
186         $friend_name = $friend->screen_name;
187         $friend_id = $friend->id;
188
189         // Update or create the Foreign_user record
190         if (!save_twitter_user($friend_id, $friend_name)) {
191             return false;
192         }
193
194         // Check to see if there's a related local user
195         $flink = Foreign_link::getByForeignID($friend_id, 1);
196
197         if ($flink) {
198
199             // Get associated user and subscribe her
200             $friend_user = User::staticGet('id', $flink->user_id);
201             subs_subscribe_to($user, $friend_user);
202             common_debug("Twitter bridge - subscribed $friend_user->nickname to $user->nickname.");
203         }
204     }
205
206     return true;
207 }
208