%PDF- %PDF- 403WebShell
403Webshell
Server IP : 37.220.80.31  /  Your IP : 18.218.11.211
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux 3051455-guretool.twc1.net 5.15.0-107-generic #117-Ubuntu SMP Fri Apr 26 12:26:49 UTC 2024 x86_64
User : www-root ( 1010)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
MySQL : OFF  |  cURL : ON  |  WGET : OFF  |  Perl : OFF  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /var/www/www-root/data/www/dev.artlot24.ru/bitrix/modules/rest/lib/event/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/www-root/data/www/dev.artlot24.ru/bitrix/modules/rest/lib/event/sender.php
<?php

namespace Bitrix\Rest\Event;

use Bitrix\Main\Context;
use Bitrix\Main\Event;
use Bitrix\Main\EventManager;
use Bitrix\Rest\Application;
use Bitrix\Rest\AppTable;
use Bitrix\Rest\OAuth\Auth;
use Bitrix\Rest\OAuthService;
use Bitrix\Rest\Sqs;
use Bitrix\Rest\UsageStatTable;
use Bitrix\Rest\Tools\Diagnostics\LoggerManager;

/**
 * Class Sender
 *
 * Transport and utility for REST events.
 *
 * @package Bitrix\Rest
 **/
class Sender
{
	protected static $initialized = false;
	protected static $forkSet = false;
	protected static $queryData = array();

	/**
	 * @var ProviderInterface
	 */
	protected static $provider;

	/**
	 * @var ProviderOfflineInterface
	 */
	protected static $providerOffline;

	protected static $defaultEventParams = array(
		"category" => Sqs::CATEGORY_DEFAULT,
		"sendAuth" => true,
		"sendRefreshToken" => false,
	);

	/**
	 * Utility function to parse pseudo-method name
	 *
	 * @param string $name Pseudo-method name.
	 *
	 * @return array
	 */
	public static function parseEventName($name)
	{
		$res = array();
		list($res['MODULE_ID'], $res['EVENT']) = explode('__', $name);

		$res['EVENT'] = str_replace('_0_', '\\', $res['EVENT']);
		$res['EVENT'] = str_replace('_1_', '::', $res['EVENT']);

		return $res;
	}

	/**
	 * Binds REST event handler on PHP event.
	 *
	 * @param string $moduleId Event owner module.
	 * @param string $eventName Event name.
	 */
	public static function bind($moduleId, $eventName)
	{
		$eventManager = EventManager::getInstance();
		$eventManager->registerEventHandler($moduleId, $eventName, "rest", "\\Bitrix\\Rest\\Event\\Callback", static::getHandlerName($moduleId, $eventName));
	}

	/**
	 * Unbinds REST event handler on PHP event.
	 *
	 * @param string $moduleId Event owner module.
	 * @param string $eventName Event name.
	 */
	public static function unbind($moduleId, $eventName)
	{
		$eventManager = EventManager::getInstance();
		$eventManager->unRegisterEventHandler($moduleId, $eventName, "rest", "\\Bitrix\\Rest\\Event\\Callback", static::getHandlerName($moduleId, $eventName));

		/* compatibility */
		$eventManager->unRegisterEventHandler($moduleId, $eventName, "rest", "CRestEventCallback", static::getHandlerName($moduleId, $eventName));
	}

	/**
	 * Getter for default event params array.
	 *
	 * @return array
	 */
	public static function getDefaultEventParams()
	{
		return static::$defaultEventParams;
	}

	/**
	 * Returns authorization array for event handlers and BP activities.
	 *
	 * @param string|int $appId Application ID or CODE.
	 * @param int $userId User ID which will be the owner of access_token.
	 * @param array $additionalData Additional data which will be stored with access_token.
	 * @param array $additional Event parameters. Keys sendAuth and sendRefreshToken supported.
	 *
	 * @return array|bool|null
	 */
	public static function getAuth($appId, $userId, array $additionalData = array(), array $additional = array())
	{
		$auth = null;

		$application = AppTable::getByClientId($appId);
		if($application)
		{
			if($userId > 0 && $additional["sendAuth"])
			{
				if(OAuthService::getEngine()->isRegistered())
				{
					$auth = Application::getAuthProvider()->get($application['CLIENT_ID'], $application['SCOPE'], $additionalData, $userId);

					if(is_array($auth) && !$additional["sendRefreshToken"])
					{
						unset($auth['refresh_token']);
					}
				}
			}

			if(!is_array($auth))
			{
				$auth = array(
					"domain" => Context::getCurrent()->getRequest()->getHttpHost(),
					"member_id" => \CRestUtil::getMemberId()
				);
			}

			$auth["application_token"] = \CRestUtil::getApplicationToken($application);
		}

		return $auth;
	}

	/**
	 * Calls or schedules the query to SQS.
	 *
	 * @param array $handlersList Event handlers to call.
	 *
	 * @throws \Bitrix\Main\ArgumentException
	 * @throws \Bitrix\Main\ObjectPropertyException
	 * @throws \Bitrix\Main\SystemException
	 */
	public static function call($handlersList)
	{
		global $USER;

		$offlineEvents = array();
		$logger = LoggerManager::getInstance()->getLogger();
		if ($logger)
		{
			$logger->debug(
				"\n{delimiter}\n"
				. "{date} - {host}\n{delimiter}\n"
				. " Sender::call() starts.\n"
				. "{handlersList}\n{delimiter} ",
				[
					'handlersList' => $handlersList,
				]
			);
		}

		foreach($handlersList as $handlerInfo)
		{
			$handler = $handlerInfo[0];
			$data = $handlerInfo[1];
			$additional = $handlerInfo[2];

			foreach(static::$defaultEventParams as $key => $value)
			{
				if(!isset($additional[$key]))
				{
					$additional[$key] = $value;
				}
			}

			$session = Session::get();
			if(!$session)
			{
				if ($logger)
				{
					$logger->debug(
						"\n{delimiter}\n"
						. "{date} - {host}\n{delimiter}\n"
						. "Session ttl exceeded {session}.\n",
						[
							'session' => $session,
						]
					);
				}

				// ttl exceeded, kill session
				return;
			}

			$userId = $handler['USER_ID'] > 0
				? $handler['USER_ID']
				: (
					// USER object can be null if event runs in BP or agent
					is_object($USER) && $USER->isAuthorized()
						? $USER->getId()
						: 0
				);

			$authData = null;
			if($handler['APP_ID'] > 0)
			{
				$dbRes = AppTable::getById($handler['APP_ID']);
				$application = $dbRes->fetch();

				$appStatus = \Bitrix\Rest\AppTable::getAppStatusInfo($application, '');
				if($appStatus['PAYMENT_ALLOW'] === 'Y')
				{
					$authData = array(
						Session::PARAM_SESSION => $session,
						Auth::PARAM_LOCAL_USER => $userId,
						"application_token" => \CRestUtil::getApplicationToken($application),
					);
				}

				if($handler['EVENT_HANDLER'] <> '')
				{
					UsageStatTable::logEvent($application['CLIENT_ID'], $handler['EVENT_NAME']);
				}
			}
			else
			{
				$application = array('CLIENT_ID' => null);

				$authData = array(
					Session::PARAM_SESSION => $session,
					Auth::PARAM_LOCAL_USER => $userId,
					'application_token' => $handler['APPLICATION_TOKEN'],
				);
			}

			if($authData)
			{
				if($handler['EVENT_HANDLER'] <> '')
				{
					self::$queryData[] = Sqs::queryItem(
						$application['CLIENT_ID'],
						$handler['EVENT_HANDLER'],
						array(
							'event' => $handler['EVENT_NAME'],
							'data' => $data,
							'ts' => time(),
						),
						$authData,
						$additional
					);
				}
				else
				{
					$offlineEvents[] = array(
						'HANDLER' => $handler,
						'APPLICATION' => $application,
						'AUTH' => $authData,
						'DATA' => $data,
					);
				}
			}
		}

		if (count($offlineEvents) > 0)
		{
			if ($logger)
			{
				$logger->debug(
					"\n{delimiter}\n"
					. "{date} - {host}\n{delimiter}\n"
					. "Event count: {eventCount}\n{delimiter}"
					. "Offline event list:\n"
					. "{offlineEvents}",
					[
						'eventCount' => count($offlineEvents),
						'offlineEvents' => $offlineEvents,
					]
				);
			}
			static::getProviderOffline()->send($offlineEvents);
		}

		if (count(static::$queryData) > 0 && !static::$forkSet)
		{
			if ($logger)
			{
				$logger->debug(
					"\n{delimiter}\n"
					. "{date} - {host}\n{delimiter}\n"
					. "Registers send event background job.\n"
					. "count: {eventCount}",
					[
						'eventCount' => count(static::$queryData),
					]
				);
			}
			\Bitrix\Main\Application::getInstance()->addBackgroundJob(array(__CLASS__, "send"));
			static::$forkSet = true;
		}
	}

	/**
	 * Sends all scheduled handlers to SQS.
	 */
	public static function send()
	{
		$logger = LoggerManager::getInstance()->getLogger();
		if ($logger)
		{
			$logger->debug(
				"\n{delimiter}\n"
				. "{date} - {host}\n{delimiter}\n"
				. "Starts method Sender::send()\n"
				. "count: {eventCount}"
				. "Event list:\n"
				. "{eventList}",
				[
					'eventCount' => count(static::$queryData),
					'eventList' => static::$queryData,
				]
			);
		}
		if (count(self::$queryData) > 0)
		{
			UsageStatTable::finalize();
			static::getProvider()->send(self::$queryData);
			self::$queryData = array();
		}
	}

	/**
	 * @return ProviderInterface
	 */
	public static function getProvider()
	{
		static::initialize();

		if(!static::$provider)
		{
			static::$provider = static::getDefaultProvider();
		}

		return static::$provider;
	}

	/**
	 * @param ProviderInterface $provider
	 */
	public static function setProvider(ProviderInterface $provider)
	{
		static::$provider = $provider;
	}

	protected static function getDefaultProvider()
	{
		return ProviderOAuth::instance();
	}

	/**
	 * @return ProviderOfflineInterface
	 */
	public static function getProviderOffline()
	{
		static::initialize();

		if(!static::$providerOffline)
		{
			static::$providerOffline = static::getDefaultProviderOffline();
		}

		return static::$providerOffline;
	}

	/**
	 * @param ProviderOfflineInterface $providerOffline
	 */
	public static function setProviderOffline(ProviderOfflineInterface $providerOffline)
	{
		static::$providerOffline = $providerOffline;
	}

	protected static function getDefaultProviderOffline()
	{
		return ProviderOffline::instance();
	}

	protected static function initialize()
	{
		if(!static::$initialized)
		{
			static::$initialized = true;

			$event = new Event('rest', 'onEventManagerInitialize');
			$event->send();
		}
	}

	protected static function getHandlerName($moduleId, $eventName)
	{
		// \Bitrix\Rest\EventTable::on
		if(mb_strpos($eventName, '::') >= 0)
		{
			$handlerName = $moduleId.'__'.ToUpper(str_replace(array("\\", '::'), array('_0_', '_1_'), $eventName));
		}
		else
		{
			$handlerName = $moduleId.'__'.ToUpper($eventName);
		}

		return $handlerName;
	}
}

Youez - 2016 - github.com/yon3zu
LinuXploit