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