]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
Merge branch 'tinymce' into 0.9.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
86     function handle($args)
87     {
88         if (!common_logged_in()) {
89             $this->clientError(_('Not logged in.'));
90         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
91             // check for this before token since all POST and FILES data
92             // is losts when size is exceeded
93             if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
94                 $this->clientError(sprintf(_('The server was unable to handle ' .
95                                              'that much POST data (%s bytes) due to its current configuration.'),
96                                            $_SERVER['CONTENT_LENGTH']));
97             }
98             parent::handle($args);
99
100             // CSRF protection
101             $token = $this->trimmed('token');
102             if (!$token || $token != common_session_token()) {
103                 $this->clientError(_('There was a problem with your session token. '.
104                                      'Try again, please.'));
105             }
106             try {
107                 $this->saveNewNotice();
108             } catch (Exception $e) {
109                 $this->showForm($e->getMessage());
110                 return;
111             }
112         } else {
113             $this->showForm();
114         }
115     }
116
117     /**
118      * Save a new notice, based on arguments
119      *
120      * If successful, will show the notice, or return an Ajax-y result.
121      * If not, it will show an error message -- possibly Ajax-y.
122      *
123      * Also, if the notice input looks like a command, it will run the
124      * command and show the results -- again, possibly ajaxy.
125      *
126      * @return void
127      */
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             $this->clientError(_('No content!'));
139             return;
140         }
141
142         $inter = new CommandInterpreter();
143
144         $cmd = $inter->handle_command($user, $content);
145
146         if ($cmd) {
147             if ($this->boolean('ajax')) {
148                 $cmd->execute(new AjaxWebChannel($this));
149             } else {
150                 $cmd->execute(new WebChannel($this));
151             }
152             return;
153         }
154
155         $content_shortened = common_shorten_links($content);
156         if (Notice::contentTooLong($content_shortened)) {
157             $this->clientError(sprintf(_('That\'s too long. '.
158                                          'Max notice size is %d chars.'),
159                                        Notice::maxContent()));
160         }
161
162         $replyto = intval($this->trimmed('inreplyto'));
163         if ($replyto) {
164             $options['replyto'] = $replyto;
165         }
166
167         $upload = null;
168         $upload = MediaFile::fromUpload('attach');
169
170         if (isset($upload)) {
171
172             if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
173                 $content_shortened .= ' ' . $upload->shortUrl();
174             }
175             Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
176
177             if (Notice::contentTooLong($content_shortened)) {
178                 $upload->delete();
179                 $this->clientError(
180                     sprintf(
181                         _('Max notice size is %d chars, including attachment URL.'),
182                           Notice::maxContent()
183                     )
184                 );
185             }
186         }
187
188         if ($user->shareLocation()) {
189             // use browser data if checked; otherwise profile data
190             if ($this->arg('notice_data-geo')) {
191                 $locOptions = Notice::locationOptions($this->trimmed('lat'),
192                                                       $this->trimmed('lon'),
193                                                       $this->trimmed('location_id'),
194                                                       $this->trimmed('location_ns'),
195                                                       $user->getProfile());
196             } else {
197                 $locOptions = Notice::locationOptions(null,
198                                                       null,
199                                                       null,
200                                                       null,
201                                                       $user->getProfile());
202             }
203
204             $options = array_merge($options, $locOptions);
205         }
206
207         $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options);
208
209         if (isset($upload)) {
210             $upload->attachToNotice($notice);
211         }
212         Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
213
214         if ($this->boolean('ajax')) {
215             header('Content-Type: text/xml;charset=utf-8');
216             $this->xw->startDocument('1.0', 'UTF-8');
217             $this->elementStart('html');
218             $this->elementStart('head');
219             $this->element('title', null, _('Notice posted'));
220             $this->elementEnd('head');
221             $this->elementStart('body');
222             $this->showNotice($notice);
223             $this->elementEnd('body');
224             $this->elementEnd('html');
225         } else {
226             $returnto = $this->trimmed('returnto');
227
228             if ($returnto) {
229                 $url = common_local_url($returnto,
230                                         array('nickname' => $user->nickname));
231             } else {
232                 $url = common_local_url('shownotice',
233                                         array('notice' => $notice->id));
234             }
235             common_redirect($url, 303);
236         }
237     }
238
239     /**
240      * Show an Ajax-y error message
241      *
242      * Goes back to the browser, where it's shown in a popup.
243      *
244      * @param string $msg Message to show
245      *
246      * @return void
247      */
248
249     function ajaxErrorMsg($msg)
250     {
251         $this->startHTML('text/xml;charset=utf-8', true);
252         $this->elementStart('head');
253         $this->element('title', null, _('Ajax Error'));
254         $this->elementEnd('head');
255         $this->elementStart('body');
256         $this->element('p', array('id' => 'error'), $msg);
257         $this->elementEnd('body');
258         $this->elementEnd('html');
259     }
260
261     /**
262      * Formerly page output
263      *
264      * This used to be the whole page output; now that's been largely
265      * subsumed by showPage. So this just stores an error message, if
266      * it was passed, and calls showPage.
267      *
268      * Note that since we started doing Ajax output, this page is rarely
269      * seen.
270      *
271      * @param string $msg An error message, if any
272      *
273      * @return void
274      */
275
276     function showForm($msg=null)
277     {
278         if ($msg && $this->boolean('ajax')) {
279             $this->ajaxErrorMsg($msg);
280             return;
281         }
282
283         $this->msg = $msg;
284         $this->showPage();
285     }
286
287     /**
288      * Overload for replies or bad results
289      *
290      * We show content in the notice form if there were replies or results.
291      *
292      * @return void
293      */
294
295     function showNoticeForm()
296     {
297         $content = $this->trimmed('status_textarea');
298         if (!$content) {
299             $replyto = $this->trimmed('replyto');
300             $inreplyto = $this->trimmed('inreplyto');
301             $profile = Profile::staticGet('nickname', $replyto);
302             if ($profile) {
303                 $content = '@' . $profile->nickname . ' ';
304             }
305         } else {
306             // @fixme most of these bits above aren't being passed on above
307             $inreplyto = null;
308         }
309
310         $notice_form = new NoticeForm($this, '', $content, null, $inreplyto);
311         $notice_form->show();
312     }
313
314     /**
315      * Show an error message
316      *
317      * Shows an error message if there is one.
318      *
319      * @return void
320      *
321      * @todo maybe show some instructions?
322      */
323
324     function showPageNotice()
325     {
326         if ($this->msg) {
327             $this->element('p', array('id' => 'error'), $this->msg);
328         }
329     }
330
331     /**
332      * Output a notice
333      *
334      * Used to generate the notice code for Ajax results.
335      *
336      * @param Notice $notice Notice that was saved
337      *
338      * @return void
339      */
340
341     function showNotice($notice)
342     {
343         $nli = new NoticeListItem($notice, $this);
344         $nli->show();
345     }
346 }
347