]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
8591522dc55e9c8f9476a42aab52a0cc3e672989
[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
135         if (!$content) {
136             $this->clientError(_('No content!'));
137             return;
138         }
139
140         $inter = new CommandInterpreter();
141
142         $cmd = $inter->handle_command($user, $content);
143
144         if ($cmd) {
145             if ($this->boolean('ajax')) {
146                 $cmd->execute(new AjaxWebChannel($this));
147             } else {
148                 $cmd->execute(new WebChannel($this));
149             }
150             return;
151         }
152
153         $content_shortened = common_shorten_links($content);
154         if (Notice::contentTooLong($content_shortened)) {
155             $this->clientError(sprintf(_('That\'s too long. '.
156                                          'Max notice size is %d chars.'),
157                                        Notice::maxContent()));
158         }
159
160         $replyto = $this->trimmed('inreplyto');
161         #If an ID of 0 is wrongly passed here, it will cause a database error,
162         #so override it...
163         if ($replyto == 0) {
164             $replyto = 'false';
165         }
166
167         $upload = null;
168         $upload = MediaFile::fromUpload('attach');
169
170         if (isset($upload)) {
171
172             $content_shortened .= ' ' . $upload->shortUrl();
173
174             if (Notice::contentTooLong($content_shortened)) {
175                 $upload->delete();
176                 $this->clientError(
177                     sprintf(
178                         _('Max notice size is %d chars, including attachment URL.'),
179                           Notice::maxContent()
180                     )
181                 );
182             }
183         }
184
185         $options = array('reply_to' => ($replyto == 'false') ? null : $replyto);
186
187         if ($user->shareLocation()) {
188
189             $lat = $this->trimmed('lat');
190             $lon = $this->trimmed('lon');
191             $location_id = $this->trimmed('location_id');
192             $location_ns = $this->trimmed('location_ns');
193
194             if (!empty($lat) && !empty($lon) && empty($location_id)) {
195                 $location = Location::fromLatLon($lat, $lon);
196                 if (!empty($location)) {
197                     $location_id = $location->location_id;
198                     $location_ns = $location->location_ns;
199                 }
200             }
201
202             $options['lat'] = $lat;
203             $options['lon'] = $lon;
204             $options['location_id'] = $location_id;
205             $options['location_ns'] = $location_ns;
206         }
207
208         $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options);
209
210         if (isset($upload)) {
211             $upload->attachToNotice($notice);
212         }
213
214         common_broadcast_notice($notice);
215
216         if ($this->boolean('ajax')) {
217             header('Content-Type: text/xml;charset=utf-8');
218             $this->xw->startDocument('1.0', 'UTF-8');
219             $this->elementStart('html');
220             $this->elementStart('head');
221             $this->element('title', null, _('Notice posted'));
222             $this->elementEnd('head');
223             $this->elementStart('body');
224             $this->showNotice($notice);
225             $this->elementEnd('body');
226             $this->elementEnd('html');
227         } else {
228             $returnto = $this->trimmed('returnto');
229
230             if ($returnto) {
231                 $url = common_local_url($returnto,
232                                         array('nickname' => $user->nickname));
233             } else {
234                 $url = common_local_url('shownotice',
235                                         array('notice' => $notice->id));
236             }
237             common_redirect($url, 303);
238         }
239     }
240
241     /**
242      * Show an Ajax-y error message
243      *
244      * Goes back to the browser, where it's shown in a popup.
245      *
246      * @param string $msg Message to show
247      *
248      * @return void
249      */
250
251     function ajaxErrorMsg($msg)
252     {
253         $this->startHTML('text/xml;charset=utf-8', true);
254         $this->elementStart('head');
255         $this->element('title', null, _('Ajax Error'));
256         $this->elementEnd('head');
257         $this->elementStart('body');
258         $this->element('p', array('id' => 'error'), $msg);
259         $this->elementEnd('body');
260         $this->elementEnd('html');
261     }
262
263     /**
264      * Formerly page output
265      *
266      * This used to be the whole page output; now that's been largely
267      * subsumed by showPage. So this just stores an error message, if
268      * it was passed, and calls showPage.
269      *
270      * Note that since we started doing Ajax output, this page is rarely
271      * seen.
272      *
273      * @param string $msg An error message, if any
274      *
275      * @return void
276      */
277
278     function showForm($msg=null)
279     {
280         if ($msg && $this->boolean('ajax')) {
281             $this->ajaxErrorMsg($msg);
282             return;
283         }
284
285         $this->msg = $msg;
286         $this->showPage();
287     }
288
289     /**
290      * Overload for replies or bad results
291      *
292      * We show content in the notice form if there were replies or results.
293      *
294      * @return void
295      */
296
297     function showNoticeForm()
298     {
299         $content = $this->trimmed('status_textarea');
300         if (!$content) {
301             $replyto = $this->trimmed('replyto');
302             $inreplyto = $this->trimmed('inreplyto');
303             $profile = Profile::staticGet('nickname', $replyto);
304             if ($profile) {
305                 $content = '@' . $profile->nickname . ' ';
306             }
307         }
308
309         $notice_form = new NoticeForm($this, '', $content, null, $inreplyto);
310         $notice_form->show();
311     }
312
313     /**
314      * Show an error message
315      *
316      * Shows an error message if there is one.
317      *
318      * @return void
319      *
320      * @todo maybe show some instructions?
321      */
322
323     function showPageNotice()
324     {
325         if ($this->msg) {
326             $this->element('p', array('id' => 'error'), $this->msg);
327         }
328     }
329
330     /**
331      * Output a notice
332      *
333      * Used to generate the notice code for Ajax results.
334      *
335      * @param Notice $notice Notice that was saved
336      *
337      * @return void
338      */
339
340     function showNotice($notice)
341     {
342         $nli = new NoticeListItem($notice, $this);
343         $nli->show();
344     }
345 }
346