3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
22 class NewnoticeAction extends Action {
24 function handle($args) {
25 parent::handle($args);
27 if (!common_logged_in()) {
28 common_user_error(_('Not logged in.'));
29 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
31 # CSRF protection - token set in common_notice_form()
32 $token = $this->trimmed('token');
33 if (!$token || $token != common_session_token()) {
34 $this->client_error(_('There was a problem with your session token. Try again, please.'));
38 $this->save_new_notice();
44 function save_new_notice() {
46 $user = common_current_user();
47 assert($user); # XXX: maybe an error instead...
48 $content = $this->trimmed('status_textarea');
51 $this->show_form(_('No content!'));
54 $content_shortened = common_shorten_links($content);
56 if (mb_strlen($content_shortened) > 140) {
57 common_debug("Content = '$content_shortened'", __FILE__);
58 common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
59 $this->show_form(_('That\'s too long. Max notice size is 140 chars.'));
64 $inter = new CommandInterpreter();
66 $cmd = $inter->handle_command($user, $content_shortened);
69 if ($this->boolean('ajax')) {
70 $cmd->execute(new AjaxWebChannel());
72 $cmd->execute(new WebChannel());
77 $replyto = $this->trimmed('inreplyto');
79 $notice = Notice::saveNew($user->id, $content, 'web', 1, ($replyto == 'false') ? NULL : $replyto);
81 if (is_string($notice)) {
82 $this->show_form($notice);
86 common_broadcast_notice($notice);
88 if ($this->boolean('ajax')) {
89 common_start_html('text/xml;charset=utf-8', false);
90 common_element_start('head');
91 common_element('title', null, _('Notice posted'));
92 common_element_end('head');
93 common_element_start('body');
94 $this->show_notice($notice);
95 common_element_end('body');
96 common_element_end('html');
98 $returnto = $this->trimmed('returnto');
101 $url = common_local_url($returnto,
102 array('nickname' => $user->nickname));
104 $url = common_local_url('shownotice',
105 array('notice' => $notice->id));
107 common_redirect($url, 303);
111 function ajax_error_msg($msg) {
112 common_start_html('text/xml;charset=utf-8', false);
113 common_element_start('head');
114 common_element('title', null, _('Ajax Error'));
115 common_element_end('head');
116 common_element_start('body');
117 common_element('p', array('class' => 'error'), $msg);
118 common_element_end('body');
119 common_element_end('html');
122 function show_top($content=NULL) {
123 common_notice_form(NULL, $content);
126 function show_form($msg=NULL) {
127 if ($msg && $this->boolean('ajax')) {
128 $this->ajax_error_msg($msg);
131 $content = $this->trimmed('status_textarea');
133 $replyto = $this->trimmed('replyto');
134 $profile = Profile::staticGet('nickname', $replyto);
136 $content = '@' . $profile->nickname . ' ';
139 common_show_header(_('New notice'), NULL, $content,
140 array($this, 'show_top'));
142 common_element('p', 'error', $msg);
144 common_show_footer();
147 function show_notice($notice) {
149 $profile = $notice->getProfile();
150 $user = common_current_user();
153 common_element_start('li', array('class' => 'notice_single',
154 'id' => 'notice-' . $notice->id));
156 if ($user->hasFave($notice)) {
157 common_disfavor_form($notice);
159 common_favor_form($notice);
163 $returnto = $this->trimmed('returnto');
165 # If this is the personal stream, we don't want avatars
166 if ($returnto != 'showstream') {
168 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
169 common_element_start('a', array('href' => $profile->profileurl));
170 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
171 'class' => 'avatar stream',
172 'width' => AVATAR_STREAM_SIZE,
173 'height' => AVATAR_STREAM_SIZE,
175 ($profile->fullname) ? $profile->fullname :
176 $profile->nickname));
177 common_element_end('a');
178 common_element('a', array('href' => $profile->profileurl,
179 'class' => 'nickname'),
183 # FIXME: URL, image, video, audio
184 common_element_start('p', array('class' => 'content'));
186 if ($notice->rendered) {
187 common_raw($notice->rendered);
189 # XXX: may be some uncooked notices in the DB,
190 # we cook them right now. This should probably disappear in future
191 # versions (>> 0.4.x)
192 common_raw(common_render_content($notice->content, $notice));
194 common_element_end('p');
197 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
198 # XXX: we need to figure this out better. Is this right?
199 if (strcmp($notice->uri, $noticeurl) != 0 && preg_match('/^http/', $notice->uri)) {
200 $noticeurl = $notice->uri;
202 common_element_start('p', 'time');
203 common_element('a', array('class' => 'permalink',
204 'href' => $noticeurl,
205 'title' => common_exact_date($notice->created)),
206 common_date_string($notice->created));
207 if ($notice->source) {
208 common_text(_(' from '));
209 $this->source_link($notice->source);
211 if ($notice->reply_to) {
212 $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
214 common_element('a', array('class' => 'inreplyto',
215 'href' => $replyurl),
216 _('in reply to...'));
219 common_element_start('a',
220 array('href' => common_local_url('newnotice',
221 array('replyto' => $profile->nickname)),
222 'onclick' => 'return doreply("'.$profile->nickname.'", '.$notice->id.');',
223 'title' => _('reply'),
224 'class' => 'replybutton'));
225 common_raw(html_entity_decode('→', ENT_NOQUOTES, 'utf-8'));
227 common_element_end('a');
228 if ($user && $notice->profile_id == $user->id) {
229 $deleteurl = common_local_url('deletenotice', array('notice' => $notice->id));
230 common_element_start('a', array('class' => 'deletenotice',
231 'href' => $deleteurl,
232 'title' => _('delete')));
233 common_raw(html_entity_decode('×', ENT_NOQUOTES, 'utf-8'));
234 common_element_end('a');
236 common_element_end('p');
237 common_element_end('li');
240 function source_link($source) {
241 $source_name = _($source);
248 common_element('span', 'noticesource', $source_name);
251 $ns = Notice_source::staticGet($source);
253 common_element('a', array('href' => $ns->url),
256 common_element('span', 'noticesource', $source_name);