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