]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/apilistusers.php
Merge activity plugin into mainline
[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             // TRANS: Client error displayed when referring to a non-existing list.
67             $this->clientError(_('List not found.'), 404, $this->format);
68             return false;
69         }
70
71         if(!$this->create && !$this->delete) {
72             $this->getUsers();
73         }
74         return true;
75     }
76
77     function requiresAuth()
78     {
79         return parent::requiresAuth() ||
80             $this->create || $this->delete;
81     }
82
83     function handle($args)
84     {
85         parent::handle($args);
86
87         if($this->delete) {
88             return $this->handleDelete();
89         }
90
91         if($this->create) {
92             return $this->handlePost();
93         }
94
95         switch($this->format) {
96         case 'xml':
97             $this->initDocument('xml');
98             $this->elementStart('users_list', array('xmlns:statusnet' =>
99                                          'http://status.net/schema/api/1/'));
100             $this->elementStart('users', array('type' => 'array'));
101
102             if (is_array($this->users)) {
103                 foreach ($this->users as $u) {
104                     $twitter_user = $this->twitterUserArray($u, true);
105                     $this->showTwitterXmlUser($twitter_user);
106                 }
107             } else {
108                 while ($this->users->fetch()) {
109                     $twitter_user = $this->twitterUserArray($this->users, true);
110                     $this->showTwitterXmlUser($twitter_user);
111                 }
112             }
113
114             $this->elementEnd('users');
115             $this->element('next_cursor', null, $this->next_cursor);
116             $this->element('previous_cursor', null, $this->prev_cursor);
117             $this->elementEnd('users_list');
118             break;
119         case 'json':
120             $this->initDocument('json');
121
122             $users = array();
123
124             if (is_array($this->users)) {
125                 foreach ($this->users as $u) {
126                     $twitter_user = $this->twitterUserArray($u, true);
127                     array_push($users, $twitter_user);
128                 }
129             } else {
130                 while ($this->users->fetch()) {
131                     $twitter_user = $this->twitterUserArray($this->users, true);
132                     array_push($users, $twitter_user);
133                 }
134             }
135
136             $users_list = array('users' => $users,
137                                 'next_cursor' => $this->next_cursor,
138                                 'next_cursor_str' => strval($this->next_cursor),
139                                 'previous_cursor' => $this->prev_cursor,
140                                 'previous_cursor_str' => strval($this->prev_cursor));
141
142             $this->showJsonObjects($users_list);
143
144             $this->endDocument('json');
145             break;
146         default:
147             $this->clientError(
148                 // TRANS: Client error displayed when coming across a non-supported API method.
149                 _('API method not found.'),
150                 404,
151                 $this->format
152             );
153             break;
154         }
155     }
156
157     function handlePost()
158     {
159     }
160
161     function handleDelete()
162     {
163     }
164
165     function getUsers()
166     {
167     }
168
169     function isReadOnly($args)
170     {
171         return false;
172     }
173
174     function lastModified()
175     {
176         if(!empty($this->list)) {
177             return strtotime($this->list->modified);
178         }
179         return null;
180     }
181
182     /**
183      * An entity tag for this list
184      *
185      * Returns an Etag based on the action name, language, user ID and
186      * timestamps of the first and last list the user has joined
187      *
188      * @return string etag
189      */
190     function etag()
191     {
192         if (!empty($this->list)) {
193
194             return '"' . implode(
195                 ':',
196                 array($this->arg('action'),
197                       common_language(),
198                       $this->list->id,
199                       strtotime($this->list->created),
200                       strtotime($this->list->modified))
201             )
202             . '"';
203         }
204
205         return null;
206     }
207 }