From 93a6e83d5ddb48ccae80ba9f5c62c63888724083 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Tue, 27 Oct 2009 19:11:18 -0700 Subject: [PATCH 1/4] Extract media upload stuff into its own library class. --- actions/newnotice.php | 198 ++++---------------------- lib/mediafile.php | 322 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 346 insertions(+), 174 deletions(-) create mode 100644 lib/mediafile.php diff --git a/actions/newnotice.php b/actions/newnotice.php index 9ee031f936..5100e79e10 100644 --- a/actions/newnotice.php +++ b/actions/newnotice.php @@ -33,7 +33,8 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } -require_once INSTALLDIR.'/lib/noticelist.php'; +require_once INSTALLDIR . '/lib/noticelist.php'; +require_once INSTALLDIR . '/lib/mediafile.php'; /** * Action for posting new notices @@ -113,33 +114,6 @@ class NewnoticeAction extends Action } } - function getUploadedFileType() { - require_once 'MIME/Type.php'; - - $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd'); - $cmd = common_config('attachments', 'filecommand'); - - $filetype = MIME_Type::autoDetect($_FILES['attach']['tmp_name']); - if (in_array($filetype, common_config('attachments', 'supported'))) { - return $filetype; - } - $media = MIME_Type::getMedia($filetype); - if ('application' !== $media) { - $hint = sprintf(_(' Try using another %s format.'), $media); - } else { - $hint = ''; - } - $this->clientError(sprintf( - _('%s is not a supported filetype on this server.'), $filetype) . $hint); - } - - function isRespectsQuota($user) { - $file = new File; - $ret = $file->isRespectsQuota($user,$_FILES['attach']['size']); - if (true === $ret) return true; - $this->clientError($ret); - } - /** * Save a new notice, based on arguments * @@ -189,78 +163,35 @@ class NewnoticeAction extends Action $replyto = 'false'; } - if (isset($_FILES['attach']['error'])) { - switch ($_FILES['attach']['error']) { - case UPLOAD_ERR_NO_FILE: - // no file uploaded, nothing to do - break; + $upload = null; - case UPLOAD_ERR_OK: - $mimetype = $this->getUploadedFileType(); - if (!$this->isRespectsQuota($user)) { - die('clientError() should trigger an exception before reaching here.'); + common_debug('looking for attachment'); + + $upload = MediaFile::fromUpload('attach'); + + common_debug("uploaded file = " . var_export($upload, true)); + + if (isset($upload)) { + common_debug('newNotice: found an upload'); + + $content_shortened .= ' ' . $upload->shortUrl(); + + common_debug('content w/upload = ' . $content_shortened); + + if (Notice::contentTooLong($content_shortened)) { + $upload->delete(); + $this->clientError(sprintf(_('Max notice size is %d chars, including attachment URL.'), + Notice::maxContent())); + } else { + common_debug('content not too long'); } - break; - - case UPLOAD_ERR_INI_SIZE: - $this->clientError(_('The uploaded file exceeds the upload_max_filesize directive in php.ini.')); - - case UPLOAD_ERR_FORM_SIZE: - $this->clientError(_('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.')); - - case UPLOAD_ERR_PARTIAL: - $this->clientError(_('The uploaded file was only partially uploaded.')); - - case UPLOAD_ERR_NO_TMP_DIR: - $this->clientError(_('Missing a temporary folder.')); - - case UPLOAD_ERR_CANT_WRITE: - $this->clientError(_('Failed to write file to disk.')); - - case UPLOAD_ERR_EXTENSION: - $this->clientError(_('File upload stopped by extension.')); - - default: - die('Should never reach here.'); - } - } - - if (isset($mimetype)) { - $filename = $this->saveFile($mimetype); - if (empty($filename)) { - $this->clientError(_('Couldn\'t save file.')); - } - - $fileRecord = $this->storeFile($filename, $mimetype); - - $fileurl = common_local_url('attachment', - array('attachment' => $fileRecord->id)); - - // not sure this is necessary -- Zach - $this->maybeAddRedir($fileRecord->id, $fileurl); - - $short_fileurl = common_shorten_url($fileurl); - if (!$short_fileurl) { - // todo -- Consider forcing default shortener if none selected? - $short_fileurl = $fileurl; - } - $content_shortened .= ' ' . $short_fileurl; - - if (Notice::contentTooLong($content_shortened)) { - $this->deleteFile($filename); - $this->clientError(sprintf(_('Max notice size is %d chars, including attachment URL.'), - Notice::maxContent())); - } - - // Also, not sure this is necessary -- Zach - $this->maybeAddRedir($fileRecord->id, $short_fileurl); } $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1, ($replyto == 'false') ? null : $replyto); - if (isset($mimetype)) { - $this->attachFile($notice, $fileRecord); + if (isset($upload)) { + $upload->attachToNotice($notice); } common_broadcast_notice($notice); @@ -288,87 +219,6 @@ class NewnoticeAction extends Action } } - function saveFile($mimetype) { - - $cur = common_current_user(); - - if (empty($cur)) { - $this->serverError(_('Somehow lost the login in saveFile')); - } - - $basename = basename($_FILES['attach']['name']); - - $filename = File::filename($cur->getProfile(), $basename, $mimetype); - - $filepath = File::path($filename); - - if (move_uploaded_file($_FILES['attach']['tmp_name'], $filepath)) { - return $filename; - } else { - $this->clientError(_('File could not be moved to destination directory.')); - } - } - - function deleteFile($filename) - { - $filepath = File::path($filename); - @unlink($filepath); - } - - function storeFile($filename, $mimetype) { - - $file = new File; - $file->filename = $filename; - - $file->url = File::url($filename); - - $filepath = File::path($filename); - - $file->size = filesize($filepath); - $file->date = time(); - $file->mimetype = $mimetype; - - $file_id = $file->insert(); - - if (!$file_id) { - common_log_db_error($file, "INSERT", __FILE__); - $this->clientError(_('There was a database error while saving your file. Please try again.')); - } - - return $file; - } - - function rememberFile($file, $short) - { - $this->maybeAddRedir($file->id, $short); - } - - function maybeAddRedir($file_id, $url) - { - $file_redir = File_redirection::staticGet('url', $url); - - if (empty($file_redir)) { - $file_redir = new File_redirection; - $file_redir->url = $url; - $file_redir->file_id = $file_id; - - $result = $file_redir->insert(); - - if (!$result) { - common_log_db_error($file_redir, "INSERT", __FILE__); - $this->clientError(_('There was a database error while saving your file. Please try again.')); - } - } - } - - function attachFile($notice, $filerec) - { - File_to_post::processNew($filerec->id, $notice->id); - - $this->maybeAddRedir($filerec->id, - common_local_url('file', array('notice' => $notice->id))); - } - /** * Show an Ajax-y error message * diff --git a/lib/mediafile.php b/lib/mediafile.php new file mode 100644 index 0000000000..be101cdcab --- /dev/null +++ b/lib/mediafile.php @@ -0,0 +1,322 @@ +. + * + * @category Media + * @package StatusNet + * @author Zach Copley + * @copyright 2008-2009 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET') && !defined('LACONICA')) { + exit(1); +} + +class MediaFile +{ + + var $filename = null; + var $fileRecord = null; + var $user = null; + var $fileurl = null; + var $short_fileurl = null; + var $mimetype = null; + + function __construct($user = null, $filename = null, $mimetype = null) + { + if ($user == null) { + $this->user = common_current_user(); + } + + common_debug('in MediaFile constructor'); + + $this->filename = $filename; + $this->mimetype = $mimetype; + + common_debug('storing file'); + $this->fileRecord = $this->storeFile(); + common_debug('finished storing file'); + + $this->fileurl = common_local_url('attachment', + array('attachment' => $this->fileRecord->id)); + + common_debug('$this->fileurl() = ' . $this->fileurl); + + // not sure this is necessary -- Zach + $this->maybeAddRedir($this->fileRecord->id, $this->fileurl); + + common_debug('shortening file url'); + $this->short_fileurl = common_shorten_url($this->fileurl); + common_debug('shortened file url = ' . $short_fileurl); + + // Also, not sure this is necessary -- Zach + $this->maybeAddRedir($this->fileRecord->id, $this->short_fileurl); + + common_debug("MediaFile: end of constructor"); + } + + function attachToNotice($notice) + { + common_debug('MediaFile::attachToNotice() -- doing File_to_post'); + File_to_post::processNew($this->fileRecord->id, $notice->id); + common_debug('MediaFile done doing File_to_post'); + + $this->maybeAddRedir($this->fileRecord->id, + common_local_url('file', array('notice' => $notice->id))); + } + + function shortUrl() + { + return $this->short_fileurl; + } + + function delete() + { + $filepath = File::path($this->filename); + @unlink($filepath); + } + + function storeFile() { + + $file = new File; + $file->filename = $this->filename; + + common_debug('storing ' . $this->filename); + + $file->url = File::url($this->filename); + common_debug('file->url = ' . $file->url); + + $filepath = File::path($this->filename); + common_debug('filepath = ' . $filepath); + + $file->size = filesize($filepath); + $file->date = time(); + $file->mimetype = $this->mimetype; + + $file_id = $file->insert(); + + if (!$file_id) { + + common_debug("storeFile: problem inserting new file"); + common_log_db_error($file, "INSERT", __FILE__); + throw new ClientException(_('There was a database error while saving your file. Please try again.')); + } + + common_debug('finished storing file'); + + return $file; + } + + function rememberFile($file, $short) + { + $this->maybeAddRedir($file->id, $short); + } + + function maybeAddRedir($file_id, $url) + { + + common_debug("maybeAddRedir: looking up url: $url for file id $file_id"); + + $file_redir = File_redirection::staticGet('url', $url); + + if (empty($file_redir)) { + + common_debug("maybeAddRedir: $url is not in the db"); + + $file_redir = new File_redirection; + $file_redir->url = $url; + $file_redir->file_id = $file_id; + + $result = $file_redir->insert(); + + if (!$result) { + common_log_db_error($file_redir, "INSERT", __FILE__); + throw new ClientException(_('There was a database error while saving your file. Please try again.')); + } + } else { + + common_debug("maybeAddRedir: no need to add $url, it's already in the db"); + } + } + + static function fromUpload($param = 'media') + { + common_debug("fromUpload: param = $param"); + + if (!isset($_FILES[$param]['error'])){ + common_debug('no file found'); + return; + } + + switch ($_FILES[$param]['error']) { + case UPLOAD_ERR_OK: // success, jump out + break; + case UPLOAD_ERR_INI_SIZE: + throw new ClientException(_('The uploaded file exceeds the ' . + 'upload_max_filesize directive in php.ini.')); + return; + case UPLOAD_ERR_FORM_SIZE: + throw new ClientException( + _('The uploaded file exceeds the MAX_FILE_SIZE directive' . + ' that was specified in the HTML form.')); + return; + case UPLOAD_ERR_PARTIAL: + @unlink($_FILES[$param]['tmp_name']); + throw new ClientException(_('The uploaded file was only' . + ' partially uploaded.')); + return; + case UPLOAD_ERR_NO_TMP_DIR: + throw new ClientException(_('Missing a temporary folder.')); + return; + case UPLOAD_ERR_CANT_WRITE: + throw new ClientException(_('Failed to write file to disk.')); + return; + case UPLOAD_ERR_EXTENSION: + throw new ClientException(_('File upload stopped by extension.')); + return; + default: + throw new ClientException(_('System error uploading file.')); + return; + } + + $user = common_current_user(); + + if (!MediaFile::respectsQuota($user, $_FILES['attach']['size'])) { + + // Should never actually get here + + @unlink($_FILES[$param]['tmp_name']); + throw new ClientException(_('File exceeds user\'s quota!')); + return; + } + + $mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name']); + + $filename = null; + + if (isset($mimetype)) { + + $basename = basename($_FILES[$param]['name']); + $filename = File::filename($user->getProfile(), $basename, $mimetype); + $filepath = File::path($filename); + + common_debug("filepath = " . $filepath); + + $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath); + + if (!$result) { + throw new ClientException(_('File could not be moved to destination directory.')); + return; + } + + } else { + throw new ClientException(_('Could not determine file\'s mime-type!')); + return; + } + + return new MediaFile($user, $filename, $mimetype); + } + + static function fromFilehandle($user, $fh) { + + $stream = stream_get_meta_data($fh); + + if (MediaFile::respectsQuota($user, filesize($stream['uri']))) { + + // Should never actually get here + + throw new ClientException(_('File exceeds user\'s quota!')); + return; + } + + $mimetype = MediaFile::getUploadedFileType($fh); + + $filename = null; + + if (isset($mimetype)) { + + $filename = File::filename($user->getProfile(), "email", $mimetype); + + $filepath = File::path($filename); + + $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664); + + if (!$result) { + throw new ClientException(_('File could not be moved to destination directory.' . + $stream['uri'] . ' ' . $filepath)); + } + } else { + throw new ClientException(_('Could not determine file\'s mime-type!')); + return; + } + + return new MediaFile($user, $filename, $mimetype); + } + + static function getUploadedFileType($f) { + require_once 'MIME/Type.php'; + + common_debug("in getUploadedFileType"); + + $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd'); + $cmd = common_config('attachments', 'filecommand'); + + $filetype = null; + + if (is_string($f)) { + + // assuming a filename + + $filetype = MIME_Type::autoDetect($f); + } else { + + // assuming a filehandle + + $stream = stream_get_meta_data($f); + $filetype = MIME_Type::autoDetect($stream['uri']); + } + + if (in_array($filetype, common_config('attachments', 'supported'))) { + return $filetype; + } + $media = MIME_Type::getMedia($filetype); + if ('application' !== $media) { + $hint = sprintf(_(' Try using another %s format.'), $media); + } else { + $hint = ''; + } + throw new ClientException(sprintf( + _('%s is not a supported filetype on this server.'), $filetype) . $hint); + } + + static function respectsQuota($user, $filesize) + { + $file = new File; + $result = $file->isRespectsQuota($user, $filesize); + if ($result === true) { + return true; + } else { + throw new ClientException($result); + } + } + +} \ No newline at end of file From 2d0aba49d913b7d4598073d5ff56237f736731d0 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Tue, 27 Oct 2009 21:45:56 -0700 Subject: [PATCH 2/4] Implement media upload in the API --- actions/apistatusesupdate.php | 72 ++++++++++++++++------- lib/mediafile.php | 106 +++++++++++++++++----------------- 2 files changed, 106 insertions(+), 72 deletions(-) diff --git a/actions/apistatusesupdate.php b/actions/apistatusesupdate.php index 0d71e15128..b7128d0846 100644 --- a/actions/apistatusesupdate.php +++ b/actions/apistatusesupdate.php @@ -38,6 +38,7 @@ if (!defined('STATUSNET')) { } require_once INSTALLDIR . '/lib/apiauth.php'; +require_once INSTALLDIR . '/lib/mediafile.php'; /** * Updates the authenticating user's status (posts a notice). @@ -60,7 +61,6 @@ class ApiStatusesUpdateAction extends ApiAuthAction var $source = null; var $status = null; var $in_reply_to_status_id = null; - static $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api'); /** @@ -76,25 +76,8 @@ class ApiStatusesUpdateAction extends ApiAuthAction { parent::prepare($args); - $this->user = $this->auth_user; - - if (empty($this->user)) { - $this->clientError(_('No such user!'), 404, $this->format); - return false; - } - + $this->user = $this->auth_user; $this->status = $this->trimmed('status'); - - if (empty($this->status)) { - $this->clientError( - 'Client must provide a \'status\' parameter with a value.', - 400, - $this->format - ); - - return false; - } - $this->source = $this->trimmed('source'); if (empty($this->source) || in_array($source, $this->reserved_sources)) { @@ -129,6 +112,27 @@ class ApiStatusesUpdateAction extends ApiAuthAction return; } + if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) { + $this->clientError(sprintf(_('The server was unable to handle ' . + 'that much POST data (%s bytes) due to its current configuration.'), + $_SERVER['CONTENT_LENGTH'])); + return; + } + + if (empty($this->user)) { + $this->clientError(_('No such user!'), 404, $this->format); + return; + } + + if (empty($this->status)) { + $this->clientError( + 'Client must provide a \'status\' parameter with a value.', + 400, + $this->format + ); + return; + } + $status_shortened = common_shorten_links($this->status); if (Notice::contentTooLong($status_shortened)) { @@ -187,14 +191,42 @@ class ApiStatusesUpdateAction extends ApiAuthAction } } + $upload = null; + + common_debug('looking for attachment'); + + $upload = MediaFile::fromUpload('media', $this->user); + + common_debug("uploaded file = " . var_export($upload, true)); + + if (isset($upload)) { + common_debug('newNotice: found an upload'); + + $status_shortened .= ' ' . $upload->shortUrl(); + + common_debug('content w/upload = ' . $status_shortened); + + if (Notice::contentTooLong($status_shortened)) { + $upload->delete(); + $this->clientError(sprintf(_('Max notice size is %d chars, including attachment URL.'), + Notice::maxContent())); + } else { + common_debug('content not too long'); + } + } + $this->notice = Notice::saveNew( $this->user->id, - html_entity_decode($this->status, ENT_NOQUOTES, 'UTF-8'), + html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8'), $this->source, 1, $reply_to ); + if (isset($upload)) { + $upload->attachToNotice($this->notice); + } + common_broadcast_notice($this->notice); } diff --git a/lib/mediafile.php b/lib/mediafile.php index be101cdcab..31868daec8 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -31,9 +31,9 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } -class MediaFile +class MediaFile { - + var $filename = null; var $fileRecord = null; var $user = null; @@ -46,16 +46,16 @@ class MediaFile if ($user == null) { $this->user = common_current_user(); } - + common_debug('in MediaFile constructor'); - + $this->filename = $filename; $this->mimetype = $mimetype; - + common_debug('storing file'); $this->fileRecord = $this->storeFile(); common_debug('finished storing file'); - + $this->fileurl = common_local_url('attachment', array('attachment' => $this->fileRecord->id)); @@ -67,24 +67,24 @@ class MediaFile common_debug('shortening file url'); $this->short_fileurl = common_shorten_url($this->fileurl); common_debug('shortened file url = ' . $short_fileurl); - + // Also, not sure this is necessary -- Zach $this->maybeAddRedir($this->fileRecord->id, $this->short_fileurl); - + common_debug("MediaFile: end of constructor"); } - + function attachToNotice($notice) { common_debug('MediaFile::attachToNotice() -- doing File_to_post'); File_to_post::processNew($this->fileRecord->id, $notice->id); common_debug('MediaFile done doing File_to_post'); - + $this->maybeAddRedir($this->fileRecord->id, - common_local_url('file', array('notice' => $notice->id))); + common_local_url('file', array('notice' => $notice->id))); } - - function shortUrl() + + function shortUrl() { return $this->short_fileurl; } @@ -115,12 +115,12 @@ class MediaFile $file_id = $file->insert(); if (!$file_id) { - + common_debug("storeFile: problem inserting new file"); common_log_db_error($file, "INSERT", __FILE__); throw new ClientException(_('There was a database error while saving your file. Please try again.')); } - + common_debug('finished storing file'); return $file; @@ -133,15 +133,15 @@ class MediaFile function maybeAddRedir($file_id, $url) { - + common_debug("maybeAddRedir: looking up url: $url for file id $file_id"); $file_redir = File_redirection::staticGet('url', $url); if (empty($file_redir)) { - + common_debug("maybeAddRedir: $url is not in the db"); - + $file_redir = new File_redirection; $file_redir->url = $url; $file_redir->file_id = $file_id; @@ -153,20 +153,24 @@ class MediaFile throw new ClientException(_('There was a database error while saving your file. Please try again.')); } } else { - + common_debug("maybeAddRedir: no need to add $url, it's already in the db"); } } - static function fromUpload($param = 'media') + static function fromUpload($param = 'media', $user = null) { common_debug("fromUpload: param = $param"); - + + if (empty($user)) { + $user = common_current_user(); + } + if (!isset($_FILES[$param]['error'])){ common_debug('no file found'); return; } - + switch ($_FILES[$param]['error']) { case UPLOAD_ERR_OK: // success, jump out break; @@ -183,7 +187,7 @@ class MediaFile @unlink($_FILES[$param]['tmp_name']); throw new ClientException(_('The uploaded file was only' . ' partially uploaded.')); - return; + return; case UPLOAD_ERR_NO_TMP_DIR: throw new ClientException(_('Missing a temporary folder.')); return; @@ -197,24 +201,22 @@ class MediaFile throw new ClientException(_('System error uploading file.')); return; } - - $user = common_current_user(); - + if (!MediaFile::respectsQuota($user, $_FILES['attach']['size'])) { - + // Should never actually get here - + @unlink($_FILES[$param]['tmp_name']); throw new ClientException(_('File exceeds user\'s quota!')); return; } $mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name']); - + $filename = null; - + if (isset($mimetype)) { - + $basename = basename($_FILES[$param]['name']); $filename = File::filename($user->getProfile(), $basename, $mimetype); $filepath = File::path($filename); @@ -222,44 +224,44 @@ class MediaFile common_debug("filepath = " . $filepath); $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath); - - if (!$result) { + + if (!$result) { throw new ClientException(_('File could not be moved to destination directory.')); return; } - + } else { throw new ClientException(_('Could not determine file\'s mime-type!')); return; } - + return new MediaFile($user, $filename, $mimetype); } - static function fromFilehandle($user, $fh) { + static function fromFilehandle($fh, $user) { $stream = stream_get_meta_data($fh); if (MediaFile::respectsQuota($user, filesize($stream['uri']))) { - + // Should never actually get here - + throw new ClientException(_('File exceeds user\'s quota!')); return; } $mimetype = MediaFile::getUploadedFileType($fh); - + $filename = null; - + if (isset($mimetype)) { $filename = File::filename($user->getProfile(), "email", $mimetype); $filepath = File::path($filename); - + $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664); - + if (!$result) { throw new ClientException(_('File could not be moved to destination directory.' . $stream['uri'] . ' ' . $filepath)); @@ -268,33 +270,33 @@ class MediaFile throw new ClientException(_('Could not determine file\'s mime-type!')); return; } - + return new MediaFile($user, $filename, $mimetype); } static function getUploadedFileType($f) { require_once 'MIME/Type.php'; - + common_debug("in getUploadedFileType"); $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd'); $cmd = common_config('attachments', 'filecommand'); - + $filetype = null; - + if (is_string($f)) { - + // assuming a filename - + $filetype = MIME_Type::autoDetect($f); } else { - + // assuming a filehandle - + $stream = stream_get_meta_data($f); $filetype = MIME_Type::autoDetect($stream['uri']); } - + if (in_array($filetype, common_config('attachments', 'supported'))) { return $filetype; } @@ -308,7 +310,7 @@ class MediaFile _('%s is not a supported filetype on this server.'), $filetype) . $hint); } - static function respectsQuota($user, $filesize) + static function respectsQuota($user, $filesize) { $file = new File; $result = $file->isRespectsQuota($user, $filesize); From b9ce23d0c2c1266b57997bd0d19891aa653d694c Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Wed, 28 Oct 2009 22:24:20 +0000 Subject: [PATCH 3/4] Rework MailDaemon to use the MediaFile class for uploads --- lib/mediafile.php | 2 +- scripts/maildaemon.php | 146 ++++++----------------------------------- 2 files changed, 21 insertions(+), 127 deletions(-) diff --git a/lib/mediafile.php b/lib/mediafile.php index 31868daec8..4aa8211c32 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -242,7 +242,7 @@ class MediaFile $stream = stream_get_meta_data($fh); - if (MediaFile::respectsQuota($user, filesize($stream['uri']))) { + if (!MediaFile::respectsQuota($user, filesize($stream['uri']))) { // Should never actually get here diff --git a/scripts/maildaemon.php b/scripts/maildaemon.php index 84dff8912e..b4e4d9f08d 100755 --- a/scripts/maildaemon.php +++ b/scripts/maildaemon.php @@ -29,6 +29,7 @@ END_OF_HELP; require_once INSTALLDIR.'/scripts/commandline.inc'; require_once(INSTALLDIR . '/lib/mail.php'); +require_once(INSTALLDIR . '/lib/mediafile.php'); require_once('Mail/mimeDecode.php'); # FIXME: we use both Mail_mimeDecode and mailparse @@ -71,43 +72,27 @@ class MailerDaemon 'Max notice size is %d chars.'), Notice::maxContent())); } - $fileRecords = array(); + + $mediafiles = array(); + foreach($attachments as $attachment){ - $mimetype = $this->getUploadedFileType($attachment); - $stream = stream_get_meta_data($attachment); - if (!$this->isRespectsQuota($user,filesize($stream['uri']))) { - die('error() should trigger an exception before reaching here.'); - } - $filename = $this->saveFile($user, $attachment,$mimetype); + $mf = null; + + try { + $mf = MediaFile::fromFileHandle($attachment, $user); + } catch(ClientException $ce) { + $this->error($from, $ce->getMessage()); + } + + $msg .= ' ' . $mf->shortUrl(); + + array_push($mediafiles, $mf); fclose($attachment); - - if (empty($filename)) { - $this->error($from,_('Couldn\'t save file.')); - } - - $fileRecord = $this->storeFile($filename, $mimetype); - $fileRecords[] = $fileRecord; - $fileurl = common_local_url('attachment', - array('attachment' => $fileRecord->id)); - - // not sure this is necessary -- Zach - $this->maybeAddRedir($fileRecord->id, $fileurl); - - $short_fileurl = common_shorten_url($fileurl); - $msg .= ' ' . $short_fileurl; - - if (Notice::contentTooLong($msg)) { - $this->deleteFile($filename); - $this->error($from, sprintf(_('Max notice size is %d chars, including attachment URL.'), - Notice::maxContent())); - } - - // Also, not sure this is necessary -- Zach - $this->maybeAddRedir($fileRecord->id, $short_fileurl); } - $err = $this->add_notice($user, $msg, $fileRecords); + $err = $this->add_notice($user, $msg, $mediafiles); + if (is_string($err)) { $this->error($from, $err); return false; @@ -116,89 +101,6 @@ class MailerDaemon } } - function saveFile($user, $attachment, $mimetype) { - - $filename = File::filename($user->getProfile(), "email", $mimetype); - - $filepath = File::path($filename); - - $stream = stream_get_meta_data($attachment); - if (copy($stream['uri'], $filepath) && chmod($filepath,0664)) { - return $filename; - } else { - $this->error(null,_('File could not be moved to destination directory.' . $stream['uri'] . ' ' . $filepath)); - } - } - - function storeFile($filename, $mimetype) { - - $file = new File; - $file->filename = $filename; - - $file->url = File::url($filename); - - $filepath = File::path($filename); - - $file->size = filesize($filepath); - $file->date = time(); - $file->mimetype = $mimetype; - - $file_id = $file->insert(); - - if (!$file_id) { - common_log_db_error($file, "INSERT", __FILE__); - $this->error(null,_('There was a database error while saving your file. Please try again.')); - } - - return $file; - } - - function maybeAddRedir($file_id, $url) - { - $file_redir = File_redirection::staticGet('url', $url); - - if (empty($file_redir)) { - $file_redir = new File_redirection; - $file_redir->url = $url; - $file_redir->file_id = $file_id; - - $result = $file_redir->insert(); - - if (!$result) { - common_log_db_error($file_redir, "INSERT", __FILE__); - $this->error(null,_('There was a database error while saving your file. Please try again.')); - } - } - } - - function getUploadedFileType($fileHandle) { - require_once 'MIME/Type.php'; - - $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd'); - $cmd = common_config('attachments', 'filecommand'); - - $stream = stream_get_meta_data($fileHandle); - $filetype = MIME_Type::autoDetect($stream['uri']); - if (in_array($filetype, common_config('attachments', 'supported'))) { - return $filetype; - } - $media = MIME_Type::getMedia($filetype); - if ('application' !== $media) { - $hint = sprintf(_(' Try using another %s format.'), $media); - } else { - $hint = ''; - } - $this->error(null,sprintf( - _('%s is not a supported filetype on this server.'), $filetype) . $hint); - } - - function isRespectsQuota($user,$fileSize) { - $file = new File; - $ret = $file->isRespectsQuota($user,$fileSize); - if (true === $ret) return true; - $this->error(null,$ret); - } - function error($from, $msg) { file_put_contents("php://stderr", $msg . "\n"); @@ -258,7 +160,7 @@ class MailerDaemon common_log($level, 'MailDaemon: '.$msg); } - function add_notice($user, $msg, $fileRecords) + function add_notice($user, $msg, $mediafiles) { try { $notice = Notice::saveNew($user->id, $msg, 'mail'); @@ -266,8 +168,8 @@ class MailerDaemon $this->log(LOG_ERR, $e->getMessage()); return $e->getMessage(); } - foreach($fileRecords as $fileRecord){ - $this->attachFile($notice, $fileRecord); + foreach($mediafiles as $mf){ + $mf->attachToNotice($notice); } common_broadcast_notice($notice); $this->log(LOG_INFO, @@ -275,14 +177,6 @@ class MailerDaemon return true; } - function attachFile($notice, $filerec) - { - File_to_post::processNew($filerec->id, $notice->id); - - $this->maybeAddRedir($filerec->id, - common_local_url('file', array('notice' => $notice->id))); - } - function parse_message($fname) { $contents = file_get_contents($fname); From e5a2f895a074a6eaaf8184f101503b1520ed780b Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Wed, 28 Oct 2009 17:12:22 -0700 Subject: [PATCH 4/4] Rearanged a couple things & removed debugging statements --- actions/apistatusesupdate.php | 46 ++++++++++++---------------- actions/newnotice.php | 26 ++++++---------- lib/mediafile.php | 57 +++++------------------------------ 3 files changed, 36 insertions(+), 93 deletions(-) diff --git a/actions/apistatusesupdate.php b/actions/apistatusesupdate.php index b7128d0846..3a030f0fe8 100644 --- a/actions/apistatusesupdate.php +++ b/actions/apistatusesupdate.php @@ -112,10 +112,12 @@ class ApiStatusesUpdateAction extends ApiAuthAction return; } - if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) { - $this->clientError(sprintf(_('The server was unable to handle ' . - 'that much POST data (%s bytes) due to its current configuration.'), - $_SERVER['CONTENT_LENGTH'])); + if (empty($this->status)) { + $this->clientError( + 'Client must provide a \'status\' parameter with a value.', + 400, + $this->format + ); return; } @@ -124,12 +126,10 @@ class ApiStatusesUpdateAction extends ApiAuthAction return; } - if (empty($this->status)) { - $this->clientError( - 'Client must provide a \'status\' parameter with a value.', - 400, - $this->format - ); + // Workaround for PHP returning empty $_FILES when POST length > PHP settings + + if (empty($_POST) && ($_SERVER['CONTENT_LENGTH'] > 0)) { + $this->clientError(_('Unable to handle that much POST data!')); return; } @@ -192,27 +192,19 @@ class ApiStatusesUpdateAction extends ApiAuthAction } $upload = null; - - common_debug('looking for attachment'); - $upload = MediaFile::fromUpload('media', $this->user); - common_debug("uploaded file = " . var_export($upload, true)); - if (isset($upload)) { - common_debug('newNotice: found an upload'); + $status_shortened .= ' ' . $upload->shortUrl(); - $status_shortened .= ' ' . $upload->shortUrl(); - - common_debug('content w/upload = ' . $status_shortened); - - if (Notice::contentTooLong($status_shortened)) { - $upload->delete(); - $this->clientError(sprintf(_('Max notice size is %d chars, including attachment URL.'), - Notice::maxContent())); - } else { - common_debug('content not too long'); - } + if (Notice::contentTooLong($status_shortened)) { + $upload->delete(); + $msg = _( + 'Max notice size is %d chars, ' . + 'including attachment URL.' + ); + $this->clientError(sprintf($msg, Notice::maxContent())); + } } $this->notice = Notice::saveNew( diff --git a/actions/newnotice.php b/actions/newnotice.php index 5100e79e10..59fb9f4619 100644 --- a/actions/newnotice.php +++ b/actions/newnotice.php @@ -164,27 +164,21 @@ class NewnoticeAction extends Action } $upload = null; - - common_debug('looking for attachment'); - $upload = MediaFile::fromUpload('attach'); - common_debug("uploaded file = " . var_export($upload, true)); - if (isset($upload)) { - common_debug('newNotice: found an upload'); - $content_shortened .= ' ' . $upload->shortUrl(); + $content_shortened .= ' ' . $upload->shortUrl(); - common_debug('content w/upload = ' . $content_shortened); - - if (Notice::contentTooLong($content_shortened)) { - $upload->delete(); - $this->clientError(sprintf(_('Max notice size is %d chars, including attachment URL.'), - Notice::maxContent())); - } else { - common_debug('content not too long'); - } + if (Notice::contentTooLong($content_shortened)) { + $upload->delete(); + $this->clientError( + sprintf( + _('Max notice size is %d chars, including attachment URL.'), + Notice::maxContent() + ) + ); + } } $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1, diff --git a/lib/mediafile.php b/lib/mediafile.php index 4aa8211c32..40f37ba61a 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -47,39 +47,21 @@ class MediaFile $this->user = common_current_user(); } - common_debug('in MediaFile constructor'); - - $this->filename = $filename; - $this->mimetype = $mimetype; - - common_debug('storing file'); + $this->filename = $filename; + $this->mimetype = $mimetype; $this->fileRecord = $this->storeFile(); - common_debug('finished storing file'); $this->fileurl = common_local_url('attachment', array('attachment' => $this->fileRecord->id)); - common_debug('$this->fileurl() = ' . $this->fileurl); - - // not sure this is necessary -- Zach $this->maybeAddRedir($this->fileRecord->id, $this->fileurl); - - common_debug('shortening file url'); $this->short_fileurl = common_shorten_url($this->fileurl); - common_debug('shortened file url = ' . $short_fileurl); - - // Also, not sure this is necessary -- Zach $this->maybeAddRedir($this->fileRecord->id, $this->short_fileurl); - - common_debug("MediaFile: end of constructor"); } function attachToNotice($notice) { - common_debug('MediaFile::attachToNotice() -- doing File_to_post'); File_to_post::processNew($this->fileRecord->id, $notice->id); - common_debug('MediaFile done doing File_to_post'); - $this->maybeAddRedir($this->fileRecord->id, common_local_url('file', array('notice' => $notice->id))); } @@ -98,31 +80,21 @@ class MediaFile function storeFile() { $file = new File; + $file->filename = $this->filename; - - common_debug('storing ' . $this->filename); - - $file->url = File::url($this->filename); - common_debug('file->url = ' . $file->url); - - $filepath = File::path($this->filename); - common_debug('filepath = ' . $filepath); - - $file->size = filesize($filepath); - $file->date = time(); + $file->url = File::url($this->filename); + $filepath = File::path($this->filename); + $file->size = filesize($filepath); + $file->date = time(); $file->mimetype = $this->mimetype; $file_id = $file->insert(); if (!$file_id) { - - common_debug("storeFile: problem inserting new file"); common_log_db_error($file, "INSERT", __FILE__); throw new ClientException(_('There was a database error while saving your file. Please try again.')); } - common_debug('finished storing file'); - return $file; } @@ -133,15 +105,10 @@ class MediaFile function maybeAddRedir($file_id, $url) { - - common_debug("maybeAddRedir: looking up url: $url for file id $file_id"); - $file_redir = File_redirection::staticGet('url', $url); if (empty($file_redir)) { - common_debug("maybeAddRedir: $url is not in the db"); - $file_redir = new File_redirection; $file_redir->url = $url; $file_redir->file_id = $file_id; @@ -152,22 +119,16 @@ class MediaFile common_log_db_error($file_redir, "INSERT", __FILE__); throw new ClientException(_('There was a database error while saving your file. Please try again.')); } - } else { - - common_debug("maybeAddRedir: no need to add $url, it's already in the db"); } } static function fromUpload($param = 'media', $user = null) { - common_debug("fromUpload: param = $param"); - if (empty($user)) { $user = common_current_user(); } if (!isset($_FILES[$param]['error'])){ - common_debug('no file found'); return; } @@ -221,8 +182,6 @@ class MediaFile $filename = File::filename($user->getProfile(), $basename, $mimetype); $filepath = File::path($filename); - common_debug("filepath = " . $filepath); - $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath); if (!$result) { @@ -277,8 +236,6 @@ class MediaFile static function getUploadedFileType($f) { require_once 'MIME/Type.php'; - common_debug("in getUploadedFileType"); - $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd'); $cmd = common_config('attachments', 'filecommand');