]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
* translator documntation updated
[quix0rs-gnu-social.git] / actions / newnotice.php
1 <?php
2 /**
3  * StatusNet, 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   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @author    Sarven Capadisli <csarven@status.net>
27  * @copyright 2008-2009 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
33     exit(1);
34 }
35
36 require_once INSTALLDIR . '/lib/noticelist.php';
37 require_once INSTALLDIR . '/lib/mediafile.php';
38
39 /**
40  * Action for posting new notices
41  *
42  * @category Personal
43  * @package  StatusNet
44  * @author   Evan Prodromou <evan@status.net>
45  * @author   Zach Copley <zach@status.net>
46  * @author   Sarven Capadisli <csarven@status.net>
47  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48  * @link     http://status.net/
49  */
50
51 class NewnoticeAction extends Action
52 {
53     /**
54      * Error message, if any
55      */
56
57     var $msg = null;
58
59     /**
60      * Title of the page
61      *
62      * Note that this usually doesn't get called unless something went wrong
63      *
64      * @return string page title
65      */
66
67     function title()
68     {
69         // TRANS: Page title for sending a new notice.
70         return _('New notice');
71     }
72
73     /**
74      * Handle input, produce output
75      *
76      * Switches based on GET or POST method. On GET, shows a form
77      * for posting a notice. On POST, saves the results of that form.
78      *
79      * Results may be a full page, or just a single notice list item,
80      * depending on whether AJAX was requested.
81      *
82      * @param array $args $_REQUEST contents
83      *
84      * @return void
85      */
86     function handle($args)
87     {
88         if (!common_logged_in()) {
89             // TRANS: Client error displayed trying to send a notice while not logged in.
90             $this->clientError(_('Not logged in.'));
91         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
92             // check for this before token since all POST and FILES data
93             // is losts when size is exceeded
94             if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
95                 // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
96                 // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
97                 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
98                           'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
99                           intval($_SERVER['CONTENT_LENGTH']));
100                 $this->clientError(sprintf($msg,$_SERVER['CONTENT_LENGTH']));
101             }
102             parent::handle($args);
103
104             // CSRF protection
105             $token = $this->trimmed('token');
106             if (!$token || $token != common_session_token()) {
107                 $this->clientError(_('There was a problem with your session token. '.
108                                      'Try again, please.'));
109             }
110             try {
111                 $this->saveNewNotice();
112             } catch (Exception $e) {
113                 $this->showForm($e->getMessage());
114                 return;
115             }
116         } else {
117             $this->showForm();
118         }
119     }
120
121     /**
122      * Save a new notice, based on arguments
123      *
124      * If successful, will show the notice, or return an Ajax-y result.
125      * If not, it will show an error message -- possibly Ajax-y.
126      *
127      * Also, if the notice input looks like a command, it will run the
128      * command and show the results -- again, possibly ajaxy.
129      *
130      * @return void
131      */
132
133     function saveNewNotice()
134     {
135         $user = common_current_user();
136         assert($user); // XXX: maybe an error instead...
137         $content = $this->trimmed('status_textarea');
138         $options = array();
139         Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
140
141         if (!$content) {
142             // TRANS: Client error displayed trying to send a notice without content.
143             $this->clientError(_('No content!'));
144             return;
145         }
146
147         $inter = new CommandInterpreter();
148
149         $cmd = $inter->handle_command($user, $content);
150
151         if ($cmd) {
152             if ($this->boolean('ajax')) {
153                 $cmd->execute(new AjaxWebChannel($this));
154             } else {
155                 $cmd->execute(new WebChannel($this));
156             }
157             return;
158         }
159
160         $content_shortened = $user->shortenLinks($content);
161         if (Notice::contentTooLong($content_shortened)) {
162             // TRANS: Client error displayed when the parameter "status" is missing.
163             // TRANS: %d is the maximum number of character for a notice.
164             $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.',
165                                           'That\'s too long. Maximum notice size is %d characters.',
166                                           Notice::maxContent()),
167                                        Notice::maxContent()));
168         }
169
170         $replyto = intval($this->trimmed('inreplyto'));
171         if ($replyto) {
172             $options['reply_to'] = $replyto;
173         }
174
175         $upload = null;
176         $upload = MediaFile::fromUpload('attach');
177
178         if (isset($upload)) {
179
180             if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
181                 $content_shortened .= ' ' . $upload->shortUrl();
182             }
183             Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
184
185             if (Notice::contentTooLong($content_shortened)) {
186                 $upload->delete();
187                 $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.',
188                                               'Maximum notice size is %d characters, including attachment URL.',
189                                               Notice::maxContent()),
190                                            Notice::maxContent()));
191             }
192         }
193
194         if ($user->shareLocation()) {
195             // use browser data if checked; otherwise profile data
196             if ($this->arg('notice_data-geo')) {
197                 $locOptions = Notice::locationOptions($this->trimmed('lat'),
198                                                       $this->trimmed('lon'),
199                                                       $this->trimmed('location_id'),
200                                                       $this->trimmed('location_ns'),
201                                                       $user->getProfile());
202             } else {
203                 $locOptions = Notice::locationOptions(null,
204                                                       null,
205                                                       null,
206                                                       null,
207                                                       $user->getProfile());
208             }
209
210             $options = array_merge($options, $locOptions);
211         }
212
213         $author_id = $user->id;
214         $text      = $content_shortened;
215
216         if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
217
218             $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options);
219
220             if (isset($upload)) {
221                 $upload->attachToNotice($notice);
222             }
223
224             Event::handle('EndNoticeSaveWeb', array($this, $notice));
225         }
226         Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
227
228         if ($this->boolean('ajax')) {
229             header('Content-Type: text/xml;charset=utf-8');
230             $this->xw->startDocument('1.0', 'UTF-8');
231             $this->elementStart('html');
232             $this->elementStart('head');
233             // TRANS: Page title after sending a notice.
234             $this->element('title', null, _('Notice posted'));
235             $this->elementEnd('head');
236             $this->elementStart('body');
237             $this->showNotice($notice);
238             $this->elementEnd('body');
239             $this->elementEnd('html');
240         } else {
241             $returnto = $this->trimmed('returnto');
242
243             if ($returnto) {
244                 $url = common_local_url($returnto,
245                                         array('nickname' => $user->nickname));
246             } else {
247                 $url = common_local_url('shownotice',
248                                         array('notice' => $notice->id));
249             }
250             common_redirect($url, 303);
251         }
252     }
253
254     /**
255      * Show an Ajax-y error message
256      *
257      * Goes back to the browser, where it's shown in a popup.
258      *
259      * @param string $msg Message to show
260      *
261      * @return void
262      */
263
264     function ajaxErrorMsg($msg)
265     {
266         $this->startHTML('text/xml;charset=utf-8', true);
267         $this->elementStart('head');
268         // TRANS: Page title after an AJAX error occurs on the send notice page.
269         $this->element('title', null, _('Ajax Error'));
270         $this->elementEnd('head');
271         $this->elementStart('body');
272         $this->element('p', array('id' => 'error'), $msg);
273         $this->elementEnd('body');
274         $this->elementEnd('html');
275     }
276
277     /**
278      * Show an Ajax-y notice form
279      *
280      * Goes back to the browser, where it's shown in a popup.
281      *
282      * @param string $msg Message to show
283      *
284      * @return void
285      */
286
287     function ajaxShowForm()
288     {
289         $this->startHTML('text/xml;charset=utf-8', true);
290         $this->elementStart('head');
291         $this->element('title', null, _('New notice'));
292         $this->elementEnd('head');
293         $this->elementStart('body');
294
295         $form = new NoticeForm($this);
296         $form->show();
297
298         $this->elementEnd('body');
299         $this->elementEnd('html');
300     }
301
302     /**
303      * Formerly page output
304      *
305      * This used to be the whole page output; now that's been largely
306      * subsumed by showPage. So this just stores an error message, if
307      * it was passed, and calls showPage.
308      *
309      * Note that since we started doing Ajax output, this page is rarely
310      * seen.
311      *
312      * @param string $msg An error message, if any
313      *
314      * @return void
315      */
316
317     function showForm($msg=null)
318     {
319         if ($this->boolean('ajax')) {
320             if ($msg) {
321                 $this->ajaxErrorMsg($msg);
322             } else {
323                 $this->ajaxShowForm();
324             }
325             return;
326         }
327
328         $this->msg = $msg;
329         $this->showPage();
330     }
331
332     /**
333      * Overload for replies or bad results
334      *
335      * We show content in the notice form if there were replies or results.
336      *
337      * @return void
338      */
339
340     function showNoticeForm()
341     {
342         $content = $this->trimmed('status_textarea');
343         if (!$content) {
344             $replyto = $this->trimmed('replyto');
345             $inreplyto = $this->trimmed('inreplyto');
346             $profile = Profile::staticGet('nickname', $replyto);
347             if ($profile) {
348                 $content = '@' . $profile->nickname . ' ';
349             }
350         } else {
351             // @fixme most of these bits above aren't being passed on above
352             $inreplyto = null;
353         }
354
355         $notice_form = new NoticeForm($this, '', $content, null, $inreplyto);
356         $notice_form->show();
357     }
358
359     /**
360      * Show an error message
361      *
362      * Shows an error message if there is one.
363      *
364      * @return void
365      *
366      * @todo maybe show some instructions?
367      */
368
369     function showPageNotice()
370     {
371         if ($this->msg) {
372             $this->element('p', array('id' => 'error'), $this->msg);
373         }
374     }
375
376     /**
377      * Output a notice
378      *
379      * Used to generate the notice code for Ajax results.
380      *
381      * @param Notice $notice Notice that was saved
382      *
383      * @return void
384      */
385
386     function showNotice($notice)
387     {
388         $nli = new NoticeListItem($notice, $this);
389         $nli->show();
390     }
391 }