]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
replace all tabs with four spaces
[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 require_once INSTALLDIR . '/lib/noticelist.php';
23
24 class NewnoticeAction extends Action {
25
26     function handle($args) {
27         parent::handle($args);
28
29         if (!common_logged_in()) {
30             common_user_error(_('Not logged in.'));
31         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
32
33             # CSRF protection - token set in common_notice_form()
34             $token = $this->trimmed('token');
35             if (!$token || $token != common_session_token()) {
36                 $this->client_error(_('There was a problem with your session token. Try again, please.'));
37                 return;
38             }
39
40             $this->save_new_notice();
41         } else {
42             $this->show_form();
43         }
44     }
45
46     function save_new_notice() {
47
48         $user = common_current_user();
49         assert($user); # XXX: maybe an error instead...
50         $content = $this->trimmed('status_textarea');
51
52         if (!$content) {
53             $this->show_form(_('No content!'));
54             return;
55         } else {
56             $content_shortened = common_shorten_links($content);
57
58             if (mb_strlen($content_shortened) > 140) {
59                 common_debug("Content = '$content_shortened'", __FILE__);
60                 common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
61                 $this->show_form(_('That\'s too long. Max notice size is 140 chars.'));
62                 return;
63             }
64         }
65
66         $inter = new CommandInterpreter();
67
68         $cmd = $inter->handle_command($user, $content_shortened);
69
70         if ($cmd) {
71             if ($this->boolean('ajax')) {
72                 $cmd->execute(new AjaxWebChannel());
73             } else {
74                 $cmd->execute(new WebChannel());
75             }
76             return;
77         }
78
79         $replyto = $this->trimmed('inreplyto');
80
81         $notice = Notice::saveNew($user->id, $content, 'web', 1, ($replyto == 'false') ? NULL : $replyto);
82
83         if (is_string($notice)) {
84             $this->show_form($notice);
85             return;
86         }
87
88         common_broadcast_notice($notice);
89
90         if ($this->boolean('ajax')) {
91             common_start_html('text/xml;charset=utf-8', true);
92             common_element_start('head');
93             common_element('title', null, _('Notice posted'));
94             common_element_end('head');
95             common_element_start('body');
96             $this->show_notice($notice);
97             common_element_end('body');
98             common_element_end('html');
99         } else {
100             $returnto = $this->trimmed('returnto');
101
102             if ($returnto) {
103                 $url = common_local_url($returnto,
104                                         array('nickname' => $user->nickname));
105             } else {
106                 $url = common_local_url('shownotice',
107                                         array('notice' => $notice->id));
108             }
109             common_redirect($url, 303);
110         }
111     }
112
113     function ajax_error_msg($msg) {
114         common_start_html('text/xml;charset=utf-8', true);
115         common_element_start('head');
116         common_element('title', null, _('Ajax Error'));
117         common_element_end('head');
118         common_element_start('body');
119         common_element('p', array('id' => 'error'), $msg);
120         common_element_end('body');
121         common_element_end('html');
122     }
123
124     function show_top($content=NULL) {
125         common_notice_form(NULL, $content);
126     }
127
128     function show_form($msg=NULL) {
129         if ($msg && $this->boolean('ajax')) {
130             $this->ajax_error_msg($msg);
131             return;
132         }
133         $content = $this->trimmed('status_textarea');
134         if (!$content) {
135             $replyto = $this->trimmed('replyto');
136             $profile = Profile::staticGet('nickname', $replyto);
137             if ($profile) {
138                 $content = '@' . $profile->nickname . ' ';
139             }
140         }
141         common_show_header(_('New notice'), NULL, $content,
142                            array($this, 'show_top'));
143         if ($msg) {
144             common_element('p', array('id' => 'error'), $msg);
145         }
146         common_show_footer();
147     }
148
149     function show_notice($notice) {
150         $nli = new NoticeListItem($notice);
151         $nli->show();
152     }
153
154 }