]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apistatusesupdate.php
Merge branch '0.9.x' of git://gitorious.org/statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / actions / apistatusesupdate.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Post a notice (update your status) through the API
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-2010 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 /* External API usage documentation. Please update when you change how this method works. */
38
39 /*! @page statusesupdate statuses/update
40
41     @section Description
42     Updates the authenticating user's status. Requires the status parameter specified below.
43     Request must be a POST.
44
45     @par URL pattern
46     /api/statuses/update.:format
47
48     @par Formats (:format)
49     xml, json
50
51     @par HTTP Method(s)
52     POST
53
54     @par Requires Authentication
55     Yes
56
57     @param status (Required) The URL-encoded text of the status update.
58     @param source (Optional) The source of the status.
59     @param in_reply_to_status_id (Optional) The ID of an existing status that the update is in reply to.
60     @param lat (Optional) The latitude the status refers to.
61     @param long (Optional) The longitude the status refers to.
62     @param media (Optional) a media upload, such as an image or movie file.
63
64     @sa @ref authentication
65     @sa @ref apiroot
66
67     @subsection usagenotes Usage notes
68
69     @li The URL pattern is relative to the @ref apiroot.
70     @li If the @e source parameter is not supplied the source of the status will default to 'api'.
71     @li The XML response uses <a href="http://georss.org/Main_Page">GeoRSS</a>
72     to encode the latitude and longitude (see example response below <georss:point>).
73     @li Data uploaded via the @e media parameter should be multipart/form-data encoded.
74
75     @subsection exampleusage Example usage
76
77     @verbatim
78     curl -u username:password http://example.com/api/statuses/update.xml -d status='Howdy!' -d lat='30.468' -d long='-94.743'
79     @endverbatim
80
81     @subsection exampleresponse Example response
82
83     @verbatim
84     <?xml version="1.0" encoding="UTF-8"?>
85     <status>
86       <text>Howdy!</text>
87       <truncated>false</truncated>
88       <created_at>Tue Mar 30 23:28:05 +0000 2010</created_at>
89       <in_reply_to_status_id/>
90       <source>api</source>
91       <id>26668724</id>
92       <in_reply_to_user_id/>
93       <in_reply_to_screen_name/>
94       <geo xmlns:georss="http://www.georss.org/georss">
95         <georss:point>30.468 -94.743</georss:point>
96       </geo>
97       <favorited>false</favorited>
98       <user>
99         <id>25803</id>
100         <name>Jed Sanders</name>
101         <screen_name>jedsanders</screen_name>
102         <location>Hoop and Holler, Texas</location>
103         <description>I like to think of myself as America's Favorite.</description>
104         <profile_image_url>http://avatar.example.com/25803-48-20080924200604.png</profile_image_url>
105         <url>http://jedsanders.net</url>
106         <protected>false</protected>
107         <followers_count>5</followers_count>
108         <profile_background_color/>
109         <profile_text_color/>
110         <profile_link_color/>
111         <profile_sidebar_fill_color/>
112         <profile_sidebar_border_color/>
113         <friends_count>2</friends_count>
114         <created_at>Wed Sep 24 20:04:00 +0000 2008</created_at>
115         <favourites_count>0</favourites_count>
116         <utc_offset>0</utc_offset>
117         <time_zone>UTC</time_zone>
118         <profile_background_image_url/>
119         <profile_background_tile>false</profile_background_tile>
120         <statuses_count>70</statuses_count>
121         <following>true</following>
122         <notifications>true</notifications>
123       </user>
124     </status>
125     @endverbatim
126 */
127
128 if (!defined('STATUSNET')) {
129     exit(1);
130 }
131
132 require_once INSTALLDIR . '/lib/apiauth.php';
133 require_once INSTALLDIR . '/lib/mediafile.php';
134
135 /**
136  * Updates the authenticating user's status (posts a notice).
137  *
138  * @category API
139  * @package  StatusNet
140  * @author   Craig Andrews <candrews@integralblue.com>
141  * @author   Evan Prodromou <evan@status.net>
142  * @author   Jeffery To <jeffery.to@gmail.com>
143  * @author   Tom Blankenship <mac65@mac65.com>
144  * @author   Mike Cochrane <mikec@mikenz.geek.nz>
145  * @author   Robin Millette <robin@millette.info>
146  * @author   Zach Copley <zach@status.net>
147  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
148  * @link     http://status.net/
149  */
150
151 class ApiStatusesUpdateAction extends ApiAuthAction
152 {
153     var $status                = null;
154     var $in_reply_to_status_id = null;
155     var $lat                   = null;
156     var $lon                   = null;
157
158     /**
159      * Take arguments for running
160      *
161      * @param array $args $_REQUEST args
162      *
163      * @return boolean success flag
164      *
165      */
166
167     function prepare($args)
168     {
169         parent::prepare($args);
170
171         $this->status = $this->trimmed('status');
172         $this->lat    = $this->trimmed('lat');
173         $this->lon    = $this->trimmed('long');
174
175         $this->in_reply_to_status_id
176             = intval($this->trimmed('in_reply_to_status_id'));
177
178         return true;
179     }
180
181     /**
182      * Handle the request
183      *
184      * Make a new notice for the update, save it, and show it
185      *
186      * @param array $args $_REQUEST data (unused)
187      *
188      * @return void
189      */
190
191     function handle($args)
192     {
193         parent::handle($args);
194
195         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
196             $this->clientError(
197                 // TRANS: Client error. POST is a HTTP command. It should not be translated.
198                 _('This method requires a POST.'),
199                 400,
200                 $this->format
201             );
202             return;
203         }
204
205         // Workaround for PHP returning empty $_POST and $_FILES when POST
206         // length > post_max_size in php.ini
207
208         if (empty($_FILES)
209             && empty($_POST)
210             && ($_SERVER['CONTENT_LENGTH'] > 0)
211         ) {
212              $msg = _('The server was unable to handle that much POST ' .
213                     'data (%s bytes) due to its current configuration.');
214
215             $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
216             return;
217         }
218
219         if (empty($this->status)) {
220             $this->clientError(
221                 _('Client must provide a \'status\' parameter with a value.'),
222                 400,
223                 $this->format
224             );
225             return;
226         }
227
228         if (empty($this->auth_user)) {
229             $this->clientError(_('No such user.'), 404, $this->format);
230             return;
231         }
232
233         $status_shortened = common_shorten_links($this->status);
234
235         if (Notice::contentTooLong($status_shortened)) {
236
237             // Note: Twitter truncates anything over 140, flags the status
238             // as "truncated."
239
240             $this->clientError(
241                 sprintf(
242                     _('That\'s too long. Max notice size is %d chars.'),
243                     Notice::maxContent()
244                 ),
245                 406,
246                 $this->format
247             );
248
249             return;
250         }
251
252         // Check for commands
253
254         $inter = new CommandInterpreter();
255         $cmd = $inter->handle_command($this->auth_user, $status_shortened);
256
257         if ($cmd) {
258
259             if ($this->supported($cmd)) {
260                 $cmd->execute(new Channel());
261             }
262
263             // Cmd not supported?  Twitter just returns your latest status.
264             // And, it returns your last status whether the cmd was successful
265             // or not!
266
267             $this->notice = $this->auth_user->getCurrentNotice();
268
269         } else {
270
271             $reply_to = null;
272
273             if (!empty($this->in_reply_to_status_id)) {
274
275                 // Check whether notice actually exists
276
277                 $reply = Notice::staticGet($this->in_reply_to_status_id);
278
279                 if ($reply) {
280                     $reply_to = $this->in_reply_to_status_id;
281                 } else {
282                     $this->clientError(
283                         _('Not found.'),
284                         $code = 404,
285                         $this->format
286                     );
287                     return;
288                 }
289             }
290
291             $upload = null;
292
293             try {
294                 $upload = MediaFile::fromUpload('media', $this->auth_user);
295             } catch (Exception $e) {
296                 $this->clientError($e->getMessage(), $e->getCode(), $this->format);
297                 return;
298             }
299
300             if (isset($upload)) {
301                 $status_shortened .= ' ' . $upload->shortUrl();
302
303                 if (Notice::contentTooLong($status_shortened)) {
304                     $upload->delete();
305                     $msg = _(
306                         'Max notice size is %d chars, ' .
307                         'including attachment URL.'
308                     );
309                     $this->clientError(
310                         sprintf($msg, Notice::maxContent()),
311                         400,
312                         $this->format
313                     );
314                 }
315             }
316
317             $content = html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8');
318
319             $options = array('reply_to' => $reply_to);
320
321             if ($this->auth_user->shareLocation()) {
322
323                 $locOptions = Notice::locationOptions($this->lat,
324                                                       $this->lon,
325                                                       null,
326                                                       null,
327                                                       $this->auth_user->getProfile());
328
329                 $options = array_merge($options, $locOptions);
330             }
331
332             try {
333                 $this->notice = Notice::saveNew(
334                     $this->auth_user->id,
335                     $content,
336                     $this->source,
337                     $options
338                 );
339             } catch (Exception $e) {
340                 $this->clientError($e->getMessage(), $e->getCode(), $this->format);
341                 return;
342             }
343
344             if (isset($upload)) {
345                 $upload->attachToNotice($this->notice);
346             }
347
348         }
349
350         $this->showNotice();
351     }
352
353     /**
354      * Show the resulting notice
355      *
356      * @return void
357      */
358
359     function showNotice()
360     {
361         if (!empty($this->notice)) {
362             if ($this->format == 'xml') {
363                 $this->showSingleXmlStatus($this->notice);
364             } elseif ($this->format == 'json') {
365                 $this->show_single_json_status($this->notice);
366             }
367         }
368     }
369
370     /**
371      * Is this command supported when doing an update from the API?
372      *
373      * @param string $cmd the command to check for
374      *
375      * @return boolean true or false
376      */
377
378     function supported($cmd)
379     {
380         static $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand',
381             'FavCommand', 'OnCommand', 'OffCommand');
382
383         if (in_array(get_class($cmd), $cmdlist)) {
384             return true;
385         }
386
387         return false;
388     }
389
390 }