]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
db stuff for URLs: redirections, oembed, etc.
[quix0rs-gnu-social.git] / actions / newnotice.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @author    Zach Copley <zach@controlyourself.ca>
26  * @author    Sarven Capadisli <csarven@controlyourself.ca>
27  * @copyright 2008-2009 Control Yourself, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://laconi.ca/
30  */
31
32 if (!defined('LACONICA')) {
33     exit(1);
34 }
35
36 require_once INSTALLDIR.'/lib/noticelist.php';
37
38 /**
39  * Action for posting new notices
40  *
41  * @category Personal
42  * @package  Laconica
43  * @author   Evan Prodromou <evan@controlyourself.ca>
44  * @author   Zach Copley <zach@controlyourself.ca>
45  * @author   Sarven Capadisli <csarven@controlyourself.ca>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://laconi.ca/
48  */
49
50 class NewnoticeAction extends Action
51 {
52     /**
53      * Error message, if any
54      */
55
56     var $msg = null;
57
58     /**
59      * Title of the page
60      *
61      * Note that this usually doesn't get called unless something went wrong
62      *
63      * @return string page title
64      */
65
66     function title()
67     {
68         return _('New notice');
69     }
70
71     /**
72      * Handle input, produce output
73      *
74      * Switches based on GET or POST method. On GET, shows a form
75      * for posting a notice. On POST, saves the results of that form.
76      *
77      * Results may be a full page, or just a single notice list item,
78      * depending on whether AJAX was requested.
79      *
80      * @param array $args $_REQUEST contents
81      *
82      * @return void
83      */
84
85     function handle($args)
86     {
87         parent::handle($args);
88
89         if (!common_logged_in()) {
90             $this->clientError(_('Not logged in.'));
91         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
92
93             // CSRF protection
94             $token = $this->trimmed('token');
95             if (!$token || $token != common_session_token()) {
96                 $this->clientError(_('There was a problem with your session token. '.
97                                      'Try again, please.'));
98                 return;
99             }
100
101             try {
102                 $this->saveNewNotice();
103             } catch (Exception $e) {
104                 $this->showForm($e->getMessage());
105                 return;
106             }
107         } else {
108             $this->showForm();
109         }
110     }
111
112     /**
113      * Save a new notice, based on arguments
114      *
115      * If successful, will show the notice, or return an Ajax-y result.
116      * If not, it will show an error message -- possibly Ajax-y.
117      *
118      * Also, if the notice input looks like a command, it will run the
119      * command and show the results -- again, possibly ajaxy.
120      *
121      * @return void
122      */
123
124     function saveNewNotice()
125     {
126         $user = common_current_user();
127         assert($user); // XXX: maybe an error instead...
128         $content = $this->trimmed('status_textarea');
129
130         if (!$content) {
131             $this->clientError(_('No content!'));
132         } else {
133             $content_shortened = common_shorten_links($content);
134
135             if (mb_strlen($content_shortened) > 140) {
136                 $this->clientError(_('That\'s too long. '.
137                                      'Max notice size is 140 chars.'));
138             }
139         }
140
141         $inter = new CommandInterpreter();
142
143         $cmd = $inter->handle_command($user, $content_shortened);
144
145         if ($cmd) {
146             if ($this->boolean('ajax')) {
147                 $cmd->execute(new AjaxWebChannel($this));
148             } else {
149                 $cmd->execute(new WebChannel($this));
150             }
151             return;
152         }
153
154         $replyto = $this->trimmed('inreplyto');
155         #If an ID of 0 is wrongly passed here, it will cause a database error,
156         #so override it...
157         if ($replyto == 0) {
158             $replyto = 'false';
159         }
160
161         $notice = Notice::saveNew($user->id, $content, 'web', 1,
162                                   ($replyto == 'false') ? null : $replyto);
163
164         if (is_string($notice)) {
165             $this->clientError($notice);
166             return;
167         }
168
169         $this->saveUrls($notice);
170
171         common_broadcast_notice($notice);
172
173         if ($this->boolean('ajax')) {
174             $this->startHTML('text/xml;charset=utf-8');
175             $this->elementStart('head');
176             $this->element('title', null, _('Notice posted'));
177             $this->elementEnd('head');
178             $this->elementStart('body');
179             $this->showNotice($notice);
180             $this->elementEnd('body');
181             $this->elementEnd('html');
182         } else {
183             $returnto = $this->trimmed('returnto');
184
185             if ($returnto) {
186                 $url = common_local_url($returnto,
187                                         array('nickname' => $user->nickname));
188             } else {
189                 $url = common_local_url('shownotice',
190                                         array('notice' => $notice->id));
191             }
192             common_redirect($url, 303);
193         }
194     }
195
196     /** save all urls in the notice to the db
197      *
198      * follow redirects and save all available file information
199      * (mimetype, date, size, oembed, etc.)
200      *
201      * @param class $notice Notice to pull URLs from
202      *
203      * @return void
204      */
205     function saveUrls($notice) {
206         common_debug("Saving all URLs");
207         common_replace_urls_callback($notice->content, array($this, 'saveUrl'), $notice->id);
208     }
209
210     function saveUrl($data) {
211         list($url, $notice_id) = $data;
212         common_debug("Saving $url for $notice_id");
213         $file = File::staticGet('url', $url);
214         if (empty($file)) {
215             common_debug('unknown file/url');
216             $file = new File;
217             $file->url = $url;
218             $file->insert();
219         }
220         common_debug('File: ' . print_r($file, true));
221         $f2p = new File_to_post;
222         $f2p->file_id = $file->id;
223         $f2p->post_id = $notice_id;
224         $f2p->insert();
225     }
226
227     /**
228      * Show an Ajax-y error message
229      *
230      * Goes back to the browser, where it's shown in a popup.
231      *
232      * @param string $msg Message to show
233      *
234      * @return void
235      */
236
237     function ajaxErrorMsg($msg)
238     {
239         $this->startHTML('text/xml;charset=utf-8', true);
240         $this->elementStart('head');
241         $this->element('title', null, _('Ajax Error'));
242         $this->elementEnd('head');
243         $this->elementStart('body');
244         $this->element('p', array('id' => 'error'), $msg);
245         $this->elementEnd('body');
246         $this->elementEnd('html');
247     }
248
249     /**
250      * Formerly page output
251      *
252      * This used to be the whole page output; now that's been largely
253      * subsumed by showPage. So this just stores an error message, if
254      * it was passed, and calls showPage.
255      *
256      * Note that since we started doing Ajax output, this page is rarely
257      * seen.
258      *
259      * @param string $msg An error message, if any
260      *
261      * @return void
262      */
263
264     function showForm($msg=null)
265     {
266         if ($msg && $this->boolean('ajax')) {
267             $this->ajaxErrorMsg($msg);
268             return;
269         }
270
271         $this->msg = $msg;
272         $this->showPage();
273     }
274
275     /**
276      * Overload for replies or bad results
277      *
278      * We show content in the notice form if there were replies or results.
279      *
280      * @return void
281      */
282
283     function showNoticeForm()
284     {
285         $content = $this->trimmed('status_textarea');
286         if (!$content) {
287             $replyto = $this->trimmed('replyto');
288             $profile = Profile::staticGet('nickname', $replyto);
289             if ($profile) {
290                 $content = '@' . $profile->nickname . ' ';
291             }
292         }
293
294         $notice_form = new NoticeForm($this, '', $content);
295         $notice_form->show();
296     }
297
298     /**
299      * Show an error message
300      *
301      * Shows an error message if there is one.
302      *
303      * @return void
304      *
305      * @todo maybe show some instructions?
306      */
307
308     function showPageNotice()
309     {
310         if ($this->msg) {
311             $this->element('p', array('id' => 'error'), $this->msg);
312         }
313     }
314
315     /**
316      * Output a notice
317      *
318      * Used to generate the notice code for Ajax results.
319      *
320      * @param Notice $notice Notice that was saved
321      *
322      * @return void
323      */
324
325     function showNotice($notice)
326     {
327         $nli = new NoticeListItem($notice, $this);
328         $nli->show();
329     }
330 }