1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HM\BackUpWordPress; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Manages both the backup path and site root |
7
|
|
|
* |
8
|
|
|
* Handles calculating & protecting the directory that backups will be stored in |
9
|
|
|
* as well as the directory that is being backed up |
10
|
|
|
*/ |
11
|
|
|
class Path { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The path to the directory that backup files are stored in |
15
|
|
|
* |
16
|
|
|
* @var string $this->path |
17
|
|
|
*/ |
18
|
|
|
private $path; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The path to the directory that will be backed up |
22
|
|
|
* |
23
|
|
|
* @var string $this->root |
24
|
|
|
*/ |
25
|
|
|
private $root; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The path to the directory that backup files are stored in |
29
|
|
|
* |
30
|
|
|
* @var string $this->path |
31
|
|
|
*/ |
32
|
|
|
private $custom_path; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Contains the instantiated Path instance |
36
|
|
|
* |
37
|
|
|
* @var Path $this->instance |
38
|
|
|
*/ |
39
|
|
|
private static $instance; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Protected constructor to prevent creating a new instance of the |
43
|
|
|
* *Singleton* via the `new` operator from outside of this class. |
44
|
|
|
*/ |
45
|
|
|
protected function __construct() {} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Private clone method to prevent cloning of the instance of the |
49
|
|
|
* *Singleton* instance. |
50
|
|
|
*/ |
51
|
|
|
private function __clone() {} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Private unserialize method to prevent unserializing of the *Singleton* |
55
|
|
|
* instance. |
56
|
|
|
*/ |
57
|
|
|
private function __wakeup() {} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the *Singleton* instance of this class. |
61
|
|
|
* |
62
|
|
|
* @staticvar Path $instance The *Singleton* instances of this class. |
63
|
|
|
* |
64
|
|
|
* @return Path The *Singleton* instance. |
65
|
|
|
*/ |
66
|
|
|
public static function get_instance() { |
67
|
|
|
|
68
|
|
|
if ( ! ( self::$instance instanceof Path ) ) { |
69
|
|
|
self::$instance = new Path(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return self::$instance; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Convenience method for quickly grabbing the path |
77
|
|
|
*/ |
78
|
|
|
public static function get_path() { |
|
|
|
|
79
|
|
|
return self::get_instance()->get_calculated_path(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Convenience method for quickly grabbing the root |
84
|
|
|
*/ |
85
|
|
|
public static function get_root() { |
|
|
|
|
86
|
|
|
return self::get_instance()->get_calculated_root(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Calculate the path to the site "home" directory. |
91
|
|
|
* |
92
|
|
|
* The home directory is the path equivalent to the home_url. That is, |
93
|
|
|
* the path to the true root of the website. In situations where WordPress is |
94
|
|
|
* installed in a subdirectory the home path is different to ABSPATH |
95
|
|
|
* |
96
|
|
|
* @param string $site_path The site_path to use when calculating the home path, defaults to ABSPATH |
97
|
|
|
*/ |
98
|
|
|
public static function get_home_path( $site_path = ABSPATH ) { |
|
|
|
|
99
|
|
|
|
100
|
|
|
if ( defined( 'HMBKP_ROOT' ) && HMBKP_ROOT ) { |
101
|
|
|
return wp_normalize_path( HMBKP_ROOT ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$home_path = $site_path; |
105
|
|
|
|
106
|
|
|
if ( path_in_php_open_basedir( dirname( $site_path ) ) ) { |
107
|
|
|
|
108
|
|
|
$home = set_url_scheme( get_option( 'home' ), 'http' ); |
109
|
|
|
$siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' ); |
110
|
|
|
if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) { |
111
|
|
|
$wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */ |
112
|
|
|
$pos = strripos( wp_normalize_path( $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) ); |
113
|
|
|
$home_path = substr( $_SERVER['SCRIPT_FILENAME'], 0, $pos ); |
114
|
|
|
$home_path = trailingslashit( $home_path ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ( is_multisite() ) { |
118
|
|
|
$slashed_home = trailingslashit( get_option( 'home' ) ); |
119
|
|
|
$base = parse_url( $slashed_home, PHP_URL_PATH ); |
120
|
|
|
$document_root_fix = wp_normalize_path( realpath( $_SERVER['DOCUMENT_ROOT'] ) ); |
121
|
|
|
$abspath_fix = wp_normalize_path( ABSPATH ); |
122
|
|
|
$home_path = 0 === strpos( $abspath_fix, $document_root_fix ) ? $document_root_fix . $base : $home_path; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return wp_normalize_path( untrailingslashit( $home_path ) ); |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* get the calculated path to the directory where backups will be stored |
132
|
|
|
*/ |
133
|
|
|
private function get_calculated_path() { |
|
|
|
|
134
|
|
|
|
135
|
|
|
// Calculate the path if needed |
136
|
|
|
if ( empty( $this->path ) || ! wp_is_writable( $this->path ) ) { |
137
|
|
|
$this->calculate_path(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Ensure the backup directory is protected |
141
|
|
|
$this->protect_path(); |
142
|
|
|
|
143
|
|
|
return wp_normalize_path( $this->path ); |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the path directly, overriding the default |
149
|
|
|
* |
150
|
|
|
* @param $path |
151
|
|
|
*/ |
152
|
|
|
public function set_path( $path ) { |
153
|
|
|
|
154
|
|
|
$this->custom_path = $path; |
155
|
|
|
|
156
|
|
|
// Re-calculate the backup path |
157
|
|
|
$this->calculate_path(); |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* get the calculated path to the directory that will be backed up |
163
|
|
|
*/ |
164
|
|
|
private function get_calculated_root() { |
|
|
|
|
165
|
|
|
|
166
|
|
|
$root = self::get_home_path(); |
167
|
|
|
|
168
|
|
|
if ( defined( 'HMBKP_ROOT' ) && HMBKP_ROOT ) { |
169
|
|
|
$root = HMBKP_ROOT; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ( $this->root ) { |
173
|
|
|
$root = $this->root; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return wp_normalize_path( $root ); |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Set the root path directly, overriding the default |
182
|
|
|
* |
183
|
|
|
* @param $root |
184
|
|
|
*/ |
185
|
|
|
public function set_root( $root ) { |
186
|
|
|
$this->root = $root; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function reset_path() { |
190
|
|
|
$this->set_path( false ); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get the path to the default backup location in wp-content |
195
|
|
|
*/ |
196
|
|
|
public function get_default_path() { |
197
|
|
|
return trailingslashit( wp_normalize_path( WP_CONTENT_DIR ) ) . 'backupwordpress-' . substr( HMBKP_SECURE_KEY, 0, 10 ) . '-backups'; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the path to the fallback backup location in uploads |
202
|
|
|
*/ |
203
|
|
|
public function get_fallback_path() { |
204
|
|
|
|
205
|
|
|
$upload_dir = wp_upload_dir(); |
206
|
|
|
|
207
|
|
|
return trailingslashit( wp_normalize_path( $upload_dir['basedir'] ) ) . 'backupwordpress-' . substr( HMBKP_SECURE_KEY, 0, 10 ) . '-backups'; |
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get the path to the custom backup location if it's been set |
213
|
|
|
*/ |
214
|
|
|
public function get_custom_path() { |
|
|
|
|
215
|
|
|
|
216
|
|
|
if ( $this->custom_path ) { |
217
|
|
|
return $this->custom_path; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if ( defined( 'HMBKP_PATH' ) && wp_is_writable( HMBKP_PATH ) ) { |
221
|
|
|
return HMBKP_PATH; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return ''; |
225
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Builds an array containing existing backups folders. |
230
|
|
|
* |
231
|
|
|
* @return array |
232
|
|
|
*/ |
233
|
|
|
public function get_existing_paths() { |
234
|
|
|
|
235
|
|
|
if ( false === $default = glob( WP_CONTENT_DIR . '/backupwordpress-*-backups', GLOB_ONLYDIR ) ) { |
236
|
|
|
$default = array(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$upload_dir = wp_upload_dir(); |
240
|
|
|
|
241
|
|
|
if ( false === $fallback = glob( $upload_dir['basedir'] . '/backupwordpress-*-backups', GLOB_ONLYDIR ) ) { |
242
|
|
|
$fallback = array(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$paths = array_merge( $default, $fallback ); |
246
|
|
|
|
247
|
|
|
$paths = array_map( 'wp_normalize_path', $paths ); |
248
|
|
|
|
249
|
|
|
return $paths; |
250
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Returns the first existing path if there is one |
255
|
|
|
* |
256
|
|
|
* @return string Backup path if found empty string if not |
257
|
|
|
*/ |
258
|
|
|
public function get_existing_path() { |
259
|
|
|
|
260
|
|
|
$paths = $this->get_existing_paths(); |
261
|
|
|
|
262
|
|
|
if ( ! empty( $paths[0] ) ) { |
263
|
|
|
return $paths[0]; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return ''; |
267
|
|
|
|
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Calculate the backup path and create the directory if it doesn't exist. |
272
|
|
|
* |
273
|
|
|
* Tries all possible locations and uses the first one possible |
274
|
|
|
* |
275
|
|
|
* @return |
276
|
|
|
*/ |
277
|
|
|
public function calculate_path() { |
278
|
|
|
|
279
|
|
|
$paths = array(); |
280
|
|
|
|
281
|
|
|
// If we have a custom path then try to use it |
282
|
|
|
if ( $this->get_custom_path() ) { |
283
|
|
|
$paths[] = $this->get_custom_path(); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
// If there is already a backups directory then try to use that |
287
|
|
|
if ( $this->get_existing_path() ) { |
288
|
|
|
$paths[] = $this->get_existing_path(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
// If not then default to a new directory in wp-content |
292
|
|
|
$paths[] = $this->get_default_path(); |
293
|
|
|
|
294
|
|
|
// If that didn't work then fallback to a new directory in uploads |
295
|
|
|
$paths[] = $this->get_fallback_path(); |
296
|
|
|
|
297
|
|
|
// Loop through possible paths, use the first one that exists/can be created and is writable |
298
|
|
|
foreach ( $paths as $path ) { |
299
|
|
|
if ( wp_mkdir_p( $path ) && wp_is_writable( $path ) ) { // Also handles fixing perms / directory already exists |
300
|
|
|
break; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
if ( file_exists( $path ) && wp_is_writable( $path ) ) { |
305
|
|
|
$this->path = $path; |
|
|
|
|
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Protect the directory that backups are stored in |
312
|
|
|
* |
313
|
|
|
* - Adds an index.html file in an attempt to disable directory browsing |
314
|
|
|
* - Adds a .httaccess file to deny direct access if on Apache |
315
|
|
|
* |
316
|
|
|
* @param string $reset |
317
|
|
|
*/ |
318
|
|
|
public function protect_path( $reset = 'no' ) { |
319
|
|
|
|
320
|
|
|
global $is_apache; |
|
|
|
|
321
|
|
|
|
322
|
|
|
// Protect against directory browsing by including an index.html file |
323
|
|
|
$index = $this->path . '/index.html'; |
324
|
|
|
|
325
|
|
|
if ( 'reset' === $reset && file_exists( $index ) ) { |
326
|
|
|
@unlink( $index ); |
|
|
|
|
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
if ( ! file_exists( $index ) && wp_is_writable( $this->path ) ) { |
330
|
|
|
file_put_contents( $index, '' ); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$htaccess = $this->path . '/.htaccess'; |
334
|
|
|
|
335
|
|
|
if ( ( 'reset' === $reset ) && file_exists( $htaccess ) ) { |
336
|
|
|
@unlink( $htaccess ); |
|
|
|
|
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
// Protect the directory with a .htaccess file on Apache servers |
340
|
|
|
if ( $is_apache && function_exists( 'insert_with_markers' ) && ! file_exists( $htaccess ) && wp_is_writable( $this->path ) ) { |
341
|
|
|
|
342
|
|
|
$contents[] = '# ' . sprintf( __( 'This %s file ensures that other people cannot download your backup files.', 'backupwordpress' ), '.htaccess' ); |
|
|
|
|
343
|
|
|
$contents[] = ''; |
344
|
|
|
$contents[] = '<IfModule mod_rewrite.c>'; |
345
|
|
|
$contents[] = 'RewriteEngine On'; |
346
|
|
|
$contents[] = 'RewriteCond %{QUERY_STRING} !key=' . HMBKP_SECURE_KEY; |
347
|
|
|
$contents[] = 'RewriteRule (.*) - [F]'; |
348
|
|
|
$contents[] = '</IfModule>'; |
349
|
|
|
$contents[] = ''; |
350
|
|
|
|
351
|
|
|
file_put_contents( $htaccess, '' ); |
352
|
|
|
|
353
|
|
|
insert_with_markers( $htaccess, 'BackUpWordPress', $contents ); |
354
|
|
|
|
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* If we have more than one path then move any existing backups to the current path and remove them |
361
|
|
|
*/ |
362
|
|
|
public function merge_existing_paths() { |
363
|
|
|
|
364
|
|
|
$paths = $this->get_existing_paths(); |
365
|
|
|
|
366
|
|
|
if ( ( $paths && $this->get_custom_path() ) || count( $paths ) > 1 ) { |
|
|
|
|
367
|
|
|
foreach ( $paths as $old_path ) { |
368
|
|
|
$this->move_old_backups( $old_path ); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Move backup files from an existing directory and the new |
376
|
|
|
* location |
377
|
|
|
* |
378
|
|
|
* @param string $path The path to move the backups from |
|
|
|
|
379
|
|
|
*/ |
380
|
|
|
public function move_old_backups( $from ) { |
381
|
|
|
|
382
|
|
|
if ( ! is_readable( $from ) ) { |
383
|
|
|
return; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
if ( ! wp_is_writable( Path::get_path() ) ) { |
387
|
|
|
return; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
// Move any existing backups |
391
|
|
|
if ( $handle = opendir( $from ) ) { |
392
|
|
|
|
393
|
|
|
// Loop through the backup directory |
394
|
|
|
while ( false !== ( $file = readdir( $handle ) ) ) { |
395
|
|
|
|
396
|
|
|
// Find all zips |
397
|
|
|
if ( 'zip' === pathinfo( $file, PATHINFO_EXTENSION ) ) { |
398
|
|
|
|
399
|
|
|
// Try to move them |
400
|
|
|
if ( ! @rename( trailingslashit( $from ) . $file, trailingslashit( Path::get_path() ) . $file ) ) { |
401
|
|
|
|
402
|
|
|
|
403
|
|
|
// If we can't move them then try to copy them |
404
|
|
|
copy( trailingslashit( $from ) . $file, trailingslashit( Path::get_path() ) . $file ); |
405
|
|
|
|
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
closedir( $handle ); |
412
|
|
|
|
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
// Delete the old directory if it's inside WP_CONTENT_DIR |
416
|
|
|
if ( false !== strpos( $from, WP_CONTENT_DIR ) && $from !== Path::get_path() ) { |
417
|
|
|
rmdirtree( $from ); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Clean any temporary / incomplete backups from the backups directory |
424
|
|
|
*/ |
425
|
|
|
public function cleanup() { |
426
|
|
|
|
427
|
|
|
// Don't cleanup a custom path, who knows what other stuff is there |
428
|
|
|
if ( Path::get_path() === $this->get_custom_path() ) { |
429
|
|
|
return; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
foreach ( new CleanUpIterator( new \DirectoryIterator( $this->path ) ) as $file ) { |
433
|
|
|
|
434
|
|
|
if ( $file->isDot() || ! $file->isReadable() || ! $file->isFile() ) { |
435
|
|
|
continue; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
@unlink( $file->getPathname() ); |
|
|
|
|
439
|
|
|
|
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
class CleanUpIterator extends \FilterIterator { |
|
|
|
|
447
|
|
|
|
448
|
|
|
// Don't match index.html,files with zip extension or status logfiles. |
449
|
|
|
public function accept() { |
450
|
|
|
return ! preg_match( '/(index\.html|.*\.zip|.*-running)/', $this->current() ); |
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.