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