]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
5e7691f33d1cae68442b8b18b4c643eb3deca24b
[quix0rs-gnu-social.git] / actions / newnotice.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Handler for posting new notices
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Personal
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @author    Zach Copley <zach@controlyourself.ca>
26  * @author    Sarven Capadisli <csarven@controlyourself.ca>
27  * @copyright 2008-2009 Control Yourself, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://laconi.ca/
30  */
31
32 if (!defined('LACONICA')) {
33     exit(1);
34 }
35
36 require_once INSTALLDIR.'/lib/noticelist.php';
37
38 /**
39  * Action for posting new notices
40  *
41  * @category Personal
42  * @package  Laconica
43  * @author   Evan Prodromou <evan@controlyourself.ca>
44  * @author   Zach Copley <zach@controlyourself.ca>
45  * @author   Sarven Capadisli <csarven@controlyourself.ca>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://laconi.ca/
48  */
49
50 class NewnoticeAction extends Action
51 {
52     /**
53      * Error message, if any
54      */
55
56     var $msg = null;
57
58     /**
59      * Title of the page
60      *
61      * Note that this usually doesn't get called unless something went wrong
62      *
63      * @return string page title
64      */
65
66     function title()
67     {
68         return _('New notice');
69     }
70
71     /**
72      * Handle input, produce output
73      *
74      * Switches based on GET or POST method. On GET, shows a form
75      * for posting a notice. On POST, saves the results of that form.
76      *
77      * Results may be a full page, or just a single notice list item,
78      * depending on whether AJAX was requested.
79      *
80      * @param array $args $_REQUEST contents
81      *
82      * @return void
83      */
84
85     function handle($args)
86     {
87         parent::handle($args);
88
89         if (!common_logged_in()) {
90             $this->clientError(_('Not logged in.'));
91         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
92
93             // CSRF protection
94             $token = $this->trimmed('token');
95             if (!$token || $token != common_session_token()) {
96                 $this->clientError(_('There was a problem with your session token. '.
97                                      'Try again, please.'));
98                 return;
99             }
100
101             $this->saveNewNotice();
102         } else {
103             $this->showForm();
104         }
105     }
106
107     /**
108      * Save a new notice, based on arguments
109      *
110      * If successful, will show the notice, or return an Ajax-y result.
111      * If not, it will show an error message -- possibly Ajax-y.
112      *
113      * Also, if the notice input looks like a command, it will run the
114      * command and show the results -- again, possibly ajaxy.
115      *
116      * @return void
117      */
118
119     function saveNewNotice()
120     {
121         $user = common_current_user();
122         assert($user); // XXX: maybe an error instead...
123         $content = $this->trimmed('status_textarea');
124
125         if (!$content) {
126             $this->showForm(_('No content!'));
127             return;
128         } else {
129             $content_shortened = common_shorten_links($content);
130
131             if (mb_strlen($content_shortened) > 140) {
132                 $this->showForm(_('That\'s too long. '.
133                                   'Max notice size is 140 chars.'));
134                 return;
135             }
136         }
137
138         $inter = new CommandInterpreter();
139
140         $cmd = $inter->handle_command($user, $content_shortened);
141
142         if ($cmd) {
143             if ($this->boolean('ajax')) {
144                 $cmd->execute(new AjaxWebChannel($this));
145             } else {
146                 $cmd->execute(new WebChannel($this));
147             }
148             return;
149         }
150
151         $replyto = $this->trimmed('inreplyto');
152
153         $notice = Notice::saveNew($user->id, $content, 'web', 1,
154                                   ($replyto == 'false') ? null : $replyto);
155
156         if (is_string($notice)) {
157             $this->showForm($notice);
158             return;
159         }
160
161         common_broadcast_notice($notice);
162
163         if ($this->boolean('ajax')) {
164             $this->startHTML('text/xml;charset=utf-8');
165             $this->elementStart('head');
166             $this->element('title', null, _('Notice posted'));
167             $this->elementEnd('head');
168             $this->elementStart('body');
169             $this->showNotice($notice);
170             $this->elementEnd('body');
171             $this->elementEnd('html');
172         } else {
173             $returnto = $this->trimmed('returnto');
174
175             if ($returnto) {
176                 $url = common_local_url($returnto,
177                                         array('nickname' => $user->nickname));
178             } else {
179                 $url = common_local_url('shownotice',
180                                         array('notice' => $notice->id));
181             }
182             common_redirect($url, 303);
183         }
184     }
185
186     /**
187      * Show an Ajax-y error message
188      *
189      * Goes back to the browser, where it's shown in a popup.
190      *
191      * @param string $msg Message to show
192      *
193      * @return void
194      */
195
196     function ajaxErrorMsg($msg)
197     {
198         $this->startHTML('text/xml;charset=utf-8', true);
199         $this->elementStart('head');
200         $this->element('title', null, _('Ajax Error'));
201         $this->elementEnd('head');
202         $this->elementStart('body');
203         $this->element('p', array('id' => 'error'), $msg);
204         $this->elementEnd('body');
205         $this->elementEnd('html');
206     }
207
208     /**
209      * Formerly page output
210      *
211      * This used to be the whole page output; now that's been largely
212      * subsumed by showPage. So this just stores an error message, if
213      * it was passed, and calls showPage.
214      *
215      * Note that since we started doing Ajax output, this page is rarely
216      * seen.
217      *
218      * @param string $msg An error message, if any
219      *
220      * @return void
221      */
222
223     function showForm($msg=null)
224     {
225         if ($msg && $this->boolean('ajax')) {
226             $this->ajaxErrorMsg($msg);
227             return;
228         }
229
230         $this->msg = $msg;
231         $this->showPage();
232     }
233
234     /**
235      * Overload for replies or bad results
236      *
237      * We show content in the notice form if there were replies or results.
238      *
239      * @return void
240      */
241
242     function showNoticeForm()
243     {
244         $content = $this->trimmed('status_textarea');
245         if (!$content) {
246             $replyto = $this->trimmed('replyto');
247             $profile = Profile::staticGet('nickname', $replyto);
248             if ($profile) {
249                 $content = '@' . $profile->nickname . ' ';
250             }
251         }
252
253         $notice_form = new NoticeForm($this, $content);
254         $notice_form->show();
255     }
256
257     /**
258      * Show an error message
259      *
260      * Shows an error message if there is one.
261      *
262      * @return void
263      *
264      * @todo maybe show some instructions?
265      */
266
267     function showPageNotice()
268     {
269         if ($this->msg) {
270             $this->element('p', array('id' => 'error'), $this->msg);
271         }
272     }
273
274     /**
275      * Output a notice
276      *
277      * Used to generate the notice code for Ajax results.
278      *
279      * @param Notice $notice Notice that was saved
280      *
281      * @return void
282      */
283
284     function showNotice($notice)
285     {
286         $nli = new NoticeListItem($notice, $this);
287         $nli->show();
288     }
289 }