]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
Remove js that crept back in, added another error string.
[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         if (!common_logged_in()) {
88             $this->clientError(_('Not logged in.'));
89         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
90             // check for this before token since all POST and FILES data
91             // is losts when size is exceeded
92             if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
93                 $this->clientError(sprintf(_('The server was unable to handle ' .
94                     'that much POST data (%s bytes) due to its current configuration.'),
95                     $_SERVER['CONTENT_LENGTH']));
96             }
97             parent::handle($args);
98
99             // CSRF protection
100             $token = $this->trimmed('token');
101             if (!$token || $token != common_session_token()) {
102                 $this->clientError(_('There was a problem with your session token. '.
103                                      'Try again, please.'));
104             }
105             try {
106                 $this->saveNewNotice();
107             } catch (Exception $e) {
108                 $this->showForm($e->getMessage());
109                 return;
110             }
111         } else {
112             $this->showForm();
113         }
114     }
115
116     function getUploadedFileType() {
117         require_once 'MIME/Type.php';
118
119         $filetype = MIME_Type::autoDetect($_FILES['attach']['tmp_name']);
120         if (in_array($filetype, common_config('attachments', 'supported'))) {
121             return $filetype;
122         }
123         $media = MIME_Type::getMedia($filetype);
124         if ('application' !== $media) {
125             $hint = sprintf(_(' Try using another %s format.'), $media);
126         } else {
127             $hint = '';
128         }
129         $this->clientError(sprintf(
130             _('%s is not a supported filetype on this server.'), $filetype) . $hint);
131     }
132
133     function isRespectsQuota($user) {
134         $file = new File;
135         $ret = $file->isRespectsQuota($user);
136         if (true === $ret) return true;
137         $this->clientError($ret);
138     }
139
140     /**
141      * Save a new notice, based on arguments
142      *
143      * If successful, will show the notice, or return an Ajax-y result.
144      * If not, it will show an error message -- possibly Ajax-y.
145      *
146      * Also, if the notice input looks like a command, it will run the
147      * command and show the results -- again, possibly ajaxy.
148      *
149      * @return void
150      */
151
152     function saveNewNotice()
153     {
154         $user = common_current_user();
155         assert($user); // XXX: maybe an error instead...
156         $content = $this->trimmed('status_textarea');
157
158         if (!$content) {
159             $this->clientError(_('No content!'));
160         } else {
161             $content_shortened = common_shorten_links($content);
162             if (mb_strlen($content_shortened) > 140) {
163                 $this->clientError(_('That\'s too long. '.
164                                      'Max notice size is 140 chars.'));
165             }
166         }
167
168         $inter = new CommandInterpreter();
169
170         $cmd = $inter->handle_command($user, $content_shortened);
171
172         if ($cmd) {
173             if ($this->boolean('ajax')) {
174                 $cmd->execute(new AjaxWebChannel($this));
175             } else {
176                 $cmd->execute(new WebChannel($this));
177             }
178             return;
179         }
180
181         $replyto = $this->trimmed('inreplyto');
182         #If an ID of 0 is wrongly passed here, it will cause a database error,
183         #so override it...
184         if ($replyto == 0) {
185             $replyto = 'false';
186         }
187
188         if (isset($_FILES['attach']['error'])) {
189             switch ($_FILES['attach']['error']) {
190                 case UPLOAD_ERR_NO_FILE:
191                     // no file uploaded, nothing to do
192                     break;
193
194                 case UPLOAD_ERR_OK:
195                     $mimetype = $this->getUploadedFileType();
196                     if (!$this->isRespectsQuota($user)) {
197                         die('clientError() should trigger an exception before reaching here.');
198                     }
199                     break;
200
201                 case UPLOAD_ERR_INI_SIZE:
202                     $this->clientError(_('The uploaded file exceeds the upload_max_filesize directive in php.ini.'));
203
204                 case UPLOAD_ERR_FORM_SIZE:
205                     $this->clientError(_('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.'));
206
207                 case UPLOAD_ERR_PARTIAL:
208                     $this->clientError(_('The uploaded file was only partially uploaded.'));
209
210                 case  UPLOAD_ERR_NO_TMP_DIR:
211                     $this->clientError(_('Missing a temporary folder.'));
212
213                 case UPLOAD_ERR_CANT_WRITE:
214                     $this->clientError(_('Failed to write file to disk.'));
215
216                 case UPLOAD_ERR_EXTENSION:
217                     $this->clientError(_('File upload stopped by extension.'));
218
219                 default:
220                     die('Should never reach here.');
221             }
222         }
223
224         $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1,
225                                   ($replyto == 'false') ? null : $replyto);
226
227         if (is_string($notice)) {
228             $this->clientError($notice);
229         }
230
231         if (isset($mimetype)) {
232             $this->storeFile($notice, $mimetype);
233         }
234         $this->saveUrls($notice);
235         common_broadcast_notice($notice);
236
237         if ($this->boolean('ajax')) {
238             $this->startHTML('text/xml;charset=utf-8');
239             $this->elementStart('head');
240             $this->element('title', null, _('Notice posted'));
241             $this->elementEnd('head');
242             $this->elementStart('body');
243             $this->showNotice($notice);
244             $this->elementEnd('body');
245             $this->elementEnd('html');
246         } else {
247             $returnto = $this->trimmed('returnto');
248
249             if ($returnto) {
250                 $url = common_local_url($returnto,
251                                         array('nickname' => $user->nickname));
252             } else {
253                 $url = common_local_url('shownotice',
254                                         array('notice' => $notice->id));
255             }
256             common_redirect($url, 303);
257         }
258     }
259
260     function storeFile($notice, $mimetype) {
261         $filename = basename($_FILES['attach']['name']);
262         $destination = "file/{$notice->id}-$filename";
263         if (move_uploaded_file($_FILES['attach']['tmp_name'], INSTALLDIR . "/$destination")) {
264             $file = new File;
265             $file->url = common_local_url('file', array('notice' => $notice->id));
266             $file->size = filesize(INSTALLDIR . "/$destination");
267             $file->date = time();
268             $file->mimetype = $mimetype;
269             if ($file_id = $file->insert()) {
270                 $file_redir = new File_redirection;
271                 $file_redir->url = common_path($destination);
272                 $file_redir->file_id = $file_id;
273                 $file_redir->insert();
274
275                 $f2p = new File_to_post;
276                 $f2p->file_id = $file_id; 
277                 $f2p->post_id = $notice->id; 
278                 $f2p->insert();
279             } else {
280                 $this->clientError(_('There was a database error while saving your file. Please try again.'));
281             }
282         } else {
283             $this->clientError(_('File could not be moved to destination directory.'));
284         }
285     }
286
287     /** save all urls in the notice to the db
288      *
289      * follow redirects and save all available file information
290      * (mimetype, date, size, oembed, etc.)
291      *
292      * @param class $notice Notice to pull URLs from
293      *
294      * @return void
295      */
296     function saveUrls($notice, $uploaded = null) {
297         common_replace_urls_callback($notice->content, array($this, 'saveUrl'), $notice->id);
298     }
299
300     function saveUrl($data) {
301         list($url, $notice_id) = $data;
302         $zzz = File::processNew($url, $notice_id);
303     }
304
305     /**
306      * Show an Ajax-y error message
307      *
308      * Goes back to the browser, where it's shown in a popup.
309      *
310      * @param string $msg Message to show
311      *
312      * @return void
313      */
314
315     function ajaxErrorMsg($msg)
316     {
317         $this->startHTML('text/xml;charset=utf-8', true);
318         $this->elementStart('head');
319         $this->element('title', null, _('Ajax Error'));
320         $this->elementEnd('head');
321         $this->elementStart('body');
322         $this->element('p', array('id' => 'error'), $msg);
323         $this->elementEnd('body');
324         $this->elementEnd('html');
325     }
326
327     /**
328      * Formerly page output
329      *
330      * This used to be the whole page output; now that's been largely
331      * subsumed by showPage. So this just stores an error message, if
332      * it was passed, and calls showPage.
333      *
334      * Note that since we started doing Ajax output, this page is rarely
335      * seen.
336      *
337      * @param string $msg An error message, if any
338      *
339      * @return void
340      */
341
342     function showForm($msg=null)
343     {
344         if ($msg && $this->boolean('ajax')) {
345             $this->ajaxErrorMsg($msg);
346             return;
347         }
348
349         $this->msg = $msg;
350         $this->showPage();
351     }
352
353     /**
354      * Overload for replies or bad results
355      *
356      * We show content in the notice form if there were replies or results.
357      *
358      * @return void
359      */
360
361     function showNoticeForm()
362     {
363         $content = $this->trimmed('status_textarea');
364         if (!$content) {
365             $replyto = $this->trimmed('replyto');
366             $profile = Profile::staticGet('nickname', $replyto);
367             if ($profile) {
368                 $content = '@' . $profile->nickname . ' ';
369             }
370         }
371
372         $notice_form = new NoticeForm($this, '', $content);
373         $notice_form->show();
374     }
375
376     /**
377      * Show an error message
378      *
379      * Shows an error message if there is one.
380      *
381      * @return void
382      *
383      * @todo maybe show some instructions?
384      */
385
386     function showPageNotice()
387     {
388         if ($this->msg) {
389             $this->element('p', array('id' => 'error'), $this->msg);
390         }
391     }
392
393     /**
394      * Output a notice
395      *
396      * Used to generate the notice code for Ajax results.
397      *
398      * @param Notice $notice Notice that was saved
399      *
400      * @return void
401      */
402
403     function showNotice($notice)
404     {
405         $nli = new NoticeListItem($notice, $this);
406         $nli->show();
407     }
408 }
409