-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathCache_File_Cleaner.php
More file actions
146 lines (123 loc) · 2.8 KB
/
Cache_File_Cleaner.php
File metadata and controls
146 lines (123 loc) · 2.8 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
<?php
/**
* File: Cache_File_Cleaner.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class Cache_File_Cleaner
*
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
* phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
* phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
* phpcs:disable WordPress.WP.AlternativeFunctions
*/
class Cache_File_Cleaner {
/**
* Cache directory
*
* @var string
*/
protected $_cache_dir = '';
/**
* Clean operation time limit
*
* @var int
*/
protected $_clean_timelimit = 0;
/**
* Exclude files
*
* @var array
*/
protected $_exclude = array();
/**
* PHP5-style constructor
*
* @param array $config Config.
*
* @return void
*/
public function __construct( $config = array() ) {
$this->_cache_dir = ( isset( $config['cache_dir'] ) ? trim( $config['cache_dir'] ) : 'cache' );
$this->_clean_timelimit = ( isset( $config['clean_timelimit'] ) ? (int) $config['clean_timelimit'] : 180 );
$this->_exclude = ( isset( $config['exclude'] ) ? (array) $config['exclude'] : array() );
}
/**
* Run clean operation
*
* @return void
*/
public function clean() {
@set_time_limit( $this->_clean_timelimit ); // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged
$this->_clean( $this->_cache_dir, false );
}
/**
* Run clean operation
*
* @return void
*/
public function clean_before() {
@set_time_limit( $this->_clean_timelimit ); // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged
$this->_clean( $this->_cache_dir, false );
}
/**
* Clean
*
* @param string $path Path.
* @param bool $remove Remove flag.
*
* @return void
*/
public function _clean( $path, $remove = true ) {
$dir = @opendir( $path );
if ( $dir ) {
$entry = @readdir( $dir );
while ( false !== $entry ) {
if ( '.' === $entry || '..' === $entry ) {
$entry = @readdir( $dir );
continue;
}
foreach ( $this->_exclude as $mask ) {
if ( fnmatch( $mask, basename( $entry ) ) ) {
continue 2;
}
}
$full_path = $path . DIRECTORY_SEPARATOR . $entry;
if ( @is_dir( $full_path ) ) {
$this->_clean( $full_path );
} elseif ( ! $this->is_valid( $full_path ) ) {
@unlink( $full_path );
}
$entry = @readdir( $dir );
}
@closedir( $dir );
if ( $remove ) {
@rmdir( $path );
}
}
}
/**
* Check if file is valid
*
* @param string $file File.
*
* @return bool
*/
public function is_valid( $file ) {
$valid = false;
if ( file_exists( $file ) ) {
$fp = @fopen( $file, 'rb' );
if ( $fp ) {
$expires = @fread( $fp, 4 );
if ( false !== $expires ) {
list( , $expires_at ) = @unpack( 'L', $expires );
$valid = ( time() < $expires_at );
}
@fclose( $fp );
}
}
return $valid;
}
}