]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
needLogin renamed checkLogin and made a property
[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  * @copyright 2013 Free Software Foundation, Inc.
29  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30  * @link      http://status.net/
31  */
32
33 if (!defined('STATUSNET')) {
34     exit(1);
35 }
36
37 require_once INSTALLDIR . '/lib/noticelist.php';
38 require_once INSTALLDIR . '/lib/mediafile.php';
39
40 /**
41  * Action for posting new notices
42  *
43  * @category Personal
44  * @package  StatusNet
45  * @author   Evan Prodromou <evan@status.net>
46  * @author   Zach Copley <zach@status.net>
47  * @author   Sarven Capadisli <csarven@status.net>
48  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49  * @link     http://status.net/
50  */
51 class NewnoticeAction extends FormAction
52 {
53     /**
54      * Title of the page
55      *
56      * Note that this usually doesn't get called unless something went wrong
57      *
58      * @return string page title
59      */
60     function title()
61     {
62         // TRANS: Page title for sending a new notice.
63         return _m('TITLE','New notice');
64     }
65
66     /**
67      * This handlePost saves a new notice, based on arguments
68      *
69      * If successful, will show the notice, or return an Ajax-y result.
70      * If not, it will show an error message -- possibly Ajax-y.
71      *
72      * Also, if the notice input looks like a command, it will run the
73      * command and show the results -- again, possibly ajaxy.
74      *
75      * @return void
76      */
77     protected function handlePost()
78     {
79         parent::handlePost();
80
81         assert($this->scoped); // XXX: maybe an error instead...
82         $user = $this->scoped->getUser();
83         $content = $this->trimmed('status_textarea');
84         $options = array();
85         Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
86
87         if (!$content) {
88             // TRANS: Client error displayed trying to send a notice without content.
89             $this->clientError(_('No content!'));
90         }
91
92         $inter = new CommandInterpreter();
93
94         $cmd = $inter->handle_command($user, $content);
95
96         if ($cmd) {
97             if (StatusNet::isAjax()) {
98                 $cmd->execute(new AjaxWebChannel($this));
99             } else {
100                 $cmd->execute(new WebChannel($this));
101             }
102             return;
103         }
104
105         $content_shortened = $user->shortenLinks($content);
106         if (Notice::contentTooLong($content_shortened)) {
107             // TRANS: Client error displayed when the parameter "status" is missing.
108             // TRANS: %d is the maximum number of character for a notice.
109             $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.',
110                                           'That\'s too long. Maximum notice size is %d characters.',
111                                           Notice::maxContent()),
112                                        Notice::maxContent()));
113         }
114
115         $replyto = intval($this->trimmed('inreplyto'));
116         if ($replyto) {
117             $options['reply_to'] = $replyto;
118         }
119
120         $upload = null;
121         $upload = MediaFile::fromUpload('attach');
122
123         if (isset($upload)) {
124
125             if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
126                 $content_shortened .= ' ' . $upload->shortUrl();
127             }
128             Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
129
130             if (Notice::contentTooLong($content_shortened)) {
131                 $upload->delete();
132                 // TRANS: Client error displayed exceeding the maximum notice length.
133                 // TRANS: %d is the maximum length for a notice.
134                 $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.',
135                                               'Maximum notice size is %d characters, including attachment URL.',
136                                               Notice::maxContent()),
137                                            Notice::maxContent()));
138             }
139         }
140
141         if ($user->shareLocation()) {
142             // use browser data if checked; otherwise profile data
143             if ($this->arg('notice_data-geo')) {
144                 $locOptions = Notice::locationOptions($this->trimmed('lat'),
145                                                       $this->trimmed('lon'),
146                                                       $this->trimmed('location_id'),
147                                                       $this->trimmed('location_ns'),
148                                                       $user->getProfile());
149             } else {
150                 $locOptions = Notice::locationOptions(null,
151                                                       null,
152                                                       null,
153                                                       null,
154                                                       $user->getProfile());
155             }
156
157             $options = array_merge($options, $locOptions);
158         }
159
160         $author_id = $user->id;
161         $text      = $content_shortened;
162
163         // Does the heavy-lifting for getting "To:" information
164
165         ToSelector::fillOptions($this, $options);
166
167         if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
168
169             $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options);
170
171             if (isset($upload)) {
172                 $upload->attachToNotice($notice);
173             }
174
175             Event::handle('EndNoticeSaveWeb', array($this, $notice));
176         }
177         Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
178
179         if (StatusNet::isAjax()) {
180             header('Content-Type: text/xml;charset=utf-8');
181             $this->xw->startDocument('1.0', 'UTF-8');
182             $this->elementStart('html');
183             $this->elementStart('head');
184             // TRANS: Page title after sending a notice.
185             $this->element('title', null, _('Notice posted'));
186             $this->elementEnd('head');
187             $this->elementStart('body');
188             $this->showNotice($notice);
189             $this->elementEnd('body');
190             $this->elementEnd('html');
191             exit;
192         } else {
193             $returnto = $this->trimmed('returnto');
194
195             if ($returnto) {
196                 $url = common_local_url($returnto,
197                                         array('nickname' => $user->nickname));
198             } else {
199                 $url = common_local_url('shownotice',
200                                         array('notice' => $notice->id));
201             }
202             common_redirect($url, 303);
203         }
204     }
205
206     /**
207      * Show an Ajax-y error message
208      *
209      * Goes back to the browser, where it's shown in a popup.
210      *
211      * @param string $msg Message to show
212      *
213      * @return void
214      */
215     function ajaxErrorMsg($msg)
216     {
217         $this->startHTML('text/xml;charset=utf-8', true);
218         $this->elementStart('head');
219         // TRANS: Page title after an AJAX error occurs on the send notice page.
220         $this->element('title', null, _('Ajax Error'));
221         $this->elementEnd('head');
222         $this->elementStart('body');
223         $this->element('p', array('id' => 'error'), $msg);
224         $this->elementEnd('body');
225         $this->elementEnd('html');
226     }
227
228     /**
229      * Show an Ajax-y notice form
230      *
231      * Goes back to the browser, where it's shown in a popup.
232      *
233      * @param string $msg Message to show
234      *
235      * @return void
236      */
237     function ajaxShowForm()
238     {
239         $this->startHTML('text/xml;charset=utf-8', true);
240         $this->elementStart('head');
241         // TRANS: Title for form to send a new notice.
242         $this->element('title', null, _m('TITLE','New notice'));
243         $this->elementEnd('head');
244         $this->elementStart('body');
245
246         $form = new NoticeForm($this);
247         $form->show();
248
249         $this->elementEnd('body');
250         $this->elementEnd('html');
251     }
252
253     /**
254      * Formerly page output
255      *
256      * This used to be the whole page output; now that's been largely
257      * subsumed by showPage. So this just stores an error message, if
258      * it was passed, and calls showPage.
259      *
260      * Note that since we started doing Ajax output, this page is rarely
261      * seen.
262      *
263      * @param string  $msg     An error/info message, if any
264      * @param boolean $success false for error indication, true for info
265      *
266      * @return void
267      */
268     function showForm($msg=null, $success=false)
269     {
270         if (StatusNet::isAjax()) {
271             if ($msg) {
272                 $this->ajaxErrorMsg($msg);
273             } else {
274                 $this->ajaxShowForm();
275             }
276             return;
277         }
278
279         parent::showForm($msg, $success);
280     }
281
282     /**
283      * // XXX: Should we be showing the notice form with microapps here?
284      *
285      * Overload for replies or bad results
286      *
287      * We show content in the notice form if there were replies or results.
288      *
289      * @return void
290      */
291     function showNoticeForm()
292     {
293         $content = $this->trimmed('status_textarea');
294         if (!$content) {
295             $replyto = $this->trimmed('replyto');
296             $inreplyto = $this->trimmed('inreplyto');
297             $profile = Profile::getKV('nickname', $replyto);
298             if ($profile) {
299                 $content = '@' . $profile->nickname . ' ';
300             }
301         } else {
302             // @fixme most of these bits above aren't being passed on above
303             $inreplyto = null;
304         }
305
306         $this->elementStart('div', 'input_forms');
307         $this->elementStart(
308             'div',
309             array(
310                 'id'    => 'input_form_status',
311                 'class' => 'input_form current nonav'
312             )
313         );
314
315         $notice_form = new NoticeForm(
316             $this,
317             array(
318                 'content' => $content,
319                 'inreplyto' => $inreplyto
320             )
321         );
322
323         $notice_form->show();
324
325         $this->elementEnd('div');
326         $this->elementEnd('div');
327     }
328
329     /**
330      * Show an error message
331      *
332      * Shows an error message if there is one.
333      *
334      * @return void
335      *
336      * @todo maybe show some instructions?
337      */
338     function showPageNotice()
339     {
340         if ($this->msg) {
341             $this->element('p', array('id' => 'error'), $this->msg);
342         }
343     }
344
345     /**
346      * Output a notice
347      *
348      * Used to generate the notice code for Ajax results.
349      *
350      * @param Notice $notice Notice that was saved
351      *
352      * @return void
353      */
354     function showNotice($notice)
355     {
356         $nli = new NoticeListItem($notice, $this);
357         $nli->show();
358     }
359 }