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