]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitterapi.php
c8d78e1a37e52e29a16a0aed3655e16a94542351
[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'] = $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.
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,2008:$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 twitter_rss_dmsg_array($message) {
102
103                 $server = common_config('site', 'server');
104                 $entry = array();
105
106                 $entry['title'] = sprintf('Message from %s to %s',
107                         $message->getFrom()->nickname, $message->getTo()->nickname);
108
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'];
114
115                 # RSS Item specific
116                 $entry['description'] = $message->content;
117                 $entry['pubDate'] = common_date_rfc2822($message->created);
118                 $entry['guid'] = $entry['link'];
119
120                 return $entry;
121         }
122
123
124         function twitter_dmsg_array($message) {
125
126                 $twitter_dm = array();
127
128                 $from_profile = $message->getFrom();
129                 $to_profile = $message->getTo();
130
131                 $twitter_dm['id'] = $message->id;
132                 $twitter_dm['sender_id'] = $message->from_profile;
133                 $twitter_dm['text'] = $message->content;
134                 $twitter_dm['recipient_id'] = $message->to_profile;
135                 $twitter_dm['created_at'] = $this->date_twitter($message->created);
136                 $twitter_dm['sender_screen_name'] = $from_profile->nickname;
137                 $twitter_dm['recipient_screen_name'] = $to_profile->nickname;
138                 $twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
139                 $twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
140
141                 return $twitter_dm;
142         }
143
144         function show_twitter_xml_status($twitter_status) {
145                 common_element_start('status');
146                 foreach($twitter_status as $element => $value) {
147                         if ($element == 'user') {
148                                 $this->show_twitter_xml_user($twitter_status['user']);
149                         } else {
150                                 common_element($element, NULL, $value);
151                         }
152                 }
153                 common_element_end('status');
154         }
155
156         function show_twitter_xml_user($twitter_user, $role='user') {
157                 common_element_start($role);
158                 foreach($twitter_user as $element => $value) {
159                         if ($element == 'status') {
160                                 $this->show_twitter_xml_status($twitter_user['status']);
161                         } else {
162                                 common_element($element, NULL, $value);
163                         }
164                 }
165                 common_element_end($role);
166         }
167
168         function show_twitter_rss_item($entry) {
169                 common_element_start('item');
170                 common_element('title', NULL, $entry['title']);
171                 common_element('description', NULL, $entry['description']);
172                 common_element('pubDate', NULL, $entry['pubDate']);
173                 common_element('guid', NULL, $entry['guid']);
174                 common_element('link', NULL, $entry['link']);
175                 common_element_end('item');
176         }
177
178         function show_twitter_atom_entry($entry) {
179             common_element_start('entry');
180                 common_element('title', NULL, $entry['title']);
181                 common_element('content', array('type' => 'html'), $entry['title']);
182                 common_element('id', NULL, $entry['id']);
183                 common_element('published', NULL, $entry['published']);
184                 common_element('updated', NULL, $entry['updated']);
185                 common_element('link', array('href' => $entry['link'], 'rel' => 'alternate', 'type' => 'text/html'), NULL);
186                 common_element_end('entry');
187         }
188
189         function show_twitter_json_statuses($twitter_statuses) {
190                 print(json_encode($twitter_statuses));
191         }
192
193         function show_twitter_json_users($twitter_users) {
194                 print(json_encode($twitter_users));
195         }
196
197         function show_twitter_json_dmsgs($twitter_dms) {
198                 print(json_encode($twitter_dms));
199         }
200
201         function show_single_xml_status($notice) {
202                 $this->init_document('xml');
203                 $twitter_status = $this->twitter_status_array($notice);
204                 $this->show_twitter_xml_status($twitter_status);
205                 $this->end_document('xml');
206                 exit();
207         }
208
209         function show_single_json_status($notice) {
210                 $this->init_document('json');
211                 $status = $this->twitter_status_array($notice);
212                 $this->show_twitter_json_statuses($status);
213                 $this->end_document('json');
214                 exit();
215         }
216
217         function show_single_xml_dmsg($message) {
218                 $this->init_document('xml');
219                 $dmsg = $this->twitter_dmsg_array($message);
220                 $this->show_twitter_xml_dmsg($dmsg);
221                 $this->end_document('xml');
222         }
223
224         function show_single_json_dmsg($message) {
225                 $this->init_document('json');
226                 $dmsg = $this->twitter_dmsg_array($message);
227                 $this->show_twitter_json_dm($dmsg);
228                 $this->end_document('json');
229         }
230
231         function show_twitter_xml_dmsg($twitter_dm) {
232                 common_element_start('direct_message');
233                 foreach($twitter_dm as $element => $value) {
234                         if ($element == 'sender' || $element == 'recipient') {
235                                 $this->show_twitter_xml_user($value, $element);
236                         } else {
237                                 common_element($element, NULL, $value);
238                         }
239                 }
240                 common_element_end('direct_message');
241         }
242
243         // Anyone know what date format this is?
244         // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
245         function date_twitter($dt) {
246                 $t = strtotime($dt);
247                 return date("D M d G:i:s O Y", $t);
248         }
249
250         function replier_by_reply($reply_id) {
251                 $notice = Notice::staticGet($reply_id);
252                 if ($notice) {
253                         $profile = $notice->getProfile();
254                         if ($profile) {
255                                 return intval($profile->id);
256                         } else {
257                                 common_debug('Can\'t find a profile for notice: ' . $notice->id, __FILE__);
258                         }
259                 } else {
260                         common_debug("Can't get notice: $reply_id", __FILE__);
261                 }
262                 return NULL;
263         }
264
265         // XXX: Candidate for a general utility method somewhere?
266         function count_subscriptions($profile) {
267
268                 $count = 0;
269                 $sub = new Subscription();
270                 $sub->subscribed = $profile->id;
271
272                 $count = $sub->find();
273
274                 if ($count > 0) {
275                         return $count - 1;
276                 } else {
277                         return 0;
278                 }
279         }
280
281         function init_document($type='xml') {
282                 switch ($type) {
283                  case 'xml':
284                         header('Content-Type: application/xml; charset=utf-8');
285                         common_start_xml();
286                         break;
287                  case 'json':
288                         header('Content-Type: application/json; charset=utf-8');
289
290                         // Check for JSONP callback
291                         $callback = $this->arg('callback');
292                         if ($callback) {
293                                 print $callback . '(';
294                         }
295                         break;
296                  case 'rss':
297                         header("Content-Type: application/rss+xml; charset=utf-8");
298                         $this->init_twitter_rss();
299                         break;
300                  case 'atom':
301                         header('Content-Type: application/atom+xml; charset=utf-8');
302                         $this->init_twitter_atom();
303                         break;
304                  default:
305                         $this->client_error(_('Not a supported data format.'));
306                         break;
307                 }
308
309                 return;
310         }
311
312         function end_document($type='xml') {
313                 switch ($type) {
314                  case 'xml':
315                         common_end_xml();
316                         break;
317                  case 'json':
318
319                         // Check for JSONP callback
320                         $callback = $this->arg('callback');
321                         if ($callback) {
322                                 print ')';
323                         }
324                         break;
325                  case 'rss':
326                         $this->end_twitter_rss();
327                         break;
328                  case 'atom':
329                         $this->end_twitter_rss();
330                         break;
331                  default:
332                         $this->client_error(_('Not a supported data format.'));
333                         break;
334                 }
335                 return;
336         }
337
338         function client_error($msg, $code = 400, $content_type = 'json') {
339
340                 static $status = array(400 => 'Bad Request',
341                                                            401 => 'Unauthorized',
342                                                            402 => 'Payment Required',
343                                                            403 => 'Forbidden',
344                                                            404 => 'Not Found',
345                                                            405 => 'Method Not Allowed',
346                                                            406 => 'Not Acceptable',
347                                                            407 => 'Proxy Authentication Required',
348                                                            408 => 'Request Timeout',
349                                                            409 => 'Conflict',
350                                                            410 => 'Gone',
351                                                            411 => 'Length Required',
352                                                            412 => 'Precondition Failed',
353                                                            413 => 'Request Entity Too Large',
354                                                            414 => 'Request-URI Too Long',
355                                                            415 => 'Unsupported Media Type',
356                                                            416 => 'Requested Range Not Satisfiable',
357                                                            417 => 'Expectation Failed');
358
359                 $action = $this->trimmed('action');
360
361                 common_debug("User error '$code' on '$action': $msg", __FILE__);
362
363                 if (!array_key_exists($code, $status)) {
364                         $code = 400;
365                 }
366
367                 $status_string = $status[$code];
368                 header('HTTP/1.1 '.$code.' '.$status_string);
369
370                 if ($content_type == 'xml') {
371                         $this->init_document('xml');
372                         common_element_start('hash');
373                         common_element('error', NULL, $msg);
374                         common_element('request', NULL, $_SERVER['REQUEST_URI']);
375                         common_element_end('hash');
376                         $this->end_document('xml');
377                 } else {
378                         $this->init_document('json');
379                         $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
380                         print(json_encode($error_array));
381                         $this->end_document('json');
382                 }
383
384                 exit();
385         }
386
387         function init_twitter_rss() {
388                 common_start_xml();
389                 common_element_start('rss', array('version' => '2.0'));
390         }
391
392         function end_twitter_rss() {
393                 common_element_end('rss');
394                 common_end_xml();
395         }
396
397         function init_twitter_atom() {
398                 common_start_xml();
399                 common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
400         }
401
402         function end_twitter_atom() {
403                 common_end_xml();
404                 common_element_end('feed');
405         }
406
407         function show_profile($profile, $content_type='xml', $notice=NULL) {
408                 $profile_array = $this->twitter_user_array($profile, true);
409                 switch ($content_type) {
410                  case 'xml':
411                         $this->show_twitter_xml_user($profile_array);
412                         break;
413                  case 'json':
414                         $this->show_twitter_json_users($profile_array);
415                         break;
416                  default:
417                         $this->client_error(_('Not a supported data format.'));
418                         return;
419                 }
420                 return;
421         }
422
423         function get_user($id) {
424                 if (is_numeric($id)) {
425                         return User::staticGet($id);
426                 } else {
427                         return User::staticGet('nickname', $id);
428                 }
429         }
430
431         function get_profile($id) {
432                 if (is_numeric($id)) {
433                         return Profile::staticGet($id);
434                 } else {
435                         $user = User::staticGet('nickname', $id);
436                         if ($user) {
437                                 return $user->getProfile();
438                         } else {
439                                 return NULL;
440                         }
441                 }
442         }
443
444         function source_link($source) {
445                 $source_name = _($source);
446                 switch ($source) {
447                  case 'web':
448                  case 'xmpp':
449                  case 'mail':
450                  case 'omb':
451                  case 'api':
452                         break;
453                  default:
454                         $ns = Notice_source::staticGet($source);
455                         if ($ns) {
456                                 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
457                         }
458                         break;
459                 }
460                 return $source_name;
461         }
462
463 }