From 4443baaab9b012601cbee301eb8debf9668fd43a Mon Sep 17 00:00:00 2001 From: westi Date: Wed, 1 Aug 2012 13:52:06 +0000 Subject: [PATCH] Multisite: Rework the upload space usage tracking code so as to be fully pluggable. * Moves some admin only functions into wp-admin/includes/ms.php from wp-includes/ms-functions.php * Reworked the variable naming to be more in line with the Coding Standards * Introduced a new get_space_used() function instead of calculating it in multiple places. Fixes #21181 props dllh and jkudish for inital work on this. git-svn-id: http://core.svn.wordpress.org/trunk@21387 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/dashboard.php | 2 +- wp-admin/includes/ms.php | 95 +++++++++++++++++++++++---------- wp-includes/ms-functions.php | 51 ------------------ 3 files changed, 68 insertions(+), 80 deletions(-) diff --git a/wp-admin/includes/dashboard.php b/wp-admin/includes/dashboard.php index 98c8318c31..b5c9c726f0 100644 --- a/wp-admin/includes/dashboard.php +++ b/wp-admin/includes/dashboard.php @@ -1097,7 +1097,7 @@ function wp_dashboard_quota() { return true; $quota = get_space_allowed(); - $used = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024; + $used = get_space_used(); if ( $used > $quota ) $percentused = '100'; diff --git a/wp-admin/includes/ms.php b/wp-admin/includes/ms.php index a6828b2ded..6980156d5b 100644 --- a/wp-admin/includes/ms.php +++ b/wp-admin/includes/ms.php @@ -25,9 +25,8 @@ function check_upload_size( $file ) { if ( defined( 'WP_IMPORTING' ) ) return $file; - $space_allowed = 1048576 * get_space_allowed(); - $space_used = get_dirsize( BLOGUPLOADDIR ); - $space_left = $space_allowed - $space_used; + $space_left = get_upload_space_available(); + $file_size = filesize( $file['tmp_name'] ); if ( $space_left < $file_size ) $file['error'] = sprintf( __( 'Not enough space to upload. %1$s KB needed.' ), number_format( ($file_size - $space_left) /1024 ) ); @@ -310,28 +309,48 @@ function get_upload_space_available() { if ( get_site_option( 'upload_space_check_disabled' ) ) return $space_allowed; - $dir_name = trailingslashit( BLOGUPLOADDIR ); - if ( !( is_dir( $dir_name) && is_readable( $dir_name ) ) ) - return $space_allowed; + $space_used = get_space_used() * 1024 * 1024; - $dir = dir( $dir_name ); - $size = 0; - - while ( $file = $dir->read() ) { - if ( $file != '.' && $file != '..' ) { - if ( is_dir( $dir_name . $file) ) { - $size += get_dirsize( $dir_name . $file ); - } else { - $size += filesize( $dir_name . $file ); - } - } - } - $dir->close(); - - if ( ( $space_allowed - $size ) <= 0 ) + if ( ( $space_allowed - $space_used ) <= 0 ) return 0; - return $space_allowed - $size; + return $space_allowed - $space_used; +} + +/** + * Check whether a blog has used its allotted upload space. + * + * @since MU + * + * @param bool $echo Optional. If $echo is set and the quota is exceeded, a warning message is echoed. Default is true. + * @return int + */ +function upload_is_user_over_quota( $echo = true ) { + if ( get_site_option( 'upload_space_check_disabled' ) ) + return false; + + $space_allowed = get_space_allowed(); + if ( empty( $space_allowed ) || !is_numeric( $space_allowed ) ) + $space_allowed = 10;// Default space allowed is 10 MB + + $space_used = get_space_used(); + + if ( ($space_allowed - $space_used ) < 0 ) { + if ( $echo ) + _e( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' ); + return true; + } else { + return false; + } +} + +function get_space_used() { + // Allow for an alternative way of tracking storage space used + $space_used = apply_filters( 'pre_get_space_used', false ); + if ( false === $space_used ) + $space_used = get_dirsize( BLOGUPLOADDIR ); + + return $space_used; } /** @@ -352,24 +371,44 @@ function get_space_allowed() { } function display_space_usage() { - $space = get_space_allowed(); - $used = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024; + $space_allowed = get_space_allowed(); + $space_used = get_space_used(); - $percentused = ( $used / $space ) * 100; + $percent_used = ( $space_used / $space_allowed ) * 100; - if ( $space > 1000 ) { - $space = number_format( $space / 1024 ); + if ( $space_allowed > 1000 ) { + $space = number_format( $space_allowed / 1024 ); /* translators: Gigabytes */ $space .= __( 'GB' ); } else { + $space = number_format( $space_allowed ); /* translators: Megabytes */ $space .= __( 'MB' ); } ?> - + insert( $wpdb->registration_log, array('email' => $user->user_email, 'IP' => preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] ), 'blog_id' => $blog_id, 'date_registered' => current_time('mysql')) ); } -/** - * Get the remaining upload space for this blog. - * - * @since MU - * @uses upload_is_user_over_quota() - * @uses get_space_allowed() - * @uses get_dirsize() - * - * @param int $size - * @return int - */ -function fix_import_form_size( $size ) { - if ( upload_is_user_over_quota( false ) == true ) - return 0; - - $spaceAllowed = 1024 * 1024 * get_space_allowed(); - $dirsize = get_dirsize( BLOGUPLOADDIR ); - if ( $size > $spaceAllowed - $dirsize ) - return $spaceAllowed - $dirsize; // remaining space - else - return $size; // default -} - /** * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table. *