return $x;
}
- function isRespectsQuota($user,$fileSize) {
-
+ public static function respectsQuota(Profile $scoped, $fileSize) {
if ($fileSize > common_config('attachments', 'file_quota')) {
// TRANS: Message used to be inserted as %2$s in the text "No file may
// TRANS: be larger than %1$d byte and the file you sent was %2$s.".
// TRANS: %1$d (used for plural) is the byte limit for uploads,
// TRANS: %2$s is the proper form of "n bytes". This is the only ways to have
// TRANS: gettext support multiple plurals in the same message, unfortunately...
- return sprintf(_m('No file may be larger than %1$d byte and the file you sent was %2$s. Try to upload a smaller version.',
+ throw new ClientException(
+ sprintf(_m('No file may be larger than %1$d byte and the file you sent was %2$s. Try to upload a smaller version.',
'No file may be larger than %1$d bytes and the file you sent was %2$s. Try to upload a smaller version.',
$fileQuota),
- $fileQuota, $fileSizeText);
+ $fileQuota, $fileSizeText));
}
- $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 + $fileSize;
+ $file = new File;
+
+ $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 = {$scoped->id} and file.url like '%/notice/%/file'";
+ $file->query($query);
+ $file->fetch();
+ $total = $file->total + $fileSize;
if ($total > common_config('attachments', 'user_quota')) {
// TRANS: Message given if an upload would exceed user quota.
// TRANS: %d (number) is the user quota in bytes and is used for plural.
- return sprintf(_m('A file this large would exceed your user quota of %d byte.',
+ throw new ClientException(
+ sprintf(_m('A file this large would exceed your user quota of %d byte.',
'A file this large would exceed your user quota of %d bytes.',
common_config('attachments', 'user_quota')),
- common_config('attachments', 'user_quota'));
+ common_config('attachments', 'user_quota')));
}
$query .= ' AND EXTRACT(month FROM file.modified) = EXTRACT(month FROM now()) and EXTRACT(year FROM file.modified) = EXTRACT(year FROM now())';
- $this->query($query);
- $this->fetch();
- $total = $this->total + $fileSize;
+ $file->query($query);
+ $file->fetch();
+ $total = $file->total + $fileSize;
if ($total > common_config('attachments', 'monthly_quota')) {
// TRANS: Message given id an upload would exceed a user's monthly quota.
// TRANS: $d (number) is the monthly user quota in bytes and is used for plural.
- return sprintf(_m('A file this large would exceed your monthly quota of %d byte.',
+ throw new ClientException(
+ sprintf(_m('A file this large would exceed your monthly quota of %d byte.',
'A file this large would exceed your monthly quota of %d bytes.',
common_config('attachments', 'monthly_quota')),
- common_config('attachments', 'monthly_quota'));
+ common_config('attachments', 'monthly_quota')));
}
return true;
}
class MediaFile
{
+ protected $scoped = null;
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)
+ function __construct(Profile $scoped, $filename = null, $mimetype = null)
{
- if ($user == null) {
- $this->user = common_current_user();
- } else {
- $this->user = $user;
- }
+ $this->scoped = $scoped;
$this->filename = $filename;
$this->mimetype = $mimetype;
return null;
}
- $outname = File::filename($this->user->getProfile(), 'thumb-' . $this->filename, $this->mimetype);
+ $outname = File::filename($this->scoped, 'thumb-' . $this->filename, $this->mimetype);
$outpath = File::path($outname);
$maxWidth = common_config('attachments', 'thumb_width');
}
}
- static function fromUpload($param = 'media', $user = null)
+ static function fromUpload($param = 'media', Profile $scoped)
{
- if (empty($user)) {
- $user = common_current_user();
+ if (is_null($scoped)) {
+ $scoped = Profile::current();
}
if (!isset($_FILES[$param]['error'])){
return;
}
- if (!MediaFile::respectsQuota($user, $_FILES[$param]['size'])) {
-
- // Should never actually get here
-
- @unlink($_FILES[$param]['tmp_name']);
- // TRANS: Client exception thrown when a file upload operation would cause a user to exceed a set quota.
- throw new ClientException(_('File exceeds user\'s quota.'));
- return;
- }
+ // Throws exception if additional size does not respect quota
+ File::respectsQuota($scoped, $_FILES[$param]['size']);
$mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name'],
$_FILES[$param]['name']);
- $filename = null;
-
- if (isset($mimetype)) {
+ $basename = basename($_FILES[$param]['name']);
+ $filename = File::filename($scoped, $basename, $mimetype);
+ $filepath = File::path($filename);
- $basename = basename($_FILES[$param]['name']);
- $filename = File::filename($user->getProfile(), $basename, $mimetype);
- $filepath = File::path($filename);
+ $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath);
- $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath);
-
- if (!$result) {
- // TRANS: Client exception thrown when a file upload operation fails because the file could
- // TRANS: not be moved from the temporary folder to the permanent file location.
- throw new ClientException(_('File could not be moved to destination directory.'));
- return;
- }
-
- } else {
- // TRANS: Client exception thrown when a file upload operation has been stopped because the MIME
- // TRANS: type of the uploaded file could not be determined.
- throw new ClientException(_('Could not determine file\'s MIME type.'));
- return;
+ if (!$result) {
+ // TRANS: Client exception thrown when a file upload operation fails because the file could
+ // TRANS: not be moved from the temporary folder to the permanent file location.
+ throw new ClientException(_('File could not be moved to destination directory.'));
}
- return new MediaFile($user, $filename, $mimetype);
+ return new MediaFile($scoped, $filename, $mimetype);
}
- static function fromFilehandle($fh, $user) {
+ static function fromFilehandle($fh, Profile $scoped) {
$stream = stream_get_meta_data($fh);
- if (!MediaFile::respectsQuota($user, filesize($stream['uri']))) {
-
- // Should never actually get here
-
- // TRANS: Client exception thrown when a file upload operation would cause a user to exceed a set quota.
- throw new ClientException(_('File exceeds user\'s quota.'));
- return;
- }
+ File::respectsQuota($scoped, filesize($stream['uri']));
$mimetype = MediaFile::getUploadedFileType($fh);
- $filename = null;
-
- if (isset($mimetype)) {
-
- $filename = File::filename($user->getProfile(), "email", $mimetype);
+ $filename = File::filename($scoped, "email", $mimetype);
- $filepath = File::path($filename);
+ $filepath = File::path($filename);
- $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664);
+ $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664);
- if (!$result) {
- // TRANS: Client exception thrown when a file upload operation fails because the file could
- // TRANS: not be moved from the temporary folder to the permanent file location.
- throw new ClientException(_('File could not be moved to destination directory.' .
- $stream['uri'] . ' ' . $filepath));
- }
- } else {
- // TRANS: Client exception thrown when a file upload operation has been stopped because the MIME
- // TRANS: type of the uploaded file could not be determined.
- throw new ClientException(_('Could not determine file\'s MIME type.'));
- return;
+ if (!$result) {
+ // TRANS: Client exception thrown when a file upload operation fails because the file could
+ // TRANS: not be moved from the temporary folder to the permanent file location.
+ throw new ClientException(_('File could not be moved to destination directory.' .
+ $stream['uri'] . ' ' . $filepath));
}
- return new MediaFile($user, $filename, $mimetype);
+ return new MediaFile($scoped, $filename, $mimetype);
}
/**
* @throws ClientException if type is known, but not supported for local uploads
*/
static function getUploadedFileType($f, $originalFilename=false) {
+ global $_PEAR;
+
require_once 'MIME/Type.php';
require_once 'MIME/Type/Extension.php';
// We have to disable auto handling of PEAR errors
- PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
+ $_PEAR->staticPushErrorHandling(PEAR_ERROR_RETURN);
$mte = new MIME_Type_Extension();
- $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd');
+ $cmd = &$_PEAR->getStaticProperty('MIME_Type', 'fileCmd');
$cmd = common_config('attachments', 'filecommand');
$filetype = null;
}
if ($supported === true || in_array($filetype, $supported)) {
// Restore PEAR error handlers for our DB code...
- PEAR::staticPopErrorHandling();
+ $_PEAR->staticPopErrorHandling();
return $filetype;
}
$media = MIME_Type::getMedia($filetype);
$hint = sprintf(_('"%s" is not a supported file type on this server.'), $filetype);
}
// Restore PEAR error handlers for our DB code...
- PEAR::staticPopErrorHandling();
+ $_PEAR->staticPopErrorHandling();
throw new ClientException($hint);
}
-
- static function respectsQuota($user, $filesize)
- {
- $file = new File;
- $result = $file->isRespectsQuota($user, $filesize);
- if ($result === true) {
- return true;
- } else {
- throw new ClientException($result);
- }
- }
-
}