]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apistatusesshow.php
e684a07eec90a56033ecf31106e886e57ab1bfd3
[quix0rs-gnu-social.git] / actions / apistatusesshow.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show a notice (as a Twitter-style status)
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    Craig Andrews <candrews@integralblue.com>
25  * @author    Evan Prodromou <evan@status.net>
26  * @author    Jeffery To <jeffery.to@gmail.com>
27  * @author    Tom Blankenship <mac65@mac65.com>
28  * @author    Mike Cochrane <mikec@mikenz.geek.nz>
29  * @author    Robin Millette <robin@millette.info>
30  * @author    Zach Copley <zach@status.net>
31  * @copyright 2009 StatusNet, Inc.
32  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
33  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
34  * @link      http://status.net/
35  */
36
37 if (!defined('STATUSNET')) {
38     exit(1);
39 }
40
41 require_once INSTALLDIR . '/lib/apiprivateauth.php';
42
43 /**
44  * Returns the notice specified by id as a Twitter-style status and inline user
45  *
46  * @category API
47  * @package  StatusNet
48  * @author   Craig Andrews <candrews@integralblue.com>
49  * @author   Evan Prodromou <evan@status.net>
50  * @author   Jeffery To <jeffery.to@gmail.com>
51  * @author   Tom Blankenship <mac65@mac65.com>
52  * @author   Mike Cochrane <mikec@mikenz.geek.nz>
53  * @author   Robin Millette <robin@millette.info>
54  * @author   Zach Copley <zach@status.net>
55  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
56  * @link     http://status.net/
57  */
58 class ApiStatusesShowAction extends ApiPrivateAuthAction
59 {
60     var $notice_id = null;
61     var $notice    = null;
62
63     /**
64      * Take arguments for running
65      *
66      * @param array $args $_REQUEST args
67      *
68      * @return boolean success flag
69      */
70     function prepare($args)
71     {
72         parent::prepare($args);
73
74         // 'id' is an undocumented parameter in Twitter's API. Several
75         // clients make use of it, so we support it too.
76
77         // show.json?id=12345 takes precedence over /show/12345.json
78
79         $this->notice_id = (int)$this->trimmed('id');
80
81         if (empty($notice_id)) {
82             $this->notice_id = (int)$this->arg('id');
83         }
84
85         $this->notice = Notice::staticGet((int)$this->notice_id);
86
87         return true;
88     }
89
90     /**
91      * Handle the request
92      *
93      * Check the format and show the notice
94      *
95      * @param array $args $_REQUEST data (unused)
96      *
97      * @return void
98      */
99     function handle($args)
100     {
101         parent::handle($args);
102
103         if (!in_array($this->format, array('xml', 'json', 'atom'))) {
104             // TRANS: Client error displayed when trying to handle an unknown API method.
105             $this->clientError(_('API method not found.'), 404);
106             return;
107         }
108
109         switch ($_SERVER['REQUEST_METHOD']) {
110         case 'GET':
111             $this->showNotice();
112             break;
113         case 'DELETE':
114             $this->deleteNotice();
115             break;
116         default:
117             $this->clientError(_('HTTP method not supported.'), 405);
118             return;
119         }
120     }
121
122     /**
123      * Show the notice
124      *
125      * @return void
126      */
127     function showNotice()
128     {
129         if (!empty($this->notice)) {
130             switch ($this->format) {
131             case 'xml':
132                 $this->showSingleXmlStatus($this->notice);
133                 break;
134             case 'json':
135                 $this->show_single_json_status($this->notice);
136                 break;
137             case 'atom':
138                 $this->showSingleAtomStatus($this->notice);
139                 break;
140             default:
141                 throw new Exception(sprintf(_("Unsupported format: %s"), $this->format));
142             }
143         } else {
144             // XXX: Twitter just sets a 404 header and doens't bother
145             // to return an err msg
146
147             $deleted = Deleted_notice::staticGet($this->notice_id);
148
149             if (!empty($deleted)) {
150                 $this->clientError(
151                     // TRANS: Client error displayed requesting a deleted status.
152                     _('Status deleted.'),
153                     410,
154                     $this->format
155                 );
156             } else {
157                 $this->clientError(
158                     // TRANS: Client error displayed requesting a status with an invalid ID.
159                     _('No status with that ID found.'),
160                     404,
161                     $this->format
162                 );
163             }
164         }
165     }
166
167     /**
168      * Is this action read only?
169      *
170      * @param array $args other arguments
171      *
172      * @return boolean true
173      */
174     
175     function isReadOnly($args)
176     {
177         if ($_SERVER['REQUEST_METHOD'] == 'GET') {
178             return true;
179         } else {
180             return false;
181         }
182     }
183
184     /**
185      * When was this notice last modified?
186      *
187      * @return string datestamp of the latest notice in the stream
188      */
189     function lastModified()
190     {
191         if (!empty($this->notice)) {
192             return strtotime($this->notice->created);
193         }
194
195         return null;
196     }
197
198     /**
199      * An entity tag for this notice
200      *
201      * Returns an Etag based on the action name, language, and
202      * timestamps of the notice
203      *
204      * @return string etag
205      */
206     function etag()
207     {
208         if (!empty($this->notice)) {
209
210             return '"' . implode(
211                 ':',
212                 array($this->arg('action'),
213                       common_user_cache_hash($this->auth_user),
214                       common_language(),
215                       $this->notice->id,
216                       strtotime($this->notice->created))
217             )
218             . '"';
219         }
220
221         return null;
222     }
223
224     function deleteNotice()
225     {
226         if ($this->format != 'atom') {
227             $this->clientError(_("Can only delete using the Atom format."));
228             return;
229         }
230
231         if (empty($this->auth_user) ||
232             ($this->notice->profile_id != $this->auth_user->id &&
233              !$this->auth_user->hasRight(Right::DELETEOTHERSNOTICE))) {
234             $this->clientError(_('Can\'t delete this notice.'), 403);
235             return;
236         }
237
238         if (Event::handle('StartDeleteOwnNotice', array($this->auth_user, $this->notice))) {
239             $this->notice->delete();
240             Event::handle('EndDeleteOwnNotice', array($this->auth_user, $this->notice));
241         }
242
243         // @fixme is there better output we could do here?
244
245         header('HTTP/1.1 200 OK');
246         header('Content-Type: text/plain');
247         print(sprintf(_('Deleted notice %d'), $this->notice->id));
248         print("\n");
249     }
250 }