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