1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
file_temp.php
См. документацию.
1<?php
2
5
7{
8 private static $is_exit_function_registered = false;
9 private static $arFiles = [];
10
11 public static function GetAbsoluteRoot()
12 {
13 if (defined('BX_TEMPORARY_FILES_DIRECTORY'))
14 {
15 return rtrim(BX_TEMPORARY_FILES_DIRECTORY, '/');
16 }
17 else
18 {
20
21 return $io->CombinePath(
22 $_SERVER["DOCUMENT_ROOT"],
23 COption::GetOptionString("main", "upload_dir", "upload"),
24 "tmp"
25 );
26 }
27 }
28
29 public static function GetFileName($file_name = '')
30 {
31 $dir_name = self::GetAbsoluteRoot();
32 $file_name = rel2abs("/", "/" . $file_name);
33
34 while (true)
35 {
36 $dir_add = Security\Random::getString(32);
37
38 $temp_path = $dir_name . "/" . $dir_add . $file_name;
39
40 if (!file_exists($temp_path))
41 {
42 //Delayed unlink
43 if (!self::$is_exit_function_registered)
44 {
45 self::$is_exit_function_registered = true;
46 register_shutdown_function(['CTempFile', 'Cleanup']);
47 }
48
49 self::$arFiles[$temp_path] = $dir_name . "/" . $dir_add;
50
51 //Function ends only here
52 return $temp_path;
53 }
54 }
55 }
56
57 public static function GetDirectoryName($hours_to_keep_files = 0, $subdir = "")
58 {
59 if ($hours_to_keep_files <= 0)
60 {
61 return self::GetFileName();
62 }
63
64 $temp_path = null;
65 if ($subdir === "")
66 {
67 $dir_name = self::GetAbsoluteRoot() . '/BXTEMP-' . date('Y-m-d/H/', time() + 3600 * $hours_to_keep_files);
68 while (true)
69 {
70 $dir_add = Security\Random::getString(32);
71 $temp_path = $dir_name . $dir_add . "/";
72
73 if (!file_exists($temp_path))
74 {
75 break;
76 }
77 }
78 }
79 else //Fixed name during the session
80 {
81 $localStorage = Main\Application::getInstance()->getLocalSession('userSessionData');
82 if (!isset($localStorage['tempFileToken']))
83 {
84 $localStorage->set('tempFileToken', Security\Random::getString(32));
85 }
86 $token = $localStorage->get('tempFileToken');
87
88 $subdir = implode("/", (is_array($subdir) ? $subdir : [$subdir, $token])) . "/";
89 while (str_contains($subdir, "//"))
90 {
91 $subdir = str_replace("//", "/", $subdir);
92 }
93 $bFound = false;
94 for ($i = $hours_to_keep_files - 1; $i > 0; $i--)
95 {
96 $dir_name = self::GetAbsoluteRoot() . '/BXTEMP-' . date('Y-m-d/H/', time() + 3600 * $i);
97 $temp_path = $dir_name . $subdir;
98 if (file_exists($temp_path) && is_dir($temp_path))
99 {
100 $bFound = true;
101 break;
102 }
103 }
104
105 if (!$bFound)
106 {
107 $dir_name = self::GetAbsoluteRoot() . '/BXTEMP-' . date('Y-m-d/H/', time() + 3600 * $hours_to_keep_files);
108 $temp_path = $dir_name . $subdir;
109 }
110 }
111
112 //Delayed unlink
113 if (!self::$is_exit_function_registered)
114 {
115 self::$is_exit_function_registered = true;
116 register_shutdown_function(['CTempFile', 'Cleanup']);
117 }
118
119 //Function ends only here
120 return $temp_path;
121 }
122
123 //PHP shutdown cleanup
124 public static function Cleanup()
125 {
126 foreach (self::$arFiles as $temp_path => $temp_dir)
127 {
128 if (file_exists($temp_path))
129 {
130 //Clean a file from CTempFile::GetFileName('some.jpg');
131 if (is_file($temp_path))
132 {
133 unlink($temp_path);
134 @rmdir($temp_dir);
135 }
136 //Clean whole temporary directory from CTempFile::GetFileName('');
137 elseif (
138 str_ends_with($temp_path, '/')
139 && !str_contains($temp_path, "BXTEMP")
140 && is_dir($temp_path)
141 )
142 {
143 CTempFile::_absolute_path_recursive_delete($temp_path);
144 }
145 }
146 elseif (file_exists($temp_dir))
147 {
148 @rmdir($temp_dir);
149 }
150 }
151
152 //Clean directories with $hours_to_keep_files > 0
153 $dir_name = self::GetAbsoluteRoot() . "/";
154 if (file_exists($dir_name))
155 {
156 if ($handle = opendir($dir_name))
157 {
158 while (($day_files_dir = readdir($handle)) !== false)
159 {
160 if ($day_files_dir == '.' || $day_files_dir == '..')
161 {
162 continue;
163 }
164 if (preg_match("/^BXTEMP-(.*?)\$/", $day_files_dir) && is_dir($dir_name . $day_files_dir))
165 {
166 CTempFile::_process_directory($dir_name, $day_files_dir);
167 }
168 }
169 closedir($handle);
170 }
171 }
172 }
173
174 private static function _process_directory($dir_name, $day_files_dir)
175 {
176 $this_day_name = 'BXTEMP-' . date('Y-m-d');
177 if ($day_files_dir < $this_day_name)
178 {
179 CTempFile::_absolute_path_recursive_delete($dir_name . $day_files_dir);
180 }
181 elseif ($day_files_dir == $this_day_name)
182 {
183 if ($hour_handle = opendir($dir_name . $day_files_dir))
184 {
185 $this_hour_name = date('H');
186 while (($hour_files_dir = readdir($hour_handle)) !== false)
187 {
188 if ($hour_files_dir == '.' || $hour_files_dir == '..')
189 {
190 continue;
191 }
192 if ($hour_files_dir < $this_hour_name)
193 {
194 CTempFile::_absolute_path_recursive_delete($dir_name . $day_files_dir . '/' . $hour_files_dir);
195 }
196 }
197 }
198 }
199 }
200
201 private static function _absolute_path_recursive_delete($path)
202 {
203 if ($path == '' || $path == '/')
204 {
205 return false;
206 }
207
208 $f = true;
209 if (is_file($path) || is_link($path))
210 {
211 if (@unlink($path))
212 {
213 return true;
214 }
215 return false;
216 }
217 elseif (is_dir($path))
218 {
219 if ($handle = opendir($path))
220 {
221 while (($file = readdir($handle)) !== false)
222 {
223 if ($file == "." || $file == "..")
224 {
225 continue;
226 }
227
228 if (!CTempFile::_absolute_path_recursive_delete($path . "/" . $file))
229 {
230 $f = false;
231 }
232 }
233 closedir($handle);
234 }
235 $r = @rmdir($path);
236 if (!$r)
237 {
238 return false;
239 }
240 return $f;
241 }
242 return false;
243 }
244}
$path
Определения access_edit.php:21
static getInstance()
Определения application.php:98
static getString($length, $caseSensitive=false)
Определения random.php:76
static GetInstance()
Определения virtual_io.php:60
Определения file_temp.php:7
static GetDirectoryName($hours_to_keep_files=0, $subdir="")
Определения file_temp.php:57
static Cleanup()
Определения file_temp.php:124
static GetFileName($file_name='')
Определения file_temp.php:29
static GetAbsoluteRoot()
Определения file_temp.php:11
$f
Определения component_props.php:52
$handle
Определения include.php:55
$bFound
Определения get_search.php:40
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
$io
Определения csv_new_run.php:98
GetFileName($path)
Определения tools.php:3001
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$i
Определения factura.php:643