]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitterapi.php
d27152cbc15ae6e31b3677886955914af4dec27d
[quix0rs-gnu-social.git] / lib / twitterapi.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 class TwitterapiAction extends Action {
23
24         function handle($args) {
25                 parent::handle($args);
26         }
27         
28         function twitter_user_array($profile, $get_notice=false) {
29                 
30                 $twitter_user = array();
31
32                 $twitter_user['name'] = $profile->getBestName();                
33                 $twitter_user['followers_count'] = $this->count_subscriptions($profile);
34                 $twitter_user['screen_name'] = $profile->nickname;
35                 $twitter_user['description'] = ($profile->bio) ? $profile->bio : NULL;
36                 $twitter_user['location'] = ($profile->location) ? $profile->location : NULL;
37                 $twitter_user['id'] = intval($profile->id);
38                 
39                 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
40                 
41                 $twitter_user['profile_image_url'] = ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE);
42                 $twitter_user['protected'] = false; # not supported by Laconica yet
43                 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : NULL;
44
45                 if ($get_notice) {
46                         $notice = $profile->getCurrentNotice();
47                         if ($notice) {
48                                 # don't get user!
49                                 $twitter_user['status'] = $this->twitter_status_array($notice, false);
50                         } 
51                 }
52                 
53                 return $twitter_user;           
54         }
55
56         function twitter_status_array($notice, $get_user=true) {
57                 
58                 $twitter_status = array();
59
60                 $twitter_status['text'] = $notice->content;             
61                 $twitter_status['truncated'] = false; # Not possible on Laconica
62                 $twitter_status['created_at'] = $this->date_twitter($notice->created);
63                 $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ? intval($notice->reply_to) : NULL;
64                 $twitter_status['source'] = NULL; # XXX: twitterific, twitterfox, etc. Not supported yet.
65                 $twitter_status['id'] = intval($notice->id);
66                 $twitter_status['in_reply_to_user_id'] = ($notice->reply_to) ? $this->replier_by_reply($notice->reply_to) : NULL;
67                 $twitter_status['favorited'] = NULL; # XXX: Not implemented on Laconica yet.
68
69                 if ($get_user) {
70                         $profile = $notice->getProfile();
71                         # Don't get notice (recursive!)
72                         $twitter_user = $this->twitter_user_array($profile, false);
73                         $twitter_status['user'] = $twitter_user;
74                 }
75                                 
76                 return $twitter_status;
77         }
78                 
79         function twitter_rss_entry_array($notice) {
80                 
81                 $profile = $notice->getProfile();
82
83                 $server = common_config('site', 'server');              
84                 $entry = array();
85         
86                 $entry['content'] = $profile->nickname . ': ' . $notice->content; 
87                 $entry['title'] = $entry['content'];
88                 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));;
89                 $entry['published'] = common_date_iso8601($notice->created);
90                 $entry['id'] = "tag:$server,$entry[published]:$entry[link]";
91                 $entry['updated'] = $entry['published'];
92
93                 # RSS Item specific
94                 $entry['description'] = $entry['content'];
95                 $entry['pubDate'] = common_date_rfc2822($notice->created);
96                 $entry['guid'] = $entry['link'];
97
98                 return $entry;
99         }
100         
101         function show_twitter_xml_status($twitter_status) {                     
102                 common_element_start('status');
103                 common_element('created_at', NULL, $twitter_status['created_at']);
104                 common_element('id', NULL, $twitter_status['id']);
105                 common_element('text', NULL, $twitter_status['text']);
106                 common_element('source', NULL, $twitter_status['source']);  
107                 common_element('truncated', NULL, $twitter_status['truncated']); 
108                 common_element('in_reply_to_status_id', NULL, $twitter_status['in_reply_to_status_id']);
109                 common_element('in_reply_to_user_id', NULL, $twitter_status['in_reply_to_user_id']);
110                 common_element('favorited', Null, $twitter_status['favorited']);  
111
112                 if ($twitter_status['user']) {
113                         $this->show_twitter_xml_user($twitter_status['user']);
114                 }
115                 
116                 common_element_end('status');
117         }       
118         
119         function show_twitter_xml_user($twitter_user) {
120                 common_element_start('user');
121                 common_element('id', NULL, $twitter_user['id']);
122                 common_element('name', NULL, $twitter_user['name']);
123                 common_element('screen_name', NULL, $twitter_user['screen_name']);
124                 common_element('location', NULL, $twitter_user['location']);
125                 common_element('description', NULL, $twitter_user['description']);              
126                 common_element('profile_image_url', NULL, $twitter_user['profile_image_url']);
127                 common_element('url', NULL, $twitter_user['url']);
128                 common_element('protected', NULL, $twitter_user['protected']);
129                 common_element('followers_count', NULL, $twitter_user['followers_count']);
130                 if ($twitter_user['status']) {
131                         $this->show_twitter_xml_status($twitter_user['status']);
132                 }
133                 common_element_end('user');
134         }
135
136         function show_twitter_rss_item($entry) {
137                 common_element_start('item');
138                 common_element('title', NULL, $entry['title']);
139                 common_element('description', NULL, $entry['description']);
140                 common_element('pubDate', NULL, $entry['pubDate']);
141                 common_element('guid', NULL, $entry['guid']);
142                 common_element('link', NULL, $entry['link']);
143                 common_element_end('item');
144         }
145                 
146         function show_twitter_atom_entry($entry) {
147             common_element_start('entry');
148                 common_element('title', NULL, $entry['title']);
149                 common_element('content', array('type' => 'html'), $entry['title']);
150                 common_element('id', NULL, $entry['id']);
151                 common_element('published', NULL, $entry['published']);
152                 common_element('updated', NULL, $entry['updated']);
153                 common_element('link', array('href' => $entry['link'], 'rel' => 'alternate', 'type' => 'text/html'), NULL);
154                 common_element_end('entry');
155         }
156         
157         function show_twitter_json_statuses($twitter_statuses) {
158                 print(json_encode($twitter_statuses));
159         }
160
161         function show_twitter_json_users($twitter_users) {
162                 print(json_encode($twitter_users));
163         }
164
165         function show($args, $apidata) {
166                 parent::handle($args);
167                 
168                 $id = $apidata['api_arg'];              
169                 $notice = Notice::staticGet($id);
170
171                 if ($notice) {
172
173                         if ($apidata['content-type'] == 'xml') { 
174                                 $this->show_single_xml_status($notice);
175                         } elseif ($apidata['content-type'] == 'json') {
176                                 $this->show_single_json_status($notice);
177                         }
178                 } else {
179                         header('HTTP/1.1 404 Not Found');
180                 }
181                 
182                 exit();
183         }
184                 
185         function show_single_xml_status($notice) {
186                 $this->init_document('xml');
187                 $twitter_status = $this->twitter_status_array($notice);                                         
188                 $this->show_twitter_xml_status($twitter_status);
189                 $this->end_document('xml');
190                 exit();
191         }
192         
193         function show_single_json_status($notice) {
194                 $this->init_document('json');
195                 $status = $this->twitter_status_array($notice);
196                 $this->show_twitter_json_statuses($status);
197                 $this->end_document('json');
198                 exit();
199         }
200         
201         // Anyone know what date format this is? 
202         // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach 
203         function date_twitter($dt) {
204                 $t = strtotime($dt);
205                 return date("D M d G:i:s O Y", $t);
206         }
207         
208         function replier_by_reply($reply_id) {  
209
210                 $notice = Notice::staticGet($reply_id);
211         
212                 if (!$notice) {
213                         common_debug("TwitterapiAction::replier_by_reply: Got a bad notice_id: $reply_id");
214                 }
215
216                 $profile = $notice->getProfile();
217                 
218                 if (!$profile) {
219                         common_debug("TwitterapiAction::replier_by_reply: Got a bad profile_id: $profile_id");
220                         return false;
221                 }
222                 
223                 return intval($profile->id);            
224         }
225
226         // XXX: Candidate for a general utility method somewhere?       
227         function count_subscriptions($profile) {
228                 
229                 $count = 0;
230                 $sub = new Subscription();
231                 $sub->subscribed = $profile->id;
232
233                 $count = $sub->find();
234                 
235                 if ($count > 0) {
236                         return $count;
237                 }
238                 
239                 return NULL;
240         }
241
242         function init_document($type='xml') {
243                 switch ($type) {
244                  case 'xml':
245                         header('Content-Type: application/xml; charset=utf-8');         
246                         common_start_xml();
247                         break;
248                  case 'json':
249                         header('Content-Type: application/json; charset=utf-8');
250                         break;
251                  case 'rss':
252                         header("Content-Type: application/rss+xml; charset=utf-8");
253                         $this->init_twitter_rss();
254                         break;
255                  case 'atom':
256                         header('Content-Type: application/atom+xml; charset=utf-8');
257                         $this->init_twitter_atom();
258                         break;
259                  default:
260                         $this->client_error(_('Unsupported type'));
261                         break;
262                 }
263                 
264                 return;
265         }
266         
267         function end_document($type='xml') {
268                 switch ($type) {
269                  case 'xml':
270                         common_end_xml();
271                         break;
272                  case 'json':
273                         break;
274                  case 'rss':
275                         $this->end_twitter_rss();
276                         break;
277                  case 'atom':
278                         $this->end_twitter_rss();
279                         break;
280                  default:
281                         $this->client_error(_('Unsupported type'));
282                         break;
283                 }
284                 return;
285         }
286                 
287         function client_error($msg, $code = 400, $content_type = 'json') {
288                 
289                 static $status = array(400 => 'Bad Request',
290                                                            401 => 'Unauthorized',
291                                                            402 => 'Payment Required',
292                                                            403 => 'Forbidden',
293                                                            404 => 'Not Found',
294                                                            405 => 'Method Not Allowed',
295                                                            406 => 'Not Acceptable',
296                                                            407 => 'Proxy Authentication Required',
297                                                            408 => 'Request Timeout',
298                                                            409 => 'Conflict',
299                                                            410 => 'Gone',
300                                                            411 => 'Length Required',
301                                                            412 => 'Precondition Failed',
302                                                            413 => 'Request Entity Too Large',
303                                                            414 => 'Request-URI Too Long',
304                                                            415 => 'Unsupported Media Type',
305                                                            416 => 'Requested Range Not Satisfiable',
306                                                            417 => 'Expectation Failed');
307                 
308                 $action = $this->trimmed('action');
309                 
310                 common_debug("User error '$code' on '$action': $msg", __FILE__);
311                 
312                 if (!array_key_exists($code, $status)) {
313                         $code = 400;
314                 }
315
316                 $status_string = $status[$code];
317                 header('HTTP/1.1 '.$code.' '.$status_string);
318                                                 
319                 if ($content_type == 'xml') {
320                         $this->init_document('xml');
321                         common_element_start('hash');
322                         common_element('error', NULL, $msg);
323                         common_element('request', NULL, $_SERVER['REQUEST_URI']);
324                         common_element_end('hash');
325                         $this->end_document('xml');
326                 } else {
327                         $this->init_document('json');
328                         $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
329                         print(json_encode($error_array));
330                         $this->end_document('json');
331                 }
332                 
333                 exit();
334         }
335
336         function init_twitter_rss() {
337                 common_start_xml();
338                 common_element_start('rss', array('version' => '2.0'));
339         }
340         
341         function end_twitter_rss() {
342                 common_element_end('rss');
343                 common_end_xml();
344         }
345         
346         function init_twitter_atom() {
347                 common_start_xml();
348                 common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
349         }
350         
351         function end_twitter_atom() {
352                 common_end_xml();
353                 common_element_end('feed');
354         }
355
356         function show_profile($profile, $content_type='xml', $notice=NULL) {                            
357                 $profile_array = $this->twitter_user_array($profile, true);
358                 switch ($content_type) {
359                  case 'xml':
360                         $this->show_twitter_xml_user($profile_array);
361                         break;
362                  case 'json':
363                         $this->show_twitter_json_users($profile_array);
364                         break;
365                  default:
366                         $this->client_error(_('not a supported data format'));
367                         return;
368                 }
369                 return;
370         }
371 }