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