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') {
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.'));
102 $this->saveNewNotice();
103 } catch (Exception $e) {
104 $this->showForm($e->getMessage());
113 * Save a new notice, based on arguments
115 * If successful, will show the notice, or return an Ajax-y result.
116 * If not, it will show an error message -- possibly Ajax-y.
118 * Also, if the notice input looks like a command, it will run the
119 * command and show the results -- again, possibly ajaxy.
124 function saveNewNotice()
126 $user = common_current_user();
127 assert($user); // XXX: maybe an error instead...
128 $content = $this->trimmed('status_textarea');
131 $this->clientError(_('No content!'));
133 $content_shortened = common_shorten_links($content);
135 if (mb_strlen($content_shortened) > 140) {
136 $this->clientError(_('That\'s too long. '.
137 'Max notice size is 140 chars.'));
141 $inter = new CommandInterpreter();
143 $cmd = $inter->handle_command($user, $content_shortened);
146 if ($this->boolean('ajax')) {
147 $cmd->execute(new AjaxWebChannel($this));
149 $cmd->execute(new WebChannel($this));
154 $replyto = $this->trimmed('inreplyto');
155 #If an ID of 0 is wrongly passed here, it will cause a database error,
161 // $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1,
162 $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1,
163 ($replyto == 'false') ? null : $replyto);
165 if (is_string($notice)) {
166 $this->clientError($notice);
170 $this->saveUrls($notice);
172 common_broadcast_notice($notice);
174 if ($this->boolean('ajax')) {
175 $this->startHTML('text/xml;charset=utf-8');
176 $this->elementStart('head');
177 $this->element('title', null, _('Notice posted'));
178 $this->elementEnd('head');
179 $this->elementStart('body');
180 $this->showNotice($notice);
181 $this->elementEnd('body');
182 $this->elementEnd('html');
184 $returnto = $this->trimmed('returnto');
187 $url = common_local_url($returnto,
188 array('nickname' => $user->nickname));
190 $url = common_local_url('shownotice',
191 array('notice' => $notice->id));
193 common_redirect($url, 303);
197 /** save all urls in the notice to the db
199 * follow redirects and save all available file information
200 * (mimetype, date, size, oembed, etc.)
202 * @param class $notice Notice to pull URLs from
206 function saveUrls($notice) {
207 common_replace_urls_callback($notice->content, array($this, 'saveUrl'), $notice->id);
210 function saveUrl($data) {
211 list($url, $notice_id) = $data;
212 $zzz = File::processNew($url, $notice_id);
216 * Show an Ajax-y error message
218 * Goes back to the browser, where it's shown in a popup.
220 * @param string $msg Message to show
225 function ajaxErrorMsg($msg)
227 $this->startHTML('text/xml;charset=utf-8', true);
228 $this->elementStart('head');
229 $this->element('title', null, _('Ajax Error'));
230 $this->elementEnd('head');
231 $this->elementStart('body');
232 $this->element('p', array('id' => 'error'), $msg);
233 $this->elementEnd('body');
234 $this->elementEnd('html');
238 * Formerly page output
240 * This used to be the whole page output; now that's been largely
241 * subsumed by showPage. So this just stores an error message, if
242 * it was passed, and calls showPage.
244 * Note that since we started doing Ajax output, this page is rarely
247 * @param string $msg An error message, if any
252 function showForm($msg=null)
254 if ($msg && $this->boolean('ajax')) {
255 $this->ajaxErrorMsg($msg);
264 * Overload for replies or bad results
266 * We show content in the notice form if there were replies or results.
271 function showNoticeForm()
273 $content = $this->trimmed('status_textarea');
275 $replyto = $this->trimmed('replyto');
276 $profile = Profile::staticGet('nickname', $replyto);
278 $content = '@' . $profile->nickname . ' ';
282 $notice_form = new NoticeForm($this, '', $content);
283 $notice_form->show();
287 * Show an error message
289 * Shows an error message if there is one.
293 * @todo maybe show some instructions?
296 function showPageNotice()
299 $this->element('p', array('id' => 'error'), $this->msg);
306 * Used to generate the notice code for Ajax results.
308 * @param Notice $notice Notice that was saved
313 function showNotice($notice)
315 $nli = new NoticeListItem($notice, $this);