]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
Let's you upload a file with a notice and have it shown with other attachments.
[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     function isFileAttached() {
113         return $_FILES['attach']['error'] === UPLOAD_ERR_OK;
114     }
115
116     /**
117      * Save a new notice, based on arguments
118      *
119      * If successful, will show the notice, or return an Ajax-y result.
120      * If not, it will show an error message -- possibly Ajax-y.
121      *
122      * Also, if the notice input looks like a command, it will run the
123      * command and show the results -- again, possibly ajaxy.
124      *
125      * @return void
126      */
127
128     function saveNewNotice()
129     {
130         $user = common_current_user();
131         assert($user); // XXX: maybe an error instead...
132         $content = $this->trimmed('status_textarea');
133
134         if (!$content) {
135             $this->clientError(_('No content!'));
136         } else {
137             $content_shortened = common_shorten_links($content);
138
139             if (mb_strlen($content_shortened) > 140) {
140                 $this->clientError(_('That\'s too long. '.
141                                      'Max notice size is 140 chars.'));
142             }
143         }
144
145         $inter = new CommandInterpreter();
146
147         $cmd = $inter->handle_command($user, $content_shortened);
148
149         if ($cmd) {
150             if ($this->boolean('ajax')) {
151                 $cmd->execute(new AjaxWebChannel($this));
152             } else {
153                 $cmd->execute(new WebChannel($this));
154             }
155             return;
156         }
157
158         $replyto = $this->trimmed('inreplyto');
159         #If an ID of 0 is wrongly passed here, it will cause a database error,
160         #so override it...
161         if ($replyto == 0) {
162             $replyto = 'false';
163         }
164
165         $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1,
166                                   ($replyto == 'false') ? null : $replyto);
167
168         if (is_string($notice)) {
169             $this->clientError($notice);
170             return;
171         }
172
173         if ($this->isFileAttached()) {
174             $this->storeFile($notice);
175         }
176         $this->saveUrls($notice);
177
178         common_broadcast_notice($notice);
179
180         if ($this->boolean('ajax')) {
181             $this->startHTML('text/xml;charset=utf-8');
182             $this->elementStart('head');
183             $this->element('title', null, _('Notice posted'));
184             $this->elementEnd('head');
185             $this->elementStart('body');
186             $this->showNotice($notice);
187             $this->elementEnd('body');
188             $this->elementEnd('html');
189         } else {
190             $returnto = $this->trimmed('returnto');
191
192             if ($returnto) {
193                 $url = common_local_url($returnto,
194                                         array('nickname' => $user->nickname));
195             } else {
196                 $url = common_local_url('shownotice',
197                                         array('notice' => $notice->id));
198             }
199             common_redirect($url, 303);
200         }
201     }
202
203     function storeFile($notice) {
204         $filename = basename($_FILES['attach']['name']);
205         $destination = "file/{$notice->id}-$filename";
206         if (move_uploaded_file($_FILES['attach']['tmp_name'], INSTALLDIR . "/$destination")) {
207             $file = new File;
208 //            $file->url = common_local_url('file', array('notice' => $notice->id));
209             $file->url = common_path($destination);
210             $file->size = filesize(INSTALLDIR . "/$destination");
211             $file->date = time();
212             $file->mimetype = $_FILES['attach']['type'];
213             if ($ok = $file->insert()) {
214                 $f2p = new File_to_post;
215                 $f2p->file_id = $ok; 
216                 $f2p->post_id = $notice->id; 
217                 $f2p->insert();
218             } else {
219                 die('inserting file, dying');
220             }
221         }
222 /*
223         $url = common_local_url('file', array('notice' => $notice->id));
224         echo "$destination<br />";
225         die($url);
226 */
227     }
228
229
230     /** save all urls in the notice to the db
231      *
232      * follow redirects and save all available file information
233      * (mimetype, date, size, oembed, etc.)
234      *
235      * @param class $notice Notice to pull URLs from
236      *
237      * @return void
238      */
239     function saveUrls($notice, $uploaded = null) {
240         common_replace_urls_callback($notice->content, array($this, 'saveUrl'), $notice->id);
241     }
242
243     function saveUrl($data) {
244         list($url, $notice_id) = $data;
245         $zzz = File::processNew($url, $notice_id);
246     }
247
248     /**
249      * Show an Ajax-y error message
250      *
251      * Goes back to the browser, where it's shown in a popup.
252      *
253      * @param string $msg Message to show
254      *
255      * @return void
256      */
257
258     function ajaxErrorMsg($msg)
259     {
260         $this->startHTML('text/xml;charset=utf-8', true);
261         $this->elementStart('head');
262         $this->element('title', null, _('Ajax Error'));
263         $this->elementEnd('head');
264         $this->elementStart('body');
265         $this->element('p', array('id' => 'error'), $msg);
266         $this->elementEnd('body');
267         $this->elementEnd('html');
268     }
269
270     /**
271      * Formerly page output
272      *
273      * This used to be the whole page output; now that's been largely
274      * subsumed by showPage. So this just stores an error message, if
275      * it was passed, and calls showPage.
276      *
277      * Note that since we started doing Ajax output, this page is rarely
278      * seen.
279      *
280      * @param string $msg An error message, if any
281      *
282      * @return void
283      */
284
285     function showForm($msg=null)
286     {
287         if ($msg && $this->boolean('ajax')) {
288             $this->ajaxErrorMsg($msg);
289             return;
290         }
291
292         $this->msg = $msg;
293         $this->showPage();
294     }
295
296     /**
297      * Overload for replies or bad results
298      *
299      * We show content in the notice form if there were replies or results.
300      *
301      * @return void
302      */
303
304     function showNoticeForm()
305     {
306         $content = $this->trimmed('status_textarea');
307         if (!$content) {
308             $replyto = $this->trimmed('replyto');
309             $profile = Profile::staticGet('nickname', $replyto);
310             if ($profile) {
311                 $content = '@' . $profile->nickname . ' ';
312             }
313         }
314
315         $notice_form = new NoticeForm($this, '', $content);
316         $notice_form->show();
317     }
318
319     /**
320      * Show an error message
321      *
322      * Shows an error message if there is one.
323      *
324      * @return void
325      *
326      * @todo maybe show some instructions?
327      */
328
329     function showPageNotice()
330     {
331         if ($this->msg) {
332             $this->element('p', array('id' => 'error'), $this->msg);
333         }
334     }
335
336     /**
337      * Output a notice
338      *
339      * Used to generate the notice code for Ajax results.
340      *
341      * @param Notice $notice Notice that was saved
342      *
343      * @return void
344      */
345
346     function showNotice($notice)
347     {
348         $nli = new NoticeListItem($notice, $this);
349         $nli->show();
350     }
351 }