]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apistatusesupdate.php
Harmonise UI message "No such user."
[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 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 if (!defined('STATUSNET')) {
37     exit(1);
38 }
39
40 require_once INSTALLDIR . '/lib/apiauth.php';
41 require_once INSTALLDIR . '/lib/mediafile.php';
42
43 /**
44  * Updates the authenticating user's status (posts a notice).
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
59 class ApiStatusesUpdateAction extends ApiAuthAction
60 {
61     var $source                = null;
62     var $status                = null;
63     var $in_reply_to_status_id = null;
64     static $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
65
66     /**
67      * Take arguments for running
68      *
69      * @param array $args $_REQUEST args
70      *
71      * @return boolean success flag
72      *
73      */
74
75     function prepare($args)
76     {
77         parent::prepare($args);
78
79         $this->user   = $this->auth_user;
80         $this->status = $this->trimmed('status');
81         $this->source = $this->trimmed('source');
82
83         if (empty($this->source) || in_array($source, self::$reserved_sources)) {
84             $this->source = 'api';
85         }
86
87         $this->in_reply_to_status_id
88             = intval($this->trimmed('in_reply_to_status_id'));
89
90         return true;
91     }
92
93     /**
94      * Handle the request
95      *
96      * Make a new notice for the update, save it, and show it
97      *
98      * @param array $args $_REQUEST data (unused)
99      *
100      * @return void
101      */
102
103     function handle($args)
104     {
105         parent::handle($args);
106
107         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
108             $this->clientError(
109                 _('This method requires a POST.'),
110                 400, $this->format
111             );
112             return;
113         }
114
115         // Workaround for PHP returning empty $_POST and $_FILES when POST
116         // length > post_max_size in php.ini
117
118         if (empty($_FILES)
119             && empty($_POST)
120             && ($_SERVER['CONTENT_LENGTH'] > 0)
121         ) {
122              $msg = _('The server was unable to handle that much POST ' .
123                     'data (%s bytes) due to its current configuration.');
124
125             $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
126             return;
127         }
128
129         if (empty($this->status)) {
130             $this->clientError(
131                 'Client must provide a \'status\' parameter with a value.',
132                 400,
133                 $this->format
134             );
135             return;
136         }
137
138         if (empty($this->user)) {
139             $this->clientError(_('No such user.'), 404, $this->format);
140             return;
141         }
142
143         $status_shortened = common_shorten_links($this->status);
144
145         if (Notice::contentTooLong($status_shortened)) {
146
147             // Note: Twitter truncates anything over 140, flags the status
148             // as "truncated."
149
150             $this->clientError(
151                 sprintf(
152                     _('That\'s too long. Max notice size is %d chars.'),
153                     Notice::maxContent()
154                 ),
155                 406,
156                 $this->format
157             );
158
159             return;
160         }
161
162         // Check for commands
163
164         $inter = new CommandInterpreter();
165         $cmd = $inter->handle_command($this->user, $status_shortened);
166
167         if ($cmd) {
168
169             if ($this->supported($cmd)) {
170                 $cmd->execute(new Channel());
171             }
172
173             // Cmd not supported?  Twitter just returns your latest status.
174             // And, it returns your last status whether the cmd was successful
175             // or not!
176
177             $this->notice = $this->user->getCurrentNotice();
178
179         } else {
180
181             $reply_to = null;
182
183             if (!empty($this->in_reply_to_status_id)) {
184
185                 // Check whether notice actually exists
186
187                 $reply = Notice::staticGet($this->in_reply_to_status_id);
188
189                 if ($reply) {
190                     $reply_to = $this->in_reply_to_status_id;
191                 } else {
192                     $this->clientError(
193                         _('Not found'),
194                         $code = 404,
195                         $this->format
196                     );
197                     return;
198                 }
199             }
200
201             $upload = null;
202
203             try {
204                 $upload = MediaFile::fromUpload('media', $this->user);
205             } catch (ClientException $ce) {
206                 $this->clientError($ce->getMessage());
207                 return;
208             }
209
210             if (isset($upload)) {
211                 $status_shortened .= ' ' . $upload->shortUrl();
212
213                 if (Notice::contentTooLong($status_shortened)) {
214                     $upload->delete();
215                     $msg = _(
216                         'Max notice size is %d chars, ' .
217                         'including attachment URL.'
218                     );
219                     $this->clientError(sprintf($msg, Notice::maxContent()));
220                 }
221             }
222
223             $this->notice = Notice::saveNew(
224                 $this->user->id,
225                 html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8'),
226                 $this->source,
227                 1,
228                 $reply_to
229             );
230
231             if (isset($upload)) {
232                 $upload->attachToNotice($this->notice);
233             }
234
235             common_broadcast_notice($this->notice);
236         }
237
238         $this->showNotice();
239     }
240
241     /**
242      * Show the resulting notice
243      *
244      * @return void
245      */
246
247     function showNotice()
248     {
249         if (!empty($this->notice)) {
250             if ($this->format == 'xml') {
251                 $this->showSingleXmlStatus($this->notice);
252             } elseif ($this->format == 'json') {
253                 $this->show_single_json_status($this->notice);
254             }
255         }
256     }
257
258     /**
259      * Is this command supported when doing an update from the API?
260      *
261      * @param string $cmd the command to check for
262      *
263      * @return boolean true or false
264      */
265
266     function supported($cmd)
267     {
268         static $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand',
269             'FavCommand', 'OnCommand', 'OffCommand');
270
271         if (in_array(get_class($cmd), $cmdlist)) {
272             return true;
273         }
274
275         return false;
276     }
277
278 }