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_json_objects($objects) {
189 print(json_encode($objects));
192 function show_single_xml_status($notice) {
193 $this->init_document('xml');
194 $twitter_status = $this->twitter_status_array($notice);
195 $this->show_twitter_xml_status($twitter_status);
196 $this->end_document('xml');
199 function show_single_json_status($notice) {
200 $this->init_document('json');
201 $status = $this->twitter_status_array($notice);
202 $this->show_json_objects($status);
203 $this->end_document('json');
206 function show_single_xml_dmsg($message) {
207 $this->init_document('xml');
208 $dmsg = $this->twitter_dmsg_array($message);
209 $this->show_twitter_xml_dmsg($dmsg);
210 $this->end_document('xml');
213 function show_single_json_dmsg($message) {
214 $this->init_document('json');
215 $dmsg = $this->twitter_dmsg_array($message);
216 $this->show_json_objects($dmsg);
217 $this->end_document('json');
220 function show_twitter_xml_dmsg($twitter_dm) {
221 common_element_start('direct_message');
222 foreach($twitter_dm as $element => $value) {
223 if ($element == 'sender' || $element == 'recipient') {
224 $this->show_twitter_xml_user($value, $element);
226 common_element($element, NULL, $value);
229 common_element_end('direct_message');
232 function show_xml_timeline($notice) {
234 $this->init_document('xml');
235 common_element_start('statuses', array('type' => 'array'));
237 if (is_array($notice)) {
238 foreach ($notice as $n) {
239 $twitter_status = $this->twitter_status_array($n);
240 $this->show_twitter_xml_status($twitter_status);
243 while ($notice->fetch()) {
244 $twitter_status = $this->twitter_status_array($notice);
245 $this->show_twitter_xml_status($twitter_status);
249 common_element_end('statuses');
250 $this->end_document('xml');
253 function show_rss_timeline($notice, $title, $link, $subtitle) {
255 $this->init_document('rss');
257 common_element_start('channel');
258 common_element('title', NULL, $title);
259 common_element('link', NULL, $link);
260 common_element('description', NULL, $subtitle);
261 common_element('language', NULL, 'en-us');
262 common_element('ttl', NULL, '40');
265 if (is_array($notice)) {
266 foreach ($notice as $n) {
267 $entry = $this->twitter_rss_entry_array($n);
268 $this->show_twitter_rss_item($entry);
271 while ($notice->fetch()) {
272 $entry = $this->twitter_rss_entry_array($notice);
273 $this->show_twitter_rss_item($entry);
277 common_element_end('channel');
278 $this->end_twitter_rss();
281 function show_atom_timeline($notice, $title, $id, $link, $subtitle=NULL) {
283 $this->init_document('atom');
285 common_element('title', NULL, $title);
286 common_element('id', NULL, $id);
287 common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), NULL);
288 common_element('subtitle', NULL, $subtitle);
290 if (is_array($notice)) {
291 foreach ($notice as $n) {
292 $entry = $this->twitter_rss_entry_array($n);
293 $this->show_twitter_atom_entry($entry);
296 while ($notice->fetch()) {
297 $entry = $this->twitter_rss_entry_array($notice);
298 $this->show_twitter_atom_entry($entry);
302 $this->end_document('atom');
306 function show_json_timeline($notice) {
308 $this->init_document('json');
312 if (is_array($notice)) {
313 foreach ($notice as $n) {
314 $twitter_status = $this->twitter_status_array($n);
315 array_push($statuses, $twitter_status);
318 while ($notice->fetch()) {
319 $twitter_status = $this->twitter_status_array($notice);
320 array_push($statuses, $twitter_status);
324 $this->show_json_objects($statuses);
326 $this->end_document('json');
329 // Anyone know what date format this is?
330 // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
331 function date_twitter($dt) {
333 return date("D M d G:i:s O Y", $t);
336 function replier_by_reply($reply_id) {
337 $notice = Notice::staticGet($reply_id);
339 $profile = $notice->getProfile();
341 return intval($profile->id);
343 common_debug('Can\'t find a profile for notice: ' . $notice->id, __FILE__);
346 common_debug("Can't get notice: $reply_id", __FILE__);
351 // XXX: Candidate for a general utility method somewhere?
352 function count_subscriptions($profile) {
355 $sub = new Subscription();
356 $sub->subscribed = $profile->id;
358 $count = $sub->find();
367 function init_document($type='xml') {
370 header('Content-Type: application/xml; charset=utf-8');
374 header('Content-Type: application/json; charset=utf-8');
376 // Check for JSONP callback
377 $callback = $this->arg('callback');
379 print $callback . '(';
383 header("Content-Type: application/rss+xml; charset=utf-8");
384 $this->init_twitter_rss();
387 header('Content-Type: application/atom+xml; charset=utf-8');
388 $this->init_twitter_atom();
391 $this->client_error(_('Not a supported data format.'));
398 function end_document($type='xml') {
405 // Check for JSONP callback
406 $callback = $this->arg('callback');
412 $this->end_twitter_rss();
415 $this->end_twitter_rss();
418 $this->client_error(_('Not a supported data format.'));
424 function client_error($msg, $code = 400, $content_type = 'json') {
426 static $status = array(400 => 'Bad Request',
427 401 => 'Unauthorized',
428 402 => 'Payment Required',
431 405 => 'Method Not Allowed',
432 406 => 'Not Acceptable',
433 407 => 'Proxy Authentication Required',
434 408 => 'Request Timeout',
437 411 => 'Length Required',
438 412 => 'Precondition Failed',
439 413 => 'Request Entity Too Large',
440 414 => 'Request-URI Too Long',
441 415 => 'Unsupported Media Type',
442 416 => 'Requested Range Not Satisfiable',
443 417 => 'Expectation Failed');
445 $action = $this->trimmed('action');
447 common_debug("User error '$code' on '$action': $msg", __FILE__);
449 if (!array_key_exists($code, $status)) {
453 $status_string = $status[$code];
454 header('HTTP/1.1 '.$code.' '.$status_string);
456 if ($content_type == 'xml') {
457 $this->init_document('xml');
458 common_element_start('hash');
459 common_element('error', NULL, $msg);
460 common_element('request', NULL, $_SERVER['REQUEST_URI']);
461 common_element_end('hash');
462 $this->end_document('xml');
464 $this->init_document('json');
465 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
466 print(json_encode($error_array));
467 $this->end_document('json');
472 function init_twitter_rss() {
474 common_element_start('rss', array('version' => '2.0'));
477 function end_twitter_rss() {
478 common_element_end('rss');
482 function init_twitter_atom() {
484 common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
487 function end_twitter_atom() {
489 common_element_end('feed');
492 function show_profile($profile, $content_type='xml', $notice=NULL) {
493 $profile_array = $this->twitter_user_array($profile, true);
494 switch ($content_type) {
496 $this->show_twitter_xml_user($profile_array);
499 $this->show_json_objects($profile_array);
502 $this->client_error(_('Not a supported data format.'));
508 function get_user($id) {
509 if (is_numeric($id)) {
510 return User::staticGet($id);
512 return User::staticGet('nickname', $id);
516 function get_profile($id) {
517 if (is_numeric($id)) {
518 return Profile::staticGet($id);
520 $user = User::staticGet('nickname', $id);
522 return $user->getProfile();
529 function source_link($source) {
530 $source_name = _($source);
539 $ns = Notice_source::staticGet($source);
541 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';