Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
filestorage.php
1
<?php
2
namespace
Bitrix\Main\Composite\Data
;
3
4
use
Bitrix\Main
;
5
use
Bitrix\Main\IO\File
;
6
7
final
class
FileStorage
extends
AbstractStorage
8
{
9
private
$cacheFile =
null
;
10
11
function
__construct
(
$cacheKey
, array
$configuration
, array
$htmlCacheOptions
)
12
{
13
parent::__construct(
$cacheKey
,
$configuration
,
$htmlCacheOptions
);
14
15
$this->cacheFile =
new
Main\IO\File
(Main\
IO
\Path::convertRelativeToAbsolute(
16
Main\
Application::getPersonalRoot
()
17
.
"/html_pages"
18
.$this->cacheKey
19
));
20
}
21
22
public
function
write
($content, $md5)
23
{
24
$written =
false
;
25
26
if
($this->cacheFile)
27
{
28
$tempFile =
new
File
($this->cacheFile->getPhysicalPath().
".tmp"
);
29
30
try
31
{
32
$written = $tempFile->putContents($content);
33
$this->cacheFile->delete();
34
if
(!$tempFile->rename($this->cacheFile->getPhysicalPath()))
35
{
36
$written =
false
;
37
}
38
}
39
catch
(\Exception $exception)
40
{
41
$written =
false
;
42
$this->cacheFile->delete();
43
$tempFile->delete();
44
}
45
}
46
47
return
$written;
48
}
49
50
public
function
read
()
51
{
52
if
($this->
exists
())
53
{
54
try
55
{
56
return
$this->cacheFile->getContents();
57
}
58
catch
(\Exception $exception)
59
{
60
61
}
62
}
63
64
return
false
;
65
}
66
67
public
function
exists
()
68
{
69
if
($this->cacheFile)
70
{
71
return
$this->cacheFile->isExists();
72
}
73
else
74
{
75
return
false
;
76
}
77
}
78
79
public
function
delete
()
80
{
81
$fileSize =
false
;
82
if
($this->cacheFile && $this->cacheFile->isExists())
83
{
84
try
85
{
86
$cacheDirectory = $this->cacheFile->getDirectory();
87
$fileSize = $this->cacheFile->getSize();
88
$this->cacheFile->delete();
89
90
//Try to cleanup directory
91
$children = $cacheDirectory->getChildren();
92
if
(empty($children))
93
{
94
$cacheDirectory->delete();
95
}
96
}
97
catch
(\Exception $exception)
98
{
99
100
}
101
}
102
103
return
$fileSize;
104
}
105
106
public
function
deleteAll
()
107
{
108
return
(
bool
)
self::deleteRecursive
(
"/"
);
109
}
110
111
public
function
getMd5
()
112
{
113
if
($this->
exists
())
114
{
115
$content = $this->
read
();
116
return
$content !==
false
? mb_substr($content, -35, 32) :
false
;
117
}
118
119
return
false
;
120
}
121
126
public
function
shouldCountQuota
()
127
{
128
return
true
;
129
}
130
131
public
function
getLastModified
()
132
{
133
if
($this->
exists
())
134
{
135
try
136
{
137
return
$this->cacheFile->getModificationTime();
138
}
139
catch
(\Exception $exception)
140
{
141
142
}
143
}
144
145
return
false
;
146
}
147
152
public
function
getSize
()
153
{
154
if
($this->cacheFile && $this->cacheFile->isExists())
155
{
156
try
157
{
158
return
$this->cacheFile->getSize();
159
}
160
catch
(\Exception $exception)
161
{
162
163
}
164
}
165
166
return
false
;
167
}
168
169
public
function
getCacheFile
()
170
{
171
return
$this->cacheFile;
172
}
173
180
public
static
function
deleteRecursive
($relativePath =
""
, $validTime = 0)
181
{
182
$bytes = 0.0;
183
if
(strpos($relativePath,
".."
) !==
false
)
184
{
185
return
$bytes;
186
}
187
188
$relativePath = rtrim($relativePath,
"/"
);
189
$baseDir = $_SERVER[
"DOCUMENT_ROOT"
].BX_PERSONAL_ROOT.
"/html_pages"
;
190
$absPath = $baseDir.$relativePath;
191
192
if
(is_file($absPath))
193
{
194
if
(
195
($validTime && filemtime($absPath) > $validTime) ||
196
in_array($relativePath, array(
"/.enabled"
,
"/.config.php"
,
"/.htaccess"
,
"/.size"
,
"/404.php"
)))
197
{
198
return
$bytes;
199
}
200
201
$bytes = filesize($absPath);
202
@unlink($absPath);
203
return
doubleval($bytes);
204
}
205
elseif (is_dir($absPath) && ($handle = opendir($absPath)) !==
false
)
206
{
207
while
(($file = readdir($handle)) !==
false
)
208
{
209
if
($file ===
"."
|| $file ===
".."
)
210
{
211
continue
;
212
}
213
214
$bytes +=
self::deleteRecursive
($relativePath.
"/"
.$file, $validTime);
215
}
216
closedir($handle);
217
@rmdir($absPath);
218
}
219
220
return
doubleval($bytes);
221
}
222
}
223
224
class_alias(
"Bitrix\\Main\\Composite\\Data\\FileStorage"
,
"Bitrix\\Main\\Data\\StaticHtmlFileStorage"
);
Bitrix\Main\Application\getPersonalRoot
static getPersonalRoot()
Definition
application.php:743
Bitrix\Main\Composite\Data\AbstractStorage
Definition
abstractstorage.php:12
Bitrix\Main\Composite\Data\AbstractStorage\$cacheKey
$cacheKey
Definition
abstractstorage.php:13
Bitrix\Main\Composite\Data\AbstractStorage\$htmlCacheOptions
$htmlCacheOptions
Definition
abstractstorage.php:15
Bitrix\Main\Composite\Data\AbstractStorage\$configuration
$configuration
Definition
abstractstorage.php:14
Bitrix\Main\Composite\Data\FileStorage
Definition
filestorage.php:8
Bitrix\Main\Composite\Data\FileStorage\getCacheFile
getCacheFile()
Definition
filestorage.php:169
Bitrix\Main\Composite\Data\FileStorage\deleteRecursive
static deleteRecursive($relativePath="", $validTime=0)
Definition
filestorage.php:180
Bitrix\Main\Composite\Data\FileStorage\read
read()
Definition
filestorage.php:50
Bitrix\Main\Composite\Data\FileStorage\__construct
__construct($cacheKey, array $configuration, array $htmlCacheOptions)
Definition
filestorage.php:11
Bitrix\Main\Composite\Data\FileStorage\getSize
getSize()
Definition
filestorage.php:152
Bitrix\Main\Composite\Data\FileStorage\getMd5
getMd5()
Definition
filestorage.php:111
Bitrix\Main\Composite\Data\FileStorage\write
write($content, $md5)
Definition
filestorage.php:22
Bitrix\Main\Composite\Data\FileStorage\getLastModified
getLastModified()
Definition
filestorage.php:131
Bitrix\Main\Composite\Data\FileStorage\deleteAll
deleteAll()
Definition
filestorage.php:106
Bitrix\Main\Composite\Data\FileStorage\exists
exists()
Definition
filestorage.php:67
Bitrix\Main\Composite\Data\FileStorage\shouldCountQuota
shouldCountQuota()
Definition
filestorage.php:126
Bitrix\Main\IO\File
Definition
file.php:7
Bitrix\Main\Composite\Data
Definition
abstractstorage.php:2
Bitrix\Main\IO
Definition
directory.php:2
Bitrix\Main
modules
main
lib
composite
data
filestorage.php
Создано системой
1.10.0