Skip to content

Commit

Permalink
Merge pull request #833 from humanmade/remove-whoami
Browse files Browse the repository at this point in the history
Remove folder permissions notices
  • Loading branch information
willmot committed Mar 23, 2017
2 parents 335865c + a02d722 commit 9901a7f
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 53 deletions.
46 changes: 46 additions & 0 deletions admin/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,52 @@ function request_delete_schedule() {
}
add_action( 'admin_post_hmbkp_request_delete_schedule', 'HM\BackUpWordPress\request_delete_schedule' );

add_action( 'admin_post_hmbkp_request_credentials', function() {

global $wp_filesystem;

ob_start();
$creds = request_filesystem_credentials( '' );
ob_end_clean();

// Default to showing an error if we're not able to connect.
$url = add_query_arg( 'connection_error', 1, get_settings_url() );

/**
* If we have valid filesystem credentials then let's attempt
* to use them to create the backups directory. If we can't create it in
* WP_CONTENT_DIR then we fallback to trying in uploads.
*/
if ( WP_Filesystem( $creds ) ) {

// If we're able to connect then no need to redirect with an error.
$url = get_settings_url();

// If the backup path exists then let's just try to chmod it to the correct permissions.
if (
is_dir( Path::get_instance()->get_default_path() ) &&
! $wp_filesystem->chmod( Path::get_instance()->get_default_path(), FS_CHMOD_DIR )
) {
$url = add_query_arg( 'creation_error', 1, get_settings_url() );
} else {

// If the path doesn't exist then try to correct the permission for the parent directory and create it.
$wp_filesystem->chmod( dirname( Path::get_instance()->get_default_path() ), FS_CHMOD_DIR );

if (
! $wp_filesystem->mkdir( Path::get_instance()->get_default_path(), FS_CHMOD_DIR ) &&
! $wp_filesystem->mkdir( Path::get_instance()->get_fallback_path(), FS_CHMOD_DIR )
) {
$url = add_query_arg( 'creation_error', 1, get_settings_url() );
}
}
}

wp_safe_redirect( $url , 303 );
die;

} );

/**
* Perform a manual backup
*
Expand Down
8 changes: 8 additions & 0 deletions admin/filesystem-credentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$_POST['action'] = 'hmbkp_request_credentials';
$extra_fields = array( 'action' );

if ( ! isset( $_GET['creation_error'] ) ) {
request_filesystem_credentials( admin_url( 'admin-post.php' ), '', isset( $_GET['connection_error'] ), false, $extra_fields );
}
14 changes: 11 additions & 3 deletions admin/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@

</h1>

<?php include_once( HMBKP_PLUGIN_PATH . 'admin/backups.php' ); ?>
<?php if ( has_server_permissions() ) : ?>

<p class="howto"><?php printf( __( 'If you\'re finding BackUpWordPress useful, please %1$s rate it on the plugin directory%2$s.', 'backupwordpress' ), '<a target="_blank" href="http://wordpress.org/support/view/plugin-reviews/backupwordpress">', '</a>' ); ?></p>
<?php include_once( HMBKP_PLUGIN_PATH . 'admin/backups.php' ); ?>

<?php include_once( HMBKP_PLUGIN_PATH . 'admin/upsell.php' ); ?>
<p class="howto"><?php printf( __( 'If you\'re finding BackUpWordPress useful, please %1$s rate it on the plugin directory%2$s.', 'backupwordpress' ), '<a target="_blank" href="http://wordpress.org/support/view/plugin-reviews/backupwordpress">', '</a>' ); ?></p>

<?php include_once( HMBKP_PLUGIN_PATH . 'admin/upsell.php' ); ?>

<?php else : ?>

<?php include_once( HMBKP_PLUGIN_PATH . 'admin/filesystem-credentials.php' ); ?>

<?php endif; ?>

</div>
32 changes: 24 additions & 8 deletions classes/class-path.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,29 @@ public function get_existing_path() {
*/
public function calculate_path() {

$paths = $this->get_possible_paths();

// Loop through possible paths, use the first one that exists/can be created and is writable.
foreach ( $paths as $path ) {
// Also handles fixing perms / directory already exists.
if ( wp_mkdir_p( $path ) && wp_is_writable( $path ) ) {
break;
}
}

/**
* If we managed to create a writable path then use that,
* otherwise just return the unwritable path.
*/
if ( file_exists( $path ) && wp_is_writable( $path ) ) {
$this->path = $path;
} else {
$this->path = reset( $paths );
}
}

public function get_possible_paths() {

$paths = array();

// If we have a custom path then try to use it.
Expand All @@ -293,13 +316,7 @@ public function calculate_path() {
// If that didn't work then fallback to a new directory in uploads.
$paths[] = $this->get_fallback_path();

// Loop through possible paths, use the first one that exists/can be created and is writable.
foreach ( $paths as $path ) {
if ( wp_mkdir_p( $path ) && file_exists( $path ) && wp_is_writable( $path ) ) { // Also handles fixing perms / directory already exists.
$this->path = $path;
break;
}
}
return $paths;
}

/**
Expand Down Expand Up @@ -410,7 +427,6 @@ public function move_old_backups( $from ) {
if ( false !== strpos( $from, WP_CONTENT_DIR ) && Path::get_path() !== $from ) {
rmdirtree( $from );
}

}

/**
Expand Down
5 changes: 4 additions & 1 deletion classes/class-requirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ class Requirement_Backup_Path_Permissions extends Requirement {
* @return string
*/
public static function test() {
return substr( sprintf( '%o', fileperms( Path::get_path() ) ), - 4 );
if ( is_readable( PATH::get_path() ) ) {
return substr( sprintf( '%o', fileperms( Path::get_path() ) ), - 4 );
}
return 'Unreadable';
}
}
Requirements::register( 'HM\BackUpWordPress\Requirement_Backup_Path_Permissions', 'Site' );
Expand Down
2 changes: 1 addition & 1 deletion classes/class-site-size.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Site Size class
*
* Use to calculate the total or partial size of the sites database and files.
* Use to calculate the total or partial size of the site's database and files.
*/
class Site_Size {

Expand Down
23 changes: 19 additions & 4 deletions functions/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,24 +393,39 @@ function rmdirtree( $dir ) {
@rmdir( $dir );

return true;

}

/**
* Check if a backup is possible with regards to file
* permissions etc.
* Check if we have read and write permission on the server
*
* @return bool
*/
function is_backup_possible() {
function has_server_permissions() {

if ( ! wp_is_writable( Path::get_path() ) || ! is_dir( Path::get_path() ) ) {
if ( ! wp_is_writable( Path::get_path() ) ) {
return false;
}

if ( ! is_readable( Path::get_root() ) ) {
return false;
}

return true;
}

/**
* Check if a backup is possible with regards to file
* permissions etc.
*
* @return bool
*/
function is_backup_possible() {

if ( ! has_server_permissions() || ! is_dir( Path::get_path() ) ) {
return false;
}

if ( ! Requirement_Mysqldump_Command_Path::test() && ! Requirement_PDO::test() ) {
return false;
}
Expand Down
Loading

0 comments on commit 9901a7f

Please sign in to comment.