From f8dae2bbc9600a4ad474e924dc540491e29e2767 Mon Sep 17 00:00:00 2001 From: Robin Millette Date: Sun, 31 May 2009 21:03:55 -0400 Subject: Refactored some attachment code and fixed upload bug in interface. --- actions/newnotice.php | 38 ++++++-------------------------------- classes/File.php | 26 ++++++++++++++++++++++++++ lib/noticeform.php | 2 -- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/actions/newnotice.php b/actions/newnotice.php index 29b748dd1..a24d62684 100644 --- a/actions/newnotice.php +++ b/actions/newnotice.php @@ -131,37 +131,10 @@ class NewnoticeAction extends Action } function isRespectsQuota($user) { - if ($_FILES['attach']['size'] > common_config('attachments', 'file_quota')) { - $this->clientError(sprintf(_('No file may be larger than %d bytes ' . - 'and the file you sent was %d bytes. Try to upload a smaller version.'), - common_config('attachments', 'file_quota'), $_FILES['attach']['size'])); - } - - $query = "select sum(size) as total from file join file_to_post on file_to_post.file_id = file.id join notice on file_to_post.post_id = notice.id where profile_id = {$user->id} and file.url like '%/notice/%/file'"; $file = new File; - $file->query($query); - $file->fetch(); - $total = $file->total + $_FILES['attach']['size']; - if ($total > common_config('attachments', 'user_quota')) { - $this->clientError(sprintf(_('A file this large would exceed your user quota of %d bytes.'), common_config('attachments', 'user_quota'))); - } - - $query .= ' month(modified) = month(now()) and year(modified) = year(now())'; - $file2 = new File; - $file2->query($query); - $file2->fetch(); - $total2 = $file2->total + $_FILES['attach']['size']; - if ($total2 > common_config('attachments', 'monthly_quota')) { - $this->clientError(sprintf(_('A file this large would exceed your monthly quota of %d bytes.'), common_config('attachments', 'monthly_quota'))); - } - return true; - } - - function isValidFileAttached($user) { - return isset($_FILES['attach']['error']) - && ($_FILES['attach']['error'] === UPLOAD_ERR_OK) - && $this->isSupportedFileType() - && $this->isRespectsQuota($user); + $ret = $file->isRespectsQuota($user); + if (true === $ret) return true; + $this->clientError($ret); } /** @@ -212,6 +185,7 @@ class NewnoticeAction extends Action $replyto = 'false'; } + if (isset($_FILES['attach']['error'])) { switch ($_FILES['attach']['error']) { case UPLOAD_ERR_NO_FILE: // no file uploaded @@ -223,8 +197,7 @@ class NewnoticeAction extends Action // lets check if we really support its format // and it doesn't go over quotas - - if (!$this->isValidFileAttached($user)) { + if (!$this->isSupportedFileType() || !$this->isRespectsQuota($user)) { die('clientError() should trigger an exception before reaching here.'); } break; @@ -250,6 +223,7 @@ class NewnoticeAction extends Action default: die('Should never reach here.'); } + } $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1, ($replyto == 'false') ? null : $replyto); diff --git a/classes/File.php b/classes/File.php index e5913115b..24ab11b8e 100644 --- a/classes/File.php +++ b/classes/File.php @@ -120,4 +120,30 @@ class File extends Memcached_DataObject File_to_post::processNew($file_id, $notice_id); return $x; } + + function isRespectsQuota($user) { + if ($_FILES['attach']['size'] > common_config('attachments', 'file_quota')) { + return sprintf(_('No file may be larger than %d bytes ' . + 'and the file you sent was %d bytes. Try to upload a smaller version.'), + common_config('attachments', 'file_quota'), $_FILES['attach']['size']); + } + + $query = "select sum(size) as total from file join file_to_post on file_to_post.file_id = file.id join notice on file_to_post.post_id = notice.id where profile_id = {$user->id} and file.url like '%/notice/%/file'"; + $this->query($query); + $this->fetch(); + $total = $this->total + $_FILES['attach']['size']; + if ($total > common_config('attachments', 'user_quota')) { + return sprintf(_('A file this large would exceed your user quota of %d bytes.'), common_config('attachments', 'user_quota')); + } + + $query .= ' month(modified) = month(now()) and year(modified) = year(now())'; + $this->query($query); + $this->fetch(); + $total = $this->total + $_FILES['attach']['size']; + if ($total > common_config('attachments', 'monthly_quota')) { + return sprintf(_('A file this large would exceed your monthly quota of %d bytes.'), common_config('attachments', 'monthly_quota')); + } + return true; + } } + diff --git a/lib/noticeform.php b/lib/noticeform.php index 5f438327d..7c88bd7b1 100644 --- a/lib/noticeform.php +++ b/lib/noticeform.php @@ -156,9 +156,7 @@ class NoticeForm extends Form $this->out->hidden('notice_return-to', $this->action, 'returnto'); } $this->out->hidden('notice_in-reply-to', $this->action, 'inreplyto'); - $this->out->hidden('MAX_FILE_SIZE', common_config('attachments', 'file_quota')); - } /** -- cgit v1.2.3-54-g00ecf