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 {
26 function handle($args) {
27 parent::handle($args);
30 function twitter_user_array($profile, $get_notice=false) {
32 $twitter_user = array();
34 $twitter_user['name'] = $profile->getBestName();
35 $twitter_user['followers_count'] = $this->count_subscriptions($profile);
36 $twitter_user['screen_name'] = $profile->nickname;
37 $twitter_user['description'] = ($profile->bio) ? $profile->bio : NULL;
38 $twitter_user['location'] = ($profile->location) ? $profile->location : NULL;
39 $twitter_user['id'] = intval($profile->id);
41 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
43 $twitter_user['profile_image_url'] = ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE);
44 $twitter_user['protected'] = 'false'; # not supported by Laconica yet
45 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : NULL;
48 $notice = $profile->getCurrentNotice();
51 $twitter_user['status'] = $this->twitter_status_array($notice, false);
58 function twitter_status_array($notice, $include_user=true) {
60 $profile = $notice->getProfile();
62 $twitter_status = array();
63 $twitter_status['text'] = $notice->content;
64 $twitter_status['truncated'] = 'false'; # Not possible on Laconica
65 $twitter_status['created_at'] = $this->date_twitter($notice->created);
66 $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ? intval($notice->reply_to) : NULL;
67 $twitter_status['source'] = $this->source_link($notice->source);
68 $twitter_status['id'] = intval($notice->id);
69 $twitter_status['in_reply_to_user_id'] = ($notice->reply_to) ? $this->replier_by_reply(intval($notice->reply_to)) : NULL;
71 if (isset($this->auth_user)) {
72 common_debug("auth user set: " . $this->auth_user->nickname);
73 $twitter_status['favorited'] = ($this->auth_user->hasFave($notice)) ? 'true' : 'false';
75 common_debug("no auth user set");
76 $twitter_status['favorited'] = 'false';
80 # Don't get notice (recursive!)
81 $twitter_user = $this->twitter_user_array($profile, false, $user);
82 $twitter_status['user'] = $twitter_user;
85 return $twitter_status;
88 function twitter_rss_entry_array($notice) {
90 $profile = $notice->getProfile();
92 $server = common_config('site', 'server');
95 $entry['content'] = $profile->nickname . ': ' . common_xml_safe_str($notice->content);
96 $entry['title'] = $entry['content'];
97 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
98 $entry['published'] = common_date_iso8601($notice->created);
99 $entry['id'] = "tag:$server,2008:$entry[link]";
100 $entry['updated'] = $entry['published'];
103 $entry['description'] = $entry['content'];
104 $entry['pubDate'] = common_date_rfc2822($notice->created);
105 $entry['guid'] = $entry['link'];
110 function twitter_rss_dmsg_array($message) {
112 $server = common_config('site', 'server');
115 $entry['title'] = sprintf('Message from %s to %s',
116 $message->getFrom()->nickname, $message->getTo()->nickname);
118 $entry['content'] = common_xml_safe_str($message->content);
119 $entry['link'] = $message->uri;
120 $entry['published'] = common_date_iso8601($message->created);
121 $entry['id'] = "tag:$server,2008:$entry[link]";
122 $entry['updated'] = $entry['published'];
125 $entry['description'] = $entry['content'];
126 $entry['pubDate'] = common_date_rfc2822($message->created);
127 $entry['guid'] = $entry['link'];
132 function twitter_dmsg_array($message) {
134 $twitter_dm = array();
136 $from_profile = $message->getFrom();
137 $to_profile = $message->getTo();
139 $twitter_dm['id'] = $message->id;
140 $twitter_dm['sender_id'] = $message->from_profile;
141 $twitter_dm['text'] = $message->content;
142 $twitter_dm['recipient_id'] = $message->to_profile;
143 $twitter_dm['created_at'] = $this->date_twitter($message->created);
144 $twitter_dm['sender_screen_name'] = $from_profile->nickname;
145 $twitter_dm['recipient_screen_name'] = $to_profile->nickname;
146 $twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
147 $twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
152 function show_twitter_xml_status($twitter_status) {
153 common_element_start('status');
154 foreach($twitter_status as $element => $value) {
157 $this->show_twitter_xml_user($twitter_status['user']);
160 common_element($element, NULL, common_xml_safe_str($value));
163 common_element($element, NULL, $value);
166 common_element_end('status');
169 function show_twitter_xml_user($twitter_user, $role='user') {
170 common_element_start($role);
171 foreach($twitter_user as $element => $value) {
172 if ($element == 'status') {
173 $this->show_twitter_xml_status($twitter_user['status']);
175 common_element($element, NULL, $value);
178 common_element_end($role);
181 function show_twitter_rss_item($entry) {
182 common_element_start('item');
183 common_element('title', NULL, $entry['title']);
184 common_element('description', NULL, $entry['description']);
185 common_element('pubDate', NULL, $entry['pubDate']);
186 common_element('guid', NULL, $entry['guid']);
187 common_element('link', NULL, $entry['link']);
188 common_element_end('item');
191 function show_twitter_atom_entry($entry) {
192 common_element_start('entry');
193 common_element('title', NULL, $entry['title']);
194 common_element('content', array('type' => 'html'), $entry['title']);
195 common_element('id', NULL, $entry['id']);
196 common_element('published', NULL, $entry['published']);
197 common_element('updated', NULL, $entry['updated']);
198 common_element('link', array('href' => $entry['link'], 'rel' => 'alternate', 'type' => 'text/html'), NULL);
199 common_element_end('entry');
202 function show_json_objects($objects) {
203 print(json_encode($objects));
206 function show_single_xml_status($notice) {
207 $this->init_document('xml');
208 $twitter_status = $this->twitter_status_array($notice);
209 $this->show_twitter_xml_status($twitter_status);
210 $this->end_document('xml');
213 function show_single_json_status($notice) {
214 $this->init_document('json');
215 $status = $this->twitter_status_array($notice);
216 $this->show_json_objects($status);
217 $this->end_document('json');
220 function show_single_xml_dmsg($message) {
221 $this->init_document('xml');
222 $dmsg = $this->twitter_dmsg_array($message);
223 $this->show_twitter_xml_dmsg($dmsg);
224 $this->end_document('xml');
227 function show_single_json_dmsg($message) {
228 $this->init_document('json');
229 $dmsg = $this->twitter_dmsg_array($message);
230 $this->show_json_objects($dmsg);
231 $this->end_document('json');
234 function show_twitter_xml_dmsg($twitter_dm) {
235 common_element_start('direct_message');
236 foreach($twitter_dm as $element => $value) {
240 $this->show_twitter_xml_user($value, $element);
243 common_element($element, NULL, common_xml_safe_str($value));
246 common_element($element, NULL, $value);
249 common_element_end('direct_message');
252 function show_xml_timeline($notice) {
254 $this->init_document('xml');
255 common_element_start('statuses', array('type' => 'array'));
257 if (is_array($notice)) {
258 foreach ($notice as $n) {
259 $twitter_status = $this->twitter_status_array($n);
260 $this->show_twitter_xml_status($twitter_status);
263 while ($notice->fetch()) {
264 $twitter_status = $this->twitter_status_array($notice);
265 $this->show_twitter_xml_status($twitter_status);
269 common_element_end('statuses');
270 $this->end_document('xml');
273 function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=NULL) {
275 $this->init_document('rss');
277 common_element_start('channel');
278 common_element('title', NULL, $title);
279 common_element('link', NULL, $link);
280 if (!is_null($suplink)) {
281 # For FriendFeed's SUP protocol
282 common_element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
283 'rel' => 'http://api.friendfeed.com/2008/03#sup',
285 'type' => 'application/json'));
287 common_element('description', NULL, $subtitle);
288 common_element('language', NULL, 'en-us');
289 common_element('ttl', NULL, '40');
292 if (is_array($notice)) {
293 foreach ($notice as $n) {
294 $entry = $this->twitter_rss_entry_array($n);
295 $this->show_twitter_rss_item($entry);
298 while ($notice->fetch()) {
299 $entry = $this->twitter_rss_entry_array($notice);
300 $this->show_twitter_rss_item($entry);
304 common_element_end('channel');
305 $this->end_twitter_rss();
308 function show_atom_timeline($notice, $title, $id, $link, $subtitle=NULL, $suplink=NULL) {
310 $this->init_document('atom');
312 common_element('title', NULL, $title);
313 common_element('id', NULL, $id);
314 common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), NULL);
315 if (!is_null($suplink)) {
316 # For FriendFeed's SUP protocol
317 common_element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
319 'type' => 'application/json'));
321 common_element('subtitle', NULL, $subtitle);
323 if (is_array($notice)) {
324 foreach ($notice as $n) {
325 $entry = $this->twitter_rss_entry_array($n);
326 $this->show_twitter_atom_entry($entry);
329 while ($notice->fetch()) {
330 $entry = $this->twitter_rss_entry_array($notice);
331 $this->show_twitter_atom_entry($entry);
335 $this->end_document('atom');
339 function show_json_timeline($notice) {
341 $this->init_document('json');
345 if (is_array($notice)) {
346 foreach ($notice as $n) {
347 $twitter_status = $this->twitter_status_array($n);
348 array_push($statuses, $twitter_status);
351 while ($notice->fetch()) {
352 $twitter_status = $this->twitter_status_array($notice);
353 array_push($statuses, $twitter_status);
357 $this->show_json_objects($statuses);
359 $this->end_document('json');
362 // Anyone know what date format this is?
363 // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
364 function date_twitter($dt) {
366 return date("D M d G:i:s O Y", $t);
369 function replier_by_reply($reply_id) {
370 $notice = Notice::staticGet($reply_id);
372 $profile = $notice->getProfile();
374 return intval($profile->id);
376 common_debug('Can\'t find a profile for notice: ' . $notice->id, __FILE__);
379 common_debug("Can't get notice: $reply_id", __FILE__);
384 // XXX: Candidate for a general utility method somewhere?
385 function count_subscriptions($profile) {
388 $sub = new Subscription();
389 $sub->subscribed = $profile->id;
391 $count = $sub->find();
400 function init_document($type='xml') {
403 header('Content-Type: application/xml; charset=utf-8');
407 header('Content-Type: application/json; charset=utf-8');
409 // Check for JSONP callback
410 $callback = $this->arg('callback');
412 print $callback . '(';
416 header("Content-Type: application/rss+xml; charset=utf-8");
417 $this->init_twitter_rss();
420 header('Content-Type: application/atom+xml; charset=utf-8');
421 $this->init_twitter_atom();
424 $this->client_error(_('Not a supported data format.'));
431 function end_document($type='xml') {
438 // Check for JSONP callback
439 $callback = $this->arg('callback');
445 $this->end_twitter_rss();
448 $this->end_twitter_rss();
451 $this->client_error(_('Not a supported data format.'));
457 function client_error($msg, $code = 400, $content_type = 'json') {
459 static $status = array(400 => 'Bad Request',
460 401 => 'Unauthorized',
461 402 => 'Payment Required',
464 405 => 'Method Not Allowed',
465 406 => 'Not Acceptable',
466 407 => 'Proxy Authentication Required',
467 408 => 'Request Timeout',
470 411 => 'Length Required',
471 412 => 'Precondition Failed',
472 413 => 'Request Entity Too Large',
473 414 => 'Request-URI Too Long',
474 415 => 'Unsupported Media Type',
475 416 => 'Requested Range Not Satisfiable',
476 417 => 'Expectation Failed');
478 $action = $this->trimmed('action');
480 common_debug("User error '$code' on '$action': $msg", __FILE__);
482 if (!array_key_exists($code, $status)) {
486 $status_string = $status[$code];
487 header('HTTP/1.1 '.$code.' '.$status_string);
489 if ($content_type == 'xml') {
490 $this->init_document('xml');
491 common_element_start('hash');
492 common_element('error', NULL, $msg);
493 common_element('request', NULL, $_SERVER['REQUEST_URI']);
494 common_element_end('hash');
495 $this->end_document('xml');
497 $this->init_document('json');
498 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
499 print(json_encode($error_array));
500 $this->end_document('json');
505 function init_twitter_rss() {
507 common_element_start('rss', array('version' => '2.0'));
510 function end_twitter_rss() {
511 common_element_end('rss');
515 function init_twitter_atom() {
517 common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
520 function end_twitter_atom() {
522 common_element_end('feed');
525 function show_profile($profile, $content_type='xml', $notice=NULL) {
526 $profile_array = $this->twitter_user_array($profile, true);
527 switch ($content_type) {
529 $this->show_twitter_xml_user($profile_array);
532 $this->show_json_objects($profile_array);
535 $this->client_error(_('Not a supported data format.'));
541 function get_user($id, $apidata=NULL) {
543 return $apidata['user'];
544 } else if (is_numeric($id)) {
545 return User::staticGet($id);
547 $nickname = common_canonical_nickname($id);
548 return User::staticGet('nickname', $nickname);
552 function get_profile($id) {
553 if (is_numeric($id)) {
554 return Profile::staticGet($id);
556 $user = User::staticGet('nickname', $id);
558 return $user->getProfile();
565 function source_link($source) {
566 $source_name = _($source);
575 $ns = Notice_source::staticGet($source);
577 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';