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