403Webshell
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 :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/burlakastudio.realcommenter/lib/NOTIFICATOR.php
<?php
/**
 * ����� "��������� ����������� D7" ��� �������
 * �������� ���� �����: www.realcommenter.com
 * �������� ���� �����������: burlaka.studio
 * ����� � ����������: ������� ������� (AlexeyGfi) -> alexeygfi@gmail.com
 */

namespace Burlakastudio\Realcommenter;

use Bitrix\Main\Application;
use Bitrix\Main\ArgumentException;
use Bitrix\Main\Localization\Loc;
use Bitrix\Main\Mail\Internal\EventMessageTable;
use Bitrix\Main\ObjectPropertyException;
use Bitrix\Main\SystemException;
use CEvent;

class NOTIFICATOR extends REALCOMMENTER_HL_GENERAL_PROVIDER
{

    /**
     * �������� ���������� ����
     *      - ���� ��� ����� �� ������ �����������:
     *          � ������ ���������� (���������� ���� �� ����), �� ������ �������� �����
     *          TODO: ����, ��� ������� ���� ����� ����� �� ��� �� ������, ��� � ���� �����������
     *      TODO: ���, ��� ��������� ������ �� ����������� �� ���� (�������� ������� - �� ��� ���� �����)
     *      - ���, ��� ��������� ������ �� ����������� �� ���� - ��� ������� ����� ���������
     *          (���� ������� - ����� tape_user_id ��� tape_user_email)
     *
     * �������� ���������� �������, ����������
     *      - �� �������� �����, �� ���������
     *      - �� ���������� ����������:
     *          � USERS_ADD_MOD � ������ ����� ������������-����������� ���������
     *          � USERS_ADD_LIMIT_MOD � ������ ����� ������������-�������������� ���������
     */

    public static function OnAfterCommentAdd()
    {

        $eventArgs = func_get_args();
        if (empty($eventArgs) || !is_array($eventArgs) || !$eventArgs[0] instanceof \Bitrix\Main\Event) {
            return;
        }

        /**
         * @var \Bitrix\Main\Event $event
         */
        $event = array_shift($eventArgs);

        $commentInfo = $event->getParameter('comment_info');
        $arParams = $event->getParameter('arParams');
        TOOLS::normalizeValues($arParams);

        if (!$commentInfo) {
            return;
        }

        $commentInfo['TALK_GROUP_ID'] = $arParams['TALK_GROUP_ID'] ?? null;

        self::sendEmailNewCommentAnswer($commentInfo);
        self::sendEmailNewCommentItapeOutside($commentInfo, $arParams);
        self::sendEmailNewCommentAdmin($commentInfo, $arParams);
    }

    public static function OnAfterCommentChangeActivity(\Bitrix\Main\Event $e)
    {

        $commentInfo = $e->getParameter('comment_info');

        if (!$commentInfo) {
            return;
        }

        self::sendEmailToAuthorAfterActivating($commentInfo);

    }

    /**
     * @param array $commentInfo
     * @throws ArgumentException
     * @throws ObjectPropertyException
     * @throws SystemException
     */
    private static function sendEmailNewCommentAnswer(&$commentInfo = [])
    {

        /*
         * ...������ ���������� (� �� ������ ���� ����),
         * �� ������ �������� ����� (����� ������� ����������, ����� ��� ����������)
         */
        if (!$commentInfo['UF_PARENT_ID']) {
            return;
        }

        /**
         * ��� ����� �� ������ ���������� �����,
         * ...�� � ������ (��� �� USER_ID) - ���� ������� � ������ ����������
         * ...� ����� � ���-��
         */

        /**
         * ������ ��� ���� ����� ��������, ���� �������� ������
         * ��� ����� ������ ����� ������������ ����������,
         * �� ������ �� � ��� ������ ������������ ������� ����
         *
         * ������ ��� � ����� ������
         */
        $parentsList = self::scanAllParents($commentInfo['UF_PARENT_ID']/*, $urlInfo*/);

        foreach ($parentsList as $parentInfo) {
            if (!$parentInfo['UF_EMAIL'] && !$parentInfo['USER_INFO']['EMAIL']) {
                continue;
            }

            $authorEmail = ($parentInfo['UF_EMAIL']) ?: $parentInfo['USER_INFO']['EMAIL'];

            $eventFields = [
                'TEXT' => Comments::compileCommentText(
                    $commentInfo,
                    ['SIMPLIFY_TO_TEXT' => true]
                ),
                'AUTHOR_EMAIL' => $authorEmail,
                'AUTHOR_NAME' => Burn::burn_comment_signature($parentInfo),
            ];

            $eventFields = self::extendEventFields($eventFields, $commentInfo);

            self::eventSend(
                self::getPostTemplateId('answer'),
                TOOLS::get_site_id(),
                $eventFields,
                ['TEXT']
            );

        }

    }

    /**
     * @param array $commentInfo
     * @param array $arParams
     * @throws ArgumentException
     * @throws ObjectPropertyException
     * @throws SystemException
     */
    private static function sendEmailNewCommentItapeOutside($commentInfo = [], &$arParams = [])
    {

        $emailsList = [];

        // ��� ������������ �� ������� ��� tape_user_id
        $tapeUserId = trim($arParams['tape_variables']['tape_user_id']);
        if ($tapeUserId) {
            $tapeUserId = preg_replace('~[^0-9,]~', '', $tapeUserId);
            $tapeUserIdList = explode(',', $tapeUserId);
            if (count($tapeUserIdList)) {
                foreach ($tapeUserIdList as $user_id) {
                    $userInfo = USER::get_info($user_id);
                    if ($userInfo && $userInfo['EMAIL']) {
                        $emailsList[] = $userInfo['EMAIL'];
                    }
                }
            }
        }

        // ���������� ������������ �� ������� ��� tape_user_email
        $tapeUserEmail = trim($arParams['tape_variables']['tape_user_email']);
        if ($tapeUserEmail) {
            $tapeUserEmail = preg_replace('~\s+~', '', $tapeUserEmail);
            $tapeUserEmailList = explode(',', $tapeUserEmail);
            if (count($tapeUserEmailList)) {
                $emailsList = array_merge($emailsList, $tapeUserEmailList);
            }
        }

        if (empty($emailsList) || empty($commentInfo)) {
            return;
        }

        /**
         * ��������� ������ ������
         * �������� ��� �����
         * ���������
         */
        foreach ($emailsList as $email) {
            $eventFields = [
                'TEXT' => Comments::compileCommentText(
                    $commentInfo,
                    ['SIMPLIFY_TO_TEXT' => true]
                ),
                'TYPE_OF_TOPIC' => self::getTapeTypeAndTitle($arParams),
                'TAPE_AUTHOR_EMAIL' => $email,
            ];

            $eventFields = self::extendEventFields($eventFields, $commentInfo);

            self::eventSend(
                self::getPostTemplateId('tape_outside_author'),
                TOOLS::get_site_id(),
                $eventFields,
                ['TEXT']
            );
        }
    }

    private static function getAdminEmailList()
    {
        $emails = OPTIONS::get('admin_email');
        if ($emails) {
            $emails = preg_replace('~\s+~', '', $emails);
            $emails = explode(',', $emails);
            return $emails;
        }

        return [];
    }

    public static function getSecurityEmailList()
    {
        $emails = OPTIONS::get('security_inform_email');
        if ($emails) {
            $emails = preg_replace('~\s+~', '', $emails);
            $emails = explode(',', $emails);
            return $emails;
        }

        return [];
    }

    /**
     * �������� ��������� ����� �� ������ ��������� �������.
     *
     * ���� ��� ������������ ����������,
     * ...����� ���������� ��� �� ��������� ����������
     *
     * @param array $commentInfo
     * @param array $arParams
     * @throws SystemException
     */
    private static function sendEmailNewCommentAdmin(&$commentInfo = [], &$arParams = [])
    {
        //������ ������ ������� ��������������� (��������� �����)
        $emailsList = self::getAdminEmailList();

        /**
         * ��� ������������-������� ��������� �� ���������� ����������
         *
         * � ��������� ���������� ��� �������� ��������-�������:
         *      DONT_TELL_MODERATORS
         * ...���� �� ���������� � ����������� � �������, ������ ������ ������ � ��������� ����������
         * ���������� �� ��������
         */
        $usersAddMod = $arParams['USERS_ADD_MOD'];
        if (!empty($usersAddMod) && !$arParams['DONT_TELL_MODERATORS']) {
            foreach ($usersAddMod as &$userId) {
                $userId = (int)preg_replace('~[^0-9,]~', '', $userId);
                $userInfo = USER::get_info($userId);
                if ($userInfo && $userInfo['EMAIL']) {
                    $emailsList[] = $userInfo['EMAIL'];
                }
            }

            unset($userId);
        }

        // ��� ������������-������ ������� ��������� �� ���������� ����������
        $usersAddLimitMod = $arParams['USERS_ADD_LIMIT_MOD'];
        if (!empty($usersAddLimitMod) && !$arParams['DONT_TELL_MODERATORS']) {

            foreach ($usersAddLimitMod as &$userId) {
                $userId = (int)preg_replace('~[^0-9,]~', '', $userId);
                $userInfo = USER::get_info($userId);
                if ($userInfo && $userInfo['EMAIL']) {
                    $emailsList[] = $userInfo['EMAIL'];
                }
            }

            unset($userId);

        }

        if (empty($emailsList)) {
            return;
        }

        $emailsList =
            array_values(
                array_filter(
                    array_unique(
                        $emailsList)));

        if (empty($emailsList) || empty($commentInfo)) {
            return;
        }

        /**
         * ���� ��� ����� �� ������ �����������,
         * ...���� ��� ������ (tape-���������)
         * ...���� ���� �������
         */
        $commentTarget = [];

        if ($commentInfo['UF_PARENT_ID']) {

            $commentTarget[] = "[{$commentInfo[ 'UF_PARENT_ID' ]}]";

            /**
             * ������ ��� ���� ����� ��������, ���� �������� ������
             * ��� ����� ������ ����� ������������ ����������,
             * �� ������ �� � ��� ������ ������������ ������� ����
             *
             * ������ ��� � ����� ������
             */
            $parentInfo = Comments::getCommentInfo($commentInfo['UF_PARENT_ID']);

            if ($parentInfo) {
                $commentTarget[] = Comments::compileCommentText(
                    $parentInfo,
                    ['SIMPLIFY_TO_TEXT' => true]
                );
            }
        } else {
            $commentTarget[] = self::getTapeTypeAndTitle($arParams);
        }

        if (empty($commentTarget)) {
            $commentTarget = Application::getInstance()->getContext()->getServer()->get('HTTP_REFERER');
        } else {
            $commentTarget = implode(' ', $commentTarget);
        }

        foreach ($emailsList as $email) {
            $eventFields = [
                'COMMENT_TEXT' => Comments::compileCommentText(
                    $commentInfo,
                    ['SIMPLIFY_TO_TEXT' => true]
                ),
                'ADMIN_EMAIL' => $email,
                'COMMENT_TARGET' => $commentTarget,
                'EVENT_NAME' => $arParams['tape_variables']['tape_general_title'],
                'COMMENT_STATUS' =>
                    Loc::getMessage(
                        'ITAPE_COMMENT_ACTIVE_STATUS_' . ($commentInfo['UF_ACTIVE'] ? 'Y' : 'N')
                    )
            ];

            $eventFields = self::extendEventFields($eventFields, $commentInfo);

            self::eventSend(
                self::getPostTemplateId('admin'),
                TOOLS::get_site_id(),
                $eventFields,
                ['COMMENT_TEXT', 'COMMENT_TARGET']
            );

        }
    }

    private static function sendEmailToAuthorAfterActivating(&$commentInfo = [])
    {
        /**
         * ����� �������� ����������� ������ �������.
         *
         * ���� ��� ���������� ����� (�������� �� ���������)
         * ...���� ������ ��� ���������� � ��� � ��� ��� ��� ���������� �����
         *
         */

        $email = '';

        if (
            $commentInfo['UF_EMAIL']
            && TOOLS::email_valid($commentInfo['UF_EMAIL'])
        ) {
            $email = $commentInfo['UF_EMAIL'];
        }

        if (!$email && $commentInfo['UF_USER_ID']) {
            $email = USER::get_email($commentInfo['UF_USER_ID']);
        }

        if (!$email) {
            return;
        }

        $eventFields = [
            'NAME' => Burn::burn_comment_signature($commentInfo),
            'COMMENT_TEXT' => Comments::compileCommentText(
                $commentInfo,
                ['SIMPLIFY_TO_TEXT' => true]
            ),
            'TAPE_AUTHOR_EMAIL' => $email,
            'DE' => $commentInfo['UF_ACTIVE']
                ? ''
                : Loc::getMessage(
                    'ITAPE_COMMENT_ACTIVE_STATUS_DE'
                ),
        ];

        $eventFields = self::extendEventFields($eventFields, $commentInfo);

        self::eventSend(
            self::getPostTemplateId('comment_change_status'),
            TOOLS::get_site_id(),
            $eventFields,
            ['COMMENT_TEXT']
        );
    }

    private static function getTapeTypeAndTitle(&$arParams = [])
    {

        $result = [];

        if ($arParams['tape_variables']['tape_user_text']) {
            $result[] = $arParams['tape_variables']['tape_user_text'];
        }

        if ($arParams['tape_variables']['tape_general_title']) {
            $result[] = '"' . $arParams['tape_variables']['tape_general_title'] . '"';
        }

        if (empty($result)) {
            $result = Loc::getMessage('TYPE_OF_TOPIC_DEFAULT');
        } else {
            $result = implode(' ', $result);
        }

        return $result;
    }

    /**
     * @param array $eventFields
     * @param array $commentInfo
     * @return array
     * @throws SystemException
     * @throws \Bitrix\Main\ArgumentNullException
     * @throws \Bitrix\Main\ArgumentOutOfRangeException
     */
    private static function extendEventFields(&$eventFields = [], &$commentInfo = [])
    {

        // #COPY_TO# - ���� ���������, �������� ��� ����� �����������
        //������ ������ ������� ���������� (��������� �����), ������ ���� �����
        $managerEmail = OPTIONS::get('manager_email');
        if ($managerEmail) {
            $managerEmail = preg_replace('~\s+~', '', $managerEmail);
            $managerEmail = explode(',', $managerEmail);
            if (count($managerEmail)) {
                $eventFields['COPY_TO'] = implode(', ', $managerEmail);
            }
        }

        /**
         * #LINK# - ��� �������� ����������.
         * #COMMENT_URL# - ��� �������� ����������.
         * #URI# - ��� �������� ����������.
         * #URL# - ��� �������� ����������.
         *
         * ���� �� �� ������� (������ ��� � ������� ���� ����� �� ������ �������������)
         * ...� �� ��������
         */
        $server = Application::getInstance()->getContext()->getServer();
        $commentUrl = Application::getInstance()->getContext()->getServer()->get('HTTP_REFERER');
        if (preg_match('~(https?):\/\/([^\/]+)(\/.+)~', $commentUrl, $matches)) {

            $eventFields['PROTOCOL'] = $matches['1'];
            $eventFields['DOMAIN_NAME'] = $matches['2'];
            $eventFields['SERVER_NAME'] = $matches['2'];

            $eventFields['LINK'] =
            $eventFields['COMMENT_URL'] =
            $eventFields['URI'] =
            $eventFields['URL'] = $matches['3'];
        }

        if (!$eventFields['DOMAIN_NAME'] || !$eventFields['SERVER_NAME']) {
            // #DOMAIN_NAME# - ������ �������� �� (��� ��������������� ��������)
            // (!) ���� ��� ����� (� � ������� ������) ��������������
            $eventFields['DOMAIN_NAME'] = $server->getHttpHost();
            $eventFields['SERVER_NAME'] = $eventFields['DOMAIN_NAME'];
        }

        if (!$eventFields['PROTOCOL']) {
            $eventFields['PROTOCOL'] = $_SERVER['REQUEST_SCHEME'];
        }

        if (!empty($commentInfo)) {
            $eventFields['COMMENT_ID'] = $commentInfo['ID'];
            $eventFields['ID'] = $commentInfo['ID'];

            $urlInfo = URL::get_info($commentInfo['UF_URL_ID']);

            $eventFields['LINK'] =
            $eventFields['COMMENT_URL'] =
            $eventFields['URI'] =
            $eventFields['URL'] =
                $urlInfo['UF_URL'] .
                (!strpos($urlInfo['UF_URL'], '?') ? '?' : '&') .
                'itape=' . $commentInfo['ID'];
        }

        // TODO: �����???? ���� � ������ ������: �������� ��������, �������� � ���, ��� �������� �����? ����� � ������ �� ���������� � ���� �� ����-������
        $eventFields['SPAM_FILTER'] = ''; //�������

        return $eventFields;
    }

    public static function getPostTemplateId($type = '')
    {
        if (!$type) {
            return '';
        }

        $types = self::post_templates();
        return $types[$type];
    }

    private static function post_templates()
    {
        return [
            'answer' => 'ITAPE_NEW_COMMENT_AUTHOR',
            'admin' => 'ITAPE_NEW_COMMENT_ADMIN',
            'tape_outside_author' => 'ITAPE_NEW_EVENT_FOR_AUTHOR',
            'comment_change_status' => 'ITAPE_COMMENT_CHANGE_STATUS',
            'security' => 'ITAPE_SECURITY'
        ];
    }

    /**
     * ��� �� �����������-�������� � �������� �����
     *
     * @param int $parentId
     *
     * @return array
     */
    private static function scanAllParents(&$parentId = 0)
    {

        if (!$parentId) {
            return [];
        }

        $return = [];
        $parentInfo = Comments::getCommentInfo($parentId);

        if ($parentInfo) {

            if ($parentInfo['USER_ID']) {

                $userInfo = USER::get_info($parentInfo['USER_ID']);
                if ($userInfo) {
                    $parentInfo['USER_INFO'] = $userInfo;
                }

            }

            $return[] = $parentInfo;

            if ($parentInfo['PARENT_ID']) {
                $return = array_merge(
                    $return,
                    self::scanAllParents($parentInfo['PARENT_ID'])
                );
            }
        }

        return $return;
    }

    public static function uploaded_file($file_info = [])
    {

        if (empty($file_info) || !is_array($file_info)) {
            return;
        }

        /**
         * !!! ������ ����� �������� � ��� ������ ������� \Bitrix\Main\Event
         *
         * ��� ������� ������, ����:
         * Array (
         *    [fileName] => 04.jpg
         *    [originalName] => 04.jpg
         *    [fileID] => 77
         *    [fileContentType] => image/jpeg
         *    [~fileSize] => 34023
         *    [fileSize] => 33.23 �
         *    [fileURL] => /bitrix/...
         *    [fileURLDelete] => /bitrix/...
         */

        //������ ������ ������� ��������������� (��������� �����)
        $emails_list = self::getSecurityEmailList();
        if (empty($emails_list)) {
            return;
        }

        ob_start();
        array_walk($file_info, function ($value, $key) {
            echo $key . ': ' . $value . "\n";
        });
        $file_info = ob_get_clean();


        $event_fields = [
            'FILE_INFO' => $file_info,
            'TO_EMAIL' => implode(',', $emails_list),
        ];

        $event_fields = self::extendEventFields($event_fields);

        self::eventSend(
            self::getPostTemplateId('security'),
            TOOLS::get_site_id(),
            $event_fields
        );

    }

    /**
     * @param string $eventName
     * @param string $lid
     * @param array $eventFields
     * @param array $htmlTypedFields - ������ �� ������� ���� � �������, ������ ������ ��� html
     * @throws SystemException
     * @throws ArgumentException
     * @throws ObjectPropertyException
     */
    private static function eventSend($eventName = '', $lid = '', &$eventFields = [], $htmlTypedFields = [])
    {

        /**
         * ��� ����� �� ���� ����� ������ ��� ����� (html / text)
         * � ��� � �������� ����� ������ ������ ���� � text-����.
         *
         * (!) ������ ����� ��������� ������, ����� ��� ����
         *
         * ���� ������ text, � �� html, ����� �� ���, ������ ������ � html
         * ��������� � �����. ���� ����� ��� ���
         *
         */

        if (!empty($htmlTypedFields)) {

            $eventArr = EventMessageTable::getList(
                [
                    'select' => [
                        'ID', 'BODY_TYPE'
                    ],
                    'filter' => [
                        'EVENT_NAME' => $eventName
                    ],
                    'limit' => 1
                ]
            )->fetch();

            if (empty($eventArr)) {
                return;
            }

            if ($eventArr['BODY_TYPE'] !== 'html') {

                foreach ($htmlTypedFields as $fieldKey) {
                    if (!$eventFields[$fieldKey]) {
                        continue;
                    }

                    $eventFields[$fieldKey] = HTMLToTxt($eventFields[$fieldKey]);
                }
            }
        }

        CEvent::Send(
            $eventName,
            $lid,
            $eventFields
        );
    }

    public static function sanctionAction($eventFields = [])
    {
        /**
         * ������������� ���, ������� �� �������� ���� ����������
         */
        self::walkFields($eventFields);

        $eventType = '';

        if ($eventFields['SANCTION_TYPE'] === 'BAN') {
            $eventType = 'ITAPE_BAN_SET';
        } else if ($eventFields['SANCTION_TYPE'] === 'UNBAN') {
            $eventType = 'ITAPE_UNBAN_SET';
        } else if ($eventFields['SANCTION_TYPE'] === 'SANCTION') {
            $eventType = 'ITAPE_SANCTION_SET';
        } else if ($eventFields['SANCTION_TYPE'] === 'UNSANCTION') {
            $eventType = 'ITAPE_SANCTION_DISCARD';
        }

        self::eventSend(
            $eventType,
            TOOLS::get_site_id(),
            $eventFields,
            ['COMMENT_TEXT']
        );

    }

    protected static function walkFields(&$eventFields = [])
    {

        self::extendEventFields($eventFields, $eventFields['COMMENT_INFO']);

        /**
         * � �������� �� �������� ����� foreach, ���� ���� ���������� �� ������ ��� ��������� ������,
         * ...� ����� �� ������� ������ ������
         */

        if ($eventFields['SANCTION_TYPE']) {
            $eventFields['DESCRIPTION'] = Sanctions::burnDescription($eventFields['SANCTION_TYPE']);
        }

        if ($eventFields['COMMENT_INFO']) {
            $eventFields['COMMENT_TEXT'] = Comments::compileCommentText(
                $eventFields['COMMENT_INFO'],
                ['SIMPLIFY_TO_TEXT' => true]
            );

            if (!$eventFields['AUTHOR_NAME']) {
                $eventFields['AUTHOR_NAME'] = Burn::burn_comment_signature($eventFields['COMMENT_INFO']);
            }

            if (!$eventFields['URL_ID']) {
                $eventFields['URL_ID'] = $eventFields['COMMENT_INFO']['UF_URL_ID'];
            }
        }

        if ($eventFields['URL_ID']) {
            $talkUrl = '';

            $urlInfo = URL::get_info($eventFields['URL_ID']);
            if (!empty($urlInfo)) {
                $talkUrl = $urlInfo['UF_URL'];
            }

            $eventFields['PAGE_URL'] = sprintf('%s://%s%s', $eventFields['PROTOCOL'], $eventFields['DOMAIN_NAME'], $talkUrl);
        }

        if ($eventFields['USER_ID']) {
            $eventFields['TAPE_AUTHOR_EMAIL'] = USER::get_email($eventFields['USER_ID']);

            if (!$eventFields['AUTHOR_NAME']) {
                $eventFields['AUTHOR_NAME'] = USER::get_formatted_name($eventFields['USER_ID']);
            }
        }
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit