Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/burlakastudio.realcommenter/lib/ |
Upload File : |
<?php /** * ����� "��������� ����������� D7" ��� ������� * �������� ���� �����: www.realcommenter.com * �������� ���� �����������: burlaka.studio * ����� � ����������: ������� ������� (AlexeyGfi) -> alexeygfi@gmail.com */ namespace Burlakastudio\Realcommenter; use Bitrix\Main\Localization\Loc; use Bitrix\Main\ObjectException; use Bitrix\Main\ORM\Entity; use Bitrix\Main\Type\DateTime; require_once 'Sanctions_TABLE.php'; class Sanctions extends REALCOMMENTER_HL_GENERAL_PROVIDER { const STATUS_CHANGED = true; const STATUS_NOT_SET = 0; const NO_WARNINGS = 0; const NOT_IN_USE = 0; public static function inUse() { return OPTIONS::get('use_warnings_and_ban'); } public static function restrictedUser($userId = null) { if (self::inUse()) { return self::userHaveBan($userId); } return self::NOT_IN_USE; } public static function burnAdminTools($userId = null) { /** * ���� ����������-�����... * �� ����������� == �� ������ ����� * * ����������� � �� � ���� == ��� ������ ���� * ����������� � ����� ������������� * ...� ��� � ����� +1 �������������� == ��� ������ ������������� */ $adminTools = []; if (!self::inUse()) { return $adminTools; } $userHaveBan = self::userHaveBan($userId); if ($userHaveBan) { $adminTools[] = 'UN_BAN'; } else { $adminTools[] = 'SEND_TO_BAN'; $userWarnings = self::getWarningsCount($userId); if ($userWarnings < self::getAllowedWarnings()) { $adminTools[] = 'INCREESE_WARNING'; } } return $adminTools; } private static function getAllowedWarnings() { return OPTIONS::get('allowedWarnings'); } /** * ����� ������ ��� �� ������ ���������� (��� �����) * ...���� ���� ������ * * @param null $userId * @param null $skipCache * @return bool|int|mixed|null */ private static function getUserStatuses($userId = null, $skipCache = null) { if (!self::inUse()) { return self::NOT_IN_USE; } if (!$userId) { return USER::USER_NOT_FOUND; } $parameters = [ 'filter' => [ 'UF_USER_ID' => $userId ], // 'cache' => [ // 'ttl' => 36000, // 'cache_json' => true // ] ]; if (!$skipCache) { $parameters['cache'] = [ 'ttl' => 36000, 'cache_json' => true ]; } $result = self::getListEx($parameters); if (empty($result)) { return false; } return array_shift($result); } public static function userHaveBan($userId = null) { if (!self::inUse()) { return self::NOT_IN_USE; } $userId = self::getUserId($userId); if (!$userId) { return USER::USER_NOT_FOUND; } $userStatuses = self::getUserStatuses($userId); if (!empty($userStatuses)) { return $userStatuses['UF_BAN']; } return self::STATUS_NOT_SET; } public static function getWarningsCount($userId = null) { if (!self::inUse()) { return self::NOT_IN_USE; } if (!$userId) { return USER::USER_NOT_FOUND; } $userStatuses = self::getUserStatuses($userId); if (!empty($userStatuses)) { return $userStatuses['UF_WARNINGS']; } return self::NO_WARNINGS; } /** * ����� ������� ������ �� �� ������� ����������, * � ������� ��� ����� * * @param null $requestType * @param array $actionInfo * @return bool|int * @throws ObjectException */ public static function makeAction($requestType = null, &$actionInfo = []) { $userId = $actionInfo['sanctionUserId']; if (!self::inUse()) { return self::NOT_IN_USE; } switch ($requestType) { case 'UN_BAN' : if (!self::userHaveBan($userId)) { return self::STATUS_CHANGED; } self::dropTheBan($userId, $actionInfo); break; case 'SEND_TO_BAN' : self::sendToBan($userId, $actionInfo); break; case 'INCREESE_WARNING' : if (self::userHaveBan()) { return self::STATUS_CHANGED; } self::newWarning($userId, $actionInfo); break; } self::dropCache(); return self::STATUS_CHANGED; } /** * @param null $userId * @param array $actionInfo * @return bool|int|null * @throws ObjectException */ private static function sendToBan($userId = null, &$actionInfo = []) { if (!self::inUse()) { return self::NOT_IN_USE; } $userId = self::getUserId($userId); if (!$userId) { return USER::USER_NOT_FOUND; } /** * ������� ����� �� ����� �� USER_ID * ... ���� ���� * * ������ UF_BAN = 1 */ $row = self::prepareRowFor($userId); if (empty($row)) { return false; } $result = self::update($row['ID'], [ 'UF_BAN' => 1, 'UF_WARNINGS' => 0, // � ������ ������� ������ �� ��������� ����� ���� ������� 'UF_USER_ID' => $userId, 'UF_DATE_MODIFY' => new DateTime() ]); if (!$result->isSuccess()) { DEBUGGER::error( Loc::getMessage('DB_REQUEST_ERROR') . ': ' . implode(', ', $result->getErrorMessages()) ); return false; } /** * �������� ����������� ��� ��� * * ������� ������ �������: * AUTHOR_NAME - ������� ���� �� ���������� ���� �� ���� ���������� * TAPE_AUTHOR_EMAIL - ������� �� USER_ID * PAGE_URL - ������� �� URL_ID, � ��� �� COMMENT_INFO * COMMENT_TEXT - ������ �� COMMENT_INFO * DESCRIPTION - ������� �� SANCTION_TYPE */ NOTIFICATOR::sanctionAction( [ 'SANCTION_TYPE' => 'BAN', 'USER_ID' => $userId, 'COMMENT_INFO' => $actionInfo['commentInfo'], ] ); return self::STATUS_CHANGED; } public static function burnDescription($type = null) { if (!$type) { return ''; } $description = ''; if ($type === 'BAN') { /** * �������� ������� �� ����, ������ �� ������������ ������ ���� * * ������� 1, ����� ����������� ��������� ����: * ������ ����� ��� ����� ���� ���. * ������ ����� ��� ����� ��� ��. * ������ ����� ��� ����� �� ����. * * ������� 2, ����� ������ �������� ����� ���������������: * ������ ����� �� ��� ����� ���������������. */ $daysDeclen = new \Bitrix\Main\Grid\Declension( Loc::getMessage('ONE_DAY'), Loc::getMessage('TWO_DAYS'), Loc::getMessage('FIVE_DAY') ); $banDropDaysPeriod = intval(OPTIONS::get('banDropDaysPeriod')); if ($banDropDaysPeriod) { $description = Loc::getMessage( 'SANCTIONS_UNBAN_BY_DAYS', [ '#CNT#' => $banDropDaysPeriod, '#DAY_LANG#' => $daysDeclen->get($banDropDaysPeriod) ] ); } else { $description = Loc::getMessage('SANCTIONS_UNBAN_BY_ADMIN'); } } else if ($type === 'SANCTION') { /** * * �� �������������� �������� ��������� -- �� ��� ����������� * * ���� 1: * ���� ����������� ��������� ��������������, ����� ����� ����� ���: * �����������, ������ ������� #CNT# #CNT_LANG#, �������������� ���������� �����. * * ���� 2: * ������� �� ����, ������ �� ������������ ������ ����: * �������������� �������� ����� #DAYS# #DAY_LANG#. */ $daysDeclen = new \Bitrix\Main\Grid\Declension( Loc::getMessage('ONE_DAY'), Loc::getMessage('TWO_DAYS'), Loc::getMessage('FIVE_DAY') ); $warningsDeclen = new \Bitrix\Main\Grid\Declension( Loc::getMessage('ONE_WARNING'), Loc::getMessage('TWO_WARNINGS'), Loc::getMessage('FIVE_WARNINGS') ); $allowedWarnings = intval(OPTIONS::get('allowedWarnings')); if ($allowedWarnings) { $description .= Loc::getMessage( 'SANCTIONS_BEFORE_BAN', [ '#CNT#' => $allowedWarnings, '#CNT_LANG#' => $warningsDeclen->get($allowedWarnings) ] ); $description .= "\n"; } $warningDropDaysPeriod = intval(OPTIONS::get('warningDropDaysPeriod')); if ($warningDropDaysPeriod) { $description .= Loc::getMessage( 'SANCTIONS_DROP_AFTER_DAYS', [ '#DAYS#' => $warningDropDaysPeriod, '#DAY_LANG#' => $daysDeclen->get($warningDropDaysPeriod) ] ); $description .= "\n"; } } return $description; } public static function dropTheBan($userId, &$actionInfo = []) { if (!self::inUse()) { return self::NOT_IN_USE; } $userId = self::getUserId($userId); if (!$userId) { return USER::USER_NOT_FOUND; } /** * ������� ����� �� ����� �� USER_ID * ... ���� ���� * * ������ UF_BAN = 1 */ $row = self::getUserStatuses($userId, true); if (empty($row)) { return self::STATUS_CHANGED; } $result = self::update($row['ID'], [ 'UF_BAN' => 0, 'UF_WARNINGS' => 0, // � ������ ������� ������ �� ��������� ����� ���� ������� 'UF_USER_ID' => $userId, 'UF_DATE_MODIFY' => new DateTime() ]); if (!$result->isSuccess()) { DEBUGGER::error( Loc::getMessage('DB_REQUEST_ERROR') . ': ' . implode(', ', $result->getErrorMessages()) ); return false; } /** * �������� ����������� ��� ������ * * ������� ������ �������: * AUTHOR_NAME - ������� ���� �� ���������� ���� �� ���� ���������� */ NOTIFICATOR::sanctionAction( [ 'SANCTION_TYPE' => 'UNBAN', 'USER_ID' => $userId, 'COMMENT_INFO' => $actionInfo['commentInfo'], ] ); return self::STATUS_CHANGED; } protected static function newWarning($userId = null, &$actionInfo = []) { if (!self::inUse()) { return self::NOT_IN_USE; } $userId = self::getUserId($userId); if (!$userId) { return USER::USER_NOT_FOUND; } /** * ������� ����� �� ����� �� USER_ID * * ������� ����� ������� ����� �������������� UF_WARNINGS * * ���� �������: * - �������� ���; * - ��������� ���� �������������� * * ... ���� ���������� ���� �������������� */ $userWarnings = self::getWarningsCount($userId); $allowedWarnings = self::getAllowedWarnings(); $userWarnings++; if ($userWarnings >= $allowedWarnings) { return self::sendToBan($userId); } $row = self::prepareRowFor($userId); if (empty($row)) { return false; } $result = self::update($row['ID'], [ 'UF_BAN' => 0, 'UF_WARNINGS' => $userWarnings, 'UF_USER_ID' => $userId, 'UF_DATE_MODIFY' => new DateTime() ]); if (!$result->isSuccess()) { DEBUGGER::error( Loc::getMessage('DB_REQUEST_ERROR') . ': ' . implode(', ', $result->getErrorMessages()) ); return false; } /** * �������� ����������� ��� ���� ����� * * CNT_TO_BAN_DESCRIPTION: * �����������, ������ ������� #CNT# #CNT_LANG#, �������������� ���������� �����. * * ������� ������ �������: * AUTHOR_NAME - ������� ���� �� ���������� ���� �� ���� ���������� * TAPE_AUTHOR_EMAIL - ������� �� USER_ID * PAGE_URL - ������� �� URL_ID, � ��� �� COMMENT_INFO * COMMENT_TEXT - ������ �� COMMENT_INFO * CNT_TO_BAN_DESCRIPTION - ������� �� SANCTION_TYPE * DESCRIPTION - ������� �� SANCTION_TYPE */ NOTIFICATOR::sanctionAction( [ 'SANCTION_TYPE' => 'SANCTION', 'USER_ID' => $userId, 'COMMENT_INFO' => $actionInfo['commentInfo'], ] ); return self::STATUS_CHANGED; } public static function skipWarning($userId = null, $cnt = 1) { if (!self::inUse()) { return self::NOT_IN_USE; } $userId = self::getUserId($userId); if (!$userId) { return USER::USER_NOT_FOUND; } /** * ������� ����� �� ����� �� USER_ID * ������� ����� ������� ����� �������������� UF_WARNINGS * �� ����� ��� � ����� */ $row = self::getUserStatuses($userId, true); $userWarnings = $row['UF_WARNINGS'] - $cnt; $userWarnings = ($userWarnings >= 0) ? $userWarnings : 0; $result = self::update($row['ID'], [ 'UF_BAN' => 0, 'UF_WARNINGS' => $userWarnings, 'UF_USER_ID' => $userId, 'UF_DATE_MODIFY' => new DateTime() ]); if (!$result->isSuccess()) { DEBUGGER::error( Loc::getMessage('DB_REQUEST_ERROR') . ': ' . implode(', ', $result->getErrorMessages()) ); return false; } /** * �������� ����������� ��� ������ * * ������� ������ �������: * AUTHOR_NAME - ������� ���� �� ���������� ���� �� ���� ���������� */ NOTIFICATOR::sanctionAction( [ 'SANCTION_TYPE' => 'UNSANCTION', 'USER_ID' => $userId, ] ); return self::STATUS_CHANGED; } protected static function getUserId($userId = null) { return $userId ?: USER::get_id(); } protected static function prepareRowFor($userId = null) { if (!$userId) { return USER::USER_NOT_FOUND; } $result = self::getUserStatuses($userId); if (empty($result)) { $result = self::get_free_row([ 'UF_USER_ID' => false ]); } return $result; } public static function OnAfterUpdate(\Bitrix\Main\ORM\Event $event) { self::dropCache(); } public static function dropCache() { /** * @var $tableClass Sanctions_TABLE */ $dataClass = self::getDataClass(); $entity = Entity::get($dataClass); $entity->cleanCache(); } }