3 * StatusNet, the distributed open-source microblogging tool
5 * Post a notice (update your status) through the API
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.
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.
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/>.
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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
33 * @link http://status.net/
36 if (!defined('STATUSNET')) {
40 require_once INSTALLDIR . '/lib/apiauth.php';
41 require_once INSTALLDIR . '/lib/mediafile.php';
44 * Updates the authenticating user's status (posts a notice).
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/
59 class ApiStatusesUpdateAction extends ApiAuthAction
63 var $in_reply_to_status_id = null;
64 static $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
67 * Take arguments for running
69 * @param array $args $_REQUEST args
71 * @return boolean success flag
75 function prepare($args)
77 parent::prepare($args);
79 $this->user = $this->auth_user;
80 $this->status = $this->trimmed('status');
81 $this->source = $this->trimmed('source');
83 if (empty($this->source) || in_array($source, $this->reserved_sources)) {
84 $this->source = 'api';
87 $this->in_reply_to_status_id
88 = intval($this->trimmed('in_reply_to_status_id'));
96 * Make a new notice for the update, save it, and show it
98 * @param array $args $_REQUEST data (unused)
103 function handle($args)
105 parent::handle($args);
107 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
109 _('This method requires a POST.'),
115 if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
116 $this->clientError(sprintf(_('The server was unable to handle ' .
117 'that much POST data (%s bytes) due to its current configuration.'),
118 $_SERVER['CONTENT_LENGTH']));
122 if (empty($this->user)) {
123 $this->clientError(_('No such user!'), 404, $this->format);
127 if (empty($this->status)) {
129 'Client must provide a \'status\' parameter with a value.',
136 $status_shortened = common_shorten_links($this->status);
138 if (Notice::contentTooLong($status_shortened)) {
140 // Note: Twitter truncates anything over 140, flags the status
145 _('That\'s too long. Max notice size is %d chars.'),
155 // Check for commands
157 $inter = new CommandInterpreter();
158 $cmd = $inter->handle_command($this->user, $status_shortened);
162 if ($this->supported($cmd)) {
163 $cmd->execute(new Channel());
166 // Cmd not supported? Twitter just returns your latest status.
167 // And, it returns your last status whether the cmd was successful
170 $this->notice = $this->user->getCurrentNotice();
176 if (!empty($this->in_reply_to_status_id)) {
178 // Check whether notice actually exists
180 $reply = Notice::staticGet($this->in_reply_to_status_id);
183 $reply_to = $this->in_reply_to_status_id;
196 common_debug('looking for attachment');
198 $upload = MediaFile::fromUpload('media', $this->user);
200 common_debug("uploaded file = " . var_export($upload, true));
202 if (isset($upload)) {
203 common_debug('newNotice: found an upload');
205 $status_shortened .= ' ' . $upload->shortUrl();
207 common_debug('content w/upload = ' . $status_shortened);
209 if (Notice::contentTooLong($status_shortened)) {
211 $this->clientError(sprintf(_('Max notice size is %d chars, including attachment URL.'),
212 Notice::maxContent()));
214 common_debug('content not too long');
218 $this->notice = Notice::saveNew(
220 html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8'),
226 if (isset($upload)) {
227 $upload->attachToNotice($this->notice);
230 common_broadcast_notice($this->notice);
237 * Show the resulting notice
242 function showNotice()
244 if (!empty($this->notice)) {
245 if ($this->format == 'xml') {
246 $this->showSingleXmlStatus($this->notice);
247 } elseif ($this->format == 'json') {
248 $this->show_single_json_status($this->notice);
254 * Is this command supported when doing an update from the API?
256 * @param string $cmd the command to check for
258 * @return boolean true or false
261 function supported($cmd)
263 static $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand',
264 'FavCommand', 'OnCommand', 'OffCommand');
266 if (in_array(get_class($cmd), $cmdlist)) {