]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - _darcs/pristine/lib/twitter.php
replace all tabs with four spaces
[quix0rs-gnu-social.git] / _darcs / pristine / 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     $options = array(
25             CURLOPT_USERPWD => sprintf("%s:%s", $screen_name, $password),
26             CURLOPT_RETURNTRANSFER    => true,
27             CURLOPT_FAILONERROR        => true,
28             CURLOPT_HEADER            => false,
29             CURLOPT_FOLLOWLOCATION    => true,
30             // CURLOPT_USERAGENT        => "identi.ca",
31             CURLOPT_CONNECTTIMEOUT    => 120,
32             CURLOPT_TIMEOUT            => 120
33     );
34
35
36     $ch = curl_init($uri);
37     curl_setopt_array($ch, $options);
38     $data = curl_exec($ch);
39     $errmsg = curl_error($ch);
40
41     if ($errmsg) {
42         common_debug("Twitter bridge - cURL error: $errmsg - trying to load: $uri with user $twit_user.",
43             __FILE__);
44     }
45
46     curl_close($ch);
47
48     return $data;
49 }
50
51 function twitter_user_info($screen_name, $password) {
52
53     $uri = "http://twitter.com/users/show/$screen_name.json";
54     $data = get_twitter_data($uri, $screen_name, $password);
55
56     if (!$data) {
57         return false;
58     }
59
60     $twit_user = json_decode($data);
61
62     if (!$twit_user) {
63         return false;
64     }
65
66     return $twit_user;
67 }
68
69 function update_twitter_user($fuser, $twitter_id, $screen_name) {
70
71     $original = clone($fuser);
72     $fuser->nickname = $screen_name;
73     $fuser->uri = 'http://twitter.com/' . $screen_name;
74     $result = $fuser->updateKeys($original);
75
76     if (!$result) {
77         common_log_db_error($fuser, 'UPDATE', __FILE__);
78         return false;
79     }
80
81     return true;
82 }
83
84 function add_twitter_user($twitter_id, $screen_name) {
85
86     // Otherwise, create a new Twitter user
87     $fuser = DB_DataObject::factory('foreign_user');
88
89     $fuser->nickname = $screen_name;
90     $fuser->uri = 'http://twitter.com/' . $screen_name;
91     $fuser->id = $twitter_id;
92     $fuser->service = 1; // Twitter
93     $fuser->created = common_sql_now();
94     $result = $fuser->insert();
95
96     if (!$result) {
97         common_debug("Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name.");
98         common_log_db_error($fuser, 'INSERT', __FILE__);
99         return false;
100     }
101
102     common_debug("Twitter bridge - Added new Twitter user: $screen_name ($twitter_id).");
103
104     return true;
105 }
106
107 // Creates or Updates a Twitter user
108 function save_twitter_user($twitter_id, $screen_name) {
109
110     // Check to see whether the Twitter user is already in the system,
111     // and update its screen name and uri if so.
112     $fuser = Foreign_user::getForeignUser($twitter_id, 1);
113
114     if ($fuser) {
115
116         // Only update if Twitter screen name has changed
117         if ($fuser->nickname != $screen_name) {
118
119             common_debug('Twitter bridge - Updated nickname (and URI) for Twitter user ' .
120                 "$fuser->id to $screen_name, was $fuser->nickname");
121
122             return update_twitter_user($fuser, $twitter_id, $screen_name);
123         }
124
125     } else {
126         return add_twitter_user($twitter_id, $screen_name);
127     }
128
129     return true;
130 }
131
132 function retreive_twitter_friends($twitter_id, $screen_name, $password) {
133
134     $uri = "http://twitter.com/statuses/friends/$twitter_id.json?page=";
135     $twitter_user = twitter_user_info($screen_name, $password);
136
137     // Calculate how many pages to get...
138     $pages = ceil($twitter_user->friends_count / 100);
139
140     if ($pages == 0) {
141         common_debug("Twitter bridge - Twitter user $screen_name has no friends! Lame.");
142     }
143
144     $friends = array();
145
146     for ($i = 1; $i <= $pages; $i++) {
147
148         $data = get_twitter_data($uri . $i, $screen_name, $password);
149
150         if (!$data) {
151             return NULL;
152         }
153
154         $more_friends = json_decode($data);
155
156         if (!$more_friends) {
157             return NULL;
158         }
159
160          $friends = array_merge($friends, $more_friends);
161     }
162
163     return $friends;
164 }
165
166 function save_twitter_friends($user, $twitter_id, $screen_name, $password) {
167
168     $friends = retreive_twitter_friends($twitter_id, $screen_name, $password);
169
170     if (is_null($friends)) {
171         common_debug("Twitter bridge - Couldn't get friends data from Twitter.");
172         return false;
173     }
174
175     foreach ($friends as $friend) {
176
177         $friend_name = $friend->screen_name;
178         $friend_id = $friend->id;
179
180         // Update or create the Foreign_user record
181         if (!save_twitter_user($friend_id, $friend_name)) {
182             return false;
183         }
184
185         // Check to see if there's a related local user
186         $flink = Foreign_link::getByForeignID($friend_id, 1);
187
188         if ($flink) {
189
190             // Get associated user and subscribe her
191             $friend_user = User::staticGet('id', $flink->user_id);
192             subs_subscribe_to($user, $friend_user);
193             common_debug("Twitter bridge - subscribed $friend_user->nickname to $user->nickname.");
194         }
195     }
196
197     return true;
198 }
199