| 1 |
<?php |
| 2 |
/** |
| 3 |
* Destination SSH/sFTP |
| 4 |
* |
| 5 |
* @package wpdbbkp |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
// If user pressed this button, this hidden field will be set to 'Y'. |
| 13 |
if ( isset( $_POST[ 'sftp_submit' ] ) && 'Save' === $_POST[ 'sftp_submit' ] ) { |
| 14 |
// Validate that the contents of the form request came from the current site and not somewhere else added 21-08-15 V.3.4. |
| 15 |
if ( ! isset( $_POST['wpdbbackup_update_setting'] ) ) { |
| 16 |
wp_die( esc_html__('Invalid form data. form request came from the somewhere else not current site!','wpdbbkp') ); |
| 17 |
} |
| 18 |
if ( ! wp_verify_nonce( $_POST['wpdbbackup_update_setting'] , 'wpdbbackup-update-setting' ) ) { |
| 19 |
wp_die( esc_html__('Invalid form data. form request came from the somewhere else not current site!','wpdbbkp') ); |
| 20 |
} |
| 21 |
// Read their posted value. |
| 22 |
|
| 23 |
$option_to_save =array(); |
| 24 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'host' ] ) ) { |
| 25 |
$option_to_save['host'] = sanitize_text_field( wp_unslash($_POST['wp_db_backup_sftp_details'][ 'host' ] ) ); |
| 26 |
} |
| 27 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'port' ] ) ) { |
| 28 |
$option_to_save['port'] = sanitize_text_field( wp_unslash( $_POST['wp_db_backup_sftp_details']['port'] )); |
| 29 |
}else{ |
| 30 |
$option_to_save['port'] = 22; |
| 31 |
} |
| 32 |
|
| 33 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'username' ] ) ) { |
| 34 |
$option_to_save['username'] = sanitize_text_field(wp_unslash( $_POST['wp_db_backup_sftp_details']['username'] )); |
| 35 |
} |
| 36 |
|
| 37 |
$wpdbbkp_auth_type_ = isset($_POST['wp_db_backup_sftp_details'][ 'auth_type' ])?sanitize_text_field( wp_unslash($_POST['wp_db_backup_sftp_details'][ 'auth_type' ])):'password'; |
| 38 |
|
| 39 |
$option_to_save['auth_type'] = $wpdbbkp_auth_type_; |
| 40 |
|
| 41 |
if($wpdbbkp_auth_type_ == 'password'){ |
| 42 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'password' ] ) ) { |
| 43 |
$option_to_save['password'] = sanitize_text_field( wp_unslash($_POST['wp_db_backup_sftp_details']['password'] )); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
if($wpdbbkp_auth_type_ == 'key'){ |
| 48 |
|
| 49 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'sftp_key' ] )) { |
| 50 |
$option_to_save['sftp_key'] = sanitize_text_field( wp_unslash($_POST['wp_db_backup_sftp_details']['sftp_key'] )); |
| 51 |
} |
| 52 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'key_password' ] ) ) { |
| 53 |
$option_to_save['key_password'] = sanitize_text_field( wp_unslash( $_POST['wp_db_backup_sftp_details']['key_password'] )); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'directory' ] ) ) { |
| 58 |
$option_to_save['directory'] = sanitize_text_field( wp_unslash($_POST['wp_db_backup_sftp_details']['directory']) ); |
| 59 |
} |
| 60 |
|
| 61 |
// Save the posted value in the database. |
| 62 |
update_option( 'wp_db_backup_sftp_details', wp_db_filter_data( $option_to_save ) ,false); |
| 63 |
|
| 64 |
if ( isset( $_POST['wp_db_backup_destination_SFTP'] ) ) { |
| 65 |
update_option( 'wp_db_backup_destination_SFTP', 1 , false); |
| 66 |
} else { |
| 67 |
update_option( 'wp_db_backup_destination_SFTP', 0 , false); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
// Put a "settings updated" message on the screen. |
| 72 |
?> |
| 73 |
<div class="updated"><p><strong><?php esc_html_e( 'Your SFTP details have been saved.', 'wpdbbkp' ); ?></strong></p></div> |
| 74 |
<?php |
| 75 |
} // end if. |
| 76 |
|
| 77 |
// If user pressed this button, this hidden field will be set to 'Y'. |
| 78 |
if ( isset( $_POST[ 'sftp_test' ] ) && 'Test Connection' === $_POST[ 'sftp_test' ] ) { |
| 79 |
// Validate that the contents of the form request came from the current site and not somewhere else added 21-08-15 V.3.4. |
| 80 |
if ( ! isset( $_POST['wpdbbackup_update_setting'] ) ) { |
| 81 |
wp_die( esc_html__('Invalid form data. form request came from the somewhere else not current site!','wpdbbkp') ); |
| 82 |
} |
| 83 |
if ( ! wp_verify_nonce( $_POST['wpdbbackup_update_setting'] , 'wpdbbackup-update-setting' ) ) { |
| 84 |
wp_die( esc_html__('Invalid form data. form request came from the somewhere else not current site!','wpdbbkp') ); |
| 85 |
} |
| 86 |
include plugin_dir_path( __FILE__ ) . 'test-sftp.php'; |
| 87 |
// update all options while we're at it. |
| 88 |
$option_to_save =array(); |
| 89 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'host' ] ) ) { |
| 90 |
$option_to_save['host'] = sanitize_text_field( wp_unslash( $_POST['wp_db_backup_sftp_details'][ 'host' ] ) ); |
| 91 |
} |
| 92 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'port' ] ) ) { |
| 93 |
$option_to_save['port'] = sanitize_text_field( wp_unslash($_POST['wp_db_backup_sftp_details']['port'] ) ); |
| 94 |
}else{ |
| 95 |
$option_to_save['port'] = 22; |
| 96 |
} |
| 97 |
|
| 98 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'username' ] ) ) { |
| 99 |
$option_to_save['username'] = sanitize_text_field( wp_unslash( $_POST['wp_db_backup_sftp_details']['username'] ) ); |
| 100 |
} |
| 101 |
|
| 102 |
$wpdbbkp_auth_type_ = isset($_POST['wp_db_backup_sftp_details'][ 'auth_type' ])?sanitize_text_field( wp_unslash($_POST['wp_db_backup_sftp_details'][ 'auth_type' ])):'password'; |
| 103 |
|
| 104 |
$option_to_save['auth_type'] = $wpdbbkp_auth_type_; |
| 105 |
|
| 106 |
if($wpdbbkp_auth_type_ == 'password'){ |
| 107 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'password' ] ) ) { |
| 108 |
$option_to_save['password'] = sanitize_text_field( wp_unslash( $_POST['wp_db_backup_sftp_details']['password'] ) ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if($wpdbbkp_auth_type_ == 'key'){ |
| 113 |
|
| 114 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'sftp_key' ] )) { |
| 115 |
$option_to_save['sftp_key'] = sanitize_text_field( wp_unslash( $_POST['wp_db_backup_sftp_details']['sftp_key'] ) ); |
| 116 |
} |
| 117 |
|
| 118 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'key_password' ] ) ) { |
| 119 |
$option_to_save['key_password'] = sanitize_text_field( wp_unslash( $_POST['wp_db_backup_sftp_details']['key_password'] ) ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if ( isset( $_POST['wp_db_backup_sftp_details'][ 'directory' ] ) ) { |
| 124 |
$option_to_save['directory'] = sanitize_text_field( wp_unslash( $_POST['wp_db_backup_sftp_details']['directory'] ) ); |
| 125 |
} |
| 126 |
// Save the posted value in the database. |
| 127 |
update_option( 'wp_db_backup_sftp_details', wp_db_filter_data( $option_to_save ) ,false); |
| 128 |
|
| 129 |
$result = wpdbbkp_test_sftp(); |
| 130 |
|
| 131 |
if ( 'OK' !== $result ) { |
| 132 |
?> |
| 133 |
<div class="error"><p><strong><?php echo esc_html__('connection has failed!', 'wpdbbkp') ?><br /></strong></p> |
| 134 |
<?php echo esc_html( $result ) . '<br /><br />'; ?> |
| 135 |
</div> |
| 136 |
<?php } else { ?> |
| 137 |
|
| 138 |
<div class="updated"><p><strong><?php echo esc_html__('Connected to ', 'wpdbbkp') ?><?php echo esc_html( $option_to_save['host']); ?>, <?php echo esc_html__('for user', 'wpdbbkp') ?> <?php echo esc_html( $option_to_save['username']); ?></strong></p></div> |
| 139 |
<?php |
| 140 |
} // end if. |
| 141 |
} // end if. |
| 142 |
|
| 143 |
// Read in existing option value from database |
| 144 |
$wp_db_backup_destination_sftp = wp_db_filter_data( get_option( 'wp_db_backup_destination_SFTP',false) ); |
| 145 |
$wpdbbkp_sftp_details = get_option( 'wp_db_backup_sftp_details',array()); |
| 146 |
$wpdbbkp_sftp_authtype = isset($wpdbbkp_sftp_details['auth_type'])?$wpdbbkp_sftp_details['auth_type']:'password'; |
| 147 |
?> |
| 148 |
<style>td, th { |
| 149 |
padding: 5px; |
| 150 |
}</style> |
| 151 |
<p><?php echo esc_html__('Enter your SFTP details for your offsite backup repository. Leave these blank for local backups or Disable SFTP Destination.', 'wpdbbkp') ?></p> |
| 152 |
<form class="form-group" name="form1" method="post" action=""> |
| 153 |
<input type="hidden" name="<?php echo esc_attr( $hidden_field_name ); ?>" value="Y"> |
| 154 |
<input name="wpdbbackup_update_setting" type="hidden" value="<?php echo esc_attr( wp_create_nonce( 'wpdbbackup-update-setting' ) ); ?>" /> |
| 155 |
<?php wp_nonce_field( 'wp-database-backup' ); ?> |
| 156 |
|
| 157 |
<div class="row form-group"> |
| 158 |
<label class="col-sm-2" for="wp_db_backup_destination_SFTP"><?php echo esc_html__('Enable SFTP Destination:', 'wpdbbkp') ?></label> |
| 159 |
<div class="col-sm-6"> |
| 160 |
<input type="checkbox" id="wp_db_backup_destination_SFTP" <?php echo ( isset( $wp_db_backup_destination_sftp ) && 1 === (int) $wp_db_backup_destination_sftp ) ? 'checked' : ''; ?> name="wp_db_backup_destination_SFTP"> |
| 161 |
</div> |
| 162 |
</div> |
| 163 |
|
| 164 |
<div class="row form-group"> |
| 165 |
<label class="col-sm-2" for="wpdbbkp_sftp_host"><?php echo esc_html__('SFTP Host:', 'wpdbbkp') ?></label> |
| 166 |
<div class="col-sm-6"> |
| 167 |
<input type="text" id="wpdbbkp_sftp_host" class="form-control" name="wp_db_backup_sftp_details[host]" value="<?php echo esc_attr( isset($wpdbbkp_sftp_details['host'])?$wpdbbkp_sftp_details['host']:''); ?>" size="25" placeholder="<?php esc_attr_e('e.g. sftp.yoursite.com','wpdbbkp');?>"> |
| 168 |
</div> |
| 169 |
</div> |
| 170 |
|
| 171 |
<div class="row form-group"> |
| 172 |
<label class="col-sm-2" for="wpdbbkp_sftp_port"><?php echo esc_html__('SFTP Port:', 'wpdbbkp') ?></label> |
| 173 |
<div class="col-sm-2"> |
| 174 |
<input type="text" id="wpdbbkp_sftp_port" class="form-control" name="wp_db_backup_sftp_details[port]" value="<?php echo esc_attr( isset($wpdbbkp_sftp_details['port'])?$wpdbbkp_sftp_details['port']:''); ?>" size="4"> |
| 175 |
</div> |
| 176 |
<div class="col-sm-4"> |
| 177 |
<em><?php echo esc_html__('defaults to 22 if left blank', 'wpdbbkp') ?> </em> |
| 178 |
</div> |
| 179 |
</div> |
| 180 |
|
| 181 |
<div class="row form-group"> |
| 182 |
<label class="col-sm-2" for="wpdbbkp_sftp_user"><?php echo esc_html__('SFTP Username:', 'wpdbbkp') ?></label> |
| 183 |
<div class="col-sm-6"> |
| 184 |
<input type="text" id="wpdbbkp_sftp_user" class="form-control" name="wp_db_backup_sftp_details[username]" value="<?php echo esc_attr( isset($wpdbbkp_sftp_details['username'])?$wpdbbkp_sftp_details['username']:''); ?>" size="25"> |
| 185 |
</div> |
| 186 |
</div> |
| 187 |
|
| 188 |
<div class="row form-group"> |
| 189 |
<label class="col-sm-2" for="wpdbbkp_sftp_auth_select"><?php echo esc_html__('SFTP Auth Type :', 'wpdbbkp') ?></label> |
| 190 |
<div class="col-sm-6"> |
| 191 |
<select id="wpdbbkp_sftp_auth_select" class="form-control" name="wp_db_backup_sftp_details[auth_type]" > |
| 192 |
<option value="password" <?php if($wpdbbkp_sftp_authtype=='password'){ echo 'selected';}?> ><?php esc_html_e('Using Password', 'wpdbbkp') ?></option> |
| 193 |
<option value="key" <?php if($wpdbbkp_sftp_authtype=='key'){ echo 'selected';}?>><?php echo esc_html_e('Using Key', 'wpdbbkp') ?></option> |
| 194 |
</select> |
| 195 |
</div> |
| 196 |
</div> |
| 197 |
<div class="row form-group" <?php if( $wpdbbkp_sftp_authtype!='password'){ echo 'style="display:none"'; }?> > |
| 198 |
<label class="col-sm-2" for="wpdbbkp_sftp_password"><?php echo esc_html__('SFTP Password:', 'wpdbbkp') ?></label> |
| 199 |
<div class="col-sm-6"> |
| 200 |
<input type="password" id="wpdbbkp_sftp_password" class="form-control" name="wp_db_backup_sftp_details[password]" value="<?php echo esc_attr( isset($wpdbbkp_sftp_details['password'])?$wpdbbkp_sftp_details['password']:''); ?>" size="25"> |
| 201 |
</div> |
| 202 |
</div> |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
<div class="row form-group" <?php if($wpdbbkp_sftp_authtype!='key'){ echo 'style="display:none"'; }?> > |
| 207 |
<label class="col-sm-2" for="wpdbbkp_sftp_sshkey"><?php echo esc_html__('SSH Key', 'wpdbbkp') ?></label> |
| 208 |
<div class="col-sm-4"> |
| 209 |
<input type="file" id="wpdbbkp_sftp_sshkey" class="form-control" size="25"> |
| 210 |
<input type="hidden" name="wp_db_backup_sftp_details[sftp_key]" id="wp_db_backup_sftp_key" value="<?php echo isset($wpdbbkp_sftp_details['sftp_key'])?esc_attr($wpdbbkp_sftp_details['sftp_key']):'';?>"> |
| 211 |
</div> |
| 212 |
<div class="col-sm-4"> |
| 213 |
<em style="color:green"><?php if(!empty($wpdbbkp_sftp_details['sftp_key'])){ esc_html_e('(Key Already exists. To change please upload new key)', 'wpdbbkp'); } ?></em> |
| 214 |
<em> <?php esc_html_e('Please use OpenSSH standard keys', 'wpdbbkp');?></em> |
| 215 |
</div> |
| 216 |
</div> |
| 217 |
<div class="row form-group" <?php if($wpdbbkp_sftp_authtype!='key'){ echo 'style="display:none"'; }?> > |
| 218 |
<label class="col-sm-2" for="wpdbbkp_sftp_sshkey_password"><?php echo esc_html__('SSH Key Password:', 'wpdbbkp') ?></label> |
| 219 |
<div class="col-sm-4"> |
| 220 |
<input type="password" id="wpdbbkp_sftp_sshkey_password" class="form-control" name="wp_db_backup_sftp_details[key_password]" value="<?php echo esc_attr( isset($wpdbbkp_sftp_details['key_password'])?$wpdbbkp_sftp_details['key_password']:''); ?>" size="25"> |
| 221 |
</div> |
| 222 |
<div class="col-sm-4"> |
| 223 |
<?php esc_html_e('Enter password only if you have set the password','wpdbbkp');?> |
| 224 |
</div> |
| 225 |
</div> |
| 226 |
<div class="row form-group"> |
| 227 |
<label class="col-sm-2" for="wpdbbkp_sftp_directory"><?php echo esc_html__('Subdirectory:', 'wpdbbkp') ?></label> |
| 228 |
<div class="col-sm-4"> |
| 229 |
<input type="text" id="wpdbbkp_sftp_directory" placeholder="<?php esc_attr_e('e.g. /httpdocs/backups','wpdbbkp');?>" class="form-control" name="wp_db_backup_sftp_details[directory]" value="<?php echo esc_attr( isset($wpdbbkp_sftp_details['directory'])?$wpdbbkp_sftp_details['directory']:''); ?>" size="25"> |
| 230 |
</div> |
| 231 |
<div class="col-sm-4"> |
| 232 |
<em><?php echo esc_html__('e.g. /httpdocs/backups or leave blank', 'wpdbbkp') ?></em> |
| 233 |
</div> |
| 234 |
</div> |
| 235 |
|
| 236 |
<p><input type="submit" name="sftp_submit" class="btn btn-primary" value="<?php esc_attr_e( 'Save', 'wpdbbkp' ); ?>" /> |
| 237 |
<input type="submit" name="sftp_test" class="btn btn-secondary" value="Test Connection" /> |
| 238 |
|
| 239 |
<br /> |
| 240 |
</p> |
| 241 |
</form> |
| 242 |
<hr /> |
| 243 |
<br /> |
| 244 |
|