1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HM\BackUpWordPress; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Manages exclude rules |
7
|
|
|
*/ |
8
|
|
|
class Excludes { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The array of exclude rules. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $excludes; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The array of default exclude rules. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $default_excludes = array( |
23
|
|
|
'.svn', |
24
|
|
|
'_svn', |
25
|
|
|
'CVS', |
26
|
|
|
'_darcs', |
27
|
|
|
'.arch-params', |
28
|
|
|
'.monotone', |
29
|
|
|
'.bzr', |
30
|
|
|
'.git', |
31
|
|
|
'.hg', |
32
|
|
|
'backupwp', |
33
|
|
|
'backwpup-*', |
34
|
|
|
'updraft', |
35
|
|
|
'(?:wp-)?snapshots?', // wp-snapshots, snapshots, snapshot |
36
|
|
|
'backupbuddy_backups', |
37
|
|
|
'pb_backupbuddy', |
38
|
|
|
'backup-db', |
39
|
|
|
'Envato-backups', |
40
|
|
|
'managewp', |
41
|
|
|
'backupwordpress-*-backups', |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
public function __construct( $excludes = array() ) { |
45
|
|
|
$this->set_excludes( $excludes ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set the exclude rules. |
50
|
|
|
* |
51
|
|
|
* Excludes rules should be a complete or partial directory or file path. |
52
|
|
|
* Wildcards can be specified with the * character. |
53
|
|
|
* |
54
|
|
|
* @param string|array $excludes The list of exclude rules, accepts either |
55
|
|
|
* a comma separated list or an array. |
56
|
|
|
*/ |
57
|
|
|
public function set_excludes( $excludes ) { |
58
|
|
|
|
59
|
|
|
if ( is_string( $excludes ) ) { |
60
|
|
|
$excludes = explode( ',', $excludes ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->excludes = $excludes; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the excludes. |
68
|
|
|
* |
69
|
|
|
* Returns any user set excludes as well as the default list. |
70
|
|
|
* |
71
|
|
|
* @return array The array of exclude rules. |
72
|
|
|
*/ |
73
|
|
|
public function get_excludes() { |
74
|
|
|
return array_merge( $this->get_default_excludes(), $this->get_user_excludes() ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the excludes prepared for use with regex. |
79
|
|
|
* |
80
|
|
|
* The primary difference being that any wildcard (*) rules are converted to the regex |
81
|
|
|
* fragment `[\s\S]*?`. |
82
|
|
|
* |
83
|
|
|
* @return array The array of exclude rules. |
84
|
|
|
*/ |
85
|
|
|
public function get_excludes_for_regex() { |
86
|
|
|
|
87
|
|
|
$excludes = $this->get_excludes(); |
88
|
|
|
|
89
|
|
|
// Prepare the exclude rules. |
90
|
|
|
foreach ( $excludes as &$exclude ) { |
91
|
|
|
|
92
|
|
|
if ( strpos( $exclude, '*' ) !== false ) { |
93
|
|
|
|
94
|
|
|
// Escape slashes. |
95
|
|
|
$exclude = str_replace( '/', '\/', $exclude ); |
96
|
|
|
|
97
|
|
|
// Convert WildCards to regex. |
98
|
|
|
$exclude = str_replace( '*', '[\s\S]*?', $exclude ); |
99
|
|
|
|
100
|
|
|
// Wrap in slashes. |
101
|
|
|
$exclude = '/' . $exclude . '/'; |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $excludes; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the user defined excludes. |
111
|
|
|
* |
112
|
|
|
* @return array The array of excludes. |
113
|
|
|
*/ |
114
|
|
|
public function get_user_excludes() { |
115
|
|
|
|
116
|
|
|
$excludes = $this->excludes; |
117
|
|
|
|
118
|
|
|
// If path() is inside root(), exclude it. |
119
|
|
|
if ( strpos( Path::get_path(), Path::get_root() ) !== false && Path::get_root() !== Path::get_path() ) { |
120
|
|
|
array_unshift( $excludes, trailingslashit( Path::get_path() ) ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->normalize( $excludes ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the array of default excludes. |
128
|
|
|
* |
129
|
|
|
* @return array The array of excludes. |
130
|
|
|
*/ |
131
|
|
|
public function get_default_excludes() { |
132
|
|
|
|
133
|
|
|
$excludes = array(); |
134
|
|
|
|
135
|
|
|
// Back compat with the old constant. |
136
|
|
|
if ( defined( 'HMBKP_EXCLUDE' ) && HMBKP_EXCLUDE ) { |
137
|
|
|
$excludes = explode( ',', implode( ',', (array) HMBKP_EXCLUDE ) ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$excludes = array_merge( $this->default_excludes, $excludes ); |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Allow the default excludes list to be modified. |
144
|
|
|
* |
145
|
|
|
* @param $excludes The array of exclude rules. |
146
|
|
|
*/ |
147
|
|
|
$excludes = apply_filters( 'hmbkp_default_excludes', $excludes ); |
148
|
|
|
|
149
|
|
|
return $this->normalize( $excludes ); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Normalise the exclude rules so they are ready to work with. |
154
|
|
|
* |
155
|
|
|
* @param array $excludes The array of exclude rules to normalise. |
156
|
|
|
* |
157
|
|
|
* @return array The array of normalised rules. |
158
|
|
|
*/ |
159
|
|
|
public function normalize( $excludes ) { |
160
|
|
|
|
161
|
|
|
$excludes = array_map( |
162
|
|
|
function( $exclude ) { |
163
|
|
|
|
164
|
|
|
// Convert absolute paths to relative. |
165
|
|
|
$exclude = str_replace( PATH::get_root(), '', wp_normalize_path( $exclude ) ); |
166
|
|
|
|
167
|
|
|
// Trim the slashes. |
168
|
|
|
$exclude = trim( $exclude ); |
169
|
|
|
$exclude = ltrim( $exclude, '/' ); |
170
|
|
|
$exclude = untrailingslashit( $exclude ); |
171
|
|
|
|
172
|
|
|
return $exclude; |
173
|
|
|
}, |
174
|
|
|
$excludes |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
// Remove duplicate or empty rules. |
178
|
|
|
$excludes = array_unique( $excludes ); |
179
|
|
|
$excludes = array_filter( $excludes ); |
180
|
|
|
|
181
|
|
|
return $excludes; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Check if a file is excluded, |
186
|
|
|
* i.e. excluded directly or is in an excluded folder. |
187
|
|
|
* |
188
|
|
|
* @param \SplFileInfo $file File to check if it's excluded. |
189
|
|
|
* |
190
|
|
|
* @return bool|null True if file is excluded, false otherwise. |
191
|
|
|
* Null - if it's not a file. |
192
|
|
|
*/ |
193
|
|
|
public function is_file_excluded( \SplFileInfo $file ) { |
194
|
|
|
|
195
|
|
|
$exclude_string = implode( '|', $this->get_excludes_for_regex() ); |
196
|
|
|
$file_path_no_root = str_ireplace( |
197
|
|
|
trailingslashit( Path::get_root() ), |
198
|
|
|
'', |
199
|
|
|
wp_normalize_path( $file->getPathname() ) |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
if ( $exclude_string && preg_match( '(' . $exclude_string . ')', $file_path_no_root ) ) { |
203
|
|
|
return true; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return false; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|