1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
temp_file.php
См. документацию.
1
<?php
2
3
class
CCloudTempFile
4
{
5
private
static
$buckets = [];
6
10
protected
static
function
GetAbsoluteRoot
()
11
{
12
return
'/tmp'
;
13
}
14
19
public
static
function
IsTempFile
($file_name)
20
{
21
return
preg_match(
'#^'
. self::GetAbsoluteRoot() .
'/BXTEMP-#'
, $file_name) > 0;
22
}
23
24
protected
static
$shutdownRegistered
=
false
;
25
26
protected
static
function
RegisterShutdown
()
27
{
28
if
(!self::$shutdownRegistered)
29
{
30
register_shutdown_function([
'CCloudTempFile'
,
'Cleanup'
]);
31
self::$shutdownRegistered =
true
;
32
}
33
}
34
40
public
static
function
GetFileName
($obBucket, $file_name =
''
)
41
{
42
$dir_name =
self::GetAbsoluteRoot
();
43
$file_name =
Rel2Abs
(
'/'
,
'/'
. $file_name);
44
$temp_path =
''
;
45
$i
= 0;
46
while
(
true
)
47
{
48
$i
++;
49
50
if
($file_name ==
'/'
)
51
{
52
$dir_add = md5(mt_rand());
53
}
54
elseif
(
$i
< 25)
55
{
56
$dir_add = mb_substr(md5(mt_rand()), 0, 3);
57
}
58
else
59
{
60
$dir_add = md5(mt_rand());
61
}
62
63
$temp_path = $dir_name .
'/'
. $dir_add . $file_name;
64
65
if
(!$obBucket->FileExists($temp_path))
66
{
67
self::$buckets[$obBucket->ID .
'|'
. $temp_path] = [
68
'bucket'
=> $obBucket,
69
'filePath'
=> $temp_path,
70
];
71
self::RegisterShutdown
();
72
break
;
73
}
74
}
75
76
return
$temp_path;
77
}
78
85
public
static
function
GetDirectoryName
($obBucket, $hours_to_keep_files = 0, $subdir =
''
)
86
{
87
if
($hours_to_keep_files <= 0)
88
{
89
return
self::GetFileName
($obBucket,
''
);
90
}
91
92
$temp_path =
''
;
//This makes code analyzers happy. $temp_path will never be empty.
93
if
($subdir ===
''
)
94
{
95
$dir_name =
self::GetAbsoluteRoot
() .
'/BXTEMP-'
. date(
'Y-m-d/H/'
, time() + 3600 * $hours_to_keep_files);
96
$i
= 0;
97
while
(
true
)
98
{
99
$i
++;
100
$dir_add = md5(mt_rand());
101
$temp_path = $dir_name . $dir_add .
'/'
;
102
103
if
(!$obBucket->FileExists($temp_path))
104
{
105
break
;
106
}
107
}
108
}
109
else
//Fixed name during the session
110
{
111
$subdir = implode(
'/'
, (is_array($subdir) ? $subdir : [$subdir,
bitrix_sessid
()])) .
'/'
;
112
while
(mb_strpos($subdir,
'//'
) !==
false
)
113
{
114
$subdir = str_replace(
'//'
,
'/'
, $subdir);
115
}
116
117
$bFound
=
false
;
118
for
(
$i
= $hours_to_keep_files;
$i
> 0;
$i
--)
119
{
120
$dir_name =
self::GetAbsoluteRoot
() .
'/BXTEMP-'
. date(
'Y-m-d/H/'
, time() + 3600 *
$i
);
121
$temp_path = $dir_name . $subdir;
122
123
$list = $obBucket->ListFiles($temp_path,
true
);
124
if
($list[
'file'
] || $list[
'dir'
])
125
{
126
$bFound
=
true
;
127
break
;
128
}
129
}
130
131
if
(!
$bFound
)
132
{
133
$dir_name =
self::GetAbsoluteRoot
() .
'/BXTEMP-'
. date(
'Y-m-d/H/'
, time() + 3600 * $hours_to_keep_files);
134
$temp_path = $dir_name . $subdir;
135
}
136
}
137
138
if
(!isset(self::$buckets[$obBucket->ID]))
139
{
140
self::$buckets[$obBucket->ID] = [
141
'bucket'
=> $obBucket,
142
'filePath'
=> null
143
];
144
}
145
self::RegisterShutdown
();
146
147
return
$temp_path;
148
}
149
150
protected
static
$my_pid
=
''
;
151
156
protected
static
function
cleanupFilesLock
($lock =
true
)
157
{
158
if
(!self::$my_pid)
159
{
160
self::$my_pid = md5(mt_rand());
161
}
162
163
$obCacheLock =
null
;
164
$cache_ttl = 300;
165
$uniq_str =
'cleanupFiles'
;
166
$init_dir =
'clouds'
;
167
168
$obCacheLock =
new
CPHPCache();
169
if
($lock)
170
{
171
if
($obCacheLock->InitCache($cache_ttl, $uniq_str, $init_dir))
172
{
173
$vars = $obCacheLock->GetVars();
174
if
(self::$my_pid !== $vars[
'pid'
])
175
{
176
return
false
;
//There is other cleaning process
177
}
178
}
179
elseif
($obCacheLock->StartDataCache())
180
{
181
$obCacheLock->EndDataCache([
'pid'
=> self::$my_pid]);
182
}
183
}
184
else
185
{
186
$obCacheLock->Clean($uniq_str, $init_dir);
187
}
188
189
return
true
;
190
}
191
198
protected
static
function
cleanupFiles
($obBucket, $dir_name, $files)
199
{
200
201
$date = new \Bitrix\Main\Type\DateTime();
202
$date->setTimeZone(
new
DateTimeZone(
'UTC'
));
203
$date->add(
'-1D'
);
204
$tmp_expiration_time = $date->format(
'Y-m-d'
) .
'T'
. $date->format(
'H:i:s'
);
205
$now = date(
'Y-m-d/H/'
, time());
206
207
foreach
($files[
'file'
] as
$i
=> $filePath)
208
{
209
//Files cleanup
210
if
(preg_match(
'#^BXTEMP-(.{4}-..-../../)#'
, $filePath, $match) && $match[1] < $now)
211
{
212
if
(!static::cleanupFilesLock())
213
{
214
return
false
;
215
}
216
$obBucket->DeleteFile($dir_name . $filePath);
217
}
218
elseif
($files[
'file_mtime'
][
$i
] < $tmp_expiration_time)
219
{
220
if
(!static::cleanupFilesLock())
221
{
222
return
false
;
223
}
224
$obBucket->DeleteFile($dir_name . $filePath);
225
}
226
}
227
228
if
(static::cleanupFilesLock())
229
{
230
static::cleanupFilesLock(
false
);
231
}
232
233
return
true
;
234
}
235
236
//PHP shutdown cleanup
237
public
static
function
Cleanup
()
238
{
239
foreach
(self::$buckets as $bucket)
240
{
241
/* @var CCloudStorageBucket $obBucket */
242
$obBucket = $bucket[
'bucket'
];
243
if
(!is_null($bucket[
'filePath'
]) && $obBucket->FileExists($bucket[
'filePath'
]))
244
{
245
$obBucket->DeleteFile($bucket[
'filePath'
]);
246
}
247
elseif
(static::cleanupFilesLock())
248
{
249
$dir_name =
self::GetAbsoluteRoot
() .
'/'
;
250
$list = $obBucket->ListFiles($dir_name,
true
);
251
if
($list)
252
{
253
static::cleanupFiles($obBucket, $dir_name, $list);
254
}
255
static::cleanupFilesLock(
false
);
256
}
257
}
258
}
259
}
CCloudTempFile
Определения
temp_file.php:4
CCloudTempFile\cleanupFiles
static cleanupFiles($obBucket, $dir_name, $files)
Определения
temp_file.php:198
CCloudTempFile\RegisterShutdown
static RegisterShutdown()
Определения
temp_file.php:26
CCloudTempFile\$my_pid
static $my_pid
Определения
temp_file.php:150
CCloudTempFile\cleanupFilesLock
static cleanupFilesLock($lock=true)
Определения
temp_file.php:156
CCloudTempFile\GetFileName
static GetFileName($obBucket, $file_name='')
Определения
temp_file.php:40
CCloudTempFile\IsTempFile
static IsTempFile($file_name)
Определения
temp_file.php:19
CCloudTempFile\Cleanup
static Cleanup()
Определения
temp_file.php:237
CCloudTempFile\$shutdownRegistered
static $shutdownRegistered
Определения
temp_file.php:24
CCloudTempFile\GetAbsoluteRoot
static GetAbsoluteRoot()
Определения
temp_file.php:10
CCloudTempFile\GetDirectoryName
static GetDirectoryName($obBucket, $hours_to_keep_files=0, $subdir='')
Определения
temp_file.php:85
$bFound
$bFound
Определения
get_search.php:40
bitrix_sessid
bitrix_sessid()
Определения
tools.php:4656
Rel2Abs
Rel2Abs($curdir, $relpath)
Определения
tools.php:3297
elseif
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения
prolog_main_admin.php:393
$i
$i
Определения
factura.php:643
bitrix
modules
clouds
classes
general
temp_file.php
Создано системой
1.14.0