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'] = $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,$entry[published]:$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 show_twitter_xml_status($twitter_status) {
102 common_element_start('status');
103 foreach($twitter_status as $element => $value) {
104 if ($element == 'user') {
105 $this->show_twitter_xml_user($twitter_status['user']);
107 common_element($element, NULL, $value);
110 common_element_end('status');
113 function show_twitter_xml_user($twitter_user) {
114 common_element_start('user');
115 foreach($twitter_user as $element => $value) {
116 if ($element == 'status') {
117 $this->show_twitter_xml_status($twitter_user['status']);
119 common_element($element, NULL, $value);
122 common_element_end('user');
125 function show_twitter_rss_item($entry) {
126 common_element_start('item');
127 common_element('title', NULL, $entry['title']);
128 common_element('description', NULL, $entry['description']);
129 common_element('pubDate', NULL, $entry['pubDate']);
130 common_element('guid', NULL, $entry['guid']);
131 common_element('link', NULL, $entry['link']);
132 common_element_end('item');
135 function show_twitter_atom_entry($entry) {
136 common_element_start('entry');
137 common_element('title', NULL, $entry['title']);
138 common_element('content', array('type' => 'html'), $entry['title']);
139 common_element('id', NULL, $entry['id']);
140 common_element('published', NULL, $entry['published']);
141 common_element('updated', NULL, $entry['updated']);
142 common_element('link', array('href' => $entry['link'], 'rel' => 'alternate', 'type' => 'text/html'), NULL);
143 common_element_end('entry');
146 function show_twitter_json_statuses($twitter_statuses) {
147 print(json_encode($twitter_statuses));
150 function show_twitter_json_users($twitter_users) {
151 print(json_encode($twitter_users));
154 function show_single_xml_status($notice) {
155 $this->init_document('xml');
156 $twitter_status = $this->twitter_status_array($notice);
157 $this->show_twitter_xml_status($twitter_status);
158 $this->end_document('xml');
162 function show_single_json_status($notice) {
163 $this->init_document('json');
164 $status = $this->twitter_status_array($notice);
165 $this->show_twitter_json_statuses($status);
166 $this->end_document('json');
170 // Anyone know what date format this is?
171 // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
172 function date_twitter($dt) {
174 return date("D M d G:i:s O Y", $t);
177 function replier_by_reply($reply_id) {
178 $notice = Notice::staticGet($reply_id);
180 $profile = $notice->getProfile();
182 return intval($profile->id);
184 common_debug('Can\'t find a profile for notice: ' . $notice->id, __FILE__);
187 common_debug("Can't get notice: $reply_id", __FILE__);
192 // XXX: Candidate for a general utility method somewhere?
193 function count_subscriptions($profile) {
196 $sub = new Subscription();
197 $sub->subscribed = $profile->id;
199 $count = $sub->find();
208 function init_document($type='xml') {
211 header('Content-Type: application/xml; charset=utf-8');
215 header('Content-Type: application/json; charset=utf-8');
217 // Check for JSON-P callback
218 $callback = $this->arg('callback');
220 print $callback . '(';
224 header("Content-Type: application/rss+xml; charset=utf-8");
225 $this->init_twitter_rss();
228 header('Content-Type: application/atom+xml; charset=utf-8');
229 $this->init_twitter_atom();
232 $this->client_error(_('Not a supported data format.'));
239 function end_document($type='xml') {
246 // Check for JSON-P callback
247 $callback = $this->arg('callback');
253 $this->end_twitter_rss();
256 $this->end_twitter_rss();
259 $this->client_error(_('Not a supported data format.'));
265 function client_error($msg, $code = 400, $content_type = 'json') {
267 static $status = array(400 => 'Bad Request',
268 401 => 'Unauthorized',
269 402 => 'Payment Required',
272 405 => 'Method Not Allowed',
273 406 => 'Not Acceptable',
274 407 => 'Proxy Authentication Required',
275 408 => 'Request Timeout',
278 411 => 'Length Required',
279 412 => 'Precondition Failed',
280 413 => 'Request Entity Too Large',
281 414 => 'Request-URI Too Long',
282 415 => 'Unsupported Media Type',
283 416 => 'Requested Range Not Satisfiable',
284 417 => 'Expectation Failed');
286 $action = $this->trimmed('action');
288 common_debug("User error '$code' on '$action': $msg", __FILE__);
290 if (!array_key_exists($code, $status)) {
294 $status_string = $status[$code];
295 header('HTTP/1.1 '.$code.' '.$status_string);
297 if ($content_type == 'xml') {
298 $this->init_document('xml');
299 common_element_start('hash');
300 common_element('error', NULL, $msg);
301 common_element('request', NULL, $_SERVER['REQUEST_URI']);
302 common_element_end('hash');
303 $this->end_document('xml');
305 $this->init_document('json');
306 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
307 print(json_encode($error_array));
308 $this->end_document('json');
314 function init_twitter_rss() {
316 common_element_start('rss', array('version' => '2.0'));
319 function end_twitter_rss() {
320 common_element_end('rss');
324 function init_twitter_atom() {
326 common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
329 function end_twitter_atom() {
331 common_element_end('feed');
334 function show_profile($profile, $content_type='xml', $notice=NULL) {
335 $profile_array = $this->twitter_user_array($profile, true);
336 switch ($content_type) {
338 $this->show_twitter_xml_user($profile_array);
341 $this->show_twitter_json_users($profile_array);
344 $this->client_error(_('Not a supported data format.'));