3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 class TwitterapiAction extends Action {
24 function handle($args) {
25 parent::handle($args);
28 function twitter_user_array($profile, $get_notice=false) {
30 $twitter_user = array();
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);
39 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
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;
46 $notice = $profile->getCurrentNotice();
49 $twitter_user['status'] = $this->twitter_status_array($notice, false);
56 function twitter_status_array($notice, $get_user=true) {
58 $twitter_status = array();
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'] = $this->source_link($notice->source);
65 $twitter_status['id'] = intval($notice->id);
66 $twitter_status['in_reply_to_user_id'] = ($notice->reply_to) ? $this->replier_by_reply(intval($notice->reply_to)) : NULL;
67 $twitter_status['favorited'] = NULL; # XXX: Not implemented on Laconica yet.
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;
76 return $twitter_status;
79 function twitter_rss_entry_array($notice) {
81 $profile = $notice->getProfile();
83 $server = common_config('site', 'server');
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,2008:$entry[link]";
91 $entry['updated'] = $entry['published'];
94 $entry['description'] = $entry['content'];
95 $entry['pubDate'] = common_date_rfc2822($notice->created);
96 $entry['guid'] = $entry['link'];
101 function twitter_rss_dmsg_array($message) {
103 $server = common_config('site', 'server');
106 $entry['title'] = sprintf('Message from %s to %s',
107 $message->getFrom()->nickname, $message->getTo()->nickname);
109 $entry['content'] = $message->content;
110 $entry['link'] = $message->uri;
111 $entry['published'] = common_date_iso8601($message->created);
112 $entry['id'] = "tag:$server,2008:$entry[link]";
113 $entry['updated'] = $entry['published'];
116 $entry['description'] = $message->content;
117 $entry['pubDate'] = common_date_rfc2822($message->created);
118 $entry['guid'] = $entry['link'];
123 function twitter_dmsg_array($message) {
125 $twitter_dm = array();
127 $from_profile = $message->getFrom();
128 $to_profile = $message->getTo();
130 $twitter_dm['id'] = $message->id;
131 $twitter_dm['sender_id'] = $message->from_profile;
132 $twitter_dm['text'] = $message->content;
133 $twitter_dm['recipient_id'] = $message->to_profile;
134 $twitter_dm['created_at'] = $this->date_twitter($message->created);
135 $twitter_dm['sender_screen_name'] = $from_profile->nickname;
136 $twitter_dm['recipient_screen_name'] = $to_profile->nickname;
137 $twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
138 $twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
143 function show_twitter_xml_status($twitter_status) {
144 common_element_start('status');
145 foreach($twitter_status as $element => $value) {
146 if ($element == 'user') {
147 $this->show_twitter_xml_user($twitter_status['user']);
149 common_element($element, NULL, $value);
152 common_element_end('status');
155 function show_twitter_xml_user($twitter_user, $role='user') {
156 common_element_start($role);
157 foreach($twitter_user as $element => $value) {
158 if ($element == 'status') {
159 $this->show_twitter_xml_status($twitter_user['status']);
161 common_element($element, NULL, $value);
164 common_element_end($role);
167 function show_twitter_rss_item($entry) {
168 common_element_start('item');
169 common_element('title', NULL, $entry['title']);
170 common_element('description', NULL, $entry['description']);
171 common_element('pubDate', NULL, $entry['pubDate']);
172 common_element('guid', NULL, $entry['guid']);
173 common_element('link', NULL, $entry['link']);
174 common_element_end('item');
177 function show_twitter_atom_entry($entry) {
178 common_element_start('entry');
179 common_element('title', NULL, $entry['title']);
180 common_element('content', array('type' => 'html'), $entry['title']);
181 common_element('id', NULL, $entry['id']);
182 common_element('published', NULL, $entry['published']);
183 common_element('updated', NULL, $entry['updated']);
184 common_element('link', array('href' => $entry['link'], 'rel' => 'alternate', 'type' => 'text/html'), NULL);
185 common_element_end('entry');
188 function show_twitter_json_statuses($twitter_statuses) {
189 print(json_encode($twitter_statuses));
192 function show_twitter_json_users($twitter_users) {
193 print(json_encode($twitter_users));
196 function show_twitter_json_dmsgs($twitter_dms) {
197 print(json_encode($twitter_dms));
200 function show_single_xml_status($notice) {
201 $this->init_document('xml');
202 $twitter_status = $this->twitter_status_array($notice);
203 $this->show_twitter_xml_status($twitter_status);
204 $this->end_document('xml');
208 function show_single_json_status($notice) {
209 $this->init_document('json');
210 $status = $this->twitter_status_array($notice);
211 $this->show_twitter_json_statuses($status);
212 $this->end_document('json');
216 function show_single_xml_dmsg($message) {
217 $this->init_document('xml');
218 $dmsg = $this->twitter_dmsg_array($message);
219 $this->show_twitter_xml_dmsg($dmsg);
220 $this->end_document('xml');
223 function show_single_json_dmsg($message) {
224 $this->init_document('json');
225 $dmsg = $this->twitter_dmsg_array($message);
226 $this->show_twitter_json_dm($dmsg);
227 $this->end_document('json');
230 function show_twitter_xml_dmsg($twitter_dm) {
231 common_element_start('direct_message');
232 foreach($twitter_dm as $element => $value) {
233 if ($element == 'sender' || $element == 'recipient') {
234 $this->show_twitter_xml_user($value, $element);
236 common_element($element, NULL, $value);
239 common_element_end('direct_message');
242 function show_xml_timeline($notice) {
244 $this->init_document('xml');
245 common_element_start('statuses', array('type' => 'array'));
247 if (is_array($notice)) {
248 foreach ($notice as $n) {
249 $twitter_status = $this->twitter_status_array($n);
250 $this->show_twitter_xml_status($twitter_status);
253 while ($notice->fetch()) {
254 $twitter_status = $this->twitter_status_array($notice);
255 $this->show_twitter_xml_status($twitter_status);
259 common_element_end('statuses');
260 $this->end_document('xml');
263 function show_rss_timeline($notice, $title, $link, $subtitle) {
265 $this->init_document('rss');
267 common_element_start('channel');
268 common_element('title', NULL, $title);
269 common_element('link', NULL, $link);
270 common_element('description', NULL, $subtitle);
271 common_element('language', NULL, 'en-us');
272 common_element('ttl', NULL, '40');
275 if (is_array($notice)) {
276 foreach ($notice as $n) {
277 $entry = $this->twitter_rss_entry_array($n);
278 $this->show_twitter_rss_item($entry);
281 while ($notice->fetch()) {
282 $entry = $this->twitter_rss_entry_array($notice);
283 $this->show_twitter_rss_item($entry);
287 common_element_end('channel');
288 $this->end_twitter_rss();
291 function show_atom_timeline($notice, $title, $id, $link, $subtitle=NULL) {
293 $this->init_document('atom');
295 common_element('title', NULL, $title);
296 common_element('id', NULL, $id);
297 common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), NULL);
298 common_element('subtitle', NULL, $subtitle);
300 if (is_array($notice)) {
301 foreach ($notice as $n) {
302 $entry = $this->twitter_rss_entry_array($n);
303 $this->show_twitter_atom_entry($entry);
306 while ($notice->fetch()) {
307 $entry = $this->twitter_rss_entry_array($notice);
308 $this->show_twitter_atom_entry($entry);
312 $this->end_document('atom');
316 function show_json_timeline($notice) {
318 $this->init_document('json');
322 if (is_array($notice)) {
323 foreach ($notice as $n) {
324 $twitter_status = $this->twitter_status_array($n);
325 array_push($statuses, $twitter_status);
328 while ($notice->fetch()) {
329 $twitter_status = $this->twitter_status_array($notice);
330 array_push($statuses, $twitter_status);
334 $this->show_twitter_json_statuses($statuses);
336 $this->end_document('json');
339 // Anyone know what date format this is?
340 // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
341 function date_twitter($dt) {
343 return date("D M d G:i:s O Y", $t);
346 function replier_by_reply($reply_id) {
347 $notice = Notice::staticGet($reply_id);
349 $profile = $notice->getProfile();
351 return intval($profile->id);
353 common_debug('Can\'t find a profile for notice: ' . $notice->id, __FILE__);
356 common_debug("Can't get notice: $reply_id", __FILE__);
361 // XXX: Candidate for a general utility method somewhere?
362 function count_subscriptions($profile) {
365 $sub = new Subscription();
366 $sub->subscribed = $profile->id;
368 $count = $sub->find();
377 function init_document($type='xml') {
380 header('Content-Type: application/xml; charset=utf-8');
384 header('Content-Type: application/json; charset=utf-8');
386 // Check for JSONP callback
387 $callback = $this->arg('callback');
389 print $callback . '(';
393 header("Content-Type: application/rss+xml; charset=utf-8");
394 $this->init_twitter_rss();
397 header('Content-Type: application/atom+xml; charset=utf-8');
398 $this->init_twitter_atom();
401 $this->client_error(_('Not a supported data format.'));
408 function end_document($type='xml') {
415 // Check for JSONP callback
416 $callback = $this->arg('callback');
422 $this->end_twitter_rss();
425 $this->end_twitter_rss();
428 $this->client_error(_('Not a supported data format.'));
434 function client_error($msg, $code = 400, $content_type = 'json') {
436 static $status = array(400 => 'Bad Request',
437 401 => 'Unauthorized',
438 402 => 'Payment Required',
441 405 => 'Method Not Allowed',
442 406 => 'Not Acceptable',
443 407 => 'Proxy Authentication Required',
444 408 => 'Request Timeout',
447 411 => 'Length Required',
448 412 => 'Precondition Failed',
449 413 => 'Request Entity Too Large',
450 414 => 'Request-URI Too Long',
451 415 => 'Unsupported Media Type',
452 416 => 'Requested Range Not Satisfiable',
453 417 => 'Expectation Failed');
455 $action = $this->trimmed('action');
457 common_debug("User error '$code' on '$action': $msg", __FILE__);
459 if (!array_key_exists($code, $status)) {
463 $status_string = $status[$code];
464 header('HTTP/1.1 '.$code.' '.$status_string);
466 if ($content_type == 'xml') {
467 $this->init_document('xml');
468 common_element_start('hash');
469 common_element('error', NULL, $msg);
470 common_element('request', NULL, $_SERVER['REQUEST_URI']);
471 common_element_end('hash');
472 $this->end_document('xml');
474 $this->init_document('json');
475 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
476 print(json_encode($error_array));
477 $this->end_document('json');
483 function init_twitter_rss() {
485 common_element_start('rss', array('version' => '2.0'));
488 function end_twitter_rss() {
489 common_element_end('rss');
493 function init_twitter_atom() {
495 common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
498 function end_twitter_atom() {
500 common_element_end('feed');
503 function show_profile($profile, $content_type='xml', $notice=NULL) {
504 $profile_array = $this->twitter_user_array($profile, true);
505 switch ($content_type) {
507 $this->show_twitter_xml_user($profile_array);
510 $this->show_twitter_json_users($profile_array);
513 $this->client_error(_('Not a supported data format.'));
519 function get_user($id) {
520 if (is_numeric($id)) {
521 return User::staticGet($id);
523 return User::staticGet('nickname', $id);
527 function get_profile($id) {
528 if (is_numeric($id)) {
529 return Profile::staticGet($id);
531 $user = User::staticGet('nickname', $id);
533 return $user->getProfile();
540 function source_link($source) {
541 $source_name = _($source);
550 $ns = Notice_source::staticGet($source);
552 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';