1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
backup.php
См. документацию.
1<?php
4{
5 private static $instance = /*.(CBitrixCloudBackup).*/ null;
6 private $init = false;
7 private $infoXML = /*.(CDataXML).*/ null;
8 private $quota = 0.0;
9 private $files = /*.(array[int][string]string).*/ [];
10 private $total_size = 0.0;
11 private $last_backup_time = 0;
12
19 public static function getInstance()
20 {
21 if (!isset(self::$instance))
22 {
23 self::$instance = new CBitrixCloudBackup;
24 }
25
26 return self::$instance;
27 }
28
36 private function _getInformation($force = false)
37 {
38 if ($this->init && !$force)
39 {
40 return true;
41 }
42 $this->init = true;
43
44 try
45 {
46 $web_service = new CBitrixCloudBackupWebService();
47 $web_service->setTimeout(10);
48 $this->infoXML = $web_service->actionGetInformation();
49 }
50 catch (CBitrixCloudException $_)
51 {
52 return false;
53 }
54 /* @var CDataXMLNode $node */
55 $node = $this->infoXML->SelectNodes('/control/quota/allow');
56 if (is_object($node))
57 {
58 $this->quota = \Bitrix\Main\Config\Ini::unformatInt($node->textContent());
59 }
60
61 $node = $this->infoXML->SelectNodes('/control/files');
62 if (is_object($node))
63 {
64 $this->last_backup_time = 0;
65 $this->total_size = 0.0;
66 $this->files = /*.(array[int][string]string).*/ [];
67 $nodeFiles = $node->elementsByName('file');
68 foreach ($nodeFiles as $nodeFile)
69 {
70 /* @var CDataXMLNode $nodeFile */
71 $size = \Bitrix\Main\Config\Ini::unformatInt($nodeFile->getAttribute('size'));
72 $name = $nodeFile->getAttribute('name');
73 $this->total_size += $size;
74 $this->files[] = [
75 'FILE_NAME' => $name,
76 'FILE_SIZE' => (string)$size,
77 ];
78 $time = strtotime(preg_replace('/^(\\d{4})(\\d\\d)(\\d\\d)_(\\d\\d)(\\d\\d)(\\d\\d)(.*)$/', '\\1-\\2-\\3 \\4:\\5:\\6', $name));
79 if ($time > $this->last_backup_time)
80 {
81 $this->last_backup_time = $time;
82 }
83 }
84 }
85
86 return true;
87 }
88
95 public function listFiles() /*. throws CBitrixCloudException .*/
96 {
97 $this->_getInformation();
98 return $this->files;
99 }
100
107 public function getQuota() /*. throws CBitrixCloudException .*/
108 {
109 $this->_getInformation();
110 return $this->quota;
111 }
112
119 public function getUsage() /*. throws CBitrixCloudException .*/
120 {
121 $this->_getInformation();
122 return $this->total_size;
123 }
124
131 public function getLastTimeBackup() /*. throws CBitrixCloudException .*/
132 {
133 $this->_getInformation();
134 return $this->last_backup_time;
135 }
136
146 private function _getBucket($operation, $check_word, $file_name)
147 {
148 if (!CModule::IncludeModule('clouds'))
149 {
150 throw new CBitrixCloudException('Module clouds not installed.');
151 }
152
153 $web_service = new CBitrixCloudBackupWebService();
154 if ($operation === 'write')
155 {
156 $obXML = $web_service->actionWriteFile($check_word, $file_name);
157 }
158 else
159 {
160 $obXML = $web_service->actionReadFile($check_word, $file_name);
161 }
162 /* @var CDataXMLNode $node */
163 $bucket_name = (is_object($node = $obXML->SelectNodes('/control/bucket/bucket_name'))) ? $node->textContent() : '';
164 $bucket_location = (is_object($node = $obXML->SelectNodes('/control/bucket/bucket_location'))) ? $node->textContent() : '';
165 $prefix = (is_object($node = $obXML->SelectNodes('/control/bucket/prefix'))) ? $node->textContent() : '';
166 $access_key = (is_object($node = $obXML->SelectNodes('/control/bucket/access_key'))) ? $node->textContent() : '';
167 $secret_key = (is_object($node = $obXML->SelectNodes('/control/bucket/secret_key'))) ? $node->textContent() : '';
168 $session_token = (is_object($node = $obXML->SelectNodes('/control/bucket/session_token'))) ? $node->textContent() : '';
169 $file_name = (is_object($node = $obXML->SelectNodes('/control/bucket/file_name'))) ? $node->textContent() : '';
170 $service_id = (is_object($node = $obXML->SelectNodes('/control/bucket/bucket_service_id'))) ? $node->textContent() : '';
171
172 return new CBitrixCloudBackupBucket(
173 $bucket_name,
174 $prefix,
176 $secret_key,
177 $session_token,
178 $check_word,
179 $file_name,
180 $bucket_location,
181 $service_id
182 );
183 }
184
193 public function getBucketToReadFile($check_word, $file_name)
194 {
195 return $this->_getBucket('read', $check_word, $file_name);
196 }
197
206 public function getBucketToWriteFile($check_word, $file_name)
207 {
208 return $this->_getBucket('write', $check_word, $file_name);
209 }
210
216 public function clearOptions()
217 {
218 CBitrixCloudOption::getOption('backup_files')->delete();
219 CBitrixCloudOption::getOption('backup_quota')->delete();
220 CBitrixCloudOption::getOption('backup_total_size')->delete();
221 CBitrixCloudOption::getOption('backup_last_backup_time')->delete();
222 return $this;
223 }
224
230 public function saveToOptions()
231 {
232 $this->_getInformation();
233 $arFiles = [];
234 foreach ($this->files as $arFile)
235 {
236 $arFiles[$arFile['FILE_NAME']] = $arFile['FILE_SIZE'];
237 }
238 ksort($arFiles);
239 CBitrixCloudOption::getOption('backup_files')->setArrayValue($arFiles);
240 CBitrixCloudOption::getOption('backup_quota')->setStringValue((string)$this->quota);
241 CBitrixCloudOption::getOption('backup_total_size')->setStringValue((string)$this->total_size);
242 CBitrixCloudOption::getOption('backup_last_backup_time')->setStringValue((string)$this->last_backup_time);
243 return $this;
244 }
245
251 public function loadFromOptions()
252 {
253 $this->files = /*.(array[int][string]string).*/ [];
254 foreach (CBitrixCloudOption::getOption('backup_files')->getArrayValue() as $FILE_NAME => $FILE_SIZE)
255 {
256 $this->files[] = [
257 'FILE_NAME' => $FILE_NAME,
258 'FILE_SIZE' => $FILE_SIZE,
259 ];
260 }
261 $this->quota = doubleval(CBitrixCloudOption::getOption('backup_quota')->getStringValue());
262 $this->total_size = doubleval(CBitrixCloudOption::getOption('backup_total_size')->getStringValue());
263 $this->last_backup_time = intval(CBitrixCloudOption::getOption('backup_last_backup_time')->getStringValue());
264 $this->init = true;
265 return $this;
266 }
267
273 public static function OnAdminInformerInsertItems()
274 {
275 global $USER;
276
277 $informerParams = [
278 'TITLE' => GetMessage('BCL_BACKUP_AI_TITLE'),
279 'COLOR' => 'peach',
280 ];
281
282 $backup = self::getInstance();
283 $backup->loadFromOptions();
284 $last_request_time_option = CBitrixCloudOption::getOption('backup_last_backup_time');
285 try
286 {
287 if (
288 $backup->getQuota() <= 0
289 && $last_request_time_option->getIntegerValue() <= 0
290 )
291 {
292 $backup->_getInformation(true);
293 $backup->saveToOptions();
294 $last_request_time_option->setStringValue((string)time());
295 }
296 }
297 catch (CBitrixCloudException $_)
298 {
299 return;
300 }
301
302 if ( $backup->getQuota() <= 0 )
303 {
304 return;
305 }
306
307 $arFiles = $backup->listFiles();
308 if (empty($arFiles))
309 {
310 $PROGRESS_FREE = 100;
311 $AVAIL = $backup->getQuota();
312 $ALLOWED = CFile::FormatSize($backup->getQuota(), 0);
313 $informerParams['ALERT'] = true;
314 $MESS = '<span class="adm-informer-strong-text">' . GetMessage('BCL_BACKUP_AI_NO_FILES') . '</span>';
315 if ($USER->CanDoOperation('bitrixcloud_backup') && $USER->CanDoOperation('edit_php'))
316 {
317 $informerParams['FOOTER'] = '<a href="/bitrix/admin/dump.php?lang=' . LANGUAGE_ID . '">' . GetMessage('BCL_BACKUP_AI_DO_BACKUP_STRONGLY') . '</a>';
318 }
319 }
320 elseif ($backup->getLastTimeBackup() < (time() - 7 * 24 * 3600))
321 {
322 $AVAIL = $backup->getQuota() - $backup->getUsage();
323 if ($AVAIL < 0.0)
324 {
325 $AVAIL = 0.0;
326 }
327
328 $PROGRESS_FREE = round($AVAIL / $backup->getQuota() * 100);
329 $ALLOWED = CFile::FormatSize($backup->getQuota(), 0);
330 $informerParams['ALERT'] = true;
331 $MESS = '<span class="adm-informer-strong-text">' . GetMessage('BCL_BACKUP_AI_LAST_TIME') . ': ' . FormatDate([
332 'today' => 'today',
333 'yesterday' => 'yesterday',
334 '' => 'dago',
335 ], $backup->getLastTimeBackup()) . '.</span>';
336 if ($USER->CanDoOperation('bitrixcloud_backup') && $USER->CanDoOperation('edit_php'))
337 {
338 $informerParams['FOOTER'] = '<a href="/bitrix/admin/dump.php?lang=' . LANGUAGE_ID . '">' . GetMessage('BCL_BACKUP_AI_DO_BACKUP_STRONGLY') . '</a>';
339 }
340 }
341 else
342 {
343 $AVAIL = $backup->getQuota() - $backup->getUsage();
344 if ($AVAIL < 0.0)
345 {
346 $AVAIL = 0.0;
347 }
348
349 $PROGRESS_FREE = round($AVAIL / $backup->getQuota() * 100);
350 $ALLOWED = CFile::FormatSize($backup->getQuota(), 0);
351 $informerParams['ALERT'] = false;
352 $MESS = GetMessage('BCL_BACKUP_AI_LAST_TIME') . ': ' . FormatDate([
353 'today' => 'today',
354 'yesterday' => 'yesterday',
355 '' => 'dago',
356 ], $backup->getLastTimeBackup());
357 if ($USER->CanDoOperation('bitrixcloud_backup') && $USER->CanDoOperation('edit_php'))
358 {
359 $informerParams['FOOTER'] = '<a href="/bitrix/admin/dump.php?lang=' . LANGUAGE_ID . '">' . GetMessage('BCL_BACKUP_AI_DO_BACKUP') . '</a>';
360 }
361 }
362
363 if (isset($informerParams['ALERT']))
364 {
365 $PROGRESS_FREE_BAR = $PROGRESS_FREE < 0 ? 0 : $PROGRESS_FREE;
366 $informerParams['HTML'] = '
367 <div class="adm-informer-item-section">
368 <span class="adm-informer-item-l">
369 <span class="adm-informer-strong-text">' . GetMessage('BCL_BACKUP_AI_USAGE_TOTAL') . '</span> ' . $ALLOWED . '
370 </span>
371 <span class="adm-informer-item-r">
372 <span class="adm-informer-strong-text">' . GetMessage('BCL_BACKUP_AI_USAGE_AVAIL') . '</span> ' . CFile::FormatSize($AVAIL, 0) . '
373 </span>
374 </div>
375 <div class="adm-informer-status-bar-block" >
376 <div class="adm-informer-status-bar-indicator" style="width:' . (100 - $PROGRESS_FREE_BAR) . '%; "></div>
377 <div class="adm-informer-status-bar-text">' . (100 - $PROGRESS_FREE) . '%</div>
378 </div>
379 ' . $MESS;
380 CAdminInformer::AddItem($informerParams);
381 }
382 }
383
395 public function addBackupJob($secret_key, $url, $time = 0, $weekdays = [])
396 {
397 try
398 {
399 $web_service = new CBitrixCloudBackupWebService();
400 $web_service->actionAddBackupJob($secret_key, $url, $time, $weekdays);
401 return '';
402 }
403 catch (CBitrixCloudException $e)
404 {
405 return $e->getMessage();//."[".htmlspecialcharsEx($e->getErrorCode())."]";
406 }
407 }
408
416 public function deleteBackupJob()
417 {
418 try
419 {
420 $web_service = new CBitrixCloudBackupWebService();
421 $web_service->actionDeleteBackupJob();
422 return '';
423 }
424 catch (CBitrixCloudException $e)
425 {
426 return $e->getMessage();//."[".htmlspecialcharsEx($e->getErrorCode())."]";
427 }
428 }
429
430 public function getBackupJob()
431 {
432 try
433 {
434 $web_service = new CBitrixCloudBackupWebService();
435 $infoXML = $web_service->actionGetBackupJob();
436 }
437 catch (CBitrixCloudException $e)
438 {
439 return $e->getMessage();//."[".htmlspecialcharsEx($e->getErrorCode())."]";
440 }
441
442 $result = [];
443 $jobList = $infoXML->SelectNodes('/control/JobList');
444 if (is_object($jobList))
445 {
446 $jobEntries = $jobList->elementsByName('JobEntry');
447 foreach ($jobEntries as $jobEntry)
448 {
449 $info = [];
450 foreach ($jobEntry->children() as $field)
451 {
452 $name = $field->name();
453 $value = $field->textContent();
454 $info[$name] = $value;
455 }
456 $result[] = [
457 'URL' => $info['Url'],
458 'TIME' => $info['Time'],
459 'WEEK_DAYS' => explode(',', $info['WeekDays']),
460 'STATUS' => $info['Status'],
461 'FINISH_TIME' => $info['FinishTime'],
462 ];
463 }
464 }
465 return $result;
466 }
467}
static AddItem($arParams)
Определения admin_informer.php:27
Определения backup.php:4
static OnAdminInformerInsertItems()
Определения backup.php:273
getBackupJob()
Определения backup.php:430
clearOptions()
Определения backup.php:216
getLastTimeBackup()
Определения backup.php:131
getQuota()
Определения backup.php:107
getUsage()
Определения backup.php:119
listFiles()
Определения backup.php:95
getBucketToReadFile($check_word, $file_name)
Определения backup.php:193
getBucketToWriteFile($check_word, $file_name)
Определения backup.php:206
static getInstance()
Определения backup.php:19
addBackupJob($secret_key, $url, $time=0, $weekdays=[])
Определения backup.php:395
saveToOptions()
Определения backup.php:230
loadFromOptions()
Определения backup.php:251
deleteBackupJob()
Определения backup.php:416
static getOption($name)
Определения option.php:25
$result
Определения get_property_values.php:14
global $MESS
Определения bill.php:2
global $USER
Определения csv_new_run.php:40
if($NS['step']==6) if( $NS[ 'step']==7) if(COption::GetOptionInt('main', 'disk_space', 0) > 0) $info
Определения backup.php:924
FormatDate($format="", $timestamp=false, $now=false, ?string $languageId=null)
Определения tools.php:871
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$name
Определения menu_edit.php:35
$time
Определения payment.php:61
$arFiles
Определения options.php:60
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$access_key
Определения result.php:8
$url
Определения iframe.php:7