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 $cmd->execute(new WebChannel());
73 $replyto = $this->trimmed('inreplyto');
75 $notice = Notice::saveNew($user->id, $content, 'web', 1, ($replyto == 'false') ? NULL : $replyto);
77 if (is_string($notice)) {
78 $this->show_form($notice);
82 common_broadcast_notice($notice);
84 if ($this->boolean('ajax')) {
85 common_start_html('text/xml;charset=utf-8', false);
86 common_element_start('head');
87 common_element('title', null, _('Notice posted'));
88 common_element_end('head');
89 common_element_start('body');
90 $this->show_notice($notice);
91 common_element_end('body');
92 common_element_end('html');
94 $returnto = $this->trimmed('returnto');
97 $url = common_local_url($returnto,
98 array('nickname' => $user->nickname));
100 $url = common_local_url('shownotice',
101 array('notice' => $notice->id));
103 common_redirect($url, 303);
107 function ajax_error_msg($msg) {
108 common_start_html('text/xml;charset=utf-8', false);
109 common_element_start('head');
110 common_element('title', null, _('Ajax Error'));
111 common_element_end('head');
112 common_element_start('body');
113 common_element('p', array('class' => 'error'), $msg);
114 common_element_end('body');
115 common_element_end('html');
118 function show_top($content=NULL) {
119 common_notice_form(NULL, $content);
122 function show_form($msg=NULL) {
123 if ($msg && $this->boolean('ajax')) {
124 $this->ajax_error_msg($msg);
127 $content = $this->trimmed('status_textarea');
129 $replyto = $this->trimmed('replyto');
130 $profile = Profile::staticGet('nickname', $replyto);
132 $content = '@' . $profile->nickname . ' ';
135 common_show_header(_('New notice'), NULL, $content,
136 array($this, 'show_top'));
138 common_element('p', 'error', $msg);
140 common_show_footer();
143 function show_notice($notice) {
145 $profile = $notice->getProfile();
146 $user = common_current_user();
149 common_element_start('li', array('class' => 'notice_single',
150 'id' => 'notice-' . $notice->id));
152 if ($user->hasFave($notice)) {
153 common_disfavor_form($notice);
155 common_favor_form($notice);
159 $returnto = $this->trimmed('returnto');
161 # If this is the personal stream, we don't want avatars
162 if ($returnto != 'showstream') {
164 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
165 common_element_start('a', array('href' => $profile->profileurl));
166 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
167 'class' => 'avatar stream',
168 'width' => AVATAR_STREAM_SIZE,
169 'height' => AVATAR_STREAM_SIZE,
171 ($profile->fullname) ? $profile->fullname :
172 $profile->nickname));
173 common_element_end('a');
174 common_element('a', array('href' => $profile->profileurl,
175 'class' => 'nickname'),
179 # FIXME: URL, image, video, audio
180 common_element_start('p', array('class' => 'content'));
182 if ($notice->rendered) {
183 common_raw($notice->rendered);
185 # XXX: may be some uncooked notices in the DB,
186 # we cook them right now. This should probably disappear in future
187 # versions (>> 0.4.x)
188 common_raw(common_render_content($notice->content, $notice));
190 common_element_end('p');
193 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
194 # XXX: we need to figure this out better. Is this right?
195 if (strcmp($notice->uri, $noticeurl) != 0 && preg_match('/^http/', $notice->uri)) {
196 $noticeurl = $notice->uri;
198 common_element_start('p', 'time');
199 common_element('a', array('class' => 'permalink',
200 'href' => $noticeurl,
201 'title' => common_exact_date($notice->created)),
202 common_date_string($notice->created));
203 if ($notice->source) {
204 common_text(_(' from '));
205 $this->source_link($notice->source);
207 if ($notice->reply_to) {
208 $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
210 common_element('a', array('class' => 'inreplyto',
211 'href' => $replyurl),
212 _('in reply to...'));
215 common_element_start('a',
216 array('href' => common_local_url('newnotice',
217 array('replyto' => $profile->nickname)),
218 'onclick' => 'return doreply("'.$profile->nickname.'", '.$notice->id.');',
219 'title' => _('reply'),
220 'class' => 'replybutton'));
221 common_raw(html_entity_decode('→', ENT_NOQUOTES, 'utf-8'));
223 common_element_end('a');
224 if ($user && $notice->profile_id == $user->id) {
225 $deleteurl = common_local_url('deletenotice', array('notice' => $notice->id));
226 common_element_start('a', array('class' => 'deletenotice',
227 'href' => $deleteurl,
228 'title' => _('delete')));
229 common_raw(html_entity_decode('×', ENT_NOQUOTES, 'utf-8'));
230 common_element_end('a');
232 common_element_end('p');
233 common_element_end('li');
236 function source_link($source) {
237 $source_name = _($source);
244 common_element('span', 'noticesource', $source_name);
247 $ns = Notice_source::staticGet($source);
249 common_element('a', array('href' => $ns->url),
252 common_element('span', 'noticesource', $source_name);