PluginProbe
WP Data Access – App Builder for Tables, Forms, Dashboards, Charts & Maps / 5.5.87
WP Data Access – App Builder for Tables, Forms, Dashboards, Charts & Maps v5.5.87
5.5.87 5.5.85 5.5.84 5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 All 162 releases
wp-data-access / WPDataAccess / Settings / WPDA_Settings_Legacy_DataBackup.php

WPDA_Settings_Legacy_DataBackup.php in WP Data Access – App Builder for Tables, Forms, Dashboards, Charts & Maps 5.5.87, at WPDataAccess/Settings/WPDA_Settings_Legacy_DataBackup.php

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