1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HM\BackUpWordPress; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Perform a file backup using the PclZip PHP fallback library |
7
|
|
|
*/ |
8
|
|
|
class Pclzip_File_Backup_Engine extends File_Backup_Engine { |
9
|
|
|
|
10
|
|
|
public function __construct() { |
11
|
|
|
parent::__construct(); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
public function load_pclzip() { |
15
|
|
|
|
16
|
|
|
if ( ! defined( 'PCLZIP_TEMPORARY_DIR' ) ) { |
17
|
|
|
define( 'PCLZIP_TEMPORARY_DIR', trailingslashit( PATH::get_path() ) ); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
require_once( ABSPATH . 'wp-admin/includes/class-pclzip.php' ); |
21
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function backup() { |
|
|
|
|
25
|
|
|
|
26
|
|
|
$this->load_pclzip(); |
27
|
|
|
|
28
|
|
|
if ( ! class_exists( 'PclZip' ) ) { |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
global $_hmbkp_exclude_string; |
|
|
|
|
33
|
|
|
$_hmbkp_exclude_string = $this->get_excludes_regex(); |
34
|
|
|
|
35
|
|
|
$zip = new \PclZip( $this->get_backup_filepath() ); |
36
|
|
|
|
37
|
|
|
$result = $zip->create( Path::get_root(), \PCLZIP_OPT_REMOVE_PATH, Path::get_root(), \PCLZIP_CB_PRE_ADD, 'HM\BackUpWordPress\hmbkp_pclzip_callback' ); |
38
|
|
|
|
39
|
|
|
if ( 0 === $result ) { |
40
|
|
|
$this->error( __CLASS__, $zip->errorInfo( true ) ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
unset( $GLOBALS['_hmbkp_exclude_string'] ); |
44
|
|
|
|
45
|
|
|
return $this->verify_backup(); |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Generate the exclude params for pclzip |
51
|
|
|
* |
52
|
|
|
* Takes the exclude rules and formats them as regex |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function get_excludes_regex() { |
57
|
|
|
|
58
|
|
|
$wildcard = '([\s\S]*?)'; |
59
|
|
|
$separator = '|'; |
60
|
|
|
|
61
|
|
|
$excludes = $this->excludes->get_excludes(); |
|
|
|
|
62
|
|
|
|
63
|
|
|
foreach ( $excludes as $key => &$rule ) { |
64
|
|
|
|
65
|
|
|
$file = $absolute = $fragment = false; |
|
|
|
|
66
|
|
|
|
67
|
|
|
// Files don't end with / |
68
|
|
View Code Duplication |
if ( ! in_array( substr( $rule, - 1 ), array( '\\', '/' ) ) ) { |
|
|
|
|
69
|
|
|
$file = true; |
|
|
|
|
70
|
|
|
} // If rule starts with a / then treat as absolute path |
71
|
|
|
elseif ( in_array( substr( $rule, 0, 1 ), array( '\\', '/' ) ) ) { |
72
|
|
|
$absolute = true; |
73
|
|
|
} // Otherwise treat as dir fragment |
74
|
|
|
else { |
75
|
|
|
$fragment = true; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Strip $this->root and conform |
79
|
|
|
$rule = str_ireplace( Path::get_root(), '', untrailingslashit( wp_normalize_path( $rule ) ) ); |
80
|
|
|
|
81
|
|
|
// Strip the preceeding slash |
82
|
|
|
if ( in_array( substr( $rule, 0, 1 ), array( '\\', '/' ) ) ) { |
83
|
|
|
$rule = substr( $rule, 1 ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$rule = str_replace( '.', '\.', $rule ); |
87
|
|
|
|
88
|
|
|
// Convert any existing wildcards |
89
|
|
|
if ( '*' !== $wildcard && false !== strpos( $rule, '*' ) ) { |
90
|
|
|
$rule = str_replace( '*', $wildcard, $rule ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Add a start carrot to absolute urls for pclzip |
94
|
|
|
if ( $absolute ) { |
95
|
|
|
$rule = '^' . $rule; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return implode( $separator, $excludes ); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
function hmbkp_pclzip_callback( $event, $file ) { |
|
|
|
|
104
|
|
|
|
105
|
|
|
global $_hmbkp_exclude_string; |
|
|
|
|
106
|
|
|
|
107
|
|
|
// Don't try to add unreadable files. |
108
|
|
|
if ( ! is_readable( $file['filename'] ) || ! file_exists( $file['filename'] ) ) { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Match everything else past the exclude list |
113
|
|
|
if ( $_hmbkp_exclude_string && preg_match( '(' . $_hmbkp_exclude_string . ')', $file['filename'] ) ) { |
|
|
|
|
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return true; |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: