Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
agent.php
1<?php
2namespace Bitrix\Landing;
3
10use Bitrix\Crm\WebForm;
13
17class Agent
18{
27 public static function addUniqueAgent(
28 string $funcName,
29 array $params = [],
30 int $time = 7200,
31 ?int $nextExecDelay = null
32 ): void
33 {
34 if (!method_exists(__CLASS__, $funcName))
35 {
36 return;
37 }
38
39 $funcName = __CLASS__ . '::' . $funcName . '(';
40 foreach ($params as $value)
41 {
42 if (is_int($value))
43 {
44 $funcName .= $value . ',';
45 }
46 else if (is_string($value))
47 {
48 $funcName .= '\'' . $value . '\'' . ',';
49 }
50 }
51 $funcName = trim($funcName, ',');
52 $funcName .= ');';
53 $res = \CAgent::getList(
54 [],
55 [
56 'MODULE_ID' => 'landing',
57 'NAME' => $funcName
58 ]
59 );
60 if (!$res->fetch())
61 {
62 if ($nextExecDelay)
63 {
64 \CAgent::addAgent($funcName,
65 'landing',
66 'N',
67 $time,
68 '',
69 'Y',
70 \ConvertTimeStamp(time() + \CTimeZone::GetOffset() + $nextExecDelay, "FULL"));
71 }
72 else
73 {
74 \CAgent::addAgent($funcName, 'landing', 'N', $time);
75 }
76 }
77 }
78
83 public static function removeBadDomain(): string
84 {
85 $maxFailCount = 7;
86
88
89 // only custom domain
90 $filterDomains = array_map(function($domain)
91 {
92 return '%.' . $domain;
93 }, Domain::B24_DOMAINS);
94 $filterDomains[] = '%' . Manager::getHttpHost();
95
96 $customDomainExist = false;
97 $resDomain = Domain::getList([
98 'select' => [
99 'ID', 'DOMAIN', 'FAIL_COUNT'
100 ],
101 'filter' => [
102 '!DOMAIN' => $filterDomains
103 ],
104 'limit' => 5,
105 'order' => [
106 'DATE_MODIFY' => 'asc'
107 ]
108 ]);
109 while ($domain = $resDomain->fetch())
110 {
111 $customDomainExist = true;
112 if (Domain\Register::isDomainActive($domain['DOMAIN']))
113 {
114 Domain::update($domain['ID'], [
115 'FAIL_COUNT' => null
116 ])->isSuccess();
117 }
118 else
119 {
120 // remove domain
121 if ($domain['FAIL_COUNT'] >= $maxFailCount - 1)
122 {
123 // wee need site for randomize domain
124 $resSite = Site::getList([
125 'select' => [
126 'ID', 'DOMAIN_ID', 'DOMAIN_NAME' => 'DOMAIN.DOMAIN'
127 ],
128 'filter' => [
129 'DOMAIN_ID' => $domain['ID']
130 ]
131 ]);
132 if ($rowSite = $resSite->fetch())
133 {
134 Debug::log('removeBadDomain-randomizeDomain', var_export($rowSite, true));
135 Site::randomizeDomain($rowSite['ID']);
136 }
137 // site not exist, delete domain
138 /*else
139 {
140 Debug::log('removeBadDomain-Domain::delete', var_export($rowSite, true));
141 Domain::delete($domain['ID'])->isSuccess();
142 }*/
143 }
144 else
145 {
146 Domain::update($domain['ID'], [
147 'FAIL_COUNT' => intval($domain['FAIL_COUNT']) + 1
148 ])->isSuccess();
149 }
150 }
151 }
152
154
155 return $customDomainExist ? __CLASS__ . '::' . __FUNCTION__ . '();' : '';
156 }
157
164 public static function clearRecycleScope(string $scope, ?int $days = null): string
165 {
166 Site\Type::setScope($scope);
167
168 self::clearRecycle($days);
169
170 return __CLASS__ . '::' . __FUNCTION__ . '(\'' . $scope . '\');';
171 }
172
178 protected static function getSubFolders(int $folderId): array
179 {
180 $folders = [];
181 $res = Folder::getList([
182 'select' => [
183 'ID'
184 ],
185 'filter' => [
186 'PARENT_ID' => $folderId
187 ]
188 ]);
189 while ($row = $res->fetch())
190 {
191 $folders[] = $row['ID'];
192 $folders = array_merge($folders, self::getSubFolders($row['ID']));
193 }
194 return $folders;
195 }
196
202 public static function clearRecycle(?int $days = null): string
203 {
204 Rights::setGlobalOff();
205
206 $days = !is_null($days)
207 ? $days
208 : (int) Manager::getOption('deleted_lifetime_days');
209
210 $date = new DateTime;
211 $date->add('-' . $days . ' days');
212
213 // check folders to delete
214 $foldersToDelete = [-1];
215 $res = Folder::getList([
216 'select' => [
217 'ID'
218 ],
219 'filter' => [
220 '=DELETED' => 'Y',
221 '<DATE_MODIFY' => $date
222 ]
223 ]);
224 while ($row = $res->fetch())
225 {
226 $foldersToDelete[] = $row['ID'];
227 $foldersToDelete = array_merge($foldersToDelete, self::getSubFolders($row['ID']));
228 }
229
230 // first delete landings
231 $res = Landing::getList([
232 'select' => [
233 'ID', 'FOLDER_ID'
234 ],
235 'filter' => [
236 [
237 'LOGIC' => 'OR',
238 [
239 '=DELETED' => 'Y',
240 '<DATE_MODIFY' => $date
241 ],
242 [
243 '=SITE.DELETED' => 'Y',
244 '<SITE.DATE_MODIFY' => $date
245 ],
246 [
247 'FOLDER_ID' => $foldersToDelete
248 ]
249 ],
250 '=DELETED' => ['Y', 'N'],
251 '=SITE.DELETED' => ['Y', 'N'],
252 'CHECK_PERMISSIONS' => 'N'
253 ],
254 'order' => [
255 'DATE_MODIFY' => 'desc'
256 ]
257 ]);
258 while ($row = $res->fetch())
259 {
260 Lock::lockDeleteLanding($row['ID'], false);
261 Landing::delete($row['ID'], true)->isSuccess();
262 }
263
264 // delete folders
265 foreach (array_unique($foldersToDelete) as $folderId)
266 {
267 if ($folderId > 0)
268 {
269 Folder::delete($folderId)->isSuccess();
270 }
271 }
272
273 // then delete sites
274 $res = Site::getList([
275 'select' => [
276 'ID'
277 ],
278 'filter' => [
279 '=DELETED' => 'Y',
280 '<DATE_MODIFY' => $date,
281 'CHECK_PERMISSIONS' => 'N'
282 ],
283 'order' => [
284 'DATE_MODIFY' => 'desc'
285 ]
286 ]);
287 while ($row = $res->fetch())
288 {
289 Lock::lockDeleteSite($row['ID'], false);
290 Site::delete($row['ID'])->isSuccess();
291 }
292
293 Rights::setGlobalOn();
294
295 return __CLASS__ . '::' . __FUNCTION__ . '();';
296 }
297
303 public static function clearFiles(?int $count = null): string
304 {
305 $count = !is_null($count) ? $count : 30;
306
307 File::deleteFinal($count);
308
309 return __CLASS__ . '::' . __FUNCTION__ . '(' . $count . ');';
310 }
311
317 public static function clearHistory(?int $days = null): string
318 {
319 Rights::setGlobalOff();
320
321 $newAgentName = __CLASS__ . '::' . __FUNCTION__ . '(' . ($days ?? '') . ');';
322
323 $days = $days ?: (int) Manager::getOption('history_lifetime_days');
324 $date = new DateTime();
325 $date->add('-' . $days . ' days');
326
327 $rows = HistoryTable::query()
328 ->setSelect(['ENTITY_ID', 'ENTITY_TYPE'])
329 ->setDistinct(true)
330 ->where('DATE_CREATE', '<', $date)
331 ->fetchAll()
332 ;
333 foreach ($rows as $row)
334 {
335 $history = new History($row['ENTITY_ID'], $row['ENTITY_TYPE']);
336 $history->clearOld($days);
337 }
338
339 return $newAgentName;
340 }
341
346 public static function sendRestStatistic(): string
347 {
348 if (
349 \Bitrix\Main\Loader::includeModule('rest')
350 && is_callable(['\Bitrix\Rest\UsageStatTable', 'logLanding'])
351 )
352 {
353 $statCode = [
354 \Bitrix\Landing\PublicAction::REST_USAGE_TYPE_BLOCK => 'LANDING_BLOCK',
355 \Bitrix\Landing\PublicAction::REST_USAGE_TYPE_PAGE => 'LANDING_PAGE',
356 ];
357 $data = PublicAction::getRestStat(false, true);
358 foreach ($data as $type => $stat)
359 {
360 if ($statCode[$type])
361 {
362 foreach ($stat as $clientId => $count)
363 {
364 \Bitrix\Rest\UsageStatTable::logLanding($clientId, $statCode[$type], $count);
365 }
366 }
367 }
368 \Bitrix\Rest\UsageStatTable::finalize();
369 }
370
371 return __CLASS__ . '::' . __FUNCTION__ . '();';
372 }
373
378 public static function clearTempFiles(): string
379 {
380 $dateTime = new DateTime();
381
382 $res = Internals\FileTable::getList([
383 'select' => [
384 'ID', 'FILE_ID'
385 ],
386 'filter' => [
387 '>FILE_ID' => 0,
388 '=TEMP' => 'Y',
389 '<FILE.TIMESTAMP_X' => $dateTime->add('-60 minute')
390 ]
391 ]);
392 while ($row = $res->fetch())
393 {
394 Internals\FileTable::update($row['ID'], [
395 'FILE_ID' => -1 * $row['FILE_ID']
396 ]);
397 }
398
399 return __CLASS__ . '::' . __FUNCTION__ . '();';
400 }
401
407 public static function repairFormUrls(int $lastLid = 0): string
408 {
409 if (Loader::includeModule('crm'))
410 {
411 $formQuery = WebForm\Internals\LandingTable::query()
412 ->addSelect('FORM_ID')
413 ->addSelect('LANDING_ID')
414 ->addOrder('LANDING_ID')
415 ->setLimit(50)
416 ->where('LANDING_ID', '>', $lastLid)
417 ->exec()
418 ;
419 $lastLid = 0;
420 while ($form = $formQuery->fetch())
421 {
422 $blocksQuery = BlockTable::query()
423 ->addSelect('ID')
424 ->where('LID', $form['LANDING_ID'])
425 ->where('CODE', '66.90.form_new_default')
426 ->exec()
427 ;
428 while ($block = $blocksQuery->fetch())
429 {
430 Subtype\Form::setFormIdToBlock($block['ID'], $form['FORM_ID']);
431 }
432 $lastLid = (int)$form['LANDING_ID'];
433 }
434
435 if ($lastLid > 0)
436 {
437 return __CLASS__ . '::' . __FUNCTION__ . '(' . $lastLid . ');';
438 }
439 }
440
441 return '';
442 }
443
449 public static function checkFileExists(int $fileId): string
450 {
451 $file = \CFile::getFileArray($fileId);
452 if (!$file)
453 {
454 return '';
455 }
456 if (!$file['SRC'] || !preg_match('#^(https?://)#', $file['SRC']))
457 {
458 return '';
459 }
460 $request = new HttpClient(["redirect" => false,]);
461 $request->query(HttpClient::HTTP_HEAD, $file['SRC']);
462 if ($request->getStatus() !== 200)
463 {
464 $filesToDelete = [$fileId];
465
466 // find duplicates of file
467 $originals = FileDuplicateTable::getList([
468 'select' => ['ORIGINAL_ID'],
469 'filter' => [
470 'DUPLICATE_ID' => $fileId,
471 ],
472 ]);
473 while ($original = $originals->fetch())
474 {
475 $filesToDelete[] = (int)$original['ORIGINAL_ID'];
476 }
477
478 $duplicates = FileDuplicateTable::getList([
479 'select' => ['DUPLICATE_ID'],
480 'filter' => [
481 'ORIGINAL_ID' => $filesToDelete,
482 ],
483 ]);
484 while ($duplicate = $duplicates->fetch())
485 {
486 $filesToDelete[] = (int)$duplicate['DUPLICATE_ID'];
487 }
488
489 $filesToDelete = array_unique($filesToDelete);
490
491 // clear LandingFile table
492 $landingFiles = FileTable::getList([
493 'select' => ['ID', 'ENTITY_ID'],
494 'filter' => [
495 'ENTITY_TYPE' => File::ENTITY_TYPE_ASSET,
496 '=FILE_ID' => $filesToDelete,
497 ],
498 ]);
499 $landingsToUpdate = [];
500 while ($landingFile = $landingFiles->fetch())
501 {
502 FileTable::delete((int)$landingFile['ID']);
503 $landingsToUpdate[] = (int)$landingFile['ENTITY_ID'];
504 }
505 $landingsToUpdate = array_unique($landingsToUpdate);
506
507 // find landings for drop public cache
508 if (Manager::isB24())
509 {
510 $sites = Landing::getList([
511 'select' => ['SITE_ID'],
512 'filter' => [
513 '=ID' => $landingsToUpdate,
514 ],
515 ]);
516 while ($site = $sites->fetch())
517 {
518 Site::update((int)$site['SITE_ID'], []);
519 }
520 }
521
522 foreach ($filesToDelete as $fileToDelete)
523 {
524 \CFile::delete($fileToDelete);
525 }
526 }
527
528 return '';
529 }
530
537 public static function rePublicationLanding($landingId): void
538 {
539 $landing = Landing::createInstance($landingId);
540 if ($landing->publication())
541 {
542 Manager::clearCacheForSite($landing->getSiteId());
543 }
544 }
545}
static clearRecycleScope(string $scope, ?int $days=null)
Definition agent.php:164
static removeBadDomain()
Definition agent.php:83
static rePublicationLanding($landingId)
Definition agent.php:537
static clearRecycle(?int $days=null)
Definition agent.php:202
static addUniqueAgent(string $funcName, array $params=[], int $time=7200, ?int $nextExecDelay=null)
Definition agent.php:27
static log($itemId, $itemDesc, $typeId='LANDING_LOG')
Definition debug.php:24
static isDomainActive(string $domainName)
Definition register.php:80
const ENTITY_TYPE_ASSET
Definition file.php:26
static clearCacheForSite(int $siteId)
Definition manager.php:1402
static setGlobalOff()
Definition rights.php:105
static getList(array $parameters=array())