| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Settings { |
| 4 |
|
| 5 |
use WPDataAccess\Utilities\WPDA_Message_Box; |
| 6 |
use WPDataAccess\Utilities\WPDA_Remote_Call; |
| 7 |
use WPDataAccess\WPDA; |
| 8 |
|
| 9 |
class WPDA_Settings_DataBackup extends WPDA_Settings { |
| 10 |
|
| 11 |
// Dropbox app client id and secret (necessary for registration) |
| 12 |
const DROPBOX_CLIENT_ID = 'rp1wxasy1irf3wf'; // 'f6e7znb7qfwaqjh'; // 'rv5japeynhpzmyy'; |
| 13 |
const DROPBOX_CLIENT_SECRET = 'm9ltsxehdpgepv8'; // '0vzaidexrtcede4'; // 'v45glikrzr6h62z'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Add data backup tab content |
| 17 |
* |
| 18 |
* See class documentation for flow explanation. |
| 19 |
* |
| 20 |
* @since 2.0.7 |
| 21 |
*/ |
| 22 |
protected function add_content() { |
| 23 |
// TEST BACKUP INTERACTIVELY |
| 24 |
// $backup_test = new \WPDataAccess\Backup\WPDA_Data_Export(); |
| 25 |
// $backup_test->wpda_data_backup('large'); |
| 26 |
|
| 27 |
if ( isset( $_REQUEST['action'] ) ) { |
| 28 |
$action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ); // input var okay. |
| 29 |
|
| 30 |
// Security check. |
| 31 |
$wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; // input var okay. |
| 32 |
if ( ! wp_verify_nonce( $wp_nonce, 'wpda-databackup-settings-' . WPDA::get_current_user_login() ) ) { |
| 33 |
wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); |
| 34 |
} |
| 35 |
|
| 36 |
if ( 'save' === $action ) { |
| 37 |
// Save options. |
| 38 |
$save_local_path = isset( $_REQUEST['local_path'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['local_path'] ) ) : ''; // input var okay. |
| 39 |
if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) ) { |
| 40 |
if ( '\\' !== substr( $save_local_path, - 1 ) ) { |
| 41 |
$save_local_path .= '\\'; |
| 42 |
} |
| 43 |
} else { |
| 44 |
if ( '/' !== substr( $save_local_path, - 1 ) ) { |
| 45 |
$save_local_path .= '/'; |
| 46 |
} |
| 47 |
} |
| 48 |
WPDA::set_option( WPDA::OPTION_DB_LOCAL_PATH, $save_local_path ); |
| 49 |
|
| 50 |
$options_activated = array(); |
| 51 |
if ( isset( $_REQUEST['local_path_activated'] ) ) { |
| 52 |
$error_level = error_reporting(); |
| 53 |
error_reporting( E_ALL ^ E_WARNING ); |
| 54 |
$local_path = WPDA::get_option( WPDA::OPTION_DB_LOCAL_PATH ); |
| 55 |
$file_permission = fileperms( $local_path ); |
| 56 |
error_reporting( $error_level ); |
| 57 |
if ( $file_permission && '4' === substr( decoct( $file_permission ), 0, 1 ) ) { |
| 58 |
$options_activated['local_path'] = true; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// Need to do this first to prevent overwriting. |
| 63 |
if ( isset( $_REQUEST['dropbox_folder'] ) ) { |
| 64 |
$dropbox_folder = sanitize_text_field( wp_unslash( $_REQUEST['dropbox_folder'] ) ); |
| 65 |
if ( '/' !== substr( $dropbox_folder, - 1 ) ) { |
| 66 |
$dropbox_folder .= '/'; |
| 67 |
} |
| 68 |
|
| 69 |
WPDA::set_option( WPDA::OPTION_DB_DROPBOX_PATH, $dropbox_folder ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( isset( $_REQUEST['dropbox_auth'] ) ) { |
| 73 |
$dropbox_auth = sanitize_text_field( wp_unslash( $_REQUEST['dropbox_auth'] ) ); |
| 74 |
} else { |
| 75 |
$dropbox_auth = ''; |
| 76 |
} |
| 77 |
$dropbox_auth_saved = get_option( 'wpda_db_dropbox_auth' ); |
| 78 |
if ( '' !== $dropbox_auth && $dropbox_auth_saved !== $dropbox_auth ) { |
| 79 |
$response = WPDA_Remote_Call::post( |
| 80 |
'https://api.dropboxapi.com/oauth2/token', |
| 81 |
array( |
| 82 |
'code' => $dropbox_auth, |
| 83 |
'grant_type' => 'authorization_code', |
| 84 |
'client_id' => self::DROPBOX_CLIENT_ID, |
| 85 |
'client_secret' => self::DROPBOX_CLIENT_SECRET, |
| 86 |
) |
| 87 |
); |
| 88 |
|
| 89 |
if ( false === $response || ! isset( $response['body'] ) ) { |
| 90 |
$this->dropbox_authorization_failed(); |
| 91 |
} else { |
| 92 |
$body_content = json_decode( $response['body'] ); |
| 93 |
if ( isset( $body_content->refresh_token ) ) { |
| 94 |
update_option( 'wpda_db_dropbox_refresh_token', $body_content->refresh_token ); |
| 95 |
WPDA::set_option( WPDA::OPTION_DB_DROPBOX_PATH ); |
| 96 |
|
| 97 |
// Remove old token used for outdated dropbox connection. |
| 98 |
delete_option( 'wpda_db_dropbox_access_token' ); |
| 99 |
} else { |
| 100 |
$this->dropbox_authorization_failed(); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
update_option( 'wpda_db_dropbox_auth', $dropbox_auth ); |
| 105 |
|
| 106 |
if ( isset( $_REQUEST['dropbox_activated'] ) ) { |
| 107 |
$options_activated['dropbox'] = true; |
| 108 |
} |
| 109 |
|
| 110 |
update_option( 'wpda_db_options_activated', $options_activated ); |
| 111 |
} elseif ( 'setdefaults' === $action ) { |
| 112 |
// Set all data backup settings back to default. |
| 113 |
WPDA::set_option( WPDA::OPTION_DB_LOCAL_PATH ); |
| 114 |
WPDA::set_option( WPDA::OPTION_DB_DROPBOX_PATH ); |
| 115 |
delete_option( 'wpda_db_options_activated' ); |
| 116 |
delete_option( 'wpda_db_dropbox_refresh_token' ); |
| 117 |
delete_option( 'wpda_db_dropbox_auth' ); |
| 118 |
|
| 119 |
// Remove old token used for outdated dropbox connection. |
| 120 |
delete_option( 'wpda_db_dropbox_access_token' ); |
| 121 |
} |
| 122 |
|
| 123 |
$msg = new WPDA_Message_Box( |
| 124 |
array( |
| 125 |
'message_text' => __( 'Settings saved', 'wp-data-access' ), |
| 126 |
) |
| 127 |
); |
| 128 |
$msg->box(); |
| 129 |
} |
| 130 |
|
| 131 |
$error_level = error_reporting(); |
| 132 |
error_reporting( E_ALL ^ E_WARNING ); |
| 133 |
$local_path = WPDA::get_option( WPDA::OPTION_DB_LOCAL_PATH ); |
| 134 |
$file_permission = fileperms( $local_path ); |
| 135 |
error_reporting( $error_level ); |
| 136 |
|
| 137 |
$owner_info = ( ( $file_permission & 0x0100 ) ? 'r' : '-' ); |
| 138 |
$owner_info .= ( ( $file_permission & 0x0080 ) ? 'w' : '-' ); |
| 139 |
$owner_info .= ( ( $file_permission & 0x0040 ) ? |
| 140 |
( ( $file_permission & 0x0800 ) ? 's' : 'x' ) : |
| 141 |
( ( $file_permission & 0x0800 ) ? 'S' : '-' ) ); |
| 142 |
$group_info = ( ( $file_permission & 0x0020 ) ? 'r' : '-' ); |
| 143 |
$group_info .= ( ( $file_permission & 0x0010 ) ? 'w' : '-' ); |
| 144 |
$group_info .= ( ( $file_permission & 0x0008 ) ? |
| 145 |
( ( $file_permission & 0x0400 ) ? 's' : 'x' ) : |
| 146 |
( ( $file_permission & 0x0400 ) ? 'S' : '-' ) ); |
| 147 |
$world_info = ( ( $file_permission & 0x0004 ) ? 'r' : '-' ); |
| 148 |
$world_info .= ( ( $file_permission & 0x0002 ) ? 'w' : '-' ); |
| 149 |
$world_info .= ( ( $file_permission & 0x0001 ) ? |
| 150 |
( ( $file_permission & 0x0200 ) ? 't' : 'x' ) : |
| 151 |
( ( $file_permission & 0x0200 ) ? 'T' : '-' ) ); |
| 152 |
|
| 153 |
$dropbox_auth = get_option( 'wpda_db_dropbox_auth' ); |
| 154 |
$dropbox_folder = WPDA::get_option( WPDA::OPTION_DB_DROPBOX_PATH ); |
| 155 |
|
| 156 |
$options_activated = get_option( 'wpda_db_options_activated' ); |
| 157 |
?> |
| 158 |
|
| 159 |
<form id="wpda_settings_databackup" method="post" |
| 160 |
action="?page=<?php echo esc_attr( $this->page ); ?>&tab=databackup"> |
| 161 |
<table class="wpda-table-settings"> |
| 162 |
<tr> |
| 163 |
<th><?php echo __( 'Local file system' ); ?></th> |
| 164 |
<td> |
| 165 |
<label> |
| 166 |
<input type="checkbox" |
| 167 |
name="local_path_activated" |
| 168 |
<?php |
| 169 |
if ( isset( $options_activated['local_path'] ) ) { |
| 170 |
echo 'checked'; |
| 171 |
} |
| 172 |
?> |
| 173 |
/> |
| 174 |
<?php echo __( 'Activated', 'wp-data-access' ); ?> |
| 175 |
</label> |
| 176 |
<br/><br/> |
| 177 |
<?php echo __( 'Enter the name of the folder where data backup files should be stored.' ); ?> |
| 178 |
<br/> |
| 179 |
<input type="text" name="local_path" value="<?php echo esc_attr( $local_path ); ?>"/> |
| 180 |
<span><?php echo __( 'Make sure the folder exists with permission to write files.' ); ?></span> |
| 181 |
<?php |
| 182 |
if ( 'WIN' !== strtoupper( substr( PHP_OS, 0, 3 ) ) ) { |
| 183 |
if ( ! $file_permission ) { |
| 184 |
echo '<br/><br/>'; |
| 185 |
echo __( 'ERROR: Invalid folder', 'wp-data-access' ); |
| 186 |
} else { |
| 187 |
if ( '4' !== substr( decoct( $file_permission ), 0, 1 ) ) { |
| 188 |
echo '<br/><br/>'; |
| 189 |
echo __( 'ERROR: Not a folder', 'wp-data-access' ); |
| 190 |
} else { |
| 191 |
$fileowner = fileowner( $local_path ); |
| 192 |
$groupowner = filegroup( $local_path ); |
| 193 |
?> |
| 194 |
<br/><br/> |
| 195 |
{ |
| 196 |
<?php echo __( '"Permission"' ); ?>: |
| 197 |
{ |
| 198 |
<?php echo __( '"owner"' ); ?>: |
| 199 |
{ |
| 200 |
<?php echo __( '"name"' ); ?>: "<?php echo esc_attr( posix_getpwuid( $fileowner )['name'] ); ?>", |
| 201 |
<?php echo __( '"access"' ); ?>: "<?php echo esc_attr( $owner_info ); ?>" |
| 202 |
}, |
| 203 |
<?php echo __( '"group"' ); ?>: |
| 204 |
{ |
| 205 |
<?php echo __( '"name"' ); ?>: "<?php echo esc_attr( posix_getpwuid( $groupowner )['name'] ); ?>", |
| 206 |
<?php echo __( '"access"' ); ?>: "<?php echo esc_attr( $group_info ); ?>" |
| 207 |
}, |
| 208 |
<?php echo __( '"world"' ); ?>: |
| 209 |
{ |
| 210 |
<?php echo __( '"access"' ); ?>: "<?php echo esc_attr( $world_info ); ?>" |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
<?php |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
?> |
| 219 |
</td> |
| 220 |
</tr> |
| 221 |
<tr> |
| 222 |
<th><?php echo __( 'Dropbox' ); ?></th> |
| 223 |
<td> |
| 224 |
<label> |
| 225 |
<input type="checkbox" |
| 226 |
name="dropbox_activated" |
| 227 |
<?php |
| 228 |
if ( isset( $options_activated['dropbox'] ) ) { |
| 229 |
echo 'checked'; |
| 230 |
} |
| 231 |
?> |
| 232 |
/> |
| 233 |
<?php echo __( 'Activated', 'wp-data-access' ); ?> |
| 234 |
</label> |
| 235 |
<br/><br/> |
| 236 |
<a href="https://www.dropbox.com/" class="button button-secondary" target="_blank"> |
| 237 |
<?php echo __( 'Create a Dropbox account' ); ?> |
| 238 |
</a> |
| 239 |
<span style="vertical-align:-webkit-baseline-middle;"> |
| 240 |
<?php echo __( 'You can skip this step if you already have an account.' ); ?> |
| 241 |
</span> |
| 242 |
<br/><br/> |
| 243 |
<?php echo __( 'Authorize the WP Data Access Dropbox app and enter the authorization code in the text box below.' ); ?> |
| 244 |
<br/> |
| 245 |
<input type="text" name="dropbox_auth" value="<?php echo esc_attr( $dropbox_auth ); ?>"/> |
| 246 |
<a href="https://www.dropbox.com/oauth2/authorize?client_id=<?php echo esc_attr( self::DROPBOX_CLIENT_ID ); ?>&response_type=code&token_access_type=offline" |
| 247 |
class="button button-secondary" |
| 248 |
target="_blank" |
| 249 |
style="vertical-align:bottom;"> |
| 250 |
<?php echo __( 'Get Dropbox authorization code' ); ?> |
| 251 |
</a> |
| 252 |
<?php |
| 253 |
if ( '' !== $dropbox_folder ) { |
| 254 |
// Only older versions are using this option. |
| 255 |
?> |
| 256 |
<br/><br/> |
| 257 |
<?php echo __( 'Enter the name of the folder where data backup files should be stored. If the folder doesn\'t exists, it\'ll be created for you.' ); ?> |
| 258 |
<br/> |
| 259 |
<input type="text" name="dropbox_folder" value="<?php echo esc_attr( $dropbox_folder ); ?>"/> |
| 260 |
<?php |
| 261 |
} |
| 262 |
?> |
| 263 |
</td> |
| 264 |
</tr> |
| 265 |
</table> |
| 266 |
<div class="wpda-table-settings-button"> |
| 267 |
<input type="hidden" name="action" value="save"/> |
| 268 |
<button type="submit" class="button button-primary"> |
| 269 |
<i class="fas fa-check wpda_icon_on_button"></i> |
| 270 |
<?php echo __( 'Save Data Backup Settings', 'wp-data-access' ); ?> |
| 271 |
</button> |
| 272 |
<a href="javascript:void(0)" |
| 273 |
onclick="if (confirm('<?php echo __( 'Reset to defaults?', 'wp-data-access' ); ?>')) { |
| 274 |
jQuery('input[name="action"]').val('setdefaults'); |
| 275 |
jQuery('#wpda_settings_databackup').trigger('submit') |
| 276 |
}" |
| 277 |
class="button"> |
| 278 |
<i class="fas fa-times-circle wpda_icon_on_button"></i> |
| 279 |
<?php echo __( 'Reset Data Backup To Defaults', 'wp-data-access' ); ?> |
| 280 |
</a> |
| 281 |
</div> |
| 282 |
<?php wp_nonce_field( 'wpda-databackup-settings-' . WPDA::get_current_user_login(), '_wpnonce', false ); ?> |
| 283 |
</form> |
| 284 |
<?php |
| 285 |
} |
| 286 |
|
| 287 |
private function dropbox_authorization_failed() { |
| 288 |
$msg = new WPDA_Message_Box( |
| 289 |
array( |
| 290 |
'message_text' => __( 'Dropbox authorization failed ', 'wp-data-access' ), |
| 291 |
'message_type' => 'error', |
| 292 |
'message_is_dismissible' => false, |
| 293 |
) |
| 294 |
); |
| 295 |
$msg->box(); |
| 296 |
} |
| 297 |
|
| 298 |
public static function dropbox_get_token() { |
| 299 |
$refresh_token = get_option( 'wpda_db_dropbox_refresh_token' ); |
| 300 |
if ( false === $refresh_token ) { |
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
$response = WPDA_Remote_Call::post( |
| 305 |
'https://api.dropboxapi.com/oauth2/token', |
| 306 |
array( |
| 307 |
'refresh_token' => $refresh_token, |
| 308 |
'grant_type' => 'refresh_token', |
| 309 |
'client_id' => self::DROPBOX_CLIENT_ID, |
| 310 |
'client_secret' => self::DROPBOX_CLIENT_SECRET, |
| 311 |
) |
| 312 |
); |
| 313 |
|
| 314 |
if ( ! isset( $response['body'] ) ) { |
| 315 |
return false; |
| 316 |
} else { |
| 317 |
$body_content = json_decode( $response['body'] ); |
| 318 |
if ( ! isset( $body_content->access_token ) ) { |
| 319 |
return false; |
| 320 |
} else { |
| 321 |
return $body_content->access_token; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
} |
| 329 |
|