]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - _darcs/pristine/lib/twitterapi.php
replace NULL with null
[quix0rs-gnu-social.git] / _darcs / pristine / 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     var $auth_user;
25
26     function handle($args) {
27         parent::handle($args);
28     }
29
30     function twitter_user_array($profile, $get_notice=false) {
31
32         $twitter_user = array();
33
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);
40
41         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
42
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;
46
47         if ($get_notice) {
48             $notice = $profile->getCurrentNotice();
49             if ($notice) {
50                 # don't get user!
51                 $twitter_user['status'] = $this->twitter_status_array($notice, false);
52             }
53         }
54
55         return $twitter_user;
56     }
57
58     function twitter_status_array($notice, $include_user=true) {
59
60         $profile = $notice->getProfile();
61
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;
70
71         if (isset($this->auth_user)) {
72             $twitter_status['favorited'] = ($this->auth_user->hasFave($notice)) ? 'true' : 'false';
73         } else {
74             $twitter_status['favorited'] = 'false';
75         }
76
77         if ($include_user) {
78             # Don't get notice (recursive!)
79             $twitter_user = $this->twitter_user_array($profile, false);
80             $twitter_status['user'] = $twitter_user;
81         }
82
83         return $twitter_status;
84     }
85
86     function twitter_rss_entry_array($notice) {
87
88         $profile = $notice->getProfile();
89
90         $server = common_config('site', 'server');
91         $entry = array();
92
93         # We trim() to avoid extraneous whitespace in the output
94
95         $entry['content'] = common_xml_safe_str(trim($notice->rendered));
96         $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->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'];
101
102         # RSS Item specific
103         $entry['description'] = $entry['content'];
104         $entry['pubDate'] = common_date_rfc2822($notice->created);
105         $entry['guid'] = $entry['link'];
106
107         return $entry;
108     }
109
110     function twitter_rss_dmsg_array($message) {
111
112         $server = common_config('site', 'server');
113         $entry = array();
114
115         $entry['title'] = sprintf('Message from %s to %s',
116             $message->getFrom()->nickname, $message->getTo()->nickname);
117
118         $entry['content'] = common_xml_safe_str(trim($message->content));
119         $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
120         $entry['published'] = common_date_iso8601($message->created);
121         $entry['id'] = "tag:$server,2008:$entry[link]";
122         $entry['updated'] = $entry['published'];
123
124         # RSS Item specific
125         $entry['description'] = $entry['content'];
126         $entry['pubDate'] = common_date_rfc2822($message->created);
127         $entry['guid'] = $entry['link'];
128
129         return $entry;
130     }
131
132     function twitter_dmsg_array($message) {
133
134         $twitter_dm = array();
135
136         $from_profile = $message->getFrom();
137         $to_profile = $message->getTo();
138
139         $twitter_dm['id'] = $message->id;
140         $twitter_dm['sender_id'] = $message->from_profile;
141         $twitter_dm['text'] = trim($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);
148
149         return $twitter_dm;
150     }
151
152     function show_twitter_xml_status($twitter_status) {
153         common_element_start('status');
154         foreach($twitter_status as $element => $value) {
155             switch ($element) {
156             case 'user':
157                 $this->show_twitter_xml_user($twitter_status['user']);
158                 break;
159             case 'text':
160                 common_element($element, null, common_xml_safe_str($value));
161                 break;
162             default:
163                 common_element($element, null, $value);
164             }
165         }
166         common_element_end('status');
167     }
168
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']);
174             } else {
175                 common_element($element, null, $value);
176             }
177         }
178         common_element_end($role);
179     }
180
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');
189     }
190
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['content']);
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');
200     }
201
202     function show_json_objects($objects) {
203         print(json_encode($objects));
204     }
205
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');
211     }
212
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');
218     }
219
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');
225     }
226
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');
232     }
233
234     function show_twitter_xml_dmsg($twitter_dm) {
235         common_element_start('direct_message');
236         foreach($twitter_dm as $element => $value) {
237             switch ($element) {
238             case 'sender':
239             case 'recipient':
240                 $this->show_twitter_xml_user($value, $element);
241                 break;
242             case 'text':
243                 common_element($element, null, common_xml_safe_str($value));
244                 break;
245             default:
246                 common_element($element, null, $value);
247             }
248         }
249         common_element_end('direct_message');
250     }
251
252     function show_xml_timeline($notice) {
253
254         $this->init_document('xml');
255         common_element_start('statuses', array('type' => 'array'));
256
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);
261             }
262         } else {
263             while ($notice->fetch()) {
264                 $twitter_status = $this->twitter_status_array($notice);
265                 $this->show_twitter_xml_status($twitter_status);
266             }
267         }
268
269         common_element_end('statuses');
270         $this->end_document('xml');
271     }
272
273     function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=null) {
274
275         $this->init_document('rss');
276
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',
284                                          'href' => $suplink,
285                                          'type' => 'application/json'));
286         }
287         common_element('description', null, $subtitle);
288         common_element('language', null, 'en-us');
289         common_element('ttl', null, '40');
290
291         if (is_array($notice)) {
292             foreach ($notice as $n) {
293                 $entry = $this->twitter_rss_entry_array($n);
294                 $this->show_twitter_rss_item($entry);
295             }
296         } else {
297             while ($notice->fetch()) {
298                 $entry = $this->twitter_rss_entry_array($notice);
299                 $this->show_twitter_rss_item($entry);
300             }
301         }
302
303         common_element_end('channel');
304         $this->end_twitter_rss();
305     }
306
307     function show_atom_timeline($notice, $title, $id, $link, $subtitle=null, $suplink=null) {
308
309         $this->init_document('atom');
310
311         common_element('title', null, $title);
312         common_element('id', null, $id);
313         common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
314         if (!is_null($suplink)) {
315             # For FriendFeed's SUP protocol
316             common_element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
317                                          'href' => $suplink,
318                                          'type' => 'application/json'));
319         }
320         common_element('subtitle', null, $subtitle);
321
322         if (is_array($notice)) {
323             foreach ($notice as $n) {
324                 $entry = $this->twitter_rss_entry_array($n);
325                 $this->show_twitter_atom_entry($entry);
326             }
327         } else {
328             while ($notice->fetch()) {
329                 $entry = $this->twitter_rss_entry_array($notice);
330                 $this->show_twitter_atom_entry($entry);
331             }
332         }
333
334         $this->end_document('atom');
335
336     }
337
338     function show_json_timeline($notice) {
339
340         $this->init_document('json');
341
342         $statuses = array();
343
344         if (is_array($notice)) {
345             foreach ($notice as $n) {
346                 $twitter_status = $this->twitter_status_array($n);
347                 array_push($statuses, $twitter_status);
348             }
349         } else {
350             while ($notice->fetch()) {
351                 $twitter_status = $this->twitter_status_array($notice);
352                 array_push($statuses, $twitter_status);
353             }
354         }
355
356         $this->show_json_objects($statuses);
357
358         $this->end_document('json');
359     }
360
361     // Anyone know what date format this is?
362     // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
363     function date_twitter($dt) {
364         $t = strtotime($dt);
365         return date("D M d G:i:s O Y", $t);
366     }
367
368     function replier_by_reply($reply_id) {
369         $notice = Notice::staticGet($reply_id);
370         if ($notice) {
371             $profile = $notice->getProfile();
372             if ($profile) {
373                 return intval($profile->id);
374             } else {
375                 common_debug('Can\'t find a profile for notice: ' . $notice->id, __FILE__);
376             }
377         } else {
378             common_debug("Can't get notice: $reply_id", __FILE__);
379         }
380         return null;
381     }
382
383     // XXX: Candidate for a general utility method somewhere?
384     function count_subscriptions($profile) {
385
386         $count = 0;
387         $sub = new Subscription();
388         $sub->subscribed = $profile->id;
389
390         $count = $sub->find();
391
392         if ($count > 0) {
393             return $count - 1;
394         } else {
395             return 0;
396         }
397     }
398
399     function init_document($type='xml') {
400         switch ($type) {
401          case 'xml':
402             header('Content-Type: application/xml; charset=utf-8');
403             common_start_xml();
404             break;
405          case 'json':
406             header('Content-Type: application/json; charset=utf-8');
407
408             // Check for JSONP callback
409             $callback = $this->arg('callback');
410             if ($callback) {
411                 print $callback . '(';
412             }
413             break;
414          case 'rss':
415             header("Content-Type: application/rss+xml; charset=utf-8");
416             $this->init_twitter_rss();
417             break;
418          case 'atom':
419             header('Content-Type: application/atom+xml; charset=utf-8');
420             $this->init_twitter_atom();
421             break;
422          default:
423             $this->client_error(_('Not a supported data format.'));
424             break;
425         }
426
427         return;
428     }
429
430     function end_document($type='xml') {
431         switch ($type) {
432          case 'xml':
433             common_end_xml();
434             break;
435          case 'json':
436
437             // Check for JSONP callback
438             $callback = $this->arg('callback');
439             if ($callback) {
440                 print ')';
441             }
442             break;
443          case 'rss':
444             $this->end_twitter_rss();
445             break;
446          case 'atom':
447             $this->end_twitter_rss();
448             break;
449          default:
450             $this->client_error(_('Not a supported data format.'));
451             break;
452         }
453         return;
454     }
455
456     function client_error($msg, $code = 400, $content_type = 'json') {
457
458         static $status = array(400 => 'Bad Request',
459                                401 => 'Unauthorized',
460                                402 => 'Payment Required',
461                                403 => 'Forbidden',
462                                404 => 'Not Found',
463                                405 => 'Method Not Allowed',
464                                406 => 'Not Acceptable',
465                                407 => 'Proxy Authentication Required',
466                                408 => 'Request Timeout',
467                                409 => 'Conflict',
468                                410 => 'Gone',
469                                411 => 'Length Required',
470                                412 => 'Precondition Failed',
471                                413 => 'Request Entity Too Large',
472                                414 => 'Request-URI Too Long',
473                                415 => 'Unsupported Media Type',
474                                416 => 'Requested Range Not Satisfiable',
475                                417 => 'Expectation Failed');
476
477         $action = $this->trimmed('action');
478
479         common_debug("User error '$code' on '$action': $msg", __FILE__);
480
481         if (!array_key_exists($code, $status)) {
482             $code = 400;
483         }
484
485         $status_string = $status[$code];
486         header('HTTP/1.1 '.$code.' '.$status_string);
487
488         if ($content_type == 'xml') {
489             $this->init_document('xml');
490             common_element_start('hash');
491             common_element('error', null, $msg);
492             common_element('request', null, $_SERVER['REQUEST_URI']);
493             common_element_end('hash');
494             $this->end_document('xml');
495         } else {
496             $this->init_document('json');
497             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
498             print(json_encode($error_array));
499             $this->end_document('json');
500         }
501
502     }
503
504     function init_twitter_rss() {
505         common_start_xml();
506         common_element_start('rss', array('version' => '2.0'));
507     }
508
509     function end_twitter_rss() {
510         common_element_end('rss');
511         common_end_xml();
512     }
513
514     function init_twitter_atom() {
515         common_start_xml();
516         common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
517     }
518
519     function end_twitter_atom() {
520         common_end_xml();
521         common_element_end('feed');
522     }
523
524     function show_profile($profile, $content_type='xml', $notice=null) {
525         $profile_array = $this->twitter_user_array($profile, true);
526         switch ($content_type) {
527          case 'xml':
528             $this->show_twitter_xml_user($profile_array);
529             break;
530          case 'json':
531             $this->show_json_objects($profile_array);
532             break;
533          default:
534             $this->client_error(_('Not a supported data format.'));
535             return;
536         }
537         return;
538     }
539
540     function get_user($id, $apidata=null) {
541         if (!$id) {
542             return $apidata['user'];
543         } else if (is_numeric($id)) {
544             return User::staticGet($id);
545         } else {
546             $nickname = common_canonical_nickname($id);
547             return User::staticGet('nickname', $nickname);
548         }
549     }
550
551     function get_profile($id) {
552         if (is_numeric($id)) {
553             return Profile::staticGet($id);
554         } else {
555             $user = User::staticGet('nickname', $id);
556             if ($user) {
557                 return $user->getProfile();
558             } else {
559                 return null;
560             }
561         }
562     }
563
564     function source_link($source) {
565         $source_name = _($source);
566         switch ($source) {
567          case 'web':
568          case 'xmpp':
569          case 'mail':
570          case 'omb':
571          case 'api':
572             break;
573          default:
574             $ns = Notice_source::staticGet($source);
575             if ($ns) {
576                 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
577             }
578             break;
579         }
580         return $source_name;
581     }
582
583     function show_extended_profile($user, $apidata) {
584
585         $this->auth_user = $apidata['user'];
586
587         $profile = $user->getProfile();
588
589         if (!$profile) {
590             common_server_error(_('User has no profile.'));
591             return;
592         }
593
594         $twitter_user = $this->twitter_user_array($profile, true);
595
596         // Add in extended user fields offered up by this method
597         $twitter_user['created_at'] = $this->date_twitter($profile->created);
598
599         $subbed = DB_DataObject::factory('subscription');
600         $subbed->subscriber = $profile->id;
601         $subbed_count = (int) $subbed->count() - 1;
602
603         $notices = DB_DataObject::factory('notice');
604         $notices->profile_id = $profile->id;
605         $notice_count = (int) $notices->count();
606
607         $twitter_user['friends_count'] = (is_int($subbed_count)) ? $subbed_count : 0;
608         $twitter_user['statuses_count'] = (is_int($notice_count)) ? $notice_count : 0;
609
610         // Other fields Twitter sends...
611         $twitter_user['profile_background_color'] = '';
612         $twitter_user['profile_text_color'] = '';
613         $twitter_user['profile_link_color'] = '';
614         $twitter_user['profile_sidebar_fill_color'] = '';
615
616         $faves = DB_DataObject::factory('fave');
617         $faves->user_id = $user->id;
618         $faves_count = (int) $faves->count();
619         $twitter_user['favourites_count'] = $faves_count;
620
621         $timezone = 'UTC';
622
623         if ($user->timezone) {
624             $timezone = $user->timezone;
625         }
626
627         $t = new DateTime;
628         $t->setTimezone(new DateTimeZone($timezone));
629         $twitter_user['utc_offset'] = $t->format('Z');
630         $twitter_user['time_zone'] = $timezone;
631
632         $following = 'false';
633
634         if (isset($this->auth_user)) {
635             if ($this->auth_user->isSubscribed($profile)) {
636                 $following = 'true';
637             }
638
639             // Not implemented yet
640             $twitter_user['notifications'] = 'false';
641         }
642
643         $twitter_user['following'] = $following;
644
645         if ($apidata['content-type'] == 'xml') {
646             $this->init_document('xml');
647             $this->show_twitter_xml_user($twitter_user);
648             $this->end_document('xml');
649         } elseif ($apidata['content-type'] == 'json') {
650             $this->init_document('json');
651             $this->show_json_objects($twitter_user);
652             $this->end_document('json');
653         }
654
655     }
656
657 }