3 * Laconica, 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@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/
32 if (!defined('LACONICA')) {
36 require_once INSTALLDIR.'/lib/noticelist.php';
39 * Action for posting new notices
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/
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 isSupportedFileType() {
117 require_once 'MIME/Type.php';
119 $filetype = MIME_Type::autoDetect($_FILES['attach']['tmp_name']);
120 if (in_array($filetype, common_config('attachments', 'supported'))) {
123 $media = MIME_Type::getMedia($filetype);
124 if ('application' !== $media) {
125 $hint = sprintf(_(' Try using another %s format.'), $media);
129 $this->clientError(sprintf(
130 _('%s is not a supported filetype on this server.'), $filetype) . $hint);
133 function isRespectsQuota($user) {
135 $ret = $file->isRespectsQuota($user);
136 if (true === $ret) return true;
137 $this->clientError($ret);
141 * Save a new notice, based on arguments
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.
146 * Also, if the notice input looks like a command, it will run the
147 * command and show the results -- again, possibly ajaxy.
152 function saveNewNotice()
154 $user = common_current_user();
155 assert($user); // XXX: maybe an error instead...
156 $content = $this->trimmed('status_textarea');
159 $this->clientError(_('No content!'));
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.'));
168 $inter = new CommandInterpreter();
170 $cmd = $inter->handle_command($user, $content_shortened);
173 if ($this->boolean('ajax')) {
174 $cmd->execute(new AjaxWebChannel($this));
176 $cmd->execute(new WebChannel($this));
181 $replyto = $this->trimmed('inreplyto');
182 #If an ID of 0 is wrongly passed here, it will cause a database error,
188 if (isset($_FILES['attach']['error'])) {
189 switch ($_FILES['attach']['error']) {
190 case UPLOAD_ERR_NO_FILE:
196 // file was uploaded alright
197 // lets check if we really support its format
198 // and it doesn't go over quotas
200 if (!$this->isSupportedFileType() || !$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 $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1,
229 ($replyto == 'false') ? null : $replyto);
231 if (is_string($notice)) {
232 $this->clientError($notice);
235 $this->storeFile($notice);
236 $this->saveUrls($notice);
237 common_broadcast_notice($notice);
239 if ($this->boolean('ajax')) {
240 $this->startHTML('text/xml;charset=utf-8');
241 $this->elementStart('head');
242 $this->element('title', null, _('Notice posted'));
243 $this->elementEnd('head');
244 $this->elementStart('body');
245 $this->showNotice($notice);
246 $this->elementEnd('body');
247 $this->elementEnd('html');
249 $returnto = $this->trimmed('returnto');
252 $url = common_local_url($returnto,
253 array('nickname' => $user->nickname));
255 $url = common_local_url('shownotice',
256 array('notice' => $notice->id));
258 common_redirect($url, 303);
262 function storeFile($notice) {
263 if (UPLOAD_ERR_NO_FILE === $_FILES['attach']['error']) return;
264 $filename = basename($_FILES['attach']['name']);
265 $destination = "file/{$notice->id}-$filename";
266 if (move_uploaded_file($_FILES['attach']['tmp_name'], INSTALLDIR . "/$destination")) {
268 $file->url = common_local_url('file', array('notice' => $notice->id));
269 $file->size = filesize(INSTALLDIR . "/$destination");
270 $file->date = time();
271 $file->mimetype = $_FILES['attach']['type'];
272 if ($file_id = $file->insert()) {
273 $file_redir = new File_redirection;
274 $file_redir->url = common_path($destination);
275 $file_redir->file_id = $file_id;
276 $file_redir->insert();
278 $f2p = new File_to_post;
279 $f2p->file_id = $file_id;
280 $f2p->post_id = $notice->id;
283 $this->clientError(_('There was a database error while saving your file. Please try again.'));
289 /** save all urls in the notice to the db
291 * follow redirects and save all available file information
292 * (mimetype, date, size, oembed, etc.)
294 * @param class $notice Notice to pull URLs from
298 function saveUrls($notice, $uploaded = null) {
299 common_replace_urls_callback($notice->content, array($this, 'saveUrl'), $notice->id);
302 function saveUrl($data) {
303 list($url, $notice_id) = $data;
304 $zzz = File::processNew($url, $notice_id);
308 * Show an Ajax-y error message
310 * Goes back to the browser, where it's shown in a popup.
312 * @param string $msg Message to show
317 function ajaxErrorMsg($msg)
319 $this->startHTML('text/xml;charset=utf-8', true);
320 $this->elementStart('head');
321 $this->element('title', null, _('Ajax Error'));
322 $this->elementEnd('head');
323 $this->elementStart('body');
324 $this->element('p', array('id' => 'error'), $msg);
325 $this->elementEnd('body');
326 $this->elementEnd('html');
330 * Formerly page output
332 * This used to be the whole page output; now that's been largely
333 * subsumed by showPage. So this just stores an error message, if
334 * it was passed, and calls showPage.
336 * Note that since we started doing Ajax output, this page is rarely
339 * @param string $msg An error message, if any
344 function showForm($msg=null)
346 if ($msg && $this->boolean('ajax')) {
347 $this->ajaxErrorMsg($msg);
356 * Overload for replies or bad results
358 * We show content in the notice form if there were replies or results.
363 function showNoticeForm()
365 $content = $this->trimmed('status_textarea');
367 $replyto = $this->trimmed('replyto');
368 $profile = Profile::staticGet('nickname', $replyto);
370 $content = '@' . $profile->nickname . ' ';
374 $notice_form = new NoticeForm($this, '', $content);
375 $notice_form->show();
379 * Show an error message
381 * Shows an error message if there is one.
385 * @todo maybe show some instructions?
388 function showPageNotice()
391 $this->element('p', array('id' => 'error'), $this->msg);
398 * Used to generate the notice code for Ajax results.
400 * @param Notice $notice Notice that was saved
405 function showNotice($notice)
407 $nli = new NoticeListItem($notice, $this);