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