]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/apilistusers.php
Merge branch 'people_tags_rebase' into 1.0.x
[quix0rs-gnu-social.git] / lib / apilistusers.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Base class for list members and list subscribers api.
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  API
23  * @package   StatusNet
24  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
25  * @link      http://status.net/
26  */
27
28 if (!defined('STATUSNET')) {
29     exit(1);
30 }
31
32 require_once INSTALLDIR . '/lib/apibareauth.php';
33
34 class ApiListUsersAction extends ApiBareAuthAction
35 {
36     var $list   = null;
37     var $user   = false;
38     var $create = false;
39     var $delete = false;
40     var $cursor = -1;
41     var $next_cursor = 0;
42     var $prev_cursor = 0;
43     var $users = null;
44
45     function prepare($args)
46     {
47         // delete list member if method is DELETE or if method is POST and an argument
48         // _method is set to DELETE
49         $this->delete = ($_SERVER['REQUEST_METHOD'] == 'DELETE' ||
50                             ($this->trimmed('_method') == 'DELETE' &&
51                              $_SERVER['REQUEST_METHOD'] == 'POST'));
52
53         // add member if method is POST
54         $this->create = (!$this->delete &&
55                          $_SERVER['REQUEST_METHOD'] == 'POST');
56
57         if($this->arg('id')) {
58             $this->user = $this->getTargetUser($this->arg('id'));
59         }
60
61         parent::prepare($args);
62
63         $this->list = $this->getTargetList($this->arg('user'), $this->arg('list_id'));
64
65         if (empty($this->list)) {
66             $this->clientError(_('Not found'), 404, $this->format);
67             return false;
68         }
69
70         if(!$this->create && !$this->delete) {
71             $this->getUsers();
72         }
73         return true;
74     }
75
76     function requiresAuth()
77     {
78         return parent::requiresAuth() ||
79             $this->create || $this->delete;
80     }
81
82     function handle($args)
83     {
84         parent::handle($args);
85
86         if($this->delete) {
87             return $this->handleDelete();
88         }
89
90         if($this->create) {
91             return $this->handlePost();
92         }
93
94         switch($this->format) {
95         case 'xml':
96             $this->initDocument('xml');
97             $this->elementStart('users_list', array('xmlns:statusnet' =>
98                                          'http://status.net/schema/api/1/'));
99             $this->elementStart('users', array('type' => 'array'));
100
101             if (is_array($this->users)) {
102                 foreach ($this->users as $u) {
103                     $twitter_user = $this->twitterUserArray($u, true);
104                     $this->showTwitterXmlUser($twitter_user);
105                 }
106             } else {
107                 while ($this->users->fetch()) {
108                     $twitter_user = $this->twitterUserArray($this->users, true);
109                     $this->showTwitterXmlUser($twitter_user);
110                 }
111             }
112
113             $this->elementEnd('users');
114             $this->element('next_cursor', null, $this->next_cursor);
115             $this->element('previous_cursor', null, $this->prev_cursor);
116             $this->elementEnd('users_list');
117             break;
118         case 'json':
119             $this->initDocument('json');
120
121             $users = array();
122
123             if (is_array($this->users)) {
124                 foreach ($this->users as $u) {
125                     $twitter_user = $this->twitterUserArray($u, true);
126                     array_push($users, $twitter_user);
127                 }
128             } else {
129                 while ($this->users->fetch()) {
130                     $twitter_user = $this->twitterUserArray($this->users, true);
131                     array_push($users, $twitter_user);
132                 }
133             }
134
135             $users_list = array('users' => $users,
136                                 'next_cursor' => $this->next_cursor,
137                                 'next_cursor_str' => strval($this->next_cursor),
138                                 'previous_cursor' => $this->prev_cursor,
139                                 'previous_cursor_str' => strval($this->prev_cursor));
140
141             $this->showJsonObjects($users_list);
142
143             $this->endDocument('json');
144             break;
145         default:
146             $this->clientError(
147                 _('API method not found.'),
148                 404,
149                 $this->format
150             );
151             break;
152         }
153     }
154
155     function handlePost()
156     {
157     }
158
159     function handleDelete()
160     {
161     }
162
163     function getUsers()
164     {
165     }
166
167     function isReadOnly($args)
168     {
169         return false;
170     }
171
172     function lastModified()
173     {
174         if(!empty($this->list)) {
175             return strtotime($this->list->modified);
176         }
177         return null;
178     }
179
180     /**
181      * An entity tag for this list
182      *
183      * Returns an Etag based on the action name, language, user ID and
184      * timestamps of the first and last list the user has joined
185      *
186      * @return string etag
187      */
188
189     function etag()
190     {
191         if (!empty($this->list)) {
192
193             return '"' . implode(
194                 ':',
195                 array($this->arg('action'),
196                       common_language(),
197                       $this->list->id,
198                       strtotime($this->list->created),
199                       strtotime($this->list->modified))
200             )
201             . '"';
202         }
203
204         return null;
205     }
206
207 }