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