]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
Merge remote branch 'gitorious/1.0.x' 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
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         return _('New notice');
70     }
71
72     /**
73      * Handle input, produce output
74      *
75      * Switches based on GET or POST method. On GET, shows a form
76      * for posting a notice. On POST, saves the results of that form.
77      *
78      * Results may be a full page, or just a single notice list item,
79      * depending on whether AJAX was requested.
80      *
81      * @param array $args $_REQUEST contents
82      *
83      * @return void
84      */
85     function handle($args)
86     {
87         if (!common_logged_in()) {
88             $this->clientError(_('Not logged in.'));
89         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
90             // check for this before token since all POST and FILES data
91             // is losts when size is exceeded
92             if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
93                 // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
94                 // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
95                 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
96                           'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
97                           intval($_SERVER['CONTENT_LENGTH']));
98                 $this->clientError(sprintf($msg,$_SERVER['CONTENT_LENGTH']));
99             }
100             parent::handle($args);
101
102             // CSRF protection
103             $token = $this->trimmed('token');
104             if (!$token || $token != common_session_token()) {
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
131     function saveNewNotice()
132     {
133         $user = common_current_user();
134         assert($user); // XXX: maybe an error instead...
135         $content = $this->trimmed('status_textarea');
136         $options = array();
137         Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
138
139         if (!$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 = common_shorten_links($content);
158         if (Notice::contentTooLong($content_shortened)) {
159             $this->clientError(sprintf(_('That\'s too long. '.
160                                          'Max notice size is %d chars.'),
161                                        Notice::maxContent()));
162         }
163
164         $replyto = intval($this->trimmed('inreplyto'));
165         if ($replyto) {
166             $options['reply_to'] = $replyto;
167         }
168
169         $upload = null;
170         $upload = MediaFile::fromUpload('attach');
171
172         if (isset($upload)) {
173
174             if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
175                 $content_shortened .= ' ' . $upload->shortUrl();
176             }
177             Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
178
179             if (Notice::contentTooLong($content_shortened)) {
180                 $upload->delete();
181                 $this->clientError(
182                     sprintf(
183                         _('Max notice size is %d chars, including attachment URL.'),
184                           Notice::maxContent()
185                     )
186                 );
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             $this->element('title', null, _('Notice posted'));
230             $this->elementEnd('head');
231             $this->elementStart('body');
232             $this->showNotice($notice);
233             $this->elementEnd('body');
234             $this->elementEnd('html');
235         } else {
236             $returnto = $this->trimmed('returnto');
237
238             if ($returnto) {
239                 $url = common_local_url($returnto,
240                                         array('nickname' => $user->nickname));
241             } else {
242                 $url = common_local_url('shownotice',
243                                         array('notice' => $notice->id));
244             }
245             common_redirect($url, 303);
246         }
247     }
248
249     /**
250      * Show an Ajax-y error message
251      *
252      * Goes back to the browser, where it's shown in a popup.
253      *
254      * @param string $msg Message to show
255      *
256      * @return void
257      */
258
259     function ajaxErrorMsg($msg)
260     {
261         $this->startHTML('text/xml;charset=utf-8', true);
262         $this->elementStart('head');
263         $this->element('title', null, _('Ajax Error'));
264         $this->elementEnd('head');
265         $this->elementStart('body');
266         $this->element('p', array('id' => 'error'), $msg);
267         $this->elementEnd('body');
268         $this->elementEnd('html');
269     }
270
271     /**
272      * Formerly page output
273      *
274      * This used to be the whole page output; now that's been largely
275      * subsumed by showPage. So this just stores an error message, if
276      * it was passed, and calls showPage.
277      *
278      * Note that since we started doing Ajax output, this page is rarely
279      * seen.
280      *
281      * @param string $msg An error message, if any
282      *
283      * @return void
284      */
285
286     function showForm($msg=null)
287     {
288         if ($msg && $this->boolean('ajax')) {
289             $this->ajaxErrorMsg($msg);
290             return;
291         }
292
293         $this->msg = $msg;
294         $this->showPage();
295     }
296
297     /**
298      * Overload for replies or bad results
299      *
300      * We show content in the notice form if there were replies or results.
301      *
302      * @return void
303      */
304
305     function showNoticeForm()
306     {
307         $content = $this->trimmed('status_textarea');
308         if (!$content) {
309             $replyto = $this->trimmed('replyto');
310             $inreplyto = $this->trimmed('inreplyto');
311             $profile = Profile::staticGet('nickname', $replyto);
312             if ($profile) {
313                 $content = '@' . $profile->nickname . ' ';
314             }
315         } else {
316             // @fixme most of these bits above aren't being passed on above
317             $inreplyto = null;
318         }
319
320         $notice_form = new NoticeForm($this, '', $content, null, $inreplyto);
321         $notice_form->show();
322     }
323
324     /**
325      * Show an error message
326      *
327      * Shows an error message if there is one.
328      *
329      * @return void
330      *
331      * @todo maybe show some instructions?
332      */
333
334     function showPageNotice()
335     {
336         if ($this->msg) {
337             $this->element('p', array('id' => 'error'), $this->msg);
338         }
339     }
340
341     /**
342      * Output a notice
343      *
344      * Used to generate the notice code for Ajax results.
345      *
346      * @param Notice $notice Notice that was saved
347      *
348      * @return void
349      */
350
351     function showNotice($notice)
352     {
353         $nli = new NoticeListItem($notice, $this);
354         $nli->show();
355     }
356 }