%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/bitrix/www/bitrix/modules/pull/classes/general/
Upload File :
Create Path :
Current File : /home/bitrix/www/bitrix/modules/pull/classes/general/pull_options.php

<?
use Bitrix\Main\Page\Asset;

class CPullOptions
{
	static $optionDefaultConfig = null;
	static $optionDefaultModule = null;

	public static function CheckNeedRun($bGetSectionStatus = true)
	{
		$arExcludeSites = CPullOptions::GetExcludeSites();
		if (isset($arExcludeSites[SITE_ID]))
			return false;

		global $CACHE_MANAGER;

		$bAdminSection = false;
		if(defined("ADMIN_SECTION") && ADMIN_SECTION == true)
			$bAdminSection = true;

		$arResult = Array();
		$res = $CACHE_MANAGER->Read(2592000, "pull_cnr");
		if ($res)
			$arResult = $CACHE_MANAGER->Get("pull_cnr");

		if(!$res)
		{
			$arResult = Array(
				'ADMIN_SECTION' => false,
				'PUBLIC_SECTION' => false
			);

			$arModule = self::GetDependentModule();
			foreach ($arModule as $moduleId => $options)
			{
				if (isset($options['ADMIN_SECTION']) && $options['ADMIN_SECTION'] == 'Y')
					$arResult['ADMIN_SECTION'] = true;
				if (isset($options['PUBLIC_SECTION']) && $options['PUBLIC_SECTION'] == 'Y')
					$arResult['PUBLIC_SECTION'] = true;
			}

			$CACHE_MANAGER->Set("pull_cnr", $arResult);
		}

		return $bGetSectionStatus? $arResult[$bAdminSection? 'ADMIN_SECTION': 'PUBLIC_SECTION']: $arResult;
	}

	public static function ModuleEnable()
	{
		$arResult = self::CheckNeedRun(false);
		return ($arResult['ADMIN_SECTION'] || $arResult['PUBLIC_SECTION'])? true: false;
	}

	public static function GetDependentModule()
	{
		$arModule = Array();
		foreach(GetModuleEvents("pull", "OnGetDependentModule", true) as $arEvent)
		{
			$ar = ExecuteModuleEventEx($arEvent);
			if (isset($ar['MODULE_ID']))
			{
				$arModule[$ar['MODULE_ID']] = Array(
					'MODULE_ID' => $ar['MODULE_ID'],
					'ADMIN_SECTION' => isset($ar['USE']) && in_array('ADMIN_SECTION', $ar['USE'])? true: false,
					'PUBLIC_SECTION' => isset($ar['USE']) && in_array('PUBLIC_SECTION', $ar['USE'])? true: false,
				);
			}
		}

		return $arModule;
	}

	public static function GetExcludeSites()
	{
		$result = COption::GetOptionString("pull", "exclude_sites", "a:0:{}", self::GetDefaultOption("exclude_sites"));
		return unserialize($result);
	}

	public static function SetExcludeSites($sites)
	{
		if (!is_array($sites))
			return false;

		COption::SetOptionString("pull", "exclude_sites", serialize($sites));

		return true;
	}

	/*
	 * @deprecated No longer used by internal code and not recommended. Use CPullOptions::GetQueueServerStatus()
	 */
	public static function GetNginxStatus()
	{
		return self::GetQueueServerStatus();
	}
	public static function GetQueueServerStatus()
	{
		$result = COption::GetOptionString("pull", "nginx", self::GetDefaultOption("nginx"));
		return $result == 'N'? false: true;
	}
	public static function GetQueueServerHeaders()
	{
		$result = COption::GetOptionString("pull", "nginx_headers", self::GetDefaultOption("nginx_headers"));
		return $result == 'Y'? true: false;
	}

	/*
	 * @deprecated No longer used by internal code and not recommended. Use CPullOptions::SetQueueServerStatus()
	 */
	public static function SetNginxStatus($flag = "N")
	{
		return self::SetQueueServerStatus($flag);
	}
	public static function SetQueueServerStatus($flag = "N")
	{
		COption::SetOptionString("pull", "nginx", $flag=='Y'?'Y':'N');

		if ($flag=='Y')
		{
			CAgent::AddAgent("CPullChannel::CheckOnlineChannel();", "pull", "N", 240, "", "Y", ConvertTimeStamp(time()+CTimeZone::GetOffset()+240, "FULL"));
			CAgent::RemoveAgent("CPullStack::CheckExpireAgent();", "pull");
		}
		else
		{
			CAgent::RemoveAgent("CPullChannel::CheckOnlineChannel();", "pull");
			CAgent::AddAgent("CPullStack::CheckExpireAgent();", "pull", "N");
		}

		return true;
	}

	public static function SetQueueServerHeaders($flag = "Y")
	{
		COption::SetOptionString("pull", "nginx_headers", $flag=='Y'?'Y':'N');
		return true;
	}

	public static function GetPushStatus()
	{
		$result = COption::GetOptionString("pull", "push", self::GetDefaultOption("push"));
		return $result == 'N'? false: true;
	}

	public static function SetPushStatus($flag = "N")
	{
		COption::SetOptionString("pull", "push", $flag=='Y'?'Y':'N');
		if ($flag == 'Y')
			CAgent::AddAgent("CPushManager::SendAgent();", "pull", "N", 30);
		else
			CAgent::RemoveAgent("CPushManager::SendAgent();", "pull");

		return true;
	}

	public static function GetPushMessagePerHit()
	{
		return intval(COption::GetOptionInt("pull", "push_message_per_hit", self::GetDefaultOption("push_message_per_hit")));
	}

	public static function SetPushMessagePerHit($count)
	{
		COption::SetOptionInt("pull", "push_message_per_hit", intval($count));

		return true;
	}

	public static function GetGuestStatus()
	{
		return COption::GetOptionString("pull", "guest", self::GetDefaultOption("guest")) == 'Y' && IsModuleInstalled('statistic');
	}

	public static function SetGuestStatus($flag = "N")
	{
		COption::SetOptionString("pull", "guest", IsModuleInstalled('statistic') && $flag=='Y'?'Y':'N');

		return true;
	}

	public static function GetPublishUrl($channelId = "")
	{
		$url = COption::GetOptionString("pull", "path_to_publish", self::GetDefaultOption("path_to_publish")).(strlen($channelId)>0?'?CHANNEL_ID='.$channelId:'');
		return $url;
	}

	public static function SetSignatureKey($signature)
	{
		COption::SetOptionString("pull", "signature_key", $signature);

		return true;
	}

	public static function GetSignatureKey()
	{
		$url = COption::GetOptionString("pull", "signature_key", self::GetDefaultOption("signature_key"));
		return $url;
	}

	public static function GetSignatureAlgorithm()
	{
		$url = COption::GetOptionString("pull", "signature_algo", self::GetDefaultOption("signature_algo"));
		return $url;
	}

	public static function SetPublishUrl($path = "")
	{
		if (strlen($path)<=0)
		{
			$path = self::GetDefaultOption('path_to_publish');
		}
		COption::SetOptionString("pull", "path_to_publish", $path);
		return true;
	}

	public static function GetListenUrl($channelId = "")
	{
		if (!is_array($channelId) && strlen($channelId) > 0)
			$channelId = Array($channelId);
		else if (!is_array($channelId))
			$channelId = Array();

		$optionName = "path_to_modern_listener";
		$url = COption::GetOptionString("pull", $optionName, self::GetDefaultOption($optionName)).(count($channelId)>0?'?CHANNEL_ID='.implode('/', $channelId):'');
		$url = str_replace('#PORT#', self::GetQueueServerVersion()>1? '': ':8893', $url);

		return $url;
	}

	public static function SetListenUrl($path = "")
	{
		if (strlen($path)<=0)
		{
			$path = self::GetDefaultOption('path_to_modern_listener');
		}
		COption::SetOptionString("pull", 'path_to_modern_listener', $path);
		return true;
	}

	public static function GetListenSecureUrl($channelId = "")
	{
		if (!is_array($channelId) && strlen($channelId) > 0)
			$channelId = Array($channelId);
		else if (!is_array($channelId))
			$channelId = Array();

		$optionName = "path_to_modern_listener_secure";
		$url = COption::GetOptionString("pull", $optionName, self::GetDefaultOption($optionName)).(count($channelId)>0?'?CHANNEL_ID='.implode('/', $channelId):'');
		$url = str_replace('#PORT#', self::GetQueueServerVersion()>1? '': ':8894', $url);

		return $url;
	}

	public static function SetListenSecureUrl($path = "")
	{
		if (strlen($path)<=0)
		{
			$path = self::GetDefaultOption('path_to_modern_listener_secure');
		}
		COption::SetOptionString("pull", 'path_to_modern_listener_secure', $path);
		return true;
	}

	/*
	 * Get version of QueueServer
	 * 1 version - nginx-push-stream-module 0.3.4
	 * 2 version - nginx-push-stream-module 0.4.0
	 * 3 version - Bitrix Push & Pull server 1.0
	 * 4 version - Bitrix Push & Pull server 2.0 (reserved)
	 */
	public static function GetQueueServerVersion()
	{
		return intval(COption::GetOptionInt("pull", "nginx_version", self::GetDefaultOption("nginx_version")));
	}

	public static function SetQueueServerVersion($version)
	{
		COption::SetOptionInt("pull", "nginx_version", intval($version));

		return true;
	}

	public static function GetCommandPerHit()
	{
		return intval(COption::GetOptionInt("pull", "nginx_command_per_hit", self::GetDefaultOption("nginx_command_per_hit")));
	}

	public static function SetCommandPerHit($count)
	{
		COption::SetOptionInt("pull", "nginx_command_per_hit", intval($count));

		return true;
	}

	public static function GetWebSocketStatus()
	{
		return self::GetWebSocket() && self::GetQueueServerVersion()>1? true: false;
	}

	public static function GetWebSocket()
	{
		$result = false;

		if (
			CPullOptions::GetQueueServerVersion() == 3
			|| COption::GetOptionString("pull", "websocket", self::GetDefaultOption("websocket")) == 'Y'
		)
		{
			$result = true;
		}

		return $result;
	}

	public static function SetWebSocket($flag = "N")
	{
		COption::SetOptionString("pull", "websocket", $flag=='Y'?'Y':'N');
		return true;
	}

	public static function GetWebSocketUrl($channelId = "")
	{
		if (!is_array($channelId) && strlen($channelId) > 0)
			$channelId = Array($channelId);
		else if (!is_array($channelId))
			$channelId = Array();

		$url = COption::GetOptionString("pull", "path_to_websocket", self::GetDefaultOption("path_to_websocket")).(count($channelId)>0?'?CHANNEL_ID='.implode('/', $channelId):'');
		return $url;
	}

	public static function SetWebSocketUrl($path = "")
	{
		if (strlen($path)<=0)
		{
			$path = self::GetDefaultOption('path_to_websocket');
		}

		COption::SetOptionString("pull", "path_to_websocket", $path);
		return true;
	}

	public static function GetWebSocketSecureUrl($channelId = "")
	{
		if (!is_array($channelId) && strlen($channelId) > 0)
			$channelId = Array($channelId);
		else if (!is_array($channelId))
			$channelId = Array();

		$url = COption::GetOptionString("pull", "path_to_websocket_secure", self::GetDefaultOption("path_to_websocket_secure")).(count($channelId)>0?'?CHANNEL_ID='.implode('/', $channelId):'');
		return $url;
	}

	public static function SetWebSocketSecureUrl($path = "")
	{
		if (strlen($path)<=0)
		{
			$path = self::GetDefaultOption('path_to_websocket_secure');
		}

		COption::SetOptionString("pull", "path_to_websocket_secure", $path);
		return true;
	}

	/* UTILITY */

	public static function SendConfigDie()
	{
		$arMessage = Array(
			'module_id' => 'pull',
			'command' => 'config_expire',
			'params' => Array()
		);
		CPullStack::AddBroadcast($arMessage);
	}

	public static function GetDefaultOption($optionName)
	{
		if (is_null(self::$optionDefaultConfig))
		{
			$config = \Bitrix\Main\Config\Configuration::getValue('pull');
			self::$optionDefaultConfig = is_null($config) ? Array() : $config;
		}

		if (is_null(self::$optionDefaultModule))
		{
			include($_SERVER['DOCUMENT_ROOT'].BX_ROOT.'/modules/pull/default_option.php');
			self::$optionDefaultModule = $pull_default_option;
		}

		if (array_key_exists($optionName, self::$optionDefaultConfig))
		{
			return self::$optionDefaultConfig[$optionName];
		}

		return array_key_exists($optionName, self::$optionDefaultModule)? self::$optionDefaultModule[$optionName]: null;
	}

	public static function ClearCheckCache()
	{
		// init module cache
		$CModule = new CModule();
		$CModule->IsInstalled();

		CAgent::RemoveAgent("CPullOptions::ClearAgent();", "pull");
		CAgent::AddAgent("CPullOptions::ClearAgent();", "pull", "N", 30, "", "Y", ConvertTimeStamp(time()+CTimeZone::GetOffset()+30, "FULL"));
	}

	public static function ClearAgent()
	{
		global $CACHE_MANAGER;
		$CACHE_MANAGER->Clean("pull_cnr");

		if (self::ModuleEnable())
		{
			CAgent::AddAgent("CPullChannel::CheckOnlineChannel();", "pull", "N", 240, "", "Y", ConvertTimeStamp(time()+CTimeZone::GetOffset()+100, "FULL"));
			CAgent::AddAgent("CPullChannel::CheckExpireAgent();", "pull", "N", 43200, "", "Y", ConvertTimeStamp(time()+CTimeZone::GetOffset() + 43200, "FULL"));
			CAgent::AddAgent("CPullStack::CheckExpireAgent();", "pull", "N", 86400, "", "Y", ConvertTimeStamp(time()+CTimeZone::GetOffset() + 86400, "FULL"));
			CAgent::AddAgent("CPullWatch::CheckExpireAgent();", "pull", "N", 600, "", "Y", ConvertTimeStamp(time()+CTimeZone::GetOffset() + 600, "FULL"));
		}
		else
		{
			CAgent::RemoveAgent("CPullChannel::CheckOnlineChannel();", "pull");
			CAgent::RemoveAgent("CPullChannel::CheckExpireAgent();", "pull");
			CAgent::RemoveAgent("CPullStack::CheckExpireAgent();", "pull");
			CAgent::RemoveAgent("CPullWatch::CheckExpireAgent();", "pull");
			CAgent::RemoveAgent("CPushManager::SendAgent();", "pull");
		}
	}

	public static function OnEpilog()
	{
		$userId = 0;
		if (defined('PULL_USER_ID'))
		{
			$userId = PULL_USER_ID;
		}
		else if (is_object($GLOBALS['USER']) && intval($GLOBALS['USER']->GetID()) > 0)
		{
			$userId = intval($GLOBALS['USER']->GetID());
		}
		else if (IsModuleInstalled('statistic') && intval($_SESSION["SESS_SEARCHER_ID"]) <= 0 && intval($_SESSION["SESS_GUEST_ID"]) > 0 && COption::GetOptionString("pull", "guest", self::GetDefaultOption("guest")) == 'Y')
		{
			$userId = intval($_SESSION["SESS_GUEST_ID"])*-1;
		}

		if (!defined('BX_PULL_SKIP_INIT') && !(isset($_REQUEST['AJAX_CALL']) && $_REQUEST['AJAX_CALL'] == 'Y') && $userId != 0 && CModule::IncludeModule('pull'))
		{
			define("BX_PULL_SKIP_INIT", true);

			if (CPullOptions::CheckNeedRun())
			{
				CJSCore::Init(array('pull'));

				global $APPLICATION;

				if(\Bitrix\Main\Page\Frame::getInstance()->getUseAppCache())
				{
					$pullInitJs = <<<JS
					
					var pullInited = false;
					BX.bind(window, "load", function(){
						var config = BX.frameCache.getPullConfig();
						if(config != null)
						{
							pullInited = true;
							BX.PULL.start(config);
						}
					});
					
					BX.addCustomEvent("pullConfigHasBeenChanged",function(config){
						if(pullInited)
						{
							BX.PULL.updateChannelID(config);
							BX.PULL.tryConnect();
							return;
						}
						
						BX.PULL.start(config);
						
					});
JS;
					Asset::getInstance()->addString('<script type="text/javascript">'.$pullInitJs.'</script>');
				}
				else
				{
					$pullConfig = CPullChannel::GetConfig($userId);
					$APPLICATION->AddAdditionalJS('<script type="text/javascript">BX.bind(window, "load", function() { BX.PULL.start('.(empty($pullConfig)? '': CUtil::PhpToJsObject($pullConfig)).'); });</script>');

				}
			}
		}
	}
}

Zerion Mini Shell 1.0