]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apilist.php
XSS vulnerability when remote-subscribing
[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     protected function prepare(array $args=array())
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);
80         }
81
82         return true;
83     }
84
85     /**
86      * Handle the request
87      *
88      * @return boolean success flag
89      */
90     protected function handle()
91     {
92         parent::handle();
93
94         if($this->delete) {
95             $this->handleDelete();
96             return true;
97         }
98
99         if($this->update) {
100             $this->handlePut();
101             return true;
102         }
103
104         switch($this->format) {
105         case 'xml':
106             $this->showSingleXmlList($this->list);
107             break;
108         case 'json':
109             $this->showSingleJsonList($this->list);
110             break;
111         default:
112             // TRANS: Client error displayed when coming across a non-supported API method.
113             $this->clientError(_('API method not found.'), 404);
114         }
115     }
116
117     /**
118      * require authentication if it is a write action or user is ambiguous
119      *
120      */
121     function requiresAuth()
122     {
123         return parent::requiresAuth() ||
124             $this->create || $this->delete;
125     }
126
127     /**
128      * Update a list
129      *
130      * @return boolean success
131      */
132     function handlePut()
133     {
134         if($this->auth_user->id != $this->list->tagger) {
135             // TRANS: Client error displayed when trying to update another user's list.
136             $this->clientError(_('You cannot update lists that do not belong to you.'), 401);
137         }
138
139         $new_list = clone($this->list);
140         $new_list->tag = common_canonical_tag($this->arg('name'));
141         $new_list->description = common_canonical_tag($this->arg('description'));
142         $new_list->private = ($this->arg('mode') === 'private') ? true : false;
143
144         $result = $new_list->update($this->list);
145
146         if(!$result) {
147             // TRANS: Client error displayed when an unknown error occurs updating a list.
148             $this->clientError(_('An error occured.'), 503);
149         }
150
151         switch($this->format) {
152         case 'xml':
153             $this->showSingleXmlList($new_list);
154             break;
155         case 'json':
156             $this->showSingleJsonList($new_list);
157             break;
158         default:
159             // TRANS: Client error displayed when coming across a non-supported API method.
160             $this->clientError(_('API method not found.'), 404);
161         }
162     }
163
164     /**
165      * Delete a list
166      *
167      * @return boolean success
168      */
169     function handleDelete()
170     {
171         if($this->auth_user->id != $this->list->tagger) {
172             // TRANS: Client error displayed when trying to delete another user's list.
173             $this->clientError(_('You cannot delete lists that do not belong to you.'), 401);
174         }
175
176         $record = clone($this->list);
177         $this->list->delete();
178
179         switch($this->format) {
180         case 'xml':
181             $this->showSingleXmlList($record);
182             break;
183         case 'json':
184             $this->showSingleJsonList($record);
185             break;
186         default:
187             // TRANS: Client error displayed when coming across a non-supported API method.
188             $this->clientError(_('API method not found.'), 404);
189         }
190     }
191
192     /**
193      * Indicate that this resource is not read-only.
194      *
195      * @return boolean is_read-only=false
196      */
197     function isReadOnly($args)
198     {
199         return false;
200     }
201
202     /**
203      * When was the list (people tag) last updated?
204      *
205      * @return String time_last_modified
206      */
207     function lastModified()
208     {
209         if(!empty($this->list)) {
210             return strtotime($this->list->modified);
211         }
212         return null;
213     }
214
215     /**
216      * An entity tag for this list
217      *
218      * Returns an Etag based on the action name, language, user ID and
219      * timestamps of the first and last list the user has joined
220      *
221      * @return string etag
222      */
223     function etag()
224     {
225         if (!empty($this->list)) {
226
227             return '"' . implode(
228                 ':',
229                 array($this->arg('action'),
230                       common_language(),
231                       $this->user->id,
232                       strtotime($this->list->created),
233                       strtotime($this->list->modified))
234             )
235             . '"';
236         }
237
238         return null;
239     }
240 }