-
Notifications
You must be signed in to change notification settings - Fork 86
/
Cache_File_Cleaner_Generic.php
192 lines (170 loc) · 3.98 KB
/
Cache_File_Cleaner_Generic.php
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
<?php
/**
* File: Cache_File_Cleaner_Generic.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class Cache_File_Cleaner_Generic
*
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
* phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
* phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
*/
class Cache_File_Cleaner_Generic extends Cache_File_Cleaner {
/**
* Number of items processed
*
* @var integer
*/
private $processed_count = 0;
/**
* Cache expire time
*
* @var int
*/
private $_expire = 0;
/**
* Minimum valid time
*
* @var int
*/
private $time_min_valid = -1;
/**
* Old file minimum valid time
*
* @var int
*/
private $old_file_time_min_valid = -1;
/**
* PHP5-style constructor
*
* @param array $config Config.
*
* @return void
*/
public function __construct( $config = array() ) {
parent::__construct( $config );
$this->_expire = ( isset( $config['expire'] ) ? (int) $config['expire'] : 0 );
if ( ! $this->_expire ) {
$this->_expire = 0;
} elseif ( $this->_expire > W3TC_CACHE_FILE_EXPIRE_MAX ) {
$this->_expire = W3TC_CACHE_FILE_EXPIRE_MAX;
}
if ( ! empty( $config['time_min_valid'] ) ) {
$this->time_min_valid = $config['time_min_valid'];
$this->old_file_time_min_valid = $config['time_min_valid'];
} elseif ( $this->_expire > 0 ) {
$this->time_min_valid = time() - $this->_expire;
$this->old_file_time_min_valid = time() - $this->_expire * 5;
}
}
/**
* Clean
*
* @param string $path Path.
* @param bool $remove Remove flag.
*
* @return void
*/
public function _clean( $path, $remove = false ) {
$dir = false;
if ( is_dir( $path ) ) {
$dir = @opendir( $path );
}
if ( $dir ) {
while ( ( $entry = @readdir( $dir ) ) !== false ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
if ( '.' === $entry || '..' === $entry ) {
continue;
}
$full_path = $path . DIRECTORY_SEPARATOR . $entry;
foreach ( $this->_exclude as $mask ) {
if ( fnmatch( $mask, basename( $entry ) ) ) {
continue 2;
}
}
if ( @is_dir( $full_path ) ) {
$this->_clean( $full_path );
} else {
$this->_clean_file( $entry, $full_path );
}
}
@closedir( $dir );
if ( $this->is_empty_dir( $path ) ) {
@rmdir( $path );
}
}
}
/**
* Clean file
*
* @param string $entry Entry.
* @param string $full_path Full path.
*
* @return void
*/
public function _clean_file( $entry, $full_path ) {
if ( '_old' === substr( $entry, -4 ) ) {
if ( ! $this->is_old_file_valid( $full_path ) ) {
$this->processed_count++;
@unlink( $full_path );
}
} elseif ( ! $this->is_valid( $full_path ) ) {
$old_entry_path = $full_path . '_old';
$this->processed_count++;
if ( ! @rename( $full_path, $old_entry_path ) ) {
// if we can delete old entry - do second attempt to store in old-entry file.
if ( @unlink( $old_entry_path ) ) {
if ( ! @rename( $full_path, $old_entry_path ) ) {
// last attempt - just remove entry.
@unlink( $full_path );
}
}
}
}
}
/**
* Checks if file is valid
*
* @param string $file File.
*
* @return bool
*/
public function is_valid( $file ) {
if ( $this->time_min_valid > 0 && file_exists( $file ) ) {
$ftime = @filemtime( $file );
if ( $ftime && $ftime >= $this->time_min_valid ) {
return true;
}
}
return false;
}
/**
* Checks if old file is valid
*
* @param string $file File.
*
* @return bool
*/
public function is_old_file_valid( $file ) {
if ( $this->old_file_time_min_valid > 0 && file_exists( $file ) ) {
$ftime = @filemtime( $file );
if ( $ftime && $ftime >= $this->old_file_time_min_valid ) {
return true;
}
}
return false;
}
/**
* Checks if directory is empty
*
* @param string $dir Directory.
*
* @return bool
*/
public function is_empty_dir( $dir ) {
$files = @scandir( $dir );
return $files && count( $files ) <= 2;
}
}