]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapiusers.php
d66dc8863280ddbc296ab3fbaaebbcd3910eafb3
[quix0rs-gnu-social.git] / actions / twitapiusers.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 require_once(INSTALLDIR.'/lib/twitterapi.php');
23
24 class TwitapiusersAction extends TwitterapiAction {
25
26         function is_readonly() {                        
27                 return true;
28         }
29
30 /*      
31         Returns extended information of a given user, specified by ID or
32         screen name as per the required id parameter below.      This information
33         includes design settings, so third party developers can theme their
34         widgets according to a given user's preferences. You must be properly
35         authenticated to request the page of a protected user.
36
37         URL: http://twitter.com/users/show/id.format
38
39         Formats: xml, json
40
41         Parameters:
42
43         * id.  Required.  The ID or screen name of a user.
44         Ex: http://twitter.com/users/show/12345.json or
45         http://twitter.com/users/show/bob.xml
46
47         * email. Optional.      The email address of a user.  Ex:
48         http://twitter.com/users/show.xml?email=test@example.com
49
50 */      
51         function show($args, $apidata) {
52                 parent::handle($args);
53                 
54                 $user = null;
55                 $email = $this->arg('email');
56                 
57                 if (isset($apidata['api_arg'])) {
58                         if (is_numeric($apidata['api_arg'])) {
59                                 // by user id
60                                 $user = User::staticGet($apidata['api_arg']);                   
61                         } else {
62                                 // by nickname
63                                 $nickname = common_canonical_nickname($apidata['api_arg']);
64                                 $user = User::staticGet('nickname', $nickname);
65                         } 
66                 } elseif ($email) {
67                         // or, find user by email address
68                         // XXX: The Twitter API spec say an id is *required*, but you can actually
69                         // pull up a user with just an email address. -- Zach
70                         $user = User::staticGet('email', $email);                       
71                 } 
72
73                 if (!$user) {
74                         // XXX: Twitter returns a random(?) user instead of throwing and err! -- Zach
75                         $this->client_error("User not found.", 404, $apidata['content-type']);
76                         exit();
77                 }
78                 
79                 $profile = $user->getProfile();
80
81                 if (!$profile) {
82                         common_server_error(_('User has no profile.'));
83                         exit();
84                 }
85
86                 $twitter_user = $this->twitter_user_array($profile, true);
87
88                 // Add in extended user fields offered up by this method
89                 $twitter_user['created_at'] = $this->date_twitter($profile->created);
90
91                 $subbed = DB_DataObject::factory('subscription');
92                 $subbed->subscribed = $profile->id;
93                 $subbed_count = (int) $subbed->count() - 1;
94
95                 $notices = DB_DataObject::factory('notice');
96                 $notices->profile_id = $profile->id;
97                 $notice_count = (int) $notices->count();
98
99                 $twitter_user['friends_count'] = (is_int($subbed_count)) ? $subbed_count : 0;
100                 $twitter_user['statuses_count'] = (is_int($notice_count)) ? $notice_count : 0;
101
102                 // Other fields Twitter sends...
103                 $twitter_user['profile_background_color'] = '';
104                 $twitter_user['profile_text_color'] = '';
105                 $twitter_user['profile_link_color'] = '';
106                 $twitter_user['profile_sidebar_fill_color'] = '';
107                 $twitter_user['favourites_count'] = 0;
108                 $twitter_user['utc_offset'] = '';
109                 $twitter_user['time_zone'] = '';
110                 $twitter_user['following'] = '';
111                 $twitter_user['notifications'] = '';
112
113                 if ($apidata['content-type'] == 'xml') { 
114                         $this->init_document('xml');
115                         $this->show_twitter_xml_user($twitter_user);
116                         $this->end_document('xml');
117                 } elseif ($apidata['content-type'] == 'json') {
118                         $this->init_document('json');
119                         $this->show_twitter_json_users($twitter_user);
120                         $this->end_document('json');
121                 } else {
122                         common_user_error("API method not found!", $code = 404);
123                 }
124                         
125                 exit();
126         }
127 }