]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitterapi.php
Twitter-compatible API: implemented /users/show.format method
[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(intval($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                 foreach($twitter_status as $element => $value) {
104                         if ($element == 'user') {
105                                 $this->show_twitter_xml_user($twitter_status['user']);
106                         } else {
107                                 common_element($element, NULL, $value);
108                         }
109                 }
110                 common_element_end('status');
111         }       
112         
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']);
118                         } else {
119                                 common_element($element, NULL, $value);
120                         }
121                 }
122                 common_element_end('user');
123         }
124
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');
133         }
134                 
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');
144         }
145         
146         function show_twitter_json_statuses($twitter_statuses) {
147                 print(json_encode($twitter_statuses));
148         }
149
150         function show_twitter_json_users($twitter_users) {
151                 print(json_encode($twitter_users));
152         }
153
154         function show($args, $apidata) {
155                 parent::handle($args);
156                 
157                 $id = $apidata['api_arg'];              
158                 $notice = Notice::staticGet($id);
159
160                 if ($notice) {
161
162                         if ($apidata['content-type'] == 'xml') { 
163                                 $this->show_single_xml_status($notice);
164                         } elseif ($apidata['content-type'] == 'json') {
165                                 $this->show_single_json_status($notice);
166                         }
167                 } else {
168                         header('HTTP/1.1 404 Not Found');
169                 }
170                 
171                 exit();
172         }
173                 
174         function show_single_xml_status($notice) {
175                 $this->init_document('xml');
176                 $twitter_status = $this->twitter_status_array($notice);                                         
177                 $this->show_twitter_xml_status($twitter_status);
178                 $this->end_document('xml');
179                 exit();
180         }
181         
182         function show_single_json_status($notice) {
183                 $this->init_document('json');
184                 $status = $this->twitter_status_array($notice);
185                 $this->show_twitter_json_statuses($status);
186                 $this->end_document('json');
187                 exit();
188         }
189         
190         // Anyone know what date format this is? 
191         // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach 
192         function date_twitter($dt) {
193                 $t = strtotime($dt);
194                 return date("D M d G:i:s O Y", $t);
195         }
196         
197         function replier_by_reply($reply_id) {  
198                 $notice = Notice::staticGet($reply_id);
199                 if ($notice) {
200                         $profile = $notice->getProfile();
201                         if ($profile) {
202                                 return intval($profile->id);
203                         } else {
204                                 common_debug('Can\'t find a profile for notice: ' . $notice->id, __FILE__);
205                         }
206                 } else {
207                         common_debug("Can't get notice: $reply_id", __FILE__);
208                 }
209                 return NULL;
210         }
211
212         // XXX: Candidate for a general utility method somewhere?       
213         function count_subscriptions($profile) {
214                 
215                 $count = 0;
216                 $sub = new Subscription();
217                 $sub->subscribed = $profile->id;
218
219                 $count = $sub->find();
220                 
221                 if ($count > 0) {
222                         return $count - 1;
223                 } else {
224                         return 0;
225                 }
226         }
227
228         function init_document($type='xml') {
229                 switch ($type) {
230                  case 'xml':
231                         header('Content-Type: application/xml; charset=utf-8');         
232                         common_start_xml();
233                         break;
234                  case 'json':
235                         header('Content-Type: application/json; charset=utf-8');
236                         break;
237                  case 'rss':
238                         header("Content-Type: application/rss+xml; charset=utf-8");
239                         $this->init_twitter_rss();
240                         break;
241                  case 'atom':
242                         header('Content-Type: application/atom+xml; charset=utf-8');
243                         $this->init_twitter_atom();
244                         break;
245                  default:
246                         $this->client_error(_('Unsupported type'));
247                         break;
248                 }
249                 
250                 return;
251         }
252         
253         function end_document($type='xml') {
254                 switch ($type) {
255                  case 'xml':
256                         common_end_xml();
257                         break;
258                  case 'json':
259                         break;
260                  case 'rss':
261                         $this->end_twitter_rss();
262                         break;
263                  case 'atom':
264                         $this->end_twitter_rss();
265                         break;
266                  default:
267                         $this->client_error(_('Unsupported type'));
268                         break;
269                 }
270                 return;
271         }
272                 
273         function client_error($msg, $code = 400, $content_type = 'json') {
274                 
275                 static $status = array(400 => 'Bad Request',
276                                                            401 => 'Unauthorized',
277                                                            402 => 'Payment Required',
278                                                            403 => 'Forbidden',
279                                                            404 => 'Not Found',
280                                                            405 => 'Method Not Allowed',
281                                                            406 => 'Not Acceptable',
282                                                            407 => 'Proxy Authentication Required',
283                                                            408 => 'Request Timeout',
284                                                            409 => 'Conflict',
285                                                            410 => 'Gone',
286                                                            411 => 'Length Required',
287                                                            412 => 'Precondition Failed',
288                                                            413 => 'Request Entity Too Large',
289                                                            414 => 'Request-URI Too Long',
290                                                            415 => 'Unsupported Media Type',
291                                                            416 => 'Requested Range Not Satisfiable',
292                                                            417 => 'Expectation Failed');
293                 
294                 $action = $this->trimmed('action');
295                 
296                 common_debug("User error '$code' on '$action': $msg", __FILE__);
297                 
298                 if (!array_key_exists($code, $status)) {
299                         $code = 400;
300                 }
301
302                 $status_string = $status[$code];
303                 header('HTTP/1.1 '.$code.' '.$status_string);
304                                                 
305                 if ($content_type == 'xml') {
306                         $this->init_document('xml');
307                         common_element_start('hash');
308                         common_element('error', NULL, $msg);
309                         common_element('request', NULL, $_SERVER['REQUEST_URI']);
310                         common_element_end('hash');
311                         $this->end_document('xml');
312                 } else {
313                         $this->init_document('json');
314                         $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
315                         print(json_encode($error_array));
316                         $this->end_document('json');
317                 }
318                 
319                 exit();
320         }
321
322         function init_twitter_rss() {
323                 common_start_xml();
324                 common_element_start('rss', array('version' => '2.0'));
325         }
326         
327         function end_twitter_rss() {
328                 common_element_end('rss');
329                 common_end_xml();
330         }
331         
332         function init_twitter_atom() {
333                 common_start_xml();
334                 common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
335         }
336         
337         function end_twitter_atom() {
338                 common_end_xml();
339                 common_element_end('feed');
340         }
341
342         function show_profile($profile, $content_type='xml', $notice=NULL) {                            
343                 $profile_array = $this->twitter_user_array($profile, true);
344                 switch ($content_type) {
345                  case 'xml':
346                         $this->show_twitter_xml_user($profile_array);
347                         break;
348                  case 'json':
349                         $this->show_twitter_json_users($profile_array);
350                         break;
351                  default:
352                         $this->client_error(_('not a supported data format'));
353                         return;
354                 }
355                 return;
356         }
357 }