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