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