web/wp-admin/includes/ms.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
    23 		return $file;
    23 		return $file;
    24 
    24 
    25 	if ( defined( 'WP_IMPORTING' ) )
    25 	if ( defined( 'WP_IMPORTING' ) )
    26 		return $file;
    26 		return $file;
    27 
    27 
    28 	$space_allowed = 1048576 * get_space_allowed();
    28 	$space_left = get_upload_space_available();
    29 	$space_used = get_dirsize( BLOGUPLOADDIR );
    29 
    30 	$space_left = $space_allowed - $space_used;
       
    31 	$file_size = filesize( $file['tmp_name'] );
    30 	$file_size = filesize( $file['tmp_name'] );
    32 	if ( $space_left < $file_size )
    31 	if ( $space_left < $file_size )
    33 		$file['error'] = sprintf( __( 'Not enough space to upload. %1$s KB needed.' ), number_format( ($file_size - $space_left) /1024 ) );
    32 		$file['error'] = sprintf( __( 'Not enough space to upload. %1$s KB needed.' ), number_format( ($file_size - $space_left) /1024 ) );
    34 	if ( $file_size > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) )
    33 	if ( $file_size > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) )
    35 		$file['error'] = sprintf(__('This file is too big. Files must be less than %1$s KB in size.'), get_site_option( 'fileupload_maxk', 1500 ) );
    34 		$file['error'] = sprintf(__('This file is too big. Files must be less than %1$s KB in size.'), get_site_option( 'fileupload_maxk', 1500 ) );
    54  */
    53  */
    55 function wpmu_delete_blog( $blog_id, $drop = false ) {
    54 function wpmu_delete_blog( $blog_id, $drop = false ) {
    56 	global $wpdb, $current_site;
    55 	global $wpdb, $current_site;
    57 
    56 
    58 	$switch = false;
    57 	$switch = false;
    59 	if ( $blog_id != $wpdb->blogid ) {
    58 	if ( get_current_blog_id() != $blog_id ) {
    60 		$switch = true;
    59 		$switch = true;
    61 		switch_to_blog( $blog_id );
    60 		switch_to_blog( $blog_id );
    62 		$blog = get_blog_details( $blog_id );
    61 	}
    63 	} else {
    62 
    64 		$blog = $GLOBALS['current_blog'];
    63 	$blog = get_blog_details( $blog_id );
    65 	}
       
    66 
    64 
    67 	do_action( 'delete_blog', $blog_id, $drop );
    65 	do_action( 'delete_blog', $blog_id, $drop );
    68 
    66 
    69 	$users = get_users( array( 'blog_id' => $blog_id, 'fields' => 'ids' ) );
    67 	$users = get_users( array( 'blog_id' => $blog_id, 'fields' => 'ids' ) );
    70 
    68 
    80 	// Don't destroy the initial, main, or root blog.
    78 	// Don't destroy the initial, main, or root blog.
    81 	if ( $drop && ( 1 == $blog_id || is_main_site( $blog_id ) || ( $blog->path == $current_site->path && $blog->domain == $current_site->domain ) ) )
    79 	if ( $drop && ( 1 == $blog_id || is_main_site( $blog_id ) || ( $blog->path == $current_site->path && $blog->domain == $current_site->domain ) ) )
    82 		$drop = false;
    80 		$drop = false;
    83 
    81 
    84 	if ( $drop ) {
    82 	if ( $drop ) {
    85 
       
    86 		$drop_tables = apply_filters( 'wpmu_drop_tables', $wpdb->tables( 'blog' ) );
    83 		$drop_tables = apply_filters( 'wpmu_drop_tables', $wpdb->tables( 'blog' ) );
    87 
    84 
    88 		foreach ( (array) $drop_tables as $table ) {
    85 		foreach ( (array) $drop_tables as $table ) {
    89 			$wpdb->query( "DROP TABLE IF EXISTS `$table`" );
    86 			$wpdb->query( "DROP TABLE IF EXISTS `$table`" );
    90 		}
    87 		}
    91 
    88 
    92 		$wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) );
    89 		$wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) );
    93 
    90 
    94 		$dir = apply_filters( 'wpmu_delete_blog_upload_dir', WP_CONTENT_DIR . "/blogs.dir/{$blog_id}/files/", $blog_id );
    91 		$uploads = wp_upload_dir();
       
    92 		$dir = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_id );
    95 		$dir = rtrim( $dir, DIRECTORY_SEPARATOR );
    93 		$dir = rtrim( $dir, DIRECTORY_SEPARATOR );
    96 		$top_dir = $dir;
    94 		$top_dir = $dir;
    97 		$stack = array($dir);
    95 		$stack = array($dir);
    98 		$index = 0;
    96 		$index = 0;
    99 
    97 
   110 					if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) )
   108 					if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) )
   111 						$stack[] = $dir . DIRECTORY_SEPARATOR . $file;
   109 						$stack[] = $dir . DIRECTORY_SEPARATOR . $file;
   112 					else if ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) )
   110 					else if ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) )
   113 						@unlink( $dir . DIRECTORY_SEPARATOR . $file );
   111 						@unlink( $dir . DIRECTORY_SEPARATOR . $file );
   114 				}
   112 				}
       
   113 				@closedir( $dh );
   115 			}
   114 			}
   116 			$index++;
   115 			$index++;
   117 		}
   116 		}
   118 
   117 
   119 		$stack = array_reverse( $stack ); // Last added dirs are deepest
   118 		$stack = array_reverse( $stack ); // Last added dirs are deepest
   120 		foreach( (array) $stack as $dir ) {
   119 		foreach( (array) $stack as $dir ) {
   121 			if ( $dir != $top_dir)
   120 			if ( $dir != $top_dir)
   122 			@rmdir( $dir );
   121 			@rmdir( $dir );
   123 		}
   122 		}
       
   123 
       
   124 		clean_blog_cache( $blog );
   124 	}
   125 	}
   125 
   126 
   126 	if ( $switch )
   127 	if ( $switch )
   127 		restore_current_blog();
   128 		restore_current_blog();
   128 }
   129 }
   221 	if ( $current_user->ID != $_POST['user_id'] )
   222 	if ( $current_user->ID != $_POST['user_id'] )
   222 		return false;
   223 		return false;
   223 
   224 
   224 	if ( $current_user->user_email != $_POST['email'] ) {
   225 	if ( $current_user->user_email != $_POST['email'] ) {
   225 		if ( !is_email( $_POST['email'] ) ) {
   226 		if ( !is_email( $_POST['email'] ) ) {
   226 			$errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ), array( 'form-field' => 'email' ) );
   227 			$errors->add( 'user_email', __( "<strong>ERROR</strong>: The email address isn&#8217;t correct." ), array( 'form-field' => 'email' ) );
   227 			return;
   228 			return;
   228 		}
   229 		}
   229 
   230 
   230 		if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM {$wpdb->users} WHERE user_email=%s", $_POST['email'] ) ) ) {
   231 		if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM {$wpdb->users} WHERE user_email=%s", $_POST['email'] ) ) ) {
   231 			$errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address is already used." ), array( 'form-field' => 'email' ) );
   232 			$errors->add( 'user_email', __( "<strong>ERROR</strong>: The email address is already used." ), array( 'form-field' => 'email' ) );
   232 			delete_option( $current_user->ID . '_new_email' );
   233 			delete_option( $current_user->ID . '_new_email' );
   233 			return;
   234 			return;
   234 		}
   235 		}
   235 
   236 
   236 		$hash = md5( $_POST['email'] . time() . mt_rand() );
   237 		$hash = md5( $_POST['email'] . time() . mt_rand() );
   271 		echo "<div class='update-nag'>" . sprintf( __( "Your email address has not been updated yet. Please check your inbox at %s for a confirmation email." ), $email['newemail'] ) . "</div>";
   272 		echo "<div class='update-nag'>" . sprintf( __( "Your email address has not been updated yet. Please check your inbox at %s for a confirmation email." ), $email['newemail'] ) . "</div>";
   272 }
   273 }
   273 add_action( 'admin_notices', 'new_user_email_admin_notice' );
   274 add_action( 'admin_notices', 'new_user_email_admin_notice' );
   274 
   275 
   275 /**
   276 /**
   276  * Determines if there is any upload space left in the current blog's quota.
   277  * Check whether a blog has used its allotted upload space.
   277  *
   278  *
   278  * @since 3.0.0
   279  * @since MU
   279  * @return bool True if space is available, false otherwise.
   280  *
   280  */
   281  * @param bool $echo Optional. If $echo is set and the quota is exceeded, a warning message is echoed. Default is true.
   281 function is_upload_space_available() {
   282  * @return int
       
   283  */
       
   284 function upload_is_user_over_quota( $echo = true ) {
   282 	if ( get_site_option( 'upload_space_check_disabled' ) )
   285 	if ( get_site_option( 'upload_space_check_disabled' ) )
       
   286 		return false;
       
   287 
       
   288 	$space_allowed = get_space_allowed();
       
   289 	if ( empty( $space_allowed ) || !is_numeric( $space_allowed ) )
       
   290 		$space_allowed = 10; // Default space allowed is 10 MB
       
   291 
       
   292 	$space_used = get_space_used();
       
   293 
       
   294 	if ( ( $space_allowed - $space_used ) < 0 ) {
       
   295 		if ( $echo )
       
   296 			_e( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' );
   283 		return true;
   297 		return true;
   284 
   298 	} else {
   285 	if ( !( $space_allowed = get_upload_space_available() ) )
       
   286 		return false;
   299 		return false;
   287 
   300 	}
   288 	return true;
   301 }
   289 }
   302 
   290 
   303 /**
   291 /**
   304  * Displays the amount of disk space used by the current blog. Not used in core.
   292  * @since 3.0.0
   305  *
   293  *
   306  * @since MU
   294  * @return int of upload size limit in bytes
   307  */
   295  */
       
   296 function upload_size_limit_filter( $size ) {
       
   297 	$fileupload_maxk = 1024 * get_site_option( 'fileupload_maxk', 1500 );
       
   298 	if ( get_site_option( 'upload_space_check_disabled' ) )
       
   299 		return min( $size, $fileupload_maxk );
       
   300 
       
   301 	return min( $size, $fileupload_maxk, get_upload_space_available() );
       
   302 }
       
   303 /**
       
   304  * Determines if there is any upload space left in the current blog's quota.
       
   305  *
       
   306  * @return int of upload space available in bytes
       
   307  */
       
   308 function get_upload_space_available() {
       
   309 	$space_allowed = get_space_allowed() * 1024 * 1024;
       
   310 	if ( get_site_option( 'upload_space_check_disabled' ) )
       
   311 		return $space_allowed;
       
   312 
       
   313 	$dir_name = trailingslashit( BLOGUPLOADDIR );
       
   314 	if ( !( is_dir( $dir_name) && is_readable( $dir_name ) ) )
       
   315 		return $space_allowed;
       
   316 
       
   317   	$dir = dir( $dir_name );
       
   318    	$size = 0;
       
   319 
       
   320 	while ( $file = $dir->read() ) {
       
   321 		if ( $file != '.' && $file != '..' ) {
       
   322 			if ( is_dir( $dir_name . $file) ) {
       
   323 				$size += get_dirsize( $dir_name . $file );
       
   324 			} else {
       
   325 				$size += filesize( $dir_name . $file );
       
   326 			}
       
   327 		}
       
   328 	}
       
   329 	$dir->close();
       
   330 
       
   331 	if ( ( $space_allowed - $size ) <= 0 )
       
   332 		return 0;
       
   333 
       
   334 	return $space_allowed - $size;
       
   335 }
       
   336 
       
   337 /**
       
   338  * Returns the upload quota for the current blog.
       
   339  *
       
   340  * @return int Quota
       
   341  */
       
   342 function get_space_allowed() {
       
   343 	$space_allowed = get_option( 'blog_upload_space' );
       
   344 
       
   345 	if ( ! is_numeric( $space_allowed ) )
       
   346 		$space_allowed = get_site_option( 'blog_upload_space' );
       
   347 
       
   348 	if ( empty( $space_allowed ) || ! is_numeric( $space_allowed ) )
       
   349 		$space_allowed = 50;
       
   350 
       
   351 	return $space_allowed;
       
   352 }
       
   353 
       
   354 function display_space_usage() {
   308 function display_space_usage() {
   355 	$space = get_space_allowed();
   309 	$space_allowed = get_space_allowed();
   356 	$used = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024;
   310 	$space_used = get_space_used();
   357 
   311 
   358 	$percentused = ( $used / $space ) * 100;
   312 	$percent_used = ( $space_used / $space_allowed ) * 100;
   359 
   313 
   360 	if ( $space > 1000 ) {
   314 	if ( $space_allowed > 1000 ) {
   361 		$space = number_format( $space / 1024 );
   315 		$space = number_format( $space_allowed / 1024 );
   362 		/* translators: Gigabytes */
   316 		/* translators: Gigabytes */
   363 		$space .= __( 'GB' );
   317 		$space .= __( 'GB' );
   364 	} else {
   318 	} else {
       
   319 		$space = number_format( $space_allowed );
   365 		/* translators: Megabytes */
   320 		/* translators: Megabytes */
   366 		$space .= __( 'MB' );
   321 		$space .= __( 'MB' );
   367 	}
   322 	}
   368 	?>
   323 	?>
   369 	<strong><?php printf( __( 'Used: %1s%% of %2s' ), number_format( $percentused ), $space ); ?></strong>
   324 	<strong><?php printf( __( 'Used: %1$s%% of %2$s' ), number_format( $percent_used ), $space ); ?></strong>
   370 	<?php
   325 	<?php
       
   326 }
       
   327 
       
   328 /**
       
   329  * Get the remaining upload space for this blog.
       
   330  *
       
   331  * @since MU
       
   332  * @uses upload_is_user_over_quota()
       
   333  * @uses get_space_allowed()
       
   334  * @uses get_upload_space_available()
       
   335  *
       
   336  * @param int $size Current max size in bytes
       
   337  * @return int Max size in bytes
       
   338  */
       
   339 function fix_import_form_size( $size ) {
       
   340 	if ( upload_is_user_over_quota( false ) == true )
       
   341 		return 0;
       
   342 
       
   343 	$available = get_upload_space_available();
       
   344 	return min( $size, $available );
   371 }
   345 }
   372 
   346 
   373 // Edit blog upload space setting on Edit Blog page
   347 // Edit blog upload space setting on Edit Blog page
   374 function upload_space_setting( $id ) {
   348 function upload_space_setting( $id ) {
   375 	$quota = get_blog_option( $id, 'blog_upload_space' );
   349 	switch_to_blog( $id );
       
   350 	$quota = get_option( 'blog_upload_space' );
       
   351 	restore_current_blog();
       
   352 
   376 	if ( !$quota )
   353 	if ( !$quota )
   377 		$quota = '';
   354 		$quota = '';
   378 
   355 
   379 	?>
   356 	?>
   380 	<tr>
   357 	<tr>
   613 		</td>
   590 		</td>
   614 	</tr>
   591 	</tr>
   615 	<?php if ( in_array( get_site_option( 'registration' ), array( 'all', 'blog' ) ) ) : ?>
   592 	<?php if ( in_array( get_site_option( 'registration' ), array( 'all', 'blog' ) ) ) : ?>
   616 		<tr>
   593 		<tr>
   617 			<th scope="row" colspan="2" class="th-full">
   594 			<th scope="row" colspan="2" class="th-full">
   618 				<a href="<?php echo apply_filters( 'wp_signup_location', network_home_url( 'wp-signup.php' ) ); ?>"><?php _e( 'Create a New Site' ); ?></a>
   595 				<a href="<?php echo apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); ?>"><?php _e( 'Create a New Site' ); ?></a>
   619 			</th>
   596 			</th>
   620 		</tr>
   597 		</tr>
   621 	<?php endif; ?>
   598 	<?php endif; ?>
   622 	</table>
   599 	</table>
   623 	<?php
   600 	<?php
   624 }
   601 }
   625 
   602 
   626 function ms_deprecated_blogs_file() {
       
   627 	if ( ! is_super_admin() )
       
   628 		return;
       
   629 	if ( ! file_exists( WP_CONTENT_DIR . '/blogs.php' ) )
       
   630 		return;
       
   631 	echo '<div class="update-nag">' . sprintf( __( 'The <code>%1$s</code> file is deprecated. Please remove it and update your server rewrite rules to use <code>%2$s</code> instead.' ), 'wp-content/blogs.php', 'wp-includes/ms-files.php' ) . '</div>';
       
   632 }
       
   633 add_action( 'network_admin_notices', 'ms_deprecated_blogs_file' );
       
   634 
       
   635 /**
   603 /**
   636  * Grants super admin privileges.
   604  * Grants super admin privileges.
   637  *
   605  *
   638  * @since 3.0.0
   606  * @since 3.0.0
   639  * @param int $user_id
   607  * @param int $user_id
   648 	do_action( 'grant_super_admin', $user_id );
   616 	do_action( 'grant_super_admin', $user_id );
   649 
   617 
   650 	// Directly fetch site_admins instead of using get_super_admins()
   618 	// Directly fetch site_admins instead of using get_super_admins()
   651 	$super_admins = get_site_option( 'site_admins', array( 'admin' ) );
   619 	$super_admins = get_site_option( 'site_admins', array( 'admin' ) );
   652 
   620 
   653 	$user = new WP_User( $user_id );
   621 	$user = get_userdata( $user_id );
   654 	if ( ! in_array( $user->user_login, $super_admins ) ) {
   622 	if ( $user && ! in_array( $user->user_login, $super_admins ) ) {
   655 		$super_admins[] = $user->user_login;
   623 		$super_admins[] = $user->user_login;
   656 		update_site_option( 'site_admins' , $super_admins );
   624 		update_site_option( 'site_admins' , $super_admins );
   657 		do_action( 'granted_super_admin', $user_id );
   625 		do_action( 'granted_super_admin', $user_id );
   658 		return true;
   626 		return true;
   659 	}
   627 	}
   676 	do_action( 'revoke_super_admin', $user_id );
   644 	do_action( 'revoke_super_admin', $user_id );
   677 
   645 
   678 	// Directly fetch site_admins instead of using get_super_admins()
   646 	// Directly fetch site_admins instead of using get_super_admins()
   679 	$super_admins = get_site_option( 'site_admins', array( 'admin' ) );
   647 	$super_admins = get_site_option( 'site_admins', array( 'admin' ) );
   680 
   648 
   681 	$user = new WP_User( $user_id );
   649 	$user = get_userdata( $user_id );
   682 	if ( $user->user_email != get_site_option( 'admin_email' ) ) {
   650 	if ( $user && $user->user_email != get_site_option( 'admin_email' ) ) {
   683 		if ( false !== ( $key = array_search( $user->user_login, $super_admins ) ) ) {
   651 		if ( false !== ( $key = array_search( $user->user_login, $super_admins ) ) ) {
   684 			unset( $super_admins[$key] );
   652 			unset( $super_admins[$key] );
   685 			update_site_option( 'site_admins', $super_admins );
   653 			update_site_option( 'site_admins', $super_admins );
   686 			do_action( 'revoked_super_admin', $user_id );
   654 			do_action( 'revoked_super_admin', $user_id );
   687 			return true;
   655 			return true;
   718 function _thickbox_path_admin_subfolder() {
   686 function _thickbox_path_admin_subfolder() {
   719 ?>
   687 ?>
   720 <script type="text/javascript">
   688 <script type="text/javascript">
   721 //<![CDATA[
   689 //<![CDATA[
   722 var tb_pathToImage = "../../wp-includes/js/thickbox/loadingAnimation.gif";
   690 var tb_pathToImage = "../../wp-includes/js/thickbox/loadingAnimation.gif";
   723 var tb_closeImage = "../../wp-includes/js/thickbox/tb-close.png";
       
   724 //]]>
   691 //]]>
   725 </script>
   692 </script>
   726 <?php
   693 <?php
   727 }
   694 }
   728 
   695