1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
cache_files_cleaner.php
См. документацию.
1
<?php
2
3
class
CFileCacheCleaner
4
{
5
private
$cacheType;
6
private
$path;
7
private
$currentBase;
8
private
$currentPath;
9
private
$fileTree;
10
private
$rootDir;
11
12
public
function
__construct
($cacheType, $rootDir =
null
)
13
{
14
global
$DB
;
15
16
$this->cacheType = $cacheType;
17
$this->rootDir = $rootDir ??
$_SERVER
[
"DOCUMENT_ROOT"
];
18
19
switch
($this->cacheType)
20
{
21
case
"menu"
:
22
$this->
path
= [
23
BX_PERSONAL_ROOT .
'/managed_cache/'
.
$DB
->type .
'/menu/'
,
24
];
25
break
;
26
case
"managed"
:
27
$this->
path
= [
28
BX_PERSONAL_ROOT .
'/managed_cache/'
,
29
BX_PERSONAL_ROOT .
'/stack_cache/'
,
30
];
31
break
;
32
case
"html"
:
33
$this->
path
= [
34
BX_PERSONAL_ROOT .
'/html_pages/'
,
35
];
36
break
;
37
case
"expired"
:
38
$this->
path
= [
39
BX_PERSONAL_ROOT .
'/cache/'
,
40
BX_PERSONAL_ROOT .
'/managed_cache/'
,
41
BX_PERSONAL_ROOT .
'/stack_cache/'
,
42
];
43
break
;
44
default
:
45
$this->
path
= [
46
BX_PERSONAL_ROOT .
'/cache/'
,
47
BX_PERSONAL_ROOT .
'/managed_cache/'
,
48
BX_PERSONAL_ROOT .
'/stack_cache/'
,
49
BX_PERSONAL_ROOT .
'/html_pages/'
,
50
];
51
break
;
52
}
53
}
54
55
public
function
InitPath
($pathToCheck)
56
{
57
if
($pathToCheck <>
''
)
58
{
59
$pathToCheck = preg_replace(
"#[\\\\/]+#"
,
"/"
,
"/"
. $pathToCheck);
60
//Check if path does not contain any injection
61
if
(preg_match(
'#/\\.\\.#'
, $pathToCheck) || preg_match(
'#\\.\\./#'
, $pathToCheck))
62
{
63
return
false
;
64
}
65
66
$base =
""
;
67
foreach
($this->
path
as $path)
68
{
69
if
(preg_match(
'#^'
. $path .
'#'
, $pathToCheck))
70
{
71
$base = $path;
72
break
;
73
}
74
}
75
76
if
($base <>
''
)
77
{
78
$this->currentBase = $base;
79
$this->currentPath = mb_substr($pathToCheck, mb_strlen($base));
80
return
true
;
81
}
82
return
false
;
83
}
84
else
85
{
86
$this->currentBase = $this->
path
[0];
87
$this->currentPath =
""
;
88
return
true
;
89
}
90
}
91
92
public
function
Start
()
93
{
94
if
($this->currentBase)
95
{
96
$this->fileTree =
new
CFileTree
($this->rootDir . $this->currentBase);
97
$this->fileTree->Start($this->currentPath);
98
}
99
}
100
101
public
function
GetNextFile
()
102
{
103
if
(is_object($this->fileTree))
104
{
105
$file = $this->fileTree->GetNextFile();
106
//Check if current cache subdirectory cleaned
107
if
($file ===
false
)
108
{
109
//Skip all checked bases
110
$arPath
=
$this->path
;
111
while
(!empty(
$arPath
))
112
{
113
$CurBase = array_shift(
$arPath
);
114
if
($CurBase == $this->currentBase)
115
{
116
break
;
117
}
118
}
119
//There is at least one cache directory not checked yet
120
//so try to find a file inside
121
while
(!empty(
$arPath
))
122
{
123
$this->currentBase = array_shift(
$arPath
);
124
$this->currentPath =
""
;
125
$this->fileTree =
new
CFileTree
($this->rootDir . $this->currentBase);
126
$this->fileTree->Start($this->currentPath);
127
$file = $this->fileTree->GetNextFile();
128
if
($file !==
false
)
129
{
130
return
$file;
131
}
132
}
133
return
false
;
134
}
135
return
$file;
136
}
137
else
138
{
139
return
false
;
140
}
141
}
142
143
public
function
GetFileExpiration
(
$fileName
)
144
{
145
if
(preg_match(
'#^'
. $this->rootDir . BX_PERSONAL_ROOT .
'/html_pages/.*\\.html$#'
,
$fileName
))
146
{
147
return
1;
// like a very old file
148
}
149
elseif
(preg_match(
'#\\.~\\d+/#'
,
$fileName
))
//delayed delete files
150
{
151
return
1;
// like a very old file
152
}
153
elseif
(str_ends_with(
$fileName
,
".php"
))
154
{
155
$fd = fopen(
$fileName
,
"rb"
);
156
if
($fd)
157
{
158
$header = fread($fd, 150);
159
fclose($fd);
160
if
(preg_match(
"/dateexpire\s*=\s*'(\d+)'/im"
, $header, $match))
161
{
162
return
doubleval($match[1]);
163
}
164
}
165
}
166
elseif
(str_ends_with(
$fileName
,
".html"
))
167
{
168
$fd = fopen(
$fileName
,
"rb"
);
169
if
($fd)
170
{
171
$header = fread($fd, 26);
172
fclose($fd);
173
if
(str_starts_with($header,
"BX"
))
174
{
175
return
doubleval(mb_substr($header, 14, 12));
176
}
177
}
178
}
179
return
false
;
180
}
181
}
$path
$path
Определения
access_edit.php:21
CFileCacheCleaner
Определения
cache_files_cleaner.php:4
CFileCacheCleaner\GetFileExpiration
GetFileExpiration($fileName)
Определения
cache_files_cleaner.php:143
CFileCacheCleaner\Start
Start()
Определения
cache_files_cleaner.php:92
CFileCacheCleaner\InitPath
InitPath($pathToCheck)
Определения
cache_files_cleaner.php:55
CFileCacheCleaner\GetNextFile
GetNextFile()
Определения
cache_files_cleaner.php:101
CFileCacheCleaner\__construct
__construct($cacheType, $rootDir=null)
Определения
cache_files_cleaner.php:12
CFileTree
Определения
file_tree.php:4
$arPath
$arPath
Определения
file_edit.php:72
$_SERVER
$_SERVER["DOCUMENT_ROOT"]
Определения
cron_frame.php:9
$DB
global $DB
Определения
cron_frame.php:29
elseif
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения
prolog_main_admin.php:393
$fileName
$fileName
Определения
quickway.php:305
path
path
Определения
template_copy.php:201
bitrix
modules
main
classes
general
cache_files_cleaner.php
Создано системой
1.14.0