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