3 * StatusNet, the distributed open-source microblogging tool
5 * Handler for posting new notices
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.
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.
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/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @author Zach Copley <zach@status.net>
26 * @author Sarven Capadisli <csarven@status.net>
27 * @copyright 2008-2009 StatusNet, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://status.net/
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
36 require_once INSTALLDIR . '/lib/noticelist.php';
37 require_once INSTALLDIR . '/lib/mediafile.php';
40 * Action for posting new notices
44 * @author Evan Prodromou <evan@status.net>
45 * @author Zach Copley <zach@status.net>
46 * @author Sarven Capadisli <csarven@status.net>
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48 * @link http://status.net/
50 class NewnoticeAction extends Action
53 * Error message, if any
60 * Note that this usually doesn't get called unless something went wrong
62 * @return string page title
66 // TRANS: Page title for sending a new notice.
67 return _('New notice');
71 * Handle input, produce output
73 * Switches based on GET or POST method. On GET, shows a form
74 * for posting a notice. On POST, saves the results of that form.
76 * Results may be a full page, or just a single notice list item,
77 * depending on whether AJAX was requested.
79 * @param array $args $_REQUEST contents
83 function handle($args)
85 if (!common_logged_in()) {
86 // TRANS: Client error displayed trying to send a notice while not logged in.
87 $this->clientError(_('Not logged in.'));
88 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
89 // check for this before token since all POST and FILES data
90 // is losts when size is exceeded
91 if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
92 // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
93 // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
94 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
95 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
96 intval($_SERVER['CONTENT_LENGTH']));
97 $this->clientError(sprintf($msg,$_SERVER['CONTENT_LENGTH']));
99 parent::handle($args);
102 $token = $this->trimmed('token');
103 if (!$token || $token != common_session_token()) {
104 // TRANS: Client error displayed when the session token does not match or is not given.
105 $this->clientError(_('There was a problem with your session token. '.
106 'Try again, please.'));
109 $this->saveNewNotice();
110 } catch (Exception $e) {
111 $this->showForm($e->getMessage());
120 * Save a new notice, based on arguments
122 * If successful, will show the notice, or return an Ajax-y result.
123 * If not, it will show an error message -- possibly Ajax-y.
125 * Also, if the notice input looks like a command, it will run the
126 * command and show the results -- again, possibly ajaxy.
130 function saveNewNotice()
132 $user = common_current_user();
133 assert($user); // XXX: maybe an error instead...
134 $content = $this->trimmed('status_textarea');
136 Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
139 // TRANS: Client error displayed trying to send a notice without content.
140 $this->clientError(_('No content!'));
144 $inter = new CommandInterpreter();
146 $cmd = $inter->handle_command($user, $content);
149 if ($this->boolean('ajax')) {
150 $cmd->execute(new AjaxWebChannel($this));
152 $cmd->execute(new WebChannel($this));
157 $content_shortened = $user->shortenLinks($content);
158 if (Notice::contentTooLong($content_shortened)) {
159 // TRANS: Client error displayed when the parameter "status" is missing.
160 // TRANS: %d is the maximum number of character for a notice.
161 $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.',
162 'That\'s too long. Maximum notice size is %d characters.',
163 Notice::maxContent()),
164 Notice::maxContent()));
167 $replyto = intval($this->trimmed('inreplyto'));
169 $options['reply_to'] = $replyto;
173 $upload = MediaFile::fromUpload('attach');
175 if (isset($upload)) {
177 if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
178 $content_shortened .= ' ' . $upload->shortUrl();
180 Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
182 if (Notice::contentTooLong($content_shortened)) {
184 $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.',
185 'Maximum notice size is %d characters, including attachment URL.',
186 Notice::maxContent()),
187 Notice::maxContent()));
191 if ($user->shareLocation()) {
192 // use browser data if checked; otherwise profile data
193 if ($this->arg('notice_data-geo')) {
194 $locOptions = Notice::locationOptions($this->trimmed('lat'),
195 $this->trimmed('lon'),
196 $this->trimmed('location_id'),
197 $this->trimmed('location_ns'),
198 $user->getProfile());
200 $locOptions = Notice::locationOptions(null,
204 $user->getProfile());
207 $options = array_merge($options, $locOptions);
210 $author_id = $user->id;
211 $text = $content_shortened;
213 // Does the heavy-lifting for getting "To:" information
215 ToSelector::fillOptions($this, $options);
217 if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
219 $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options);
221 if (isset($upload)) {
222 $upload->attachToNotice($notice);
225 Event::handle('EndNoticeSaveWeb', array($this, $notice));
227 Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
229 if ($this->boolean('ajax')) {
230 header('Content-Type: text/xml;charset=utf-8');
231 $this->xw->startDocument('1.0', 'UTF-8');
232 $this->elementStart('html');
233 $this->elementStart('head');
234 // TRANS: Page title after sending a notice.
235 $this->element('title', null, _('Notice posted'));
236 $this->elementEnd('head');
237 $this->elementStart('body');
238 $this->showNotice($notice);
239 $this->elementEnd('body');
240 $this->elementEnd('html');
242 $returnto = $this->trimmed('returnto');
245 $url = common_local_url($returnto,
246 array('nickname' => $user->nickname));
248 $url = common_local_url('shownotice',
249 array('notice' => $notice->id));
251 common_redirect($url, 303);
256 * Show an Ajax-y error message
258 * Goes back to the browser, where it's shown in a popup.
260 * @param string $msg Message to show
264 function ajaxErrorMsg($msg)
266 $this->startHTML('text/xml;charset=utf-8', true);
267 $this->elementStart('head');
268 // TRANS: Page title after an AJAX error occurs on the send notice page.
269 $this->element('title', null, _('Ajax Error'));
270 $this->elementEnd('head');
271 $this->elementStart('body');
272 $this->element('p', array('id' => 'error'), $msg);
273 $this->elementEnd('body');
274 $this->elementEnd('html');
278 * Show an Ajax-y notice form
280 * Goes back to the browser, where it's shown in a popup.
282 * @param string $msg Message to show
286 function ajaxShowForm()
288 $this->startHTML('text/xml;charset=utf-8', true);
289 $this->elementStart('head');
290 $this->element('title', null, _('New notice'));
291 $this->elementEnd('head');
292 $this->elementStart('body');
294 $form = new NoticeForm($this);
297 $this->elementEnd('body');
298 $this->elementEnd('html');
302 * Formerly page output
304 * This used to be the whole page output; now that's been largely
305 * subsumed by showPage. So this just stores an error message, if
306 * it was passed, and calls showPage.
308 * Note that since we started doing Ajax output, this page is rarely
311 * @param string $msg An error message, if any
315 function showForm($msg=null)
317 if ($this->boolean('ajax')) {
319 $this->ajaxErrorMsg($msg);
321 $this->ajaxShowForm();
331 * Overload for replies or bad results
333 * We show content in the notice form if there were replies or results.
337 function showNoticeForm()
339 $content = $this->trimmed('status_textarea');
341 $replyto = $this->trimmed('replyto');
342 $inreplyto = $this->trimmed('inreplyto');
343 $profile = Profile::staticGet('nickname', $replyto);
345 $content = '@' . $profile->nickname . ' ';
348 // @fixme most of these bits above aren't being passed on above
352 $notice_form = new NoticeForm($this, array('content' => $content,
353 'inreplyto' => $inreplyto));
355 $notice_form->show();
359 * Show an error message
361 * Shows an error message if there is one.
365 * @todo maybe show some instructions?
367 function showPageNotice()
370 $this->element('p', array('id' => 'error'), $this->msg);
377 * Used to generate the notice code for Ajax results.
379 * @param Notice $notice Notice that was saved
383 function showNotice($notice)
385 $nli = new NoticeListItem($notice, $this);