]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apilist.php
Removing unnecessary require_once lines (autoload!)
[quix0rs-gnu-social.git] / actions / apilist.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show, update or delete a list.
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  * @author    Shashi Gowda <connect2shashi@gmail.com>
25  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26  * @link      http://status.net/
27  */
28
29 if (!defined('STATUSNET')) {
30     exit(1);
31 }
32
33 class ApiListAction extends ApiBareAuthAction
34 {
35     /**
36      * The list in question in the current request
37      */
38     var $list   = null;
39
40     /**
41      * Is this an update request?
42      */
43     var $update = false;
44
45     /**
46      * Is this a delete request?
47      */
48     var $delete = false;
49
50     /**
51      * Set the flags for handling the request. Show list if this is a GET
52      * request, update it if it is POST, delete list if method is DELETE
53      * or if method is POST and an argument _method is set to DELETE. Act
54      * like we don't know if the current user has no access to the list.
55      *
56      * Takes parameters:
57      *     - user: the user id or nickname
58      *     - id:   the id of the tag or the tag itself
59      *
60      * @return boolean success flag
61      */
62     function prepare($args)
63     {
64         parent::prepare($args);
65
66         $this->delete = ($_SERVER['REQUEST_METHOD'] == 'DELETE' ||
67                             ($this->trimmed('_method') == 'DELETE' &&
68                              $_SERVER['REQUEST_METHOD'] == 'POST'));
69
70         // update list if method is POST or PUT and $this->delete is not true
71         $this->update = (!$this->delete &&
72                          in_array($_SERVER['REQUEST_METHOD'], array('POST', 'PUT')));
73
74         $this->user = $this->getTargetUser($this->arg('user'));
75         $this->list = $this->getTargetList($this->arg('user'), $this->arg('id'));
76
77         if (empty($this->list)) {
78             // TRANS: Client error displayed when referring to a non-existing list.
79             $this->clientError(_('List not found.'), 404, $this->format);
80             return false;
81         }
82
83         return true;
84     }
85
86     /**
87      * Handle the request
88      *
89      * @return boolean success flag
90      */
91     function handle($args)
92     {
93         parent::handle($args);
94
95         if($this->delete) {
96             $this->handleDelete();
97             return true;
98         }
99
100         if($this->update) {
101             $this->handlePut();
102             return true;
103         }
104
105         switch($this->format) {
106         case 'xml':
107             $this->showSingleXmlList($this->list);
108             break;
109         case 'json':
110             $this->showSingleJsonList($this->list);
111             break;
112         default:
113             $this->clientError(
114                 // TRANS: Client error displayed when coming across a non-supported API method.
115                 _('API method not found.'),
116                 404,
117                 $this->format
118             );
119             break;
120         }
121     }
122
123     /**
124      * require authentication if it is a write action or user is ambiguous
125      *
126      */
127     function requiresAuth()
128     {
129         return parent::requiresAuth() ||
130             $this->create || $this->delete;
131     }
132
133     /**
134      * Update a list
135      *
136      * @return boolean success
137      */
138     function handlePut()
139     {
140         if($this->auth_user->id != $this->list->tagger) {
141             $this->clientError(
142                 // TRANS: Client error displayed when trying to update another user's list.
143                 _('You cannot update lists that do not belong to you.'),
144                 401,
145                 $this->format
146             );
147         }
148
149         $new_list = clone($this->list);
150         $new_list->tag = common_canonical_tag($this->arg('name'));
151         $new_list->description = common_canonical_tag($this->arg('description'));
152         $new_list->private = ($this->arg('mode') === 'private') ? true : false;
153
154         $result = $new_list->update($this->list);
155
156         if(!$result) {
157             $this->clientError(
158                 // TRANS: Client error displayed when an unknown error occurs updating a list.
159                 _('An error occured.'),
160                 503,
161                 $this->format
162             );
163         }
164
165         switch($this->format) {
166         case 'xml':
167             $this->showSingleXmlList($new_list);
168             break;
169         case 'json':
170             $this->showSingleJsonList($new_list);
171             break;
172         default:
173             $this->clientError(
174                 // TRANS: Client error displayed when coming across a non-supported API method.
175                 _('API method not found.'),
176                 404,
177                 $this->format
178             );
179             break;
180         }
181     }
182
183     /**
184      * Delete a list
185      *
186      * @return boolean success
187      */
188     function handleDelete()
189     {
190         if($this->auth_user->id != $this->list->tagger) {
191             $this->clientError(
192                 // TRANS: Client error displayed when trying to delete another user's list.
193                 _('You cannot delete lists that do not belong to you.'),
194                 401,
195                 $this->format
196             );
197         }
198
199         $record = clone($this->list);
200         $this->list->delete();
201
202         switch($this->format) {
203         case 'xml':
204             $this->showSingleXmlList($record);
205             break;
206         case 'json':
207             $this->showSingleJsonList($record);
208             break;
209         default:
210             $this->clientError(
211                 // TRANS: Client error displayed when coming across a non-supported API method.
212                 _('API method not found.'),
213                 404,
214                 $this->format
215             );
216             break;
217         }
218     }
219
220     /**
221      * Indicate that this resource is not read-only.
222      *
223      * @return boolean is_read-only=false
224      */
225     function isReadOnly($args)
226     {
227         return false;
228     }
229
230     /**
231      * When was the list (people tag) last updated?
232      *
233      * @return String time_last_modified
234      */
235     function lastModified()
236     {
237         if(!empty($this->list)) {
238             return strtotime($this->list->modified);
239         }
240         return null;
241     }
242
243     /**
244      * An entity tag for this list
245      *
246      * Returns an Etag based on the action name, language, user ID and
247      * timestamps of the first and last list the user has joined
248      *
249      * @return string etag
250      */
251     function etag()
252     {
253         if (!empty($this->list)) {
254
255             return '"' . implode(
256                 ':',
257                 array($this->arg('action'),
258                       common_language(),
259                       $this->user->id,
260                       strtotime($this->list->created),
261                       strtotime($this->list->modified))
262             )
263             . '"';
264         }
265
266         return null;
267     }
268 }