]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/apilistusers.php
Protected function prepare with array $args defaulting to array()
[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 class ApiListUsersAction extends ApiBareAuthAction
33 {
34     var $list   = null;
35     var $user   = false;
36     var $create = false;
37     var $delete = false;
38     var $cursor = -1;
39     var $next_cursor = 0;
40     var $prev_cursor = 0;
41     var $users = null;
42
43     protected function prepare(array $args=array())
44     {
45         // delete list member if method is DELETE or if method is POST and an argument
46         // _method is set to DELETE
47         $this->delete = ($_SERVER['REQUEST_METHOD'] == 'DELETE' ||
48                             ($this->trimmed('_method') == 'DELETE' &&
49                              $_SERVER['REQUEST_METHOD'] == 'POST'));
50
51         // add member if method is POST
52         $this->create = (!$this->delete &&
53                          $_SERVER['REQUEST_METHOD'] == 'POST');
54
55         if ($this->arg('id')) {
56             $this->target = $this->getTargetProfile($this->arg('id'));
57         }
58
59         parent::prepare($args);
60
61         $this->list = $this->getTargetList($this->arg('user'), $this->arg('list_id'));
62
63         if (empty($this->list)) {
64             // TRANS: Client error displayed when referring to a non-existing list.
65             $this->clientError(_('List not found.'), 404, $this->format);
66             return false;
67         }
68
69         if(!$this->create && !$this->delete) {
70             $this->getUsers();
71         }
72         return true;
73     }
74
75     function requiresAuth()
76     {
77         return parent::requiresAuth() ||
78             $this->create || $this->delete;
79     }
80
81     protected function handle()
82     {
83         parent::handle();
84
85         if($this->delete) {
86             return $this->handleDelete();
87         }
88
89         if($this->create) {
90             return $this->handlePost();
91         }
92
93         switch($this->format) {
94         case 'xml':
95             $this->initDocument('xml');
96             $this->elementStart('users_list', array('xmlns:statusnet' =>
97                                          'http://status.net/schema/api/1/'));
98             $this->elementStart('users', array('type' => 'array'));
99
100             if (is_array($this->users)) {
101                 foreach ($this->users as $u) {
102                     $twitter_user = $this->twitterUserArray($u, true);
103                     $this->showTwitterXmlUser($twitter_user);
104                 }
105             } else {
106                 while ($this->users->fetch()) {
107                     $twitter_user = $this->twitterUserArray($this->users, true);
108                     $this->showTwitterXmlUser($twitter_user);
109                 }
110             }
111
112             $this->elementEnd('users');
113             $this->element('next_cursor', null, $this->next_cursor);
114             $this->element('previous_cursor', null, $this->prev_cursor);
115             $this->elementEnd('users_list');
116             break;
117         case 'json':
118             $this->initDocument('json');
119
120             $users = array();
121
122             if (is_array($this->users)) {
123                 foreach ($this->users as $u) {
124                     $twitter_user = $this->twitterUserArray($u, true);
125                     array_push($users, $twitter_user);
126                 }
127             } else {
128                 while ($this->users->fetch()) {
129                     $twitter_user = $this->twitterUserArray($this->users, true);
130                     array_push($users, $twitter_user);
131                 }
132             }
133
134             $users_list = array('users' => $users,
135                                 'next_cursor' => $this->next_cursor,
136                                 'next_cursor_str' => strval($this->next_cursor),
137                                 'previous_cursor' => $this->prev_cursor,
138                                 'previous_cursor_str' => strval($this->prev_cursor));
139
140             $this->showJsonObjects($users_list);
141
142             $this->endDocument('json');
143             break;
144         default:
145             $this->clientError(
146                 // TRANS: Client error displayed when coming across a non-supported API method.
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     function etag()
189     {
190         if (!empty($this->list)) {
191
192             return '"' . implode(
193                 ':',
194                 array($this->arg('action'),
195                       common_language(),
196                       $this->list->id,
197                       strtotime($this->list->created),
198                       strtotime($this->list->modified))
199             )
200             . '"';
201         }
202
203         return null;
204     }
205 }