1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
quota.php
См. документацию.
1<?php
2
4
6{
7 var $max_execution_time = 20; // 20 sec
8 var $LAST_ERROR = false;
9 private static $instance;
10 protected static $recalculateDb = false;
11
12 public static function getInstance()
13 {
14 if (!isset(self::$instance))
15 {
16 $c = __CLASS__;
17 self::$instance = new $c;
18 }
19
20 return self::$instance;
21 }
22
23 public function __construct($params = array())
24 {
25 if(array_key_exists("max_execution_time", $params) && intval($params["max_execution_time"]) > 0)
26 $this->max_execution_time = intval($params["max_execution_time"]);
27 }
28
29 public static function recalculateDb(bool $mode = true): void
30 {
31 static::$recalculateDb = $mode;
32 }
33
34 public static function SetDBSize()
35 {
36 global $DB;
37 $DBSize = 0;
38
39 if (static::$recalculateDb && COption::GetOptionInt("main", "disk_space") > 0)
40 {
41 $db_res = $DB->Query("
42 SELECT sum(Data_length + Index_length) as TOTAL_LEN
43 FROM information_schema.tables
44 WHERE table_schema = '{$DB->ForSql($DB->DBName)}'
45 ");
46 if ($db_res && ($res = $db_res->Fetch()))
47 {
48 $DBSize = $res['TOTAL_LEN'];
49 }
50
51 COption::SetOptionString("main_size", "~db", $DBSize);
52 $params = array("status" => "d", "time" => time());
53 COption::SetOptionString("main_size", "~db_params", serialize($params));
54
55 static::recalculateDb(false);
56 }
57 else
58 {
59 $params = array("time" => false);
60 }
61
62 return array("status" => "done", "size" => $DBSize, "time" => $params["time"]);
63 }
64
65 public function SetDirSize($path="", $name="", $recount=false, $skip_dir = false)
66 {
67 if (empty($name))
68 $name = $path;
69
70 if ((empty($name) && empty($path)) || ($path == "files"))
71 {
72 $name = "files";
73 $path = "";
74 }
75
76 $abs_path = str_replace("//", "/", $_SERVER['DOCUMENT_ROOT']."/".$path."/");
77
78 $result = array();
79
80 if (empty($abs_path))
81 return false;
82
83 $record = array("size" => COption::GetOptionString("main_size", "~".$name));
84
85 if ($skip_dir)
86 {
87 if (!is_array($skip_dir))
88 $skip_dir = array($skip_dir);
89 foreach ($skip_dir as $key => $path_to_skip_dir)
90 $skip_dir[$key] = str_replace("//", "/", $abs_path.$path_to_skip_dir."/");
91 }
92
93 if (!empty($record["size"]) && !$recount)
94 {
95 $record = array_merge(
96 unserialize(COption::GetOptionString("main_size", "~".$name."_params"), ['allowed_classes' => false]),
97 $record);
98
99 $record["path_to_last_file"] = str_replace("//", "/", $abs_path.$record["file"]);
100 if (is_file($record["path_to_last_file"]) && $record["status"] == "c")
101 {
102 $res = $this->GetDirListFromLastFile($abs_path, $record["path_to_last_file"], true, $skip_dir);
103 if ($res["status"] == "done" || $res["status"] == "continue")
104 {
105 $properties = array(
106 "status" => mb_substr($res["status"], 0, 1),
107 "file" => str_replace($abs_path, "", str_replace("//", "/", $res["last_file"])),
108 "time" => time());
109
110 $record["size"] = doubleVal($record["size"])+doubleVal($res["size"]);
111 COption::SetOptionString("main_size", "~".$name, $record["size"]);
112 COption::SetOptionString("main_size", "~".$name."_params", serialize($properties));
113 $result = $res;
114 $result["size"] = $record["size"];
115 }
116 }
117 elseif ($record["status"] == "d")
118 {
119 return array("status" => "done", "last_file" => $record["file"], "size" => $record["size"]);
120 }
121 }
122
123 if (empty($result))
124 {
125
126 $res = $this->GetDirListSimple($abs_path, true, $skip_dir);
127
128 if ($res["status"] == "done" || $res["status"] == "continue")
129 {
130 $properties = array(
131 "status" => mb_substr($res["status"], 0, 1),
132 "file" => str_replace($abs_path, "", str_replace("//", "/", $res["last_file"])),
133 "time" => time());
134
135 COption::SetOptionString("main_size", "~".$name, doubleVal($res["size"]));
136 COption::SetOptionString("main_size", "~".$name."_params", serialize($properties));
137 $result = $res;
138 }
139 }
140
141 if (!empty($result))
142 return $result;
143 return array("status" => "error");
144 }
145
146 public function GetDirListSimple($path, $check_time = true, $skip_dir=false)
147 {
148 $path = str_replace("//", "/", $path."/");
149 $res = array();
150 $size = 0;
151 $handle = @opendir($path);
152
153 if ($handle)
154 {
155 while($file = readdir($handle))
156 {
157 if($file == "." || $file == "..")
158 {
159 continue;
160 }
161
162 if(is_dir($path.$file))
163 {
164 if (is_array($skip_dir) && (in_array(str_replace("//", "/", $path.$file."/"), $skip_dir)))
165 {
166 continue;
167 }
168
169 $res_rec = $this->GetDirListSimple($path.$file, $check_time, $skip_dir);
170 $res = array_merge($res, $res_rec["tree"]);
171 $size += doubleVal($res_rec["size"]);
172 if ($res_rec["status"] == "continue")
173 {
174 $res_rec["tree"] = $res;
175 $res_rec["size"] = doubleVal($size);
176 return $res_rec;
177 }
178 }
179 else
180 {
181 $res[] = $path.$file;
182 $size += filesize($path.$file);
183 if ($check_time && intval(microtime(true) - START_EXEC_TIME) >= $this->max_execution_time)
184 {
185 return array("tree" => $res, "status" => "continue", "last_file" => $path.$file, "size" => $size);
186 }
187 }
188 }
189 @closedir($handle);
190 }
191 else
192 {
193 return array("status" => "error");
194 }
195 return array("tree" => $res, "status" => "done", "last_file" => $path.$file, "size" => $size);
196 }
197
198 public function GetDirListFromLastFile($path, $path_to_last_file="", $check_time = true, $skip_dir = false)
199 {
200 $path = str_replace("//", "/", $path."/");
201 $path_to_last_file = str_replace("//", "/", $path_to_last_file);
202 $path_to_lf = str_replace($path, "", $path_to_last_file);
203 $size = 0;
204 $res = array();
205 $file = '';
206 $path_tree = explode("/", $path_to_lf);
207 while ($lf = array_pop($path_tree))
208 {
209 $path_to_dir = str_replace("//", "/", $path.implode("/", $path_tree)."/");
210 $handle = @opendir($path_to_dir);
211 $search = true;
212 if ($handle)
213 {
214 while($file = readdir($handle))
215 {
216 if($file == "." || $file == ".." || $search)
217 {
218 if ($file == $lf)
219 $search = false;
220 continue;
221 }
222
223 if(is_dir($path_to_dir.$file))
224 {
225 if (is_array($skip_dir) && (in_array(str_replace("//", "/", $path.$file."/"), $skip_dir)))
226 {
227 continue;
228 }
229 $res_rec = $this->GetDirListSimple($path_to_dir.$file, $check_time);
230 $res = array_merge($res, $res_rec["tree"]);
231 $size += doubleVal($res_rec["size"]);
232 if ($res_rec["status"] == "continue")
233 {
234 $res_rec["tree"] = $res;
235 $res_rec["size"] = doubleVal($size);
236 return $res_rec;
237 }
238 }
239 else
240 {
241
242 $res[] = $path_to_dir.$file;
243 $size += filesize($path_to_dir.$file);
244 if ($check_time && intval(microtime(true) - START_EXEC_TIME) >= $this->max_execution_time)
245 {
246 return array("tree" => $res, "status" => "continue", "last_file" => $path_to_dir.$file, "size" => $size);
247 }
248 }
249 }
250 }
251 @closedir($handle);
252 }
253 return array("tree" => $res, "status" => "done", "last_file" => $path.$file, "size" => $size);
254 }
255
256 public function Recount($id, $recount=false)
257 {
258 if ((COption::GetOptionInt("main", "disk_space") <= 0))
259 return true;
260
261 if ($id != "files" && (!is_dir($_SERVER['DOCUMENT_ROOT']."/".$id)))
262 return array("status" => "error");
263
264 if ($id == "files")
265 {
266 $result = $this->SetDirSize("", "files", $recount, array("bitrix"));
267 }
268 else
269 {
270 $result = $this->SetDirSize($id, "", $recount);
271 }
272
273 if (empty($result["time"]))
274 {
275 $result["time"] = time();
276 }
277 return $result;
278 }
279
280 public function GetDiskQuota()
281 {
282 if (COption::GetOptionInt("main", "disk_space") <= 0)
283 return true;
284
285 $this->LAST_ERROR = "";
286 $arMsg = Array();
287
288 if (COption::GetOptionInt("main_size", "~db") <= 0)
289 {
290 static::recalculateDb();
291 }
292
293 $quota = doubleVal(COption::GetOptionInt("main", "disk_space")*1024*1024 -
294 COption::GetOptionInt("main_size", "~db") -
295 COption::GetOptionInt("main_size", "~files"));
296
297 if ($quota > 0)
298 {
299 return $quota;
300 }
301
302 $this->LAST_ERROR = GetMessage("MAIN_QUOTA_BAD");
303 $arMsg[] = array("id"=>"QUOTA_BAD", "text"=> GetMessage("MAIN_QUOTA_BAD"));
304
305 $e = new CAdminException($arMsg);
306 $GLOBALS["APPLICATION"]->ThrowException($e);
307
308 return false;
309 }
310
311 public static function UpdateDiskQuota($type, $size, $action)
312 {
313 if (COption::GetOptionInt("main", "disk_space") <= 0)
314 return true;
315
316 if (empty($type) || empty($size) || empty($action))
317 return false;
318
319 if (is_array($size))
320 $size = mb_strlen(implode("", $size));
321 elseif (doubleval($size) > 0)
322 $size = doubleval($size);
323 else
324 $size = mb_strlen($size);
325
326 $size = doubleval($size);
327
328 $name = mb_strtolower($type) == "db" ? "db" : "files";
329
330 if (in_array(mb_strtolower($action), array("delete", "del")))
331 {
332 COption::SetOptionString("main_size", "~".$name,
333 doubleval(COption::GetOptionInt("main_size", "~".$name) - $size));
334 return true;
335 }
336 if (in_array(mb_strtolower($action), array("update", "insert", "add", "copy")))
337 {
338 COption::SetOptionString("main_size", "~".$name,
339 doubleval(COption::GetOptionInt("main_size", "~".$name) + $size));
340 return true;
341 }
342 return false;
343 }
344
345 public function CheckDiskQuota($params = array())
346 {
347 if (COption::GetOptionInt("main", "disk_space") <= 0)
348 return true;
349
350 if (defined("SKIP_DISK_QUOTA_CHECK") && constant("SKIP_DISK_QUOTA_CHECK") === true)
351 return true;
352
353 $quota = $this->GetDiskQuota();
354
355 if ($quota === true || $quota === false)
356 return $quota;
357
358 if (!empty($params))
359 {
360 if (is_array($params))
361 {
362 if (is_set($params, "FILE_SIZE"))
363 $size = $params["FILE_SIZE"];
364 elseif (is_set($params, "SIZE"))
365 $size = $params["SIZE"];
366 elseif (is_set($params, "file_size"))
367 $size = $params["file_size"];
368 elseif (is_set($params, "size"))
369 $size = $params["size"];
370 else
371 $size = mb_strlen(serialize($params));
372
373 if ($size !== false)
374 return ((double)$quota - $size) > 0;
375 }
376 if (!is_array($params) && doubleVal($params) > 0 && ((double)$quota - $params) > 0)
377 return true;
378 if (((double)$quota - mb_strlen($params)) > 0)
379 return true;
380 }
381 return false;
382 }
383}
$path
Определения access_edit.php:21
$type
Определения options.php:106
$db_res
Определения options_user_settings.php:8
Определения quota.php:6
static recalculateDb(bool $mode=true)
Определения quota.php:29
Recount($id, $recount=false)
Определения quota.php:256
GetDirListSimple($path, $check_time=true, $skip_dir=false)
Определения quota.php:146
__construct($params=array())
Определения quota.php:23
GetDiskQuota()
Определения quota.php:280
static $recalculateDb
Определения quota.php:10
static SetDBSize()
Определения quota.php:34
$max_execution_time
Определения quota.php:7
$LAST_ERROR
Определения quota.php:8
SetDirSize($path="", $name="", $recount=false, $skip_dir=false)
Определения quota.php:65
static UpdateDiskQuota($type, $size, $action)
Определения quota.php:311
static getInstance()
Определения quota.php:12
GetDirListFromLastFile($path, $path_to_last_file="", $check_time=true, $skip_dir=false)
Определения quota.php:198
CheckDiskQuota($params=array())
Определения quota.php:345
$abs_path
Определения component_props2.php:76
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$handle
Определения include.php:55
$result
Определения get_property_values.php:14
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
global $DB
Определения cron_frame.php:29
const START_EXEC_TIME
Определения start.php:12
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
is_set($a, $k=false)
Определения tools.php:2133
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$name
Определения menu_edit.php:35
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$action
Определения file_dialog.php:21
$GLOBALS['_____370096793']
Определения update_client.php:1