3 * Laconica, 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@controlyourself.ca>
25 * @author Zach Copley <zach@controlyourself.ca>
26 * @author Sarven Capadisli <csarven@controlyourself.ca>
27 * @copyright 2008-2009 Control Yourself, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://laconi.ca/
32 if (!defined('LACONICA')) {
36 require_once INSTALLDIR.'/lib/noticelist.php';
39 * Action for posting new notices
43 * @author Evan Prodromou <evan@controlyourself.ca>
44 * @author Zach Copley <zach@controlyourself.ca>
45 * @author Sarven Capadisli <csarven@controlyourself.ca>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://laconi.ca/
50 class NewnoticeAction extends Action
53 * Error message, if any
61 * Note that this usually doesn't get called unless something went wrong
63 * @return string page title
68 return _('New notice');
72 * Handle input, produce output
74 * Switches based on GET or POST method. On GET, shows a form
75 * for posting a notice. On POST, saves the results of that form.
77 * Results may be a full page, or just a single notice list item,
78 * depending on whether AJAX was requested.
80 * @param array $args $_REQUEST contents
85 function handle($args)
87 parent::handle($args);
89 if (!common_logged_in()) {
90 $this->clientError(_('Not logged in.'));
91 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
93 // CSRF protection - token set in common_notice_form()
94 $token = $this->trimmed('token');
95 if (!$token || $token != common_session_token()) {
96 $this->clientError(_('There was a problem with your session token. '.
97 'Try again, please.'));
101 $this->saveNewNotice();
108 * Save a new notice, based on arguments
110 * If successful, will show the notice, or return an Ajax-y result.
111 * If not, it will show an error message -- possibly Ajax-y.
113 * Also, if the notice input looks like a command, it will run the
114 * command and show the results -- again, possibly ajaxy.
119 function saveNewNotice()
121 $user = common_current_user();
122 assert($user); // XXX: maybe an error instead...
123 $content = $this->trimmed('status_textarea');
126 $this->showForm(_('No content!'));
129 $content_shortened = common_shorten_links($content);
131 if (mb_strlen($content_shortened) > 140) {
132 $this->showForm(_('That\'s too long. '.
133 'Max notice size is 140 chars.'));
138 $inter = new CommandInterpreter();
140 $cmd = $inter->handle_command($user, $content_shortened);
143 if ($this->boolean('ajax')) {
144 $cmd->execute(new AjaxWebChannel());
146 $cmd->execute(new WebChannel());
151 $replyto = $this->trimmed('inreplyto');
153 $notice = Notice::saveNew($user->id, $content, 'web', 1,
154 ($replyto == 'false') ? null : $replyto);
156 if (is_string($notice)) {
157 $this->showForm($notice);
161 common_broadcast_notice($notice);
163 if ($this->boolean('ajax')) {
164 $this->startHTML('text/xml;charset=utf-8', true);
165 $this->elementStart('head');
166 $this->element('title', null, _('Notice posted'));
167 $this->elementEnd('head');
168 $this->elementStart('body');
169 $this->showNotice($notice);
170 $this->elementEnd('body');
171 $this->elementEnd('html');
173 $returnto = $this->trimmed('returnto');
176 $url = common_local_url($returnto,
177 array('nickname' => $user->nickname));
179 $url = common_local_url('shownotice',
180 array('notice' => $notice->id));
182 common_redirect($url, 303);
187 * Show an Ajax-y error message
189 * Goes back to the browser, where it's shown in a popup.
191 * @param string $msg Message to show
196 function ajaxErrorMsg($msg)
198 common_start_html('text/xml;charset=utf-8', true);
199 $this->elementStart('head');
200 $this->element('title', null, _('Ajax Error'));
201 $this->elementEnd('head');
202 $this->elementStart('body');
203 $this->element('p', array('id' => 'error'), $msg);
204 $this->elementEnd('body');
205 $this->elementEnd('html');
209 * Formerly page output
211 * This used to be the whole page output; now that's been largely
212 * subsumed by showPage. So this just stores an error message, if
213 * it was passed, and calls showPage.
215 * Note that since we started doing Ajax output, this page is rarely
218 * @param string $msg An error message, if any
223 function showForm($msg=null)
225 if ($msg && $this->boolean('ajax')) {
226 $this->ajaxErrorMsg($msg);
235 * Overload for replies or bad results
237 * We show content in the notice form if there were replies or results.
242 function showNoticeForm()
244 $content = $this->trimmed('status_textarea');
246 $replyto = $this->trimmed('replyto');
247 $profile = Profile::staticGet('nickname', $replyto);
249 $content = '@' . $profile->nickname . ' ';
253 $notice_form = new NoticeForm($this, $content);
254 $notice_form->show();
258 * Show an error message
260 * Shows an error message if there is one.
264 * @todo maybe show some instructions?
267 function showPageNotice()
270 $this->element('p', array('id' => 'error'), $this->msg);
277 * Used to generate the notice code for Ajax results.
279 * @param Notice $notice Notice that was saved
284 function showNotice($notice)
286 $nli = new NoticeListItem($notice, $this);