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/
51 class NewnoticeAction extends Action
54 * Error message, if any
62 * Note that this usually doesn't get called unless something went wrong
64 * @return string page title
69 return _('New notice');
73 * Handle input, produce output
75 * Switches based on GET or POST method. On GET, shows a form
76 * for posting a notice. On POST, saves the results of that form.
78 * Results may be a full page, or just a single notice list item,
79 * depending on whether AJAX was requested.
81 * @param array $args $_REQUEST contents
86 function handle($args)
88 if (!common_logged_in()) {
89 $this->clientError(_('Not logged in.'));
90 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
91 // check for this before token since all POST and FILES data
92 // is losts when size is exceeded
93 if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
94 $this->clientError(sprintf(_('The server was unable to handle ' .
95 'that much POST data (%s bytes) due to its current configuration.'),
96 $_SERVER['CONTENT_LENGTH']));
98 parent::handle($args);
101 $token = $this->trimmed('token');
102 if (!$token || $token != common_session_token()) {
103 $this->clientError(_('There was a problem with your session token. '.
104 'Try again, please.'));
107 $this->saveNewNotice();
108 } catch (Exception $e) {
109 $this->showForm($e->getMessage());
118 * Save a new notice, based on arguments
120 * If successful, will show the notice, or return an Ajax-y result.
121 * If not, it will show an error message -- possibly Ajax-y.
123 * Also, if the notice input looks like a command, it will run the
124 * command and show the results -- again, possibly ajaxy.
129 function saveNewNotice()
131 $user = common_current_user();
132 assert($user); // XXX: maybe an error instead...
133 $content = $this->trimmed('status_textarea');
136 $this->clientError(_('No content!'));
140 $inter = new CommandInterpreter();
142 $cmd = $inter->handle_command($user, $content);
145 if ($this->boolean('ajax')) {
146 $cmd->execute(new AjaxWebChannel($this));
148 $cmd->execute(new WebChannel($this));
153 $content_shortened = common_shorten_links($content);
154 if (Notice::contentTooLong($content_shortened)) {
155 $this->clientError(sprintf(_('That\'s too long. '.
156 'Max notice size is %d chars.'),
157 Notice::maxContent()));
160 $replyto = $this->trimmed('inreplyto');
161 #If an ID of 0 is wrongly passed here, it will cause a database error,
168 $upload = MediaFile::fromUpload('attach');
170 if (isset($upload)) {
172 $content_shortened .= ' ' . $upload->shortUrl();
174 if (Notice::contentTooLong($content_shortened)) {
178 _('Max notice size is %d chars, including attachment URL.'),
185 $options = array('reply_to' => ($replyto == 'false') ? null : $replyto);
187 if ($user->shareLocation()) {
188 // use browser data if checked; otherwise profile data
189 if ($this->arg('notice_data-geo')) {
190 $locOptions = Notice::locationOptions($this->trimmed('lat'),
191 $this->trimmed('lon'),
192 $this->trimmed('location_id'),
193 $this->trimmed('location_ns'),
194 $user->getProfile());
196 $locOptions = Notice::locationOptions(null,
200 $user->getProfile());
203 $options = array_merge($options, $locOptions);
206 Event::handle('SaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
207 $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options);
209 if (isset($upload)) {
210 $upload->attachToNotice($notice);
213 if ($this->boolean('ajax')) {
214 header('Content-Type: text/xml;charset=utf-8');
215 $this->xw->startDocument('1.0', 'UTF-8');
216 $this->elementStart('html');
217 $this->elementStart('head');
218 $this->element('title', null, _('Notice posted'));
219 $this->elementEnd('head');
220 $this->elementStart('body');
221 $this->showNotice($notice);
222 $this->elementEnd('body');
223 $this->elementEnd('html');
225 $returnto = $this->trimmed('returnto');
228 $url = common_local_url($returnto,
229 array('nickname' => $user->nickname));
231 $url = common_local_url('shownotice',
232 array('notice' => $notice->id));
234 common_redirect($url, 303);
239 * Show an Ajax-y error message
241 * Goes back to the browser, where it's shown in a popup.
243 * @param string $msg Message to show
248 function ajaxErrorMsg($msg)
250 $this->startHTML('text/xml;charset=utf-8', true);
251 $this->elementStart('head');
252 $this->element('title', null, _('Ajax Error'));
253 $this->elementEnd('head');
254 $this->elementStart('body');
255 $this->element('p', array('id' => 'error'), $msg);
256 $this->elementEnd('body');
257 $this->elementEnd('html');
261 * Formerly page output
263 * This used to be the whole page output; now that's been largely
264 * subsumed by showPage. So this just stores an error message, if
265 * it was passed, and calls showPage.
267 * Note that since we started doing Ajax output, this page is rarely
270 * @param string $msg An error message, if any
275 function showForm($msg=null)
277 if ($msg && $this->boolean('ajax')) {
278 $this->ajaxErrorMsg($msg);
287 * Overload for replies or bad results
289 * We show content in the notice form if there were replies or results.
294 function showNoticeForm()
296 $content = $this->trimmed('status_textarea');
298 $replyto = $this->trimmed('replyto');
299 $inreplyto = $this->trimmed('inreplyto');
300 $profile = Profile::staticGet('nickname', $replyto);
302 $content = '@' . $profile->nickname . ' ';
305 // @fixme most of these bits above aren't being passed on above
309 $notice_form = new NoticeForm($this, '', $content, null, $inreplyto);
310 $notice_form->show();
314 * Show an error message
316 * Shows an error message if there is one.
320 * @todo maybe show some instructions?
323 function showPageNotice()
326 $this->element('p', array('id' => 'error'), $this->msg);
333 * Used to generate the notice code for Ajax results.
335 * @param Notice $notice Notice that was saved
340 function showNotice($notice)
342 $nli = new NoticeListItem($notice, $this);