]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
generate an etag for shownotice
[quix0rs-gnu-social.git] / actions / newnotice.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 class NewnoticeAction extends Action {
23
24         function handle($args) {
25                 parent::handle($args);
26
27                 if (!common_logged_in()) {
28                         common_user_error(_('Not logged in.'));
29                 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
30
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.'));
35                                 return;
36                         }
37
38                         $this->save_new_notice();
39                 } else {
40                         $this->show_form();
41                 }
42         }
43
44         function save_new_notice() {
45
46                 $user = common_current_user();
47                 assert($user); # XXX: maybe an error instead...
48                 $content = $this->trimmed('status_textarea');
49
50                 if (!$content) {
51                         $this->show_form(_('No content!'));
52                         return;
53                 } else {
54                         $content_shortened = common_shorten_links($content);
55
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.'));
60                                 return;
61                         }
62                 }
63
64                 $inter = new CommandInterpreter();
65
66                 $cmd = $inter->handle_command($user, $content_shortened);
67
68                 if ($cmd) {
69                         $cmd->execute(new WebChannel());
70                         return;
71                 }
72
73                 $replyto = $this->trimmed('inreplyto');
74
75                 $notice = Notice::saveNew($user->id, $content, 'web', 1, ($replyto == 'false') ? NULL : $replyto);
76
77                 if (is_string($notice)) {
78                         $this->show_form($notice);
79                         return;
80                 }
81
82                 common_broadcast_notice($notice);
83
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');
93                 } else {
94                         $returnto = $this->trimmed('returnto');
95
96                         if ($returnto) {
97                                 $url = common_local_url($returnto,
98                                                                                 array('nickname' => $user->nickname));
99                         } else {
100                                 $url = common_local_url('shownotice',
101                                                                                 array('notice' => $notice->id));
102                         }
103                         common_redirect($url, 303);
104                 }
105         }
106
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');
116         }
117
118         function show_top($content=NULL) {
119                 common_notice_form(NULL, $content);
120         }
121
122         function show_form($msg=NULL) {
123                 if ($msg && $this->boolean('ajax')) {
124                         $this->ajax_error_msg($msg);
125                         return;
126                 }
127                 $content = $this->trimmed('status_textarea');
128                 if (!$content) {
129                         $replyto = $this->trimmed('replyto');
130                         $profile = Profile::staticGet('nickname', $replyto);
131                         if ($profile) {
132                                 $content = '@' . $profile->nickname . ' ';
133                         }
134                 }
135                 common_show_header(_('New notice'), NULL, $content,
136                                                    array($this, 'show_top'));
137                 if ($msg) {
138                         common_element('p', 'error', $msg);
139                 }
140                 common_show_footer();
141         }
142
143         function show_notice($notice) {
144                 global $config;
145                 $profile = $notice->getProfile();
146                 $user = common_current_user();
147
148                 # XXX: RDFa
149                 common_element_start('li', array('class' => 'notice_single',
150                                                                                   'id' => 'notice-' . $notice->id));
151                 if ($user) {
152                         if ($user->hasFave($notice)) {
153                                 common_disfavor_form($notice);
154                         } else {
155                                 common_favor_form($notice);
156                         }
157                 }
158
159                 $returnto = $this->trimmed('returnto');
160
161                 # If this is the personal stream, we don't want avatars
162                 if ($returnto != 'showstream') {
163
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,
170                                                                                 'alt' =>
171                                                                                 ($profile->fullname) ? $profile->fullname :
172                                                                                 $profile->nickname));
173                         common_element_end('a');
174                         common_element('a', array('href' => $profile->profileurl,
175                                                                           'class' => 'nickname'),
176                                                    $profile->nickname);
177                 }
178
179                 # FIXME: URL, image, video, audio
180                 common_element_start('p', array('class' => 'content'));
181
182                 if ($notice->rendered) {
183                         common_raw($notice->rendered);
184                 } else {
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));
189                 }
190                 common_element_end('p');
191
192
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;
197                 }
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);
206                 }
207                 if ($notice->reply_to) {
208                         $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
209                         common_text(' (');
210                         common_element('a', array('class' => 'inreplyto',
211                                                                           'href' => $replyurl),
212                                                    _('in reply to...'));
213                         common_text(')');
214                 }
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('&rarr;', ENT_NOQUOTES, 'utf-8'));
222                 
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('&times;', ENT_NOQUOTES, 'utf-8'));
230                         common_element_end('a');
231                 }
232                 common_element_end('p');
233                 common_element_end('li');
234         }
235
236         function source_link($source) {
237                 $source_name = _($source);
238                 switch ($source) {
239                  case 'web':
240                  case 'xmpp':
241                  case 'mail':
242                  case 'omb':
243                  case 'api':
244                         common_element('span', 'noticesource', $source_name);
245                         break;
246                  default:
247                         $ns = Notice_source::staticGet($source);
248                         if ($ns) {
249                                 common_element('a', array('href' => $ns->url),
250                                                            $ns->name);
251                         } else {
252                                 common_element('span', 'noticesource', $source_name);
253                         }
254                         break;
255                 }
256                 return;
257         }
258
259
260 }