]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
Merge branch '3cl' into 1.0.x
[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 _('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: Client error displayed trying to send a notice while not logged in.
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                 $this->clientError(_('There was a problem with your session token. '.
105                                      'Try again, please.'));
106             }
107             try {
108                 $this->saveNewNotice();
109             } catch (Exception $e) {
110                 $this->showForm($e->getMessage());
111                 return;
112             }
113         } else {
114             $this->showForm();
115         }
116     }
117
118     /**
119      * Save a new notice, based on arguments
120      *
121      * If successful, will show the notice, or return an Ajax-y result.
122      * If not, it will show an error message -- possibly Ajax-y.
123      *
124      * Also, if the notice input looks like a command, it will run the
125      * command and show the results -- again, possibly ajaxy.
126      *
127      * @return void
128      */
129     function saveNewNotice()
130     {
131         $user = common_current_user();
132         assert($user); // XXX: maybe an error instead...
133         $content = $this->trimmed('status_textarea');
134         $options = array();
135         Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
136
137         if (!$content) {
138             // TRANS: Client error displayed trying to send a notice without content.
139             $this->clientError(_('No content!'));
140             return;
141         }
142
143         $inter = new CommandInterpreter();
144
145         $cmd = $inter->handle_command($user, $content);
146
147         if ($cmd) {
148             if ($this->boolean('ajax')) {
149                 $cmd->execute(new AjaxWebChannel($this));
150             } else {
151                 $cmd->execute(new WebChannel($this));
152             }
153             return;
154         }
155
156         $content_shortened = $user->shortenLinks($content);
157         if (Notice::contentTooLong($content_shortened)) {
158             // TRANS: Client error displayed when the parameter "status" is missing.
159             // TRANS: %d is the maximum number of character for a notice.
160             $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.',
161                                           'That\'s too long. Maximum notice size is %d characters.',
162                                           Notice::maxContent()),
163                                        Notice::maxContent()));
164         }
165
166         $replyto = intval($this->trimmed('inreplyto'));
167         if ($replyto) {
168             $options['reply_to'] = $replyto;
169         }
170
171         $upload = null;
172         $upload = MediaFile::fromUpload('attach');
173
174         if (isset($upload)) {
175
176             if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
177                 $content_shortened .= ' ' . $upload->shortUrl();
178             }
179             Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
180
181             if (Notice::contentTooLong($content_shortened)) {
182                 $upload->delete();
183                 $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.',
184                                               'Maximum notice size is %d characters, including attachment URL.',
185                                               Notice::maxContent()),
186                                            Notice::maxContent()));
187             }
188         }
189
190         if ($user->shareLocation()) {
191             // use browser data if checked; otherwise profile data
192             if ($this->arg('notice_data-geo')) {
193                 $locOptions = Notice::locationOptions($this->trimmed('lat'),
194                                                       $this->trimmed('lon'),
195                                                       $this->trimmed('location_id'),
196                                                       $this->trimmed('location_ns'),
197                                                       $user->getProfile());
198             } else {
199                 $locOptions = Notice::locationOptions(null,
200                                                       null,
201                                                       null,
202                                                       null,
203                                                       $user->getProfile());
204             }
205
206             $options = array_merge($options, $locOptions);
207         }
208
209         $author_id = $user->id;
210         $text      = $content_shortened;
211
212         if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
213
214             $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options);
215
216             if (isset($upload)) {
217                 $upload->attachToNotice($notice);
218             }
219
220             Event::handle('EndNoticeSaveWeb', array($this, $notice));
221         }
222         Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
223
224         if ($this->boolean('ajax')) {
225             header('Content-Type: text/xml;charset=utf-8');
226             $this->xw->startDocument('1.0', 'UTF-8');
227             $this->elementStart('html');
228             $this->elementStart('head');
229             // TRANS: Page title after sending a notice.
230             $this->element('title', null, _('Notice posted'));
231             $this->elementEnd('head');
232             $this->elementStart('body');
233             $this->showNotice($notice);
234             $this->elementEnd('body');
235             $this->elementEnd('html');
236         } else {
237             $returnto = $this->trimmed('returnto');
238
239             if ($returnto) {
240                 $url = common_local_url($returnto,
241                                         array('nickname' => $user->nickname));
242             } else {
243                 $url = common_local_url('shownotice',
244                                         array('notice' => $notice->id));
245             }
246             common_redirect($url, 303);
247         }
248     }
249
250     /**
251      * Show an Ajax-y error message
252      *
253      * Goes back to the browser, where it's shown in a popup.
254      *
255      * @param string $msg Message to show
256      *
257      * @return void
258      */
259     function ajaxErrorMsg($msg)
260     {
261         $this->startHTML('text/xml;charset=utf-8', true);
262         $this->elementStart('head');
263         // TRANS: Page title after an AJAX error occurs on the send notice page.
264         $this->element('title', null, _('Ajax Error'));
265         $this->elementEnd('head');
266         $this->elementStart('body');
267         $this->element('p', array('id' => 'error'), $msg);
268         $this->elementEnd('body');
269         $this->elementEnd('html');
270     }
271
272     /**
273      * Show an Ajax-y notice form
274      *
275      * Goes back to the browser, where it's shown in a popup.
276      *
277      * @param string $msg Message to show
278      *
279      * @return void
280      */
281     function ajaxShowForm()
282     {
283         $this->startHTML('text/xml;charset=utf-8', true);
284         $this->elementStart('head');
285         $this->element('title', null, _('New notice'));
286         $this->elementEnd('head');
287         $this->elementStart('body');
288
289         $form = new NoticeForm($this);
290         $form->show();
291
292         $this->elementEnd('body');
293         $this->elementEnd('html');
294     }
295
296     /**
297      * Formerly page output
298      *
299      * This used to be the whole page output; now that's been largely
300      * subsumed by showPage. So this just stores an error message, if
301      * it was passed, and calls showPage.
302      *
303      * Note that since we started doing Ajax output, this page is rarely
304      * seen.
305      *
306      * @param string $msg An error message, if any
307      *
308      * @return void
309      */
310     function showForm($msg=null)
311     {
312         if ($this->boolean('ajax')) {
313             if ($msg) {
314                 $this->ajaxErrorMsg($msg);
315             } else {
316                 $this->ajaxShowForm();
317             }
318             return;
319         }
320
321         $this->msg = $msg;
322         $this->showPage();
323     }
324
325     /**
326      * Overload for replies or bad results
327      *
328      * We show content in the notice form if there were replies or results.
329      *
330      * @return void
331      */
332     function showNoticeForm()
333     {
334         $content = $this->trimmed('status_textarea');
335         if (!$content) {
336             $replyto = $this->trimmed('replyto');
337             $inreplyto = $this->trimmed('inreplyto');
338             $profile = Profile::staticGet('nickname', $replyto);
339             if ($profile) {
340                 $content = '@' . $profile->nickname . ' ';
341             }
342         } else {
343             // @fixme most of these bits above aren't being passed on above
344             $inreplyto = null;
345         }
346
347         $notice_form = new NoticeForm($this, '', $content, null, $inreplyto);
348         $notice_form->show();
349     }
350
351     /**
352      * Show an error message
353      *
354      * Shows an error message if there is one.
355      *
356      * @return void
357      *
358      * @todo maybe show some instructions?
359      */
360     function showPageNotice()
361     {
362         if ($this->msg) {
363             $this->element('p', array('id' => 'error'), $this->msg);
364         }
365     }
366
367     /**
368      * Output a notice
369      *
370      * Used to generate the notice code for Ajax results.
371      *
372      * @param Notice $notice Notice that was saved
373      *
374      * @return void
375      */
376     function showNotice($notice)
377     {
378         $nli = new NoticeListItem($notice, $this);
379         $nli->show();
380     }
381 }