3 * StatusNet, the distributed open-source microblogging tool
5 * Handler for posting new notices
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.
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.
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/>.
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/
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
36 require_once INSTALLDIR.'/lib/noticelist.php';
39 * Action for posting new notices
43 * @author Evan Prodromou <evan@status.net>
44 * @author Zach Copley <zach@status.net>
45 * @author Sarven Capadisli <csarven@status.net>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
50 class NewnoticeAction extends Action
53 * Error message, if any
61 * Note that this usually doesn't get called unless something went wrong
63 * @return string page title
68 return _('New notice');
72 * Handle input, produce output
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.
77 * Results may be a full page, or just a single notice list item,
78 * depending on whether AJAX was requested.
80 * @param array $args $_REQUEST contents
85 function handle($args)
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']));
97 parent::handle($args);
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.'));
106 $this->saveNewNotice();
107 } catch (Exception $e) {
108 $this->showForm($e->getMessage());
116 function getUploadedFileType() {
117 require_once 'MIME/Type.php';
119 $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd');
120 $cmd = common_config('attachments', 'filecommand');
122 $filetype = MIME_Type::autoDetect($_FILES['attach']['tmp_name']);
123 if (in_array($filetype, common_config('attachments', 'supported'))) {
126 $media = MIME_Type::getMedia($filetype);
127 if ('application' !== $media) {
128 $hint = sprintf(_(' Try using another %s format.'), $media);
132 $this->clientError(sprintf(
133 _('%s is not a supported filetype on this server.'), $filetype) . $hint);
136 function isRespectsQuota($user) {
138 $ret = $file->isRespectsQuota($user,$_FILES['attach']['size']);
139 if (true === $ret) return true;
140 $this->clientError($ret);
144 * Save a new notice, based on arguments
146 * If successful, will show the notice, or return an Ajax-y result.
147 * If not, it will show an error message -- possibly Ajax-y.
149 * Also, if the notice input looks like a command, it will run the
150 * command and show the results -- again, possibly ajaxy.
155 function saveNewNotice()
157 $user = common_current_user();
158 assert($user); // XXX: maybe an error instead...
159 $content = $this->trimmed('status_textarea');
162 $this->clientError(_('No content!'));
164 $content_shortened = common_shorten_links($content);
165 if (Notice::contentTooLong($content_shortened)) {
166 $this->clientError(sprintf(_('That\'s too long. '.
167 'Max notice size is %d chars.'),
168 Notice::maxContent()));
172 $inter = new CommandInterpreter();
174 $cmd = $inter->handle_command($user, $content_shortened);
177 if ($this->boolean('ajax')) {
178 $cmd->execute(new AjaxWebChannel($this));
180 $cmd->execute(new WebChannel($this));
185 $replyto = $this->trimmed('inreplyto');
186 #If an ID of 0 is wrongly passed here, it will cause a database error,
192 if (isset($_FILES['attach']['error'])) {
193 switch ($_FILES['attach']['error']) {
194 case UPLOAD_ERR_NO_FILE:
195 // no file uploaded, nothing to do
199 $mimetype = $this->getUploadedFileType();
200 if (!$this->isRespectsQuota($user)) {
201 die('clientError() should trigger an exception before reaching here.');
205 case UPLOAD_ERR_INI_SIZE:
206 $this->clientError(_('The uploaded file exceeds the upload_max_filesize directive in php.ini.'));
208 case UPLOAD_ERR_FORM_SIZE:
209 $this->clientError(_('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.'));
211 case UPLOAD_ERR_PARTIAL:
212 $this->clientError(_('The uploaded file was only partially uploaded.'));
214 case UPLOAD_ERR_NO_TMP_DIR:
215 $this->clientError(_('Missing a temporary folder.'));
217 case UPLOAD_ERR_CANT_WRITE:
218 $this->clientError(_('Failed to write file to disk.'));
220 case UPLOAD_ERR_EXTENSION:
221 $this->clientError(_('File upload stopped by extension.'));
224 die('Should never reach here.');
228 if (isset($mimetype)) {
229 $filename = $this->saveFile($mimetype);
230 if (empty($filename)) {
231 $this->clientError(_('Couldn\'t save file.'));
234 $fileRecord = $this->storeFile($filename, $mimetype);
236 $fileurl = common_local_url('attachment',
237 array('attachment' => $fileRecord->id));
239 // not sure this is necessary -- Zach
240 $this->maybeAddRedir($fileRecord->id, $fileurl);
242 $short_fileurl = common_shorten_url($fileurl);
243 $content_shortened .= ' ' . $short_fileurl;
245 if (Notice::contentTooLong($content_shortened)) {
246 $this->deleteFile($filename);
247 $this->clientError(sprintf(_('Max notice size is %d chars, including attachment URL.'),
248 Notice::maxContent()));
251 // Also, not sure this is necessary -- Zach
252 $this->maybeAddRedir($fileRecord->id, $short_fileurl);
255 $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1,
256 ($replyto == 'false') ? null : $replyto);
258 if (is_string($notice)) {
259 if (isset($filename)) {
260 $this->deleteFile($filename);
262 $this->clientError($notice);
265 if (isset($mimetype)) {
266 $this->attachFile($notice, $fileRecord);
269 common_broadcast_notice($notice);
271 if ($this->boolean('ajax')) {
272 $this->startHTML('text/xml;charset=utf-8');
273 $this->elementStart('head');
274 $this->element('title', null, _('Notice posted'));
275 $this->elementEnd('head');
276 $this->elementStart('body');
277 $this->showNotice($notice);
278 $this->elementEnd('body');
279 $this->elementEnd('html');
281 $returnto = $this->trimmed('returnto');
284 $url = common_local_url($returnto,
285 array('nickname' => $user->nickname));
287 $url = common_local_url('shownotice',
288 array('notice' => $notice->id));
290 common_redirect($url, 303);
294 function saveFile($mimetype) {
296 $cur = common_current_user();
299 $this->serverError(_('Somehow lost the login in saveFile'));
302 $basename = basename($_FILES['attach']['name']);
304 $filename = File::filename($cur->getProfile(), $basename, $mimetype);
306 $filepath = File::path($filename);
308 if (move_uploaded_file($_FILES['attach']['tmp_name'], $filepath)) {
311 $this->clientError(_('File could not be moved to destination directory.'));
315 function deleteFile($filename)
317 $filepath = File::path($filename);
321 function storeFile($filename, $mimetype) {
324 $file->filename = $filename;
326 $file->url = File::url($filename);
328 $filepath = File::path($filename);
330 $file->size = filesize($filepath);
331 $file->date = time();
332 $file->mimetype = $mimetype;
334 $file_id = $file->insert();
337 common_log_db_error($file, "INSERT", __FILE__);
338 $this->clientError(_('There was a database error while saving your file. Please try again.'));
344 function rememberFile($file, $short)
346 $this->maybeAddRedir($file->id, $short);
349 function maybeAddRedir($file_id, $url)
351 $file_redir = File_redirection::staticGet('url', $url);
353 if (empty($file_redir)) {
354 $file_redir = new File_redirection;
355 $file_redir->url = $url;
356 $file_redir->file_id = $file_id;
358 $result = $file_redir->insert();
361 common_log_db_error($file_redir, "INSERT", __FILE__);
362 $this->clientError(_('There was a database error while saving your file. Please try again.'));
367 function attachFile($notice, $filerec)
369 File_to_post::processNew($filerec->id, $notice->id);
371 $this->maybeAddRedir($filerec->id,
372 common_local_url('file', array('notice' => $notice->id)));
376 * Show an Ajax-y error message
378 * Goes back to the browser, where it's shown in a popup.
380 * @param string $msg Message to show
385 function ajaxErrorMsg($msg)
387 $this->startHTML('text/xml;charset=utf-8', true);
388 $this->elementStart('head');
389 $this->element('title', null, _('Ajax Error'));
390 $this->elementEnd('head');
391 $this->elementStart('body');
392 $this->element('p', array('id' => 'error'), $msg);
393 $this->elementEnd('body');
394 $this->elementEnd('html');
398 * Formerly page output
400 * This used to be the whole page output; now that's been largely
401 * subsumed by showPage. So this just stores an error message, if
402 * it was passed, and calls showPage.
404 * Note that since we started doing Ajax output, this page is rarely
407 * @param string $msg An error message, if any
412 function showForm($msg=null)
414 if ($msg && $this->boolean('ajax')) {
415 $this->ajaxErrorMsg($msg);
424 * Overload for replies or bad results
426 * We show content in the notice form if there were replies or results.
431 function showNoticeForm()
433 $content = $this->trimmed('status_textarea');
435 $replyto = $this->trimmed('replyto');
436 $inreplyto = $this->trimmed('inreplyto');
437 $profile = Profile::staticGet('nickname', $replyto);
439 $content = '@' . $profile->nickname . ' ';
443 $notice_form = new NoticeForm($this, '', $content, null, $inreplyto);
444 $notice_form->show();
448 * Show an error message
450 * Shows an error message if there is one.
454 * @todo maybe show some instructions?
457 function showPageNotice()
460 $this->element('p', array('id' => 'error'), $this->msg);
467 * Used to generate the notice code for Ajax results.
469 * @param Notice $notice Notice that was saved
474 function showNotice($notice)
476 $nli = new NoticeListItem($notice, $this);