-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclass-boldgrid-backup-admin-folder-exclusion.php
More file actions
550 lines (476 loc) · 14.2 KB
/
class-boldgrid-backup-admin-folder-exclusion.php
File metadata and controls
550 lines (476 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?php
/**
* File: class-boldgrid-backup-admin-folder-exclusion.php
*
* @link https://www.boldgrid.com
* @since 1.6.0
*
* @package Boldgrid_Backup
* @subpackage Boldgrid_Backup/admin
* @copyright BoldGrid
* @version $Id$
* @author BoldGrid <support@boldgrid.com>
*/
// phpcs:disable WordPress.VIP, WordPress.CSRF.NonceVerification.NoNonceVerification
/**
* Class: Boldgrid_Backup_Admin_Folder_Exclusion
*
* @since 1.6.0
*/
class Boldgrid_Backup_Admin_Folder_Exclusion {
/**
* The default exclude value.
*
* @since 1.6.0
* @var string
*/
public $default_exclude = '.git,node_modules,/wp-content/cache,';
/**
* The default include value.
*
* @since 1.6.0
* @var string
*/
public $default_include = 'WPCORE,/wp-content';
/**
* By default, backup all files and folders (use default settings).
*
* @since 1.6.0
* @var string
*/
public $default_type = 'full';
/**
* Our exclude value.
*
* @since 1.6.0
* @var string|null
*/
public $exclude = null;
/**
* Our include value.
*
* @since 1.6.0
* @var string|null
*/
public $include = null;
/**
* Whether or not we're in the ajax preview.
*
* @since 1.6.0
* @var bool
*/
public $in_ajax_preview = false;
/**
* Filename of our restore-info.json file.
*
* @since 1.14.10
* @var string
*/
public $restore_info_filename;
/**
* Determine the type of backup we are performing.
*
* Usually it will be 'full' or 'custom'.
*
* @since 1.6.0
* @var null|string
*/
public $type = null;
/**
* Allowable types.
*
* @since 1.6.0
* @var array
*/
public $types = array( 'include', 'exclude', 'type' );
/**
* Valid backup types.
*
* @since 1.6.0
* @var array
*/
public $valid_types = array( 'full', 'custom' );
/**
* The core class object.
*
* @since 1.6.0
* @access private
* @var Boldgrid_Backup_Admin_Core
*/
private $core;
/**
* Constructor.
*
* @since 1.6.0
*
* @param Boldgrid_Backup_Admin_Core $core Core class object.
*/
public function __construct( $core ) {
$this->core = $core;
/**
* Allow the filtering of the default include and exclude settings.
*
* As of 1.6.0, the backup plugin is not yet "properly" designed to
* allow for these settings to be filtered. Yes, these filters below are
* being ran, however they're being ran in the constructor. The only
* plugins we can guarantee that will be able to hook into these filters
* are mu-plugins. One possible scenario this would be used is an mu-plugin
* hooking into these filters to ensure "mu-plugins" are excluded from
* backups.
*
* @since 1.6.0
*
* @param string $this->default_include Default include values.
*/
$this->default_include = apply_filters( 'boldgrid_backup_default_folder_include', $this->default_include );
$this->default_exclude = apply_filters( 'boldgrid_backup_default_folder_exclude', $this->default_exclude );
// Set in the constructor so as to prevent excessive calls in self::allow_file.
$this->restore_info_filename = basename( \Boldgrid\Backup\Cli\Info::get_results_filepath() );
}
/**
* Determine if we should allow a file in the backup.
*
* @since 1.6.0
*
* @param string $file File path.
* @return bool
*/
public function allow_file( $file ) {
// If this file is in our backup directory, do not allow it.
if ( $this->core->backup_dir->file_in_dir( ABSPATH . $file, true ) ) {
return false;
}
// Do not allow the "cron/restore-info.json" file used for emergency restorations.
if ( $this->is_match( 'cron/' . $this->restore_info_filename, $file ) ) {
return false;
}
if ( $this->is_banned( $file ) ) {
return false;
}
// Get comma-delimited lists from user input or settings. Sanitizing is done below.
$include = $this->in_ajax_preview ? $_POST['include'] : $this->from_settings( 'include' );
$exclude = $this->in_ajax_preview ? $_POST['exclude'] : $this->from_settings( 'exclude' );
// Convert comma-delimited strings to arrays, and sanitize (also trim whitespace).
$includes = array_map( 'sanitize_text_field', explode( ',', $include ) );
$excludes = array_map( 'sanitize_text_field', explode( ',', $exclude ) );
// Default values, include everything and exclude nothing.
$is_match_include = false;
$is_match_exclude = false;
foreach ( $includes as $include ) {
if ( $this->is_match( $include, $file ) ) {
$is_match_include = true;
}
}
// If we're not including this file, we don't need to check excludes.
if ( ! $is_match_include ) {
return false;
}
// If the user left "excludes" blank, then we're not excluding anything.
if ( empty( $exclude ) ) {
return true;
}
foreach ( $excludes as $exclude ) {
if ( $this->is_match( $exclude, $file ) ) {
$is_match_exclude = true;
}
}
return ! $is_match_exclude;
}
/**
* Generate a section for email alerts including information about files and
* folders excluded.
*
* @since 1.6.0
*
* @param array $info File and folder settings.
* @return string
*/
public function email_part( $info ) {
$body = '';
$has_folder_included = isset( $info['folder_include'] );
$has_folder_excluded = isset( $info['folder_exclude'] );
if ( $has_folder_included || $has_folder_excluded ) {
$body .= "\n" . __( 'FILE AND FOLDER SETTINGS', 'boldgrid-backup' ) . "\n";
}
if ( $has_folder_included ) {
$body .= sprintf(
// translators: 1: Included folder list.
esc_html__( 'Included: %1$s', 'boldgrid-backup' ),
$info['folder_include']
) . "\n";
}
if ( $has_folder_excluded ) {
$body .= sprintf(
// translators: 1: Excluded folder list.
esc_html__( 'Excluded: %1$s', 'boldgrid-backup' ),
$info['folder_exclude']
) . "\n";
}
return $body;
}
/**
* Create our regex pattern.
*
* If the user enters wp-* for their include / exclude value, then we need
* to convert that into a proper regex pattern.
*
* When we look for matches, we want to keep it specific to one folder.
* For example, if we're given wp-adm*n, the expectation is that we want
* to find everything within the wp-admin folder.
*
* To prevent this false positive:
* wp-admin/images/media-button.png
* wp-adm************************ng
* ... we will set the wildcard to match everything except a directory separator.
*
* @since 1.6.0
*
* @param string $value Input string.
* @return string
*/
public function create_pattern( $value ) {
$first_char = substr( $value, 0, 1 );
// Clean up our value. Forward slashes will be hanlded uniquely.
$value = trim( $value, ' /' );
// Escape everything except the wildcard.
$value = preg_quote( $value );
$value = str_replace( '\*', '*', $value );
/*
* If the first character is a /, then assume we're requiring the $file
* to be directly in the ABSPATH, and so the $file should beging with
* ^$value.
*
* Otherwise assume our $file should begin with ^value or procede a
* forward slash /$value.
*/
$pattern = '/' === $first_char ? '#^' : '#(^|/)';
/*
* To keep wildcards within a path part, ensure the wildcard matches
* anything except a /. This ensure that wp-adm* matches wp-adm*n and
* not wp-admin/file.png.
*/
$pattern .= str_replace( '*', '[^/]*', $value );
/*
* Our $file should either end with our $$value, or our $value should be
* followed by a forward slash $value/.
*/
$pattern .= '($|/)#';
return $pattern;
}
/**
* Enqueue scripts.
*
* @since 1.6.0
*/
public function enqueue_scripts() {
$handle = 'boldgrid-backup-admin-folder-exclude';
wp_register_script(
$handle,
plugin_dir_url( __FILE__ ) . 'js/boldgrid-backup-admin-folder-exclude.js',
array( 'jquery' ),
BOLDGRID_BACKUP_VERSION,
false
);
$translation = array(
'default_include' => $this->default_include,
'default_exclude' => $this->default_exclude,
'items' => __( 'items', 'boldgrid-backup' ),
'no_results' => __( 'No results', 'boldgrid-backup' ),
'of' => __( 'of', 'boldgrid-backup' ),
);
wp_localize_script( $handle, 'BoldGridBackupAdminFolderExclude', $translation );
wp_enqueue_script( $handle );
// Enqueue CSS for folder exclude functionality.
wp_enqueue_style(
$handle,
plugin_dir_url( __FILE__ ) . 'css/boldgrid-backup-admin-folder-exclude.css', array(),
BOLDGRID_BACKUP_VERSION
);
wp_enqueue_script( 'jquery-effects-core' );
wp_enqueue_script( 'jquery-effects-bounce' );
}
/**
* Get our include or exclude value from the settings.
*
* @since 1.6.0
*
* @param string $type Either 'include' or 'exclude'.
* @param array $settings File and folder settings.
* @return string
*/
public function from_settings( $type, $settings = false ) {
if ( ! in_array( $type, $this->types, true ) ) {
return false;
}
$key = 'folder_exclusion_' . $type;
$default = 'default_' . $type;
/*
* Determine if we need to do a full backup.
*
* Scenarios include:
* # We are in the middle of creating a backup file for update protection.
* # We are creating a 'full' backup.
* # We are creating a backup immediately before a WordPress auto update.
*/
if ( $this->core->is_archiving_update_protection || $this->core->is_backup_full || $this->core->pre_auto_update ) {
return $this->$default;
}
/*
* If we are backing up a site now (not for update protection) and
* we've posted folder settings, use those.
*/
if ( $this->core->is_backup_now && isset( $_POST[ $key ] ) ) {
$this->$type = $this->from_post( $type );
return $this->$type;
}
if ( ! is_null( $this->$type ) ) {
return $this->$type;
}
if ( $this->core->settings->is_saving_settings ) {
$this->$type = $this->from_post( $type );
} elseif ( isset( $settings['folder_exclusion_type'] ) && 'full' === $settings['folder_exclusion_type'] ) {
/*
* If the user configured "Backup all files" as the "Files and Folders" settings, then
* use the default values.
*/
$this->$default;
} elseif ( isset( $settings[ $key ] ) ) {
/*
* Is there value for this in the settings?
*
* Initially, we checked to make sure $settings[$key] wasn't empty and
* it was a string. Now, we'll simply see if it is set. This will allow
* for the user to enter nothing in the exclude field.
*/
$this->$type = $settings[ $key ];
} elseif ( ! $settings ) {
$settings = $this->core->settings->get_settings();
if ( ! empty( $settings[ $key ] ) && is_string( $settings[ $key ] ) ) {
$this->$type = $settings[ $key ];
}
}
if ( is_null( $this->$type ) ) {
$this->$type = $this->$default;
}
return $this->$type;
}
/**
* Whether or not a file is banned.
*
* Some files are just bad for business. Files in this list won't be backed up, and the user has
* no control at this time to modify. Only files we're certain should be banned, should be.
*
* @since 1.14.13
*
* @param string $file A filepath. Not absolute, but relative to ABSPATH, such as wp-admin/css/about.css
*
* @return bool
*/
public function is_banned( $filepath ) {
$banned = Boldgrid_Backup_Admin::get_configs()['banned'];
// @todo Allow for regular expressions in the future.
return in_array( basename( $filepath ), $banned, true );
}
/**
* Determine if a include/exclude value matches a file.
*
* For example, if I pass in "wp-content" as a $value and
* wp-content/file.php as a $file, it should match. If I pass in "joec" as a
* $value and wp-content/file.php as a $file, it should not match.
*
* @param string $value Input string.
* @param string $file File path relative to ABSPATH, such as:
* "wp-content/plugins/boldgrid-backup/boldgrid-backup.php".
* @return bool
*/
public function is_match( $value, $file ) {
if ( '*' === $value ) {
return true;
}
// Handle filtering of core WordPress files.
if ( 'WPCORE' === $value ) {
return $this->core->core_files->is_core_file( $file );
}
/*
* Convert a Windows filepath to Linux. In this method we're going to
* assume that / is the directory separator.
*/
$file = str_replace( '\\', '/', $file );
$pattern = $this->create_pattern( $value );
preg_match( $pattern, $file, $matches );
return ! empty( $matches );
}
/**
* Is using default settings?
*
* @since 1.6.0
*
* @return bool
*/
public function is_using_defaults() {
$type = $this->from_settings( 'type' );
return 'full' === $type;
}
/**
* Get our include / exclude settings from $_POST.
*
* @since 1.6.0
*
* @param string $type Either include or exclude.
* @return string
*/
public function from_post( $type ) {
if ( ! in_array( $type, $this->types, true ) ) {
return false;
}
$key = 'folder_exclusion_' . $type;
switch ( $type ) {
case 'include':
/*
* If you submit an empty "include" setting, it will be
* interpreted as include all, *.
*/
$value = ! empty( $_POST[ $key ] ) ? $_POST[ $key ] : $this->default_include;
break;
case 'exclude':
/*
* You are allowed to submit a blank "exclude" setting. It means
* you do not want to exclude anything.
*/
$value = empty( $_POST[ $key ] ) ? '' : $_POST[ $key ];
break;
case 'type':
$value = ! empty( $_POST[ $key ] ) &&
in_array( $_POST[ $key ], $this->valid_types, true ) ?
$_POST[ $key ] : $this->default_type;
break;
}
$value = sanitize_text_field( $value );
return $value;
}
/**
* Handle the ajax request to preview the filters.
*
* @since 1.6.0
*/
public function wp_ajax_preview() {
if ( ! check_ajax_referer( 'folder_exclusion_preview', 'security', false ) ) {
wp_send_json_error( __( 'Invalid nonce.', 'boldgrid-backup' ) );
}
$include = isset( $_POST['include'] ) ? sanitize_text_field( $_POST['include'] ) : null;
$exclude = isset( $_POST['exclude'] ) ? sanitize_text_field( $_POST['exclude'] ) : null;
if ( is_null( $include ) || is_null( $exclude ) ) {
wp_send_json_error( __( 'Invalid include / exclude values.', 'boldgrid-backup' ) );
}
$this->in_ajax_preview = true;
$filelist = $this->core->get_filtered_filelist();
if ( empty( $filelist ) ) {
wp_send_json_error( __( 'No files match your criteria.', 'boldgrid-backup' ) );
}
$markup = array();
foreach ( $filelist as $file ) {
$markup[] = $file[1];
}
wp_send_json_success( $markup );
}
}