recursive_sanitize_text_field( wp_unslash( $_POST['wp_db_exclude_table'] ) ) ); // phpcs:ignore
} else {
update_option( 'wp_db_exclude_table', '' );
}
$nonce = wp_create_nonce( 'wp-database-backup' );
wp_safe_redirect( site_url() . '/wp-admin/admin.php?page=wp-database-backup¬ification=save&_wpnonce=' . $nonce );
}
if ( true === isset( $_POST['wp_db_local_backup_path'] ) ) {
update_option( 'wp_db_local_backup_path', wp_db_filter_data( sanitize_text_field( wp_unslash( $_POST['wp_db_local_backup_path'] ) ) ) );
}
if ( isset( $_POST['wp_db_backup_email_id'] ) ) {
update_option( 'wp_db_backup_email_id', wp_db_filter_data( sanitize_email( wp_unslash( $_POST['wp_db_backup_email_id'] ) ) ) );
}
if ( isset( $_POST['wp_db_backup_email_attachment'] ) ) {
$email_attachment = sanitize_text_field( wp_unslash( $_POST['wp_db_backup_email_attachment'] ) );
update_option( 'wp_db_backup_email_attachment', $email_attachment );
}
if ( isset( $_POST['Submit'] ) && 'Save Settings' === $_POST['Submit'] ) {
if ( isset( $_POST['wp_db_backup_destination_Email'] ) ) {
update_option( 'wp_db_backup_destination_Email', 1 );
} else {
update_option( 'wp_db_backup_destination_Email', 0 );
}
if ( true === isset( $_POST['wp_db_local_backup'] ) ) {
update_option( 'wp_db_local_backup', 1 );
} else {
update_option( 'wp_db_local_backup', 0 );
}
}
}
$wp_db_backup_destination_email = get_option( 'wp_db_backup_destination_Email' );
if ( isset( $_GET['page'] ) && 'wp-database-backup' === $_GET['page'] && isset( $_GET['action'] ) && 'unlink' === $_GET['action'] ) {
// Specify the target directory and add forward slash.
$dir = plugin_dir_path( __FILE__ ) . 'Destination/Dropbox/tokens/';
// Open the directory.
$dir_handle = opendir( $dir );
// Loop over all of the files in the folder.
$file = readdir( $dir_handle );
while ( $file ) {
// If $file is NOT a directory remove it.
if ( ! is_dir( $file ) ) {
unlink( $dir . $file );
}
}
// Close the directory.
closedir( $dir_handle );
wp_safe_redirect( site_url() . '/wp-admin/admin.php?page=wp-database-backup' );
}
$nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : '';
if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $nonce, 'wp-database-backup' ) ) {
if ( isset( $_GET['action'] ) && current_user_can( 'manage_options' ) ) {
switch ( (string) $_GET['action'] ) {
case 'createdbbackup':
$this->wp_db_backup_event_process();
wp_safe_redirect( site_url() . '/wp-admin/admin.php?page=wp-database-backup¬ification=create&_wpnonce=' . $nonce );
break;
case 'removebackup':
if ( true === isset( $_GET['index'] ) ) {
$index = (int) $_GET['index'];
$options = get_option( 'wp_db_backup_backups' );
$newoptions = array();
$count = 0;
foreach ( $options as $option ) {
if ( $count !== $index ) {
$newoptions[] = $option;
}
$count++;
}
if ( file_exists( $options[ $index ]['dir'] ) ) {
unlink( $options[ $index ]['dir'] );
}
$file_sql = explode( '.', $options[ $index ]['dir'] );
if ( file_exists( $file_sql[0] . '.sql' ) ) {
unlink( $file_sql[0] . '.sql' );
}
update_option( 'wp_db_backup_backups', $newoptions );
$nonce = wp_create_nonce( 'wp-database-backup' );
wp_safe_redirect( site_url() . '/wp-admin/admin.php?page=wp-database-backup¬ification=delete&_wpnonce=' . $nonce );
}
break;
case 'clear_temp_db_backup_file':
$options = get_option( 'wp_db_backup_backups' );
$newoptions = array();
$backup_check_list = array( '.htaccess', 'index.php' );
$delete_message = 'WPDB : Deleted Files:';
foreach ( $options as $option ) {
$backup_check_list[] = $option['filename'];
}
$path_info = wp_upload_dir();
$wp_db_backup_path = $path_info['basedir'] . '/db-backup';
// Open a directory, and read its contents.
if ( is_dir( $wp_db_backup_path ) ) {
$dh = opendir( $wp_db_backup_path );
if ( $dh ) {
$file = readdir( $dh );
while ( false !== $file ) {
if ( ! ( in_array( $file, $backup_check_list, true ) ) ) {
if ( file_exists( $wp_db_backup_path . '/' . $file ) ) {
unlink( $wp_db_backup_path . '/' . $file );
}
$delete_message .= ' ' . $file;
}
}
closedir( $dh );
}
}
wp_safe_redirect( site_url() . '/wp-admin/admin.php?page=wp-database-backup¬ification=clear_temp_db_backup_file&_wpnonce=' . $nonce );
break;
case 'restorebackup':
$index = (int) $_GET['index'];
$options = get_option( 'wp_db_backup_backups' );
$newoptions = array();
$count = 0;
foreach ( $options as $option ) {
if ( $count !== $index ) {
$newoptions[] = $option;
}
$count++;
}
if ( isset( $options[ $index ]['sqlfile'] ) ) { // Added for extract zip file V.3.3.0.
$database_file = ( $options[ $index ]['sqlfile'] );
} else {
$database_file = ( $options[ $index ]['dir'] );
$file_sql = explode( '.', $options[ $index ]['dir'] );
$database_file = ( $file_sql[0] . '.sql' );
}
$database_name = $this->wp_backup_get_config_db_name();
$database_user = $this->wp_backup_get_config_data( 'DB_USER' );
$datadase_password = $this->wp_backup_get_config_data( 'DB_PASSWORD' );
$database_host = $this->wp_backup_get_config_data( 'DB_HOST' );
$path_info = wp_upload_dir();
// Added for extract zip file V.3.3.0.
$ext_path_info = $path_info['basedir'] . '/db-backup';
$database_zip_file = $options[ $index ]['dir'];
if ( class_exists( 'ZipArchive' ) ) {
$zip = new ZipArchive();
if ( $zip->open( $database_zip_file ) === true ) {
$zip->extractTo( $ext_path_info );
$zip->close();
}
} else {
require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
$archive = new PclZip( $database_zip_file );
$dir = $path_info['basedir'] . '/db-backup/';
if ( ! $archive->extract( PCLZIP_OPT_PATH, $dir ) ) {
wp_die( 'Unable to extract zip file. Please check that zlib php extension is enabled.', 'ZIP Error' );
}
}
// End for extract zip file V.3.3.0.
set_time_limit( 0 );
if ( '' !== ( trim( (string) $database_name ) ) && '' !== ( trim( (string) $database_user ) ) && '' !== ( trim( (string) $datadase_password ) ) && '' !== ( trim( (string) $database_host ) ) ) {
$conn = mysqli_connect( (string) $database_host, (string) $database_user, (string) $datadase_password ); // phpcs:ignore
if ( $conn ) {
// Start Select the database.
if ( ! mysqli_select_db( (string) $database_name, $conn ) ) { // phpcs:ignore
$sql = 'CREATE DATABASE IF NOT EXISTS `' . (string) $database_name . '`';
mysqli_query( $sql, $conn ); // phpcs:ignore
mysqli_select_db( (string) $database_name, $conn ); // phpcs:ignore
}
/* END: Select the Database */
/* BEGIN: Remove All Tables from the Database */
$found_tables = null;
$result = mysqli_query( 'SHOW TABLES FROM `{' . (string) $database_name . '}`', $conn ); // phpcs:ignore
if ( $result ) {
$row = mysqli_fetch_row( $result ); // phpcs:ignore
while ( $row ) {
$found_tables[] = $row[0];
}
if ( count( $found_tables ) > 0 ) {
foreach ( $found_tables as $table_name ) {
mysqli_query( 'DROP TABLE `{' . (string) $database_name . "}`.{$table_name}", $conn ); // phpcs:ignore
}
}
}
/* END: Remove All Tables from the Database */
/* BEGIN: Restore Database Content */
if ( isset( $database_file ) ) {
$database_file = $database_file;
if ( file_exists( $database_file ) ) {
$sql_file = file_get_contents( $database_file, true );
$sql_queries = explode( ";\n", $sql_file );
$sql_queries_count = count( $sql_queries );
for ( $i = 0; $i < $sql_queries_count; $i++ ) {
mysqli_query( $sql_queries[ $i ], $conn ); // phpcs:ignore
}
}
}
}
}
if ( isset( $options[ $index ]['sqlfile'] ) && file_exists( $options[ $index ]['sqlfile'] ) ) { // Added for extract zip file V.3.3.0.
if ( file_exists( $options[ $index ]['sqlfile'] ) ) {
unlink( $options[ $index ]['sqlfile'] );
}
} else {
$database_file = ( $options[ $index ]['dir'] );
$file_sql = explode( '.', $options[ $index ]['dir'] );
$database_file = ( $file_sql[0] . '.sql' );
if ( file_exists( $database_file ) ) {
unlink( $database_file );
}
}
wp_safe_redirect( site_url() . '/wp-admin/admin.php?page=wp-database-backup¬ification=restore&_wpnonce=' . $nonce );
break;
/* END: Restore Database Content */
}
}
}
}
}
if ( is_admin() && current_user_can( 'manage_options' ) ) {
register_setting( 'wp_db_backup_options', 'wp_db_backup_options', array( $this, 'wp_db_backup_validate' ) );
add_settings_section( 'wp_db_backup_main', 'WP-Database-Backup', array( $this, 'wp_db_backup_section_text' ), 'manage_options' );
}
}
/**
* Validate data.
*
* @param string $input - Input data.
*/
public function wp_db_backup_validate( $input ) {
return $input;
}
/**
* Setting page.
*/
public function wp_db_backup_settings_page() {
$options = get_option( 'wp_db_backup_backups' );
$settings = get_option( 'wp_db_backup_options' ); ?>
';
}
$upload_dir = wp_upload_dir();
$dir = $upload_dir['basedir'] . '/db-backup';
if ( ! is_dir( $dir ) ) {
$dir = $upload_dir['basedir'];
}
if ( is_dir( $dir ) && ! is_writable( $dir ) ) {
?>
×
WP Database Backup
Error: Permission denied, make sure you have write permission for
folder
';
echo '
';
$nonce = wp_create_nonce( 'wp-database-backup' );
$wp_db_backup_search_text = get_option( 'wp_db_backup_search_text' );
$wp_db_backup_replace_text = get_option( 'wp_db_backup_replace_text' );
if ( ( false === empty( $wp_db_backup_search_text ) ) && ( false === empty( $wp_db_backup_replace_text ) ) ) {
echo '
Create New Database Backup with Search/Replace ';
echo '
Backup file will replace ' . esc_attr( $wp_db_backup_search_text ) . ' text with ' . esc_attr( $wp_db_backup_replace_text ) . ' . For Regular Database Backup without replace then Go to Dashboard=>Tool=>WP-DB Backup > Settings > Search and Replace - Set Blank Fields
';
} else {
echo '
Create New Database Backup ';
}
?>
var $j = jQuery.noConflict();
$j(document).ready(function () {
$j(".popoverid").popover();
var table = $j("#example").DataTable({
order: [[0, "desc"]],
});
$j("#create_backup").click(function() {
$j("#backup_process").show();
$j("#create_backup").attr("disabled", true);
});
});
function excludetableall(){
var checkboxes = document.getElementsByClassName("wp_db_exclude_table");
var checked = "";
if($j("#wp_db_exclude_table_all").prop("checked") == true){
checked = "checked";
}
$j(".wp_db_exclude_table").each(function() {
this.checked = checked;
});
}
';
echo '
';
} else {
echo '
No Database Backups Created!
';
}
echo '
If you like WP Database Backup please leave us a rating . Many thanks in advance!
';
echo '
';
echo '
';
echo '
';
?>
Vision & Mission
We strive to provide the best Backup Plugin in the world.
Delivering a good user experience means a lot to us, so we try our best to reply each and every question.
wp_db_backup_format_bytes( $df );
$du = $this->wp_db_backup_format_bytes( $du );
$dt = $this->wp_db_backup_format_bytes( $dt );
?>
Disk Space
Clear all old/temp database backup files
Click above button to clear all your old or temporary created database backup
files.
It only delete file from backup directory which is not in 'Database Backups'
listing(all other file excluding backup files listed in 'Database Backups' ).
Before
using this option make sure that you have save your database backup on safe
place.
The disk that your backup is saved on doesn't have enough free space? Backup disk
is
almost full? Low disk space for backup? Backup failed due to lack of space? As
you
may set up a schedule to automatically do backup daily or weekly, and the size
of
disk space is limited, so your backup disk will run out of space quickly or
someday.
It is a real pain to manually delete old backups. Don't worry about it. WP
Database
Backup makes it easy to delete old/temparary backup files using this option.
Not writable ' : '
writable
'; ?>
Not writable ' : '
writable
'; ?>
get_mysqldump_command_path() ) ? 'Yes ' : '
No
';
?>
Setting
Value
Database Host
Database Name
Database User
Database Type
MYSQL
get_var( 'SELECT VERSION() AS version' ); // phpcs:ignore
wp_cache_set( 'wpdb_mysqlversion', $mysqlversion, '', 18000 );
}
?>
Database Version
v
No.
Tables
Records
get_results( 'SHOW TABLE STATUS' ); // phpcs:ignore
foreach ( $tablesstatus as $tablestatus ) {
$tablestatus_arr = (array) $tablestatus;
if ( 0 === ( $no % 2 ) ) {
$style = '';
} else {
$style = ' class="alternate"';
}
$no++;
echo '';
echo '' . esc_attr( number_format_i18n( $no ) ) . ' ';
echo '' . esc_attr( $tablestatus_arr['Name'] ) . ' ';
echo '' . esc_attr( number_format_i18n( $tablestatus_arr['Rows'] ) ) . ' ';
$row_usage += $tablestatus_arr['Rows'];
echo ' ';
}
echo '';
echo ' Total: ';
echo '' . esc_attr( number_format_i18n( $no ) ) . ' Table ';
echo '' . esc_attr( number_format_i18n( $row_usage ) ) . ' Records ';
echo ' ';
?>
Setting
Value
WordPress Version
Home URL
Site URL
Upload directory URL
Plugin Name
Version
' . esc_attr( $plugin['Name'] ) . '
' . esc_attr( $plugin['Version'] ) . '
';
}
?>
Active Theme Name
Version
' . esc_attr( $my_theme->get( 'Name' ) ) . '
' . esc_attr( $my_theme->get( 'Version' ) ) . '
';
?>
Drafts Post Count
draft );
?>
Publish Post Count
publish );
?>
Drafts Pages Count
draft );
?>
Publish Pages Count
publish );
?>
Approved Comments Count
approved );
?>
get_col( 'SHOW TABLES' ); // phpcs:ignore
$output = '';
foreach ( $tables as $table ) {
if ( empty( $wp_db_exclude_table ) || ( ! ( in_array( $table, $wp_db_exclude_table, true ) ) ) ) {
$result = $wpdb->get_results( "SELECT * FROM {$table}", ARRAY_N ); // phpcs:ignore
$row2 = $wpdb->get_row( 'SHOW CREATE TABLE ' . $table, ARRAY_N ); // phpcs:ignore
$output .= "\n\n" . $row2[1] . ";\n\n";
$result_count = count( $result );
for ( $i = 0; $i < $result_count; $i++ ) {
$row = $result[ $i ];
$output .= 'INSERT INTO ' . $table . ' VALUES(';
$result_o_index = count( $result[0] );
for ( $j = 0; $j < $result_o_index; $j++ ) {
$row[ $j ] = $wpdb->_real_escape( $row[ $j ] );
$output .= ( isset( $row[ $j ] ) ) ? '"' . $row[ $j ] . '"' : '""';
if ( $j < ( $result_o_index - 1 ) ) {
$output .= ',';
}
}
$output .= ");\n";
}
$output .= "\n";
}
}
$wpdb->flush();
/* BEGIN : Prevent saving backup plugin settings in the database dump */
add_option( 'wp_db_backup_backups', $options_backup );
add_option( 'wp_db_backup_options', $settings_backup );
/* END : Prevent saving backup plugin settings in the database dump */
return $output;
}
/**
* Mysql Dump set path.
*
* @param string $path - Path.
*/
public function set_mysqldump_command_path( $path ) {
$this->mysqldump_command_path = $path;
}
/**
* Mysql Dump get path.
*/
public function get_mysqldump_command_path() {
// Check shell_exec is available.
if ( ! self::is_shell_exec_available() ) {
return '';
}
// Return now if it's already been set.
if ( isset( $this->mysqldump_command_path ) ) {
return $this->mysqldump_command_path;
}
$this->mysqldump_command_path = '';
// Does mysqldump work.
if ( is_null( shell_exec( 'hash mysqldump 2>&1' ) ) ) { // phpcs:ignore
// If so store it for later.
$this->set_mysqldump_command_path( 'mysqldump' );
// And return now.
return $this->mysqldump_command_path;
}
// List of possible mysqldump locations.
$mysqldump_locations = array(
'/usr/local/bin/mysqldump',
'/usr/local/mysql/bin/mysqldump',
'/usr/mysql/bin/mysqldump',
'/usr/bin/mysqldump',
'/opt/local/lib/mysql6/bin/mysqldump',
'/opt/local/lib/mysql5/bin/mysqldump',
'/opt/local/lib/mysql4/bin/mysqldump',
'/xampp/mysql/bin/mysqldump',
'/Program Files/xampp/mysql/bin/mysqldump',
'/Program Files/MySQL/MySQL Server 6.0/bin/mysqldump',
'/Program Files/MySQL/MySQL Server 5.5/bin/mysqldump',
'/Program Files/MySQL/MySQL Server 5.4/bin/mysqldump',
'/Program Files/MySQL/MySQL Server 5.1/bin/mysqldump',
'/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump',
'/Program Files/MySQL/MySQL Server 4.1/bin/mysqldump',
);
// Find the one which works.
foreach ( $mysqldump_locations as $location ) {
if ( is_executable( self::conform_dir( $location ) ) ) {
$this->set_mysqldump_command_path( $location );
}
}
return $this->mysqldump_command_path;
}
/**
* Check dir.
*
* @param string $dir - Dir Details.
* @param bool $recursive - Recursive.
*/
public static function conform_dir( $dir, $recursive = false ) {
// Assume empty dir is root.
if ( ! $dir ) {
$dir = '/';
}
// Replace single forward slash (looks like double slash because we have to escape it).
$dir = str_replace( '\\', '/', $dir );
$dir = str_replace( '//', '/', $dir );
// Remove the trailing slash.
if ( '/' !== $dir ) {
$dir = untrailingslashit( $dir );
}
// Carry on until completely normalized.
if ( ! $recursive && self::conform_dir( $dir, true ) !== $dir ) {
return self::conform_dir( $dir );
}
return (string) $dir;
}
/**
* Check Shell.
*/
public static function is_shell_exec_available() {
// Are we in Safe Mode.
if ( self::is_safe_mode_active() ) {
return false;
}
// Is shell_exec or escapeshellcmd or escapeshellarg disabled?
if ( array_intersect( array( 'shell_exec', 'escapeshellarg', 'escapeshellcmd' ), array_map( 'trim', explode( ',', ini_get( 'disable_functions' ) ) ) ) ) {
return false;
}
// Can we issue a simple echo command?
if ( ! shell_exec( 'echo WP Backup' ) ) { // phpcs:ignore
return false;
}
return true;
}
/**
* Check Safe mode active.
*
* @param string $ini_get_callback - String cmd.
* @return bool
*/
public static function is_safe_mode_active( $ini_get_callback = 'ini_get' ) {
$safe_mode = call_user_func( $ini_get_callback, 'safe_mode' );
if ( ( $safe_mode ) && 'off' !== strtolower( $safe_mode ) ) {
return true;
}
return false;
}
/**
* Database dump.
*
* @param string $sql_filename - File name.
*/
public function mysqldump( $sql_filename ) {
$this->mysqldump_method = 'mysqldump';
$host = explode( ':', DB_HOST );
$host = reset( $host );
$port = strpos( DB_HOST, ':' ) ? end( explode( ':', DB_HOST ) ) : '';
// Path to the mysqldump executable.
$cmd = escapeshellarg( $this->get_mysqldump_command_path() );
// We don't want to create a new DB.
$cmd .= ' --no-create-db';
// Allow lock-tables to be overridden.
if ( ! defined( 'WPDB_MYSQLDUMP_SINGLE_TRANSACTION' ) || WPDB_MYSQLDUMP_SINGLE_TRANSACTION !== false ) {
$cmd .= ' --single-transaction';
}
// Make sure binary data is exported properly.
$cmd .= ' --hex-blob';
// Username.
$cmd .= ' -u ' . escapeshellarg( DB_USER );
// Don't pass the password if it's blank.
if ( DB_PASSWORD ) {
$cmd .= ' -p' . escapeshellarg( DB_PASSWORD );
}
// Set the host.
$cmd .= ' -h ' . escapeshellarg( $host );
// Set the port if it was set.
if ( ! empty( $port ) && is_numeric( $port ) ) {
$cmd .= ' -P ' . $port;
}
// The file we're saving too.
$cmd .= ' -r ' . escapeshellarg( $sql_filename );
$wp_db_exclude_table = array();
$wp_db_exclude_table = get_option( 'wp_db_exclude_table' );
if ( ! empty( $wp_db_exclude_table ) ) {
foreach ( $wp_db_exclude_table as $wp_db_exclude_table ) {
$cmd .= ' --ignore-table=' . DB_NAME . '.' . $wp_db_exclude_table;
}
}
// The database we're dumping.
$cmd .= ' ' . escapeshellarg( DB_NAME );
// Pipe STDERR to STDOUT.
$cmd .= ' 2>&1';
// Store any returned data in an error.
$stderr = shell_exec( $cmd ); // phpcs:ignore
// Skip the new password warning that is output in mysql > 5.6.
if ( trim( $stderr ) === 'Warning: Using a password on the command line interface can be insecure.' ) {
$stderr = '';
}
if ( $stderr ) {
$this->error( $this->get_mysqldump_method(), $stderr );
}
return $this->verify_mysqldump( $sql_filename );
}
/**
* Error.
*
* @param string $context - Data.
* @param object $error - Error data.
*/
public function error( $context, $error ) {
if ( empty( $context ) || empty( $error ) ) {
return;
}
$error_str = implode( ':', (array) $error );
$_key = md5( $error_str );
$this->errors[ $context ][ $_key ] = $error;
}
/**
* Verify Dump.
*
* @param string $sql_filename - Sql file.
* @return bool
*/
public function verify_mysqldump( $sql_filename ) {
// If we've already passed then no need to check again.
if ( ! empty( $this->mysqldump_verified ) ) {
return true;
}
// If there are mysqldump errors delete the database dump file as mysqldump will still have written one.
if ( $this->get_errors( $this->get_mysqldump_method() ) && file_exists( $sql_filename ) ) {
if ( file_exists( $database_file ) ) {
unlink( $database_file );
}
}
// If we have an empty file delete it.
if ( 0 === filesize( $sql_filename ) ) {
if ( file_exists( $sql_filename ) ) {
unlink( $sql_filename );
}
}
// If the file still exists then it must be good.
if ( file_exists( $sql_filename ) ) {
$this->mysqldump_verified = true;
return $this->mysqldump_verified;
}
return false;
}
/**
* Get error.
*
* @param string $context - Data.
* @return string
*/
public function get_errors( $context = null ) {
if ( ! empty( $context ) ) {
return isset( $this->errors[ $context ] ) ? $this->errors[ $context ] : array();
}
return $this->errors;
}
/**
* Get mysql dump method.
*
* @return string
*/
public function get_mysqldump_method() {
return $this->mysqldump_method;
}
// End : Generate SQL DUMP using cmd 06-03-2016.
/**
* Create zip.
*/
public function wp_db_backup_create_archive() {
// Begin : Setup Upload Directory, Secure it and generate a random file name.
$source_directory = $this->wp_db_backup_wp_config_path();
$path_info = wp_upload_dir();
$htasses_text = '';
wp_mkdir_p( $path_info['basedir'] . '/db-backup' );
fclose( fopen( $path_info['basedir'] . '/db-backup/index.php', 'w' ) ); // phpcs:ignore
// Added htaccess file 08-05-2015 for prevent directory listing.
// Fixed Vulnerability 22-06-2016 for prevent direct download.
if ( 1 === (int) get_option( 'wp_db_backup_enable_htaccess' ) ) {
$f = fopen( $path_info['basedir'] . '/db-backup/.htaccess', 'w' ); // phpcs:ignore
fwrite( // phpcs:ignore
$f,
'#These next two lines will already exist in your .htaccess file
RewriteEngine On
RewriteBase /
# Add these lines right after the preceding two
RewriteCond %{REQUEST_FILENAME} ^.*(.zip)$
RewriteCond %{HTTP_COOKIE} !^.*can_download.*$ [NC]
RewriteRule . - [R=403,L]'
); // phpcs:ignore
fclose( $f ); // phpcs:ignore
}
// Begin : Generate SQL DUMP and save to file database.sql.
$wp_site_name = preg_replace( '/[^A-Za-z0-9\_]/', '_', get_bloginfo( 'name' ) );
$wp_db_file_name = $wp_site_name . '_' . gmdate( 'Y_m_d' ) . '_' . time() . '_' . substr( md5( AUTH_KEY ), 0, 7 ) . '_wpdb';
$sql_filename = $wp_db_file_name . '.sql';
$filename = $wp_db_file_name . '.zip';
// Begin : Generate SQL DUMP using cmd 06-03-2016.
$my_sql_dump = 0;
if ( $this->get_mysqldump_command_path() ) {
if ( ! $this->mysqldump( $path_info['basedir'] . '/db-backup/' . $sql_filename ) ) {
$my_sql_dump = 1;
}
} else {
$my_sql_dump = 1;
}
if ( 1 === (int) $my_sql_dump ) {
$handle = fopen( $path_info['basedir'] . '/db-backup/' . $sql_filename, 'w+' ); // phpcs:ignore
fwrite( $handle, $this->wp_db_backup_create_mysql_backup() ); // phpcs:ignore
fclose( $handle ); // phpcs:ignore
}
/* End : Generate SQL DUMP using cmd 06-03-2016 */
$wp_db_backup_search_text = get_option( 'wp_db_backup_search_text' );
$wp_db_backup_replace_text = get_option( 'wp_db_backup_replace_text' );
if ( ( false === empty( $wp_db_backup_search_text ) ) && ( false === empty( $wp_db_backup_replace_text ) ) ) {
$backup_str = file_get_contents( $path_info['basedir'] . '/db-backup/' . $sql_filename ); // phpcs:ignore
$filecontent = wp_remote_get( $path_info['basedir'] . '/db-backup/' . $sql_filename );
$backup_str = str_replace( $wp_db_backup_search_text, $wp_db_backup_replace_text, $backup_str ); // phpcs:ignore
file_put_contents( $path_info['basedir'] . '/db-backup/' . $sql_filename, $backup_str ); // phpcs:ignore
}
/* End : Generate SQL DUMP and save to file database.sql */
$upload_path = array(
'filename' => ( $filename ),
'dir' => ( $path_info['basedir'] . '/db-backup/' . $filename ),
'url' => ( $path_info['baseurl'] . '/db-backup/' . $filename ),
'size' => 0,
);
$arcname = $path_info['basedir'] . '/db-backup/' . $wp_db_file_name . '.zip';
if ( class_exists( 'ZipArchive' ) ) {
$zip = new ZipArchive();
$zip->open( $arcname, ZipArchive::CREATE );
$zip->addFile( $path_info['basedir'] . '/db-backup/' . $sql_filename, $sql_filename );
$zip->close();
} else {
require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
$archive = new PclZip( $arcname );
$v_dir = $path_info['basedir'] . '/db-backup/' . $sql_filename;
$v_remove = $path_info['basedir'] . '/db-backup';
// Create the archive.
$v_list = $archive->create( $v_dir, PCLZIP_OPT_REMOVE_PATH, $v_remove );
}
global $wpdb;
$mysqlversion = wp_cache_get( 'wpdb_mysqlversion' );
if ( true === empty( $mysqlversion ) ) {
$mysqlversion = $wpdb->get_var( 'SELECT VERSION() AS version' ); // phpcs:ignore
wp_cache_set( 'wpdb_mysqlversion', $mysqlversion, '', 18000 );
}
$my_theme = wp_get_theme();
$active_plugin = count(get_option('active_plugins'));
$total_plugin = count(get_plugins());
$log_message = 'WordPress Version : ' . get_bloginfo( 'version' );
$log_message .= ' Database Version : ' . $mysqlversion;
$log_message .= ' Active Theme Name : ' . $my_theme->get( 'Name' );
$log_message .= ' Theme Version : ' . $my_theme->get( 'Version' );
$log_message .= ' Plugin Count : ' . $total_plugin;
$log_message .= ' Active Plugins : ' . $active_plugin;
$upload_path['size'] = filesize( $upload_path['dir'] );
$upload_path['sqlfile'] = $path_info['basedir'] . '/db-backup/' . $sql_filename;
$wp_db_log = get_option( 'wp_db_log' );
if ( 1 === (int) $wp_db_log ) {
$wp_db_exclude_table = get_option( 'wp_db_exclude_table' );
if ( ! empty( $wp_db_exclude_table ) ) {
$log_message .= ' Exclude Table : ' . implode( ', ', $wp_db_exclude_table );
}
$upload_path['log'] = $log_message;
}
$options = get_option( 'wp_db_backup_backups' );
$newoptions = array();
$number_of_existing_backups = count( (array) $options );
$number_of_backups_from_user = get_option( 'wp_local_db_backup_count' );
if ( ! empty( $number_of_backups_from_user ) ) {
if ( ! ( $number_of_existing_backups < $number_of_backups_from_user ) ) {
$diff = $number_of_existing_backups - $number_of_backups_from_user;
for ( $i = 0; $i <= $diff; $i++ ) {
$index = $i;
if ( file_exists( $options[ $index ]['dir'] ) ) {
unlink( $options[ $index ]['dir'] );
}
$file_sql = explode( '.', $options[ $index ]['dir'] );
if ( file_exists( $file_sql[0] . '.sql' ) ) {
unlink( $file_sql[0] . '.sql' );
}
}
for ( $i = ( $diff + 1 ); $i < $number_of_existing_backups; $i++ ) {
$index = $i;
$newoptions[] = $options[ $index ];
}
update_option( 'wp_db_backup_backups', $newoptions );
}
}
if ( file_exists( $path_info['basedir'] . '/db-backup/' . $sql_filename ) ) {
unlink( $path_info['basedir'] . '/db-backup/' . $sql_filename );
}
return $upload_path;
}
/**
* Config Path.
*/
public function wp_db_backup_wp_config_path() {
$base = dirname( __FILE__ );
$path = false;
if ( file_exists( dirname( dirname( $base ) ) . '/wp-config.php' ) ) {
$path = dirname( dirname( $base ) );
} else {
if ( file_exists( dirname( dirname( dirname( $base ) ) ) . '/wp-config.php' ) ) {
$path = dirname( dirname( dirname( $base ) ) );
} else {
$path = false;
}
}
if ( false !== $path ) {
$path = str_replace( '\\', '/', $path );
}
return $path;
}
/**
* Backup Process.
*/
public function wp_db_backup_event_process() {
// Added in v.3.9.5!
set_time_limit( 0 );
$details = $this->wp_db_backup_create_archive();
$options = get_option( 'wp_db_backup_backups' );
if ( ! $options ) {
$options = array();
}
$is_search_replace_flag = 0;
$wp_db_log = get_option( 'wp_db_log' );
if ( 1 === (int) $wp_db_log ) {
$log_message = $details['log'];
$wp_db_backup_search_text = get_option( 'wp_db_backup_search_text' );
$wp_db_backup_replace_text = get_option( 'wp_db_backup_replace_text' );
if ( ( false === empty( $wp_db_backup_search_text ) ) && ( false === empty( $wp_db_backup_replace_text ) ) ) {
$log_message .= ' Replaced/Search text - ' . $wp_db_backup_search_text . ' With -' . $wp_db_backup_replace_text;
$is_search_replace_flag = 1;
}
} else {
$log_message = '';
}
$options[] = array(
'date' => time(),
'filename' => $details['filename'],
'url' => $details['url'],
'dir' => $details['dir'],
'log' => $log_message,
'search_replace' => $is_search_replace_flag,
'sqlfile' => $details['sqlfile'],
'size' => $details['size'],
);
$wp_db_remove_local_backup = get_option( 'wp_db_remove_local_backup' );
if ( 1 !== (int) $wp_db_remove_local_backup ) {
update_option( 'wp_db_backup_backups', $options );
}
$wp_db_remove_local_backup = get_option( 'wp_db_remove_local_backup' );
$destination = ( 1 === (int) $wp_db_remove_local_backup ) ? '' : 'Local, ';
$args = array( $details['filename'], $details['dir'], $log_message, $details['size'], $destination );
do_action_ref_array( 'wp_db_backup_completed', array( &$args ) );
}
/**
* Cron schedule.
*
* @param array $schedules - Schedules details.
*/
public function wp_db_backup_cron_schedules( $schedules ) {
$schedules['hourly'] = array(
'interval' => 3600,
'display' => 'hourly',
);
$schedules['twicedaily'] = array(
'interval' => 43200,
'display' => 'twicedaily',
);
$schedules['daily'] = array(
'interval' => 86400,
'display' => 'daily',
);
$schedules['weekly'] = array(
'interval' => 604800,
'display' => 'Once Weekly',
);
$schedules['monthly'] = array(
'interval' => 2635200,
'display' => 'Once a month',
);
return $schedules;
}
/**
* Schedular activation.
*/
public function wp_db_backup_scheduler_activation() {
$options = get_option( 'wp_db_backup_options' );
if ( ( ! wp_next_scheduled( 'wp_db_backup_event' ) ) && ( true === isset( $options['enable_autobackups'] ) ) ) {
wp_schedule_event( time(), $options['autobackup_frequency'], 'wp_db_backup_event' );
}
}
/**
* Config data.
*
* @param string $key - key name.
*/
public function wp_backup_get_config_data( $key ) {
$filepath = get_home_path() . '/wp-config.php';
$config_file = file_get_contents( "$filepath", true );
switch ( $key ) {
case 'DB_NAME':
preg_match( "/'DB_NAME',\s*'(.*)?'/", $config_file, $matches );
break;
case 'DB_USER':
preg_match( "/'DB_USER',\s*'(.*)?'/", $config_file, $matches );
break;
case 'DB_PASSWORD':
preg_match( "/'DB_PASSWORD',\s*'(.*)?'/", $config_file, $matches );
break;
case 'DB_HOST':
preg_match( "/'DB_HOST',\s*'(.*)?'/", $config_file, $matches );
break;
}
return $matches[1];
}
/**
* Get db name from config.
*/
public function wp_backup_get_config_db_name() {
$filepath = get_home_path() . '/wp-config.php';
$config_file = file_get_contents( "$filepath", true );
preg_match( "/'DB_NAME',\s*'(.*)?'/", $config_file, $matches );
return $matches[1];
}
/**
* Recursive sanitation for an array
*
* @param array $array - Array data to sanitize.
*
* @return mixed
*/
public function recursive_sanitize_text_field( $array ) {
foreach ( $array as $key => &$value ) {
if ( is_array( $value ) ) {
$value = $this->recursive_sanitize_text_field( $value );
} else {
$value = sanitize_text_field( $value );
}
}
return $array;
}
/**
* Enqueue scripts and style
*/
public function admin_scripts_style() {
if ( true === $this->is_wpdb_page() ) {
wp_enqueue_script( 'jquery' );
wp_register_script( 'bootstrapjs', WPDB_PLUGIN_URL . '/assets/js/bootstrap.min.js', array( 'jquery' ), WPDB_VERSION, true );
wp_enqueue_script( 'bootstrapjs' );
wp_register_style( 'bootstrapcss', WPDB_PLUGIN_URL . '/assets/css/bootstrap.min.css', array(), WPDB_VERSION );
wp_enqueue_style( 'bootstrapcss' );
wp_register_script( 'dataTables', WPDB_PLUGIN_URL . '/assets/js/jquery.dataTables.js', array( 'jquery' ), WPDB_VERSION, true );
wp_enqueue_script( 'dataTables' );
wp_register_style( 'dataTablescss', WPDB_PLUGIN_URL . '/assets/css/jquery.dataTables.css', array(), WPDB_VERSION );
wp_enqueue_style( 'dataTablescss' );
wp_register_style( 'wpdbcss', WPDB_PLUGIN_URL . '/assets/css/wpdb_admin.css', array(), WPDB_VERSION );
wp_enqueue_style( 'wpdbcss' );
}
}
/**
* Check is plugin page.
*/
public function is_wpdb_page() {
if ( is_admin() ) {
return isset( $_REQUEST['page'] ) && preg_match( '/wp-database-backup/', $_REQUEST['page'] ) ? true : false; // phpcs:ignore
} else {
return true;
}
}
// loading admin scripts/styles on admin page only
public function wpdbbkp_admin_style($hook_suffix)
{
if($hook_suffix=="tools_page_wp-database-backup" || $hook_suffix=="toplevel_page_wp-database-backup")
{
wp_enqueue_script('wpdbbkp-admin-script', WPDB_PLUGIN_URL . '/assets/js/wpdbbkp-admin.js', array('jquery'), WPDB_VERSION, 'true' );
wp_localize_script('wpdbbkp-admin-script', 'wpdbbkp_script_vars', array(
'nonce' => wp_create_nonce( 'wpdbbkp-admin-nonce' ),
)
);
}
}
public function wpdbbkp_admin_newsletter_script($hook_suffix ) {
if($hook_suffix=="tools_page_wp-database-backup" || $hook_suffix=="toplevel_page_wp-database-backup")
{
wp_enqueue_script('wpdbbkp-admin-newsletter-script', WPDB_PLUGIN_URL . '/assets/js/wpdbbkp-admin-newsletter.js', array('jquery'), WPDB_VERSION, 'true' );
$current_screen = get_current_screen();
if(isset($current_screen->post_type)){
$post_type = $current_screen->post_type;
}
$post_id = get_the_ID();
if(isset($_GET['tag_ID'])){
$post_id = intval($_GET['tag_ID']);
}
$data = array(
'current_url' => wpdbbkp_get_current_url(),
'post_id' => $post_id,
'ajax_url' => admin_url( 'admin-ajax.php' ),
'post_type' => $post_type,
'page_now' => $hook_suffix,
'wpdbbkp_security_nonce' => wp_create_nonce('wpdbbkp_ajax_check_nonce'),
);
$data = apply_filters('wpdbbkp_localize_filter',$data,'wpdbbkp_localize_data');
wp_localize_script( 'wpdbbkp-admin-newsletter-script', 'wpdbbkp_localize_data', $data );
}
}
public function wpdbbkp_sanitize_textarea_field( $str ) {
if ( is_object( $str ) || is_array( $str ) ) {
return '';
}
$str = (string) $str;
$filtered = wp_check_invalid_utf8( $str );
if ( strpos( $filtered, '<' ) !== false ) {
$filtered = wp_pre_kses_less_than( $filtered );
// This will strip extra whitespace for us.
$filtered = wp_strip_all_tags( $filtered, false );
// Use HTML entities in a special case to make sure no later
// newline stripping stage could lead to a functional tag.
$filtered = str_replace( "<\n", "<\n", $filtered );
}
$filtered = trim( $filtered );
$found = false;
while ( preg_match( '/%[a-f0-9]{2}/i', $filtered, $match ) ) {
$filtered = str_replace( $match[0], '', $filtered );
$found = true;
}
if ( $found ) {
// Strip out the whitespace that may now exist after removing the octets.
$filtered = trim( preg_replace( '/ +/', ' ', $filtered ) );
}
return $filtered;
}
public function wpdbbkp_send_query_message(){
if ( ! isset( $_POST['wpdbbkp_security_nonce'] ) ){
return;
}
if ( !wp_verify_nonce( $_POST['wpdbbkp_security_nonce'], 'wpdbbkp-admin-nonce' ) ){
return;
}
$message = $this->wpdbbkp_sanitize_textarea_field($_POST['message']);
$email = $this->wpdbbkp_sanitize_textarea_field($_POST['email']);
if(function_exists('wp_get_current_user')){
$user = wp_get_current_user();
$message = ''.$message.'
'.'Query from BackupforWP plugin support tab';
$user_data = $user->data;
$user_email = $user_data->user_email;
if($email){
$user_email = $email;
}
//php mailer variables
$sendto = 'team@magazine3.in';
$subject = "BackupforWP Query";
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: '. esc_attr($user_email);
$headers[] = 'Reply-To: ' . esc_attr($user_email);
// Load WP components, no themes.
$sent = wp_mail($sendto, $subject, $message, $headers);
if($sent){
echo json_encode(array('status'=>'t'));
}else{
echo json_encode(array('status'=>'f'));
}
}
wp_die();
}
}
new Wpdb_Admin();