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