Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
filestorage.php
1<?php
3
6
7final class FileStorage extends AbstractStorage
8{
9 private $cacheFile = null;
10
12 {
13 parent::__construct($cacheKey, $configuration, $htmlCacheOptions);
14
15 $this->cacheFile = new Main\IO\File(Main\IO\Path::convertRelativeToAbsolute(
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
224class_alias("Bitrix\\Main\\Composite\\Data\\FileStorage", "Bitrix\\Main\\Data\\StaticHtmlFileStorage");
static deleteRecursive($relativePath="", $validTime=0)
__construct($cacheKey, array $configuration, array $htmlCacheOptions)