]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apistatusesupdate.php
82fe5a537e534cf69478c85a707de6d7028ae9d2
[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         if (empty($this->status)) {
116             $this->clientError(
117                 'Client must provide a \'status\' parameter with a value.',
118                 400,
119                 $this->format
120             );
121             return;
122         }
123
124         if (empty($this->user)) {
125             $this->clientError(_('No such user!'), 404, $this->format);
126             return;
127         }
128
129         // Workaround for PHP returning empty $_FILES when POST length > PHP settings
130
131         if (empty($_FILES) && ($_SERVER['CONTENT_LENGTH'] > 0)) {
132             $this->clientError(_('Unable to handle that much POST data!'));
133             return;
134         }
135
136         $status_shortened = common_shorten_links($this->status);
137
138         if (Notice::contentTooLong($status_shortened)) {
139
140             // Note: Twitter truncates anything over 140, flags the status
141             // as "truncated."
142
143             $this->clientError(
144                 sprintf(
145                     _('That\'s too long. Max notice size is %d chars.'),
146                     Notice::maxContent()
147                 ),
148                 406,
149                 $this->format
150             );
151
152             return;
153         }
154
155         // Check for commands
156
157         $inter = new CommandInterpreter();
158         $cmd = $inter->handle_command($this->user, $status_shortened);
159
160         if ($cmd) {
161
162             if ($this->supported($cmd)) {
163                 $cmd->execute(new Channel());
164             }
165
166             // Cmd not supported?  Twitter just returns your latest status.
167             // And, it returns your last status whether the cmd was successful
168             // or not!
169
170             $this->notice = $this->user->getCurrentNotice();
171
172         } else {
173
174             $reply_to = null;
175
176             if (!empty($this->in_reply_to_status_id)) {
177
178                 // Check whether notice actually exists
179
180                 $reply = Notice::staticGet($this->in_reply_to_status_id);
181
182                 if ($reply) {
183                     $reply_to = $this->in_reply_to_status_id;
184                 } else {
185                     $this->clientError(
186                         _('Not found'),
187                         $code = 404,
188                         $this->format
189                     );
190                     return;
191                 }
192             }
193
194             $upload = null;
195
196             try {
197                 $upload = MediaFile::fromUpload('media', $this->user);
198             } catch (ClientException $ce) {
199                 $this->clientError($ce->getMessage());
200                 return;
201             }
202
203             if (isset($upload)) {
204                 $status_shortened .= ' ' . $upload->shortUrl();
205
206                 if (Notice::contentTooLong($status_shortened)) {
207                     $upload->delete();
208                     $msg = _(
209                         'Max notice size is %d chars, ' .
210                         'including attachment URL.'
211                     );
212                     $this->clientError(sprintf($msg, Notice::maxContent()));
213                 }
214             }
215
216             $this->notice = Notice::saveNew(
217                 $this->user->id,
218                 html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8'),
219                 $this->source,
220                 1,
221                 $reply_to
222             );
223
224             if (isset($upload)) {
225                 $upload->attachToNotice($this->notice);
226             }
227
228             common_broadcast_notice($this->notice);
229         }
230
231         $this->showNotice();
232     }
233
234     /**
235      * Show the resulting notice
236      *
237      * @return void
238      */
239
240     function showNotice()
241     {
242         if (!empty($this->notice)) {
243             if ($this->format == 'xml') {
244                 $this->showSingleXmlStatus($this->notice);
245             } elseif ($this->format == 'json') {
246                 $this->show_single_json_status($this->notice);
247             }
248         }
249     }
250
251     /**
252      * Is this command supported when doing an update from the API?
253      *
254      * @param string $cmd the command to check for
255      *
256      * @return boolean true or false
257      */
258
259     function supported($cmd)
260     {
261         static $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand',
262             'FavCommand', 'OnCommand', 'OffCommand');
263
264         if (in_array(get_class($cmd), $cmdlist)) {
265             return true;
266         }
267
268         return false;
269     }
270
271 }