]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
Profile block base style
[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                         if ($this->boolean('ajax')) {
70                                 $cmd->execute(new AjaxWebChannel());
71                         } else {
72                                 $cmd->execute(new WebChannel());
73                         }
74                         return;
75                 }
76
77                 $replyto = $this->trimmed('inreplyto');
78
79                 $notice = Notice::saveNew($user->id, $content, 'web', 1, ($replyto == 'false') ? NULL : $replyto);
80
81                 if (is_string($notice)) {
82                         $this->show_form($notice);
83                         return;
84                 }
85
86                 common_broadcast_notice($notice);
87
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');
97                 } else {
98                         $returnto = $this->trimmed('returnto');
99
100                         if ($returnto) {
101                                 $url = common_local_url($returnto,
102                                                                                 array('nickname' => $user->nickname));
103                         } else {
104                                 $url = common_local_url('shownotice',
105                                                                                 array('notice' => $notice->id));
106                         }
107                         common_redirect($url, 303);
108                 }
109         }
110
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');
120         }
121
122         function show_top($content=NULL) {
123                 common_notice_form(NULL, $content);
124         }
125
126         function show_form($msg=NULL) {
127                 if ($msg && $this->boolean('ajax')) {
128                         $this->ajax_error_msg($msg);
129                         return;
130                 }
131                 $content = $this->trimmed('status_textarea');
132                 if (!$content) {
133                         $replyto = $this->trimmed('replyto');
134                         $profile = Profile::staticGet('nickname', $replyto);
135                         if ($profile) {
136                                 $content = '@' . $profile->nickname . ' ';
137                         }
138                 }
139                 common_show_header(_('New notice'), NULL, $content,
140                                                    array($this, 'show_top'));
141                 if ($msg) {
142                         common_element('p', 'error', $msg);
143                 }
144                 common_show_footer();
145         }
146
147         function show_notice($notice) {
148                 global $config;
149                 $profile = $notice->getProfile();
150                 $user = common_current_user();
151
152                 # XXX: RDFa
153                 common_element_start('li', array('class' => 'notice_single',
154                                                                                   'id' => 'notice-' . $notice->id));
155                 if ($user) {
156                         if ($user->hasFave($notice)) {
157                                 common_disfavor_form($notice);
158                         } else {
159                                 common_favor_form($notice);
160                         }
161                 }
162
163                 $returnto = $this->trimmed('returnto');
164
165                 # If this is the personal stream, we don't want avatars
166                 if ($returnto != 'showstream') {
167
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,
174                                                                                 'alt' =>
175                                                                                 ($profile->fullname) ? $profile->fullname :
176                                                                                 $profile->nickname));
177                         common_element_end('a');
178                         common_element('a', array('href' => $profile->profileurl,
179                                                                           'class' => 'nickname'),
180                                                    $profile->nickname);
181                 }
182
183                 # FIXME: URL, image, video, audio
184                 common_element_start('p', array('class' => 'content'));
185
186                 if ($notice->rendered) {
187                         common_raw($notice->rendered);
188                 } else {
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));
193                 }
194                 common_element_end('p');
195
196
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;
201                 }
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);
210                 }
211                 if ($notice->reply_to) {
212                         $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
213                         common_text(' (');
214                         common_element('a', array('class' => 'inreplyto',
215                                                                           'href' => $replyurl),
216                                                    _('in reply to...'));
217                         common_text(')');
218                 }
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('&rarr;', ENT_NOQUOTES, 'utf-8'));
226                 
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('&times;', ENT_NOQUOTES, 'utf-8'));
234                         common_element_end('a');
235                 }
236                 common_element_end('p');
237                 common_element_end('li');
238         }
239
240         function source_link($source) {
241                 $source_name = _($source);
242                 switch ($source) {
243                  case 'web':
244                  case 'xmpp':
245                  case 'mail':
246                  case 'omb':
247                  case 'api':
248                         common_element('span', 'noticesource', $source_name);
249                         break;
250                  default:
251                         $ns = Notice_source::staticGet($source);
252                         if ($ns) {
253                                 common_element('a', array('href' => $ns->url),
254                                                            $ns->name);
255                         } else {
256                                 common_element('span', 'noticesource', $source_name);
257                         }
258                         break;
259                 }
260                 return;
261         }
262
263
264 }