Your IP : 108.162.241.184


Current Path : /var/www/element/data/www/greenpr.ru/bitrix/modules/main/classes/general/
Upload File :
Current File : /var/www/element/data/www/greenpr.ru/bitrix/modules/main/classes/general/component_template.php

<?
global $arBXAvailableTemplateEngines;
global $arBXRuntimeTemplateEngines;

$arBXAvailableTemplateEngines = array(
	"php" => array(
		"templateExt" => array("php"),
		"function" => ""
	)
);

$arBXRuntimeTemplateEngines = False;

class CBitrixComponentTemplate
{
	var $__name = "";
	var $__page = "";
	var $__engineID = "";

	var $__file = "";
	var $__folder = "";
	var $__siteTemplate = "";
	var $__templateInTheme = False;

	var $__component = null;

	var $__bInited = False;

	function CBitrixComponentTemplate()
	{
		$this->__bInited = False;

		$this->__file = "";
		$this->__folder = "";
	}

	/***********  GET  ***************/
	function GetName()
	{
		if (!$this->__bInited)
			return null;

		return $this->__name;
	}

	function GetPageName()
	{
		if (!$this->__bInited)
			return null;

		return $this->__page;
	}

	function GetFile()
	{
		if (!$this->__bInited)
			return null;

		return $this->__file;
	}

	function GetFolder()
	{
		if (!$this->__bInited)
			return null;

		return $this->__folder;
	}

	function GetSiteTemplate()
	{
		if (!$this->__bInited)
			return null;

		return $this->__siteTemplate;
	}

	function IsInTheme()
	{
		if (!$this->__bInited)
			return null;

		return $this->__templateInTheme;
	}

	function &GetCachedData()
	{
		$arReturn = null;

		if (!$this->__bInited)
			return $arReturn;

		$arReturn = array();

		if (StrLen($this->__folder) > 0)
		{
			$fname = $_SERVER["DOCUMENT_ROOT"].$this->__folder."/style.css";
			if (file_exists($fname))
			{
				$bOpera = (strpos($_SERVER["HTTP_USER_AGENT"], "Opera") !== false);
				$arReturn["additionalCSS"] = $this->__folder."/style.css".($bOpera? '':'?'.filemtime($fname));
			}
		}

		return $arReturn;
	}

	/***********  INIT  ***************/
	function ApplyCachedData($arData)
	{
		global $APPLICATION;

		if ($arData && is_array($arData))
		{
			if (array_key_exists("additionalCSS", $arData) && StrLen($arData["additionalCSS"]) > 0)
				$APPLICATION->SetAdditionalCSS($arData["additionalCSS"]);
		}
	}

	function InitTemplateEngines($arTemplateEngines = array())
	{
		global $arBXAvailableTemplateEngines, $arBXRuntimeTemplateEngines;

		if (array_key_exists("arCustomTemplateEngines", $GLOBALS)
			&& is_array($GLOBALS["arCustomTemplateEngines"])
			&& count($GLOBALS["arCustomTemplateEngines"]) > 0)
		{
			$arBXAvailableTemplateEngines = $arBXAvailableTemplateEngines + $GLOBALS["arCustomTemplateEngines"];
		}

		if (is_array($arTemplateEngines) && count($arTemplateEngines) > 0)
			$arBXAvailableTemplateEngines = $arBXAvailableTemplateEngines + $arTemplateEngines;

		$arBXRuntimeTemplateEngines = array();

		foreach ($arBXAvailableTemplateEngines as $engineID => $engineValue)
			foreach ($engineValue["templateExt"] as $ext)
				$arBXRuntimeTemplateEngines[$ext] = $engineID;
	}

	function Init(&$component, $siteTemplate = false)
	{
		global $arBXAvailableTemplateEngines, $arBXRuntimeTemplateEngines;

		$this->__bInited = False;

		if ($siteTemplate === False)
			$this->__siteTemplate = SITE_TEMPLATE_ID;
		else
			$this->__siteTemplate = $siteTemplate;

		if (StrLen($this->__siteTemplate) <= 0)
			$this->__siteTemplate = ".default";

		$this->__file = "";
		$this->__folder = "";

		if (!$arBXRuntimeTemplateEngines)
			$this->InitTemplateEngines();

		if (StrToLower(get_class($component)) != "cbitrixcomponent")
			return False;

		$this->__component = &$component;

		$this->__name = $this->__component->GetTemplateName();
		if (StrLen($this->__name) <= 0)
			$this->__name = ".default";

		$this->__name = preg_replace("'[\\\/]+'", "/", $this->__name);
		$this->__name = Trim($this->__name, "/");

		if (!$this->CheckName($this->__name))
			$this->__name = ".default";

		$this->__page = $this->__component->GetTemplatePage();
		if (StrLen($this->__page) <= 0)
			$this->__page = "template";

		if (!$this->__SearchTemplate())
			return False;

		$this->__GetTemplateEngine();

		$this->__bInited = True;

		return True;
	}
	
	function CheckName($name)
	{
		return preg_match("#^([A-Za-z0-9_.-]+)(/[A-Za-z0-9_.-]+)?$#i", $name);
	}

	/***********  SEARCH  ***************/
	// Search file by its path and name without extention
	function __SearchTemplateFile($path, $fileName)
	{
		global $arBXAvailableTemplateEngines, $arBXRuntimeTemplateEngines;

		if (!$arBXRuntimeTemplateEngines)
			$this->InitTemplateEngines();

		if (file_exists($_SERVER["DOCUMENT_ROOT"].$path."/".$fileName.".php")
			&& is_file($_SERVER["DOCUMENT_ROOT"].$path."/".$fileName.".php"))
		{
			return $fileName.".php";
		}
		else
		{
			// Look at glob() function for PHP >= 4.3.0 !!!

			foreach ($arBXRuntimeTemplateEngines as $templateExt => $engineID)
			{
				if ($templateExt == "php")
					continue;

				if (file_exists($_SERVER["DOCUMENT_ROOT"].$path."/".$fileName.".".$templateExt)
					&& is_file($_SERVER["DOCUMENT_ROOT"].$path."/".$fileName.".".$templateExt))
				{
					return $fileName.".".$templateExt;
				}
			}
		}

		return False;
	}

	function __SearchTemplate()
	{
		$this->__file = "";
		$this->__folder = "";

		$arFolders = array();
		$relativePath = $this->__component->GetRelativePath();

		$parentComponent = & $this->__component->GetParent();
		if ($parentComponent)
		{
			$parentRelativePath = $parentComponent->GetRelativePath();
			$parentTemplate = & $parentComponent->GetTemplate();
			$parentTemplateName = $parentTemplate->GetName();

			$arFolders[] = BX_PERSONAL_ROOT."/templates/".$this->__siteTemplate."/components".$parentRelativePath."/".$parentTemplateName.$relativePath;
			$arFolders[] = BX_PERSONAL_ROOT."/templates/.default/components".$parentRelativePath."/".$parentTemplateName.$relativePath;
			$arFolders[] = "/bitrix/components".$parentRelativePath."/templates/".$parentTemplateName.$relativePath;
		}
		$arFolders[] = BX_PERSONAL_ROOT."/templates/".$this->__siteTemplate."/components".$relativePath;
		$arFolders[] = BX_PERSONAL_ROOT."/templates/.default/components".$relativePath;
		$arFolders[] = "/bitrix/components".$relativePath."/templates";

		for ($i = 0, $cnt = count($arFolders); $i < $cnt; $i++)
		{
			if (file_exists($_SERVER["DOCUMENT_ROOT"].$arFolders[$i]."/".$this->__name))
			{
				if (is_dir($_SERVER["DOCUMENT_ROOT"].$arFolders[$i]."/".$this->__name))
				{
					if ($templatePageFile = $this->__SearchTemplateFile($arFolders[$i]."/".$this->__name, $this->__page))
					{
						$this->__file = $arFolders[$i]."/".$this->__name."/".$templatePageFile;
						$this->__folder = $arFolders[$i]."/".$this->__name;
					}
				}
				elseif (is_file($_SERVER["DOCUMENT_ROOT"].$arFolders[$i]."/".$this->__name))
				{
					$this->__file = $arFolders[$i]."/".$this->__name;
					if (StrPos($this->__name, "/") !== False)
						$this->__folder = $arFolders[$i]."/".SubStr($this->__name, 0, bxstrrpos($this->__name, "/"));
				}
			}
			else
			{
				if ($templatePageFile = $this->__SearchTemplateFile($arFolders[$i], $this->__name))
					$this->__file = $arFolders[$i]."/".$templatePageFile;
			}

			if (StrLen($this->__file) > 0)
			{
				if ($i == 0 || $i == 3)
					$this->__siteTemplate = $this->__siteTemplate;
				elseif ($i == 1 || $i == 4)
					$this->__siteTemplate = ".default";
				else
					$this->__siteTemplate = "";

				if ($parentComponent && $i < 3)
					$this->__templateInTheme = True;
				else
					$this->__templateInTheme = False;

				break;
			}
		}

		return (StrLen($this->__file) > 0);
	}

	/***********  INCLUDE  ***************/
	function __IncludePHPTemplate(&$arResult, &$arParams, $parentTemplateFolder = "")
	{
		global $APPLICATION, $USER, $DB;

		if (!$this->__bInited)
			return False;

		$templateName = $this->__name;
		$templateFile = $this->__file;
		$templateFolder = $this->__folder;

		$componentPath = $this->__component->GetPath();

		$component = & $this->__component;

		include($_SERVER["DOCUMENT_ROOT"].$this->__file);
	}

	function IncludeTemplate($arResult)
	{
		global $arBXAvailableTemplateEngines;

		if (!$this->__bInited)
			return False;

		$arParams = $this->__component->arParams;

		if (StrLen($this->__folder) > 0)
		{
			$this->__IncludeMutatorFile($arResult, $arParams);
			$this->__IncludeCSSFile();
			$this->__IncludeJSFile();
			$arLangMessages = $this->IncludeLangFile();
		}

		$parentTemplateFolder = "";
		$parentComponent = & $this->__component->GetParent();
		if ($parentComponent)
		{
			$parentTemplate = & $parentComponent->GetTemplate();
			$parentTemplateFolder = $parentTemplate->GetFolder();
		}

		if (StrLen($arBXAvailableTemplateEngines[$this->__engineID]["function"]) > 0
			&& function_exists($arBXAvailableTemplateEngines[$this->__engineID]["function"]))
		{
			$result = call_user_func(
				$arBXAvailableTemplateEngines[$this->__engineID]["function"],
				$this->__file,
				$arResult,
				$arParams,
				$arLangMessages,
				$this->__folder,
				$parentTemplateFolder,
				$this
			);
		}
		else
		{
			$result = $this->__IncludePHPTemplate($arResult, $arParams, $parentTemplateFolder);
		}

		return $result;
	}

	function __IncludeLangFile($path)
	{
		$MESS = array();

		if (file_exists($path))
			include($path);

		$MESS1 =& $MESS;

		global $MESS, $ALL_LANG_FILES;

		$ALL_LANG_FILES[] = $path;
		$MESS = $MESS1 + $MESS;

		return $MESS1;
	}

	function IncludeLangFile()
	{
		$arLangMessages = array();

		if (StrLen($this->__folder) > 0)
		{
			/*if (file_exists($_SERVER["DOCUMENT_ROOT"].$this->__folder."/lang")
				&& is_dir($_SERVER["DOCUMENT_ROOT"].$this->__folder."/lang"))
			{*/
				$templateFileName = SubStr($this->__file, bxstrrpos($this->__file, "/") + 1);
				$templateFileName = SubStr($templateFileName, 0, bxstrrpos($templateFileName, "."));

				if (LANGUAGE_ID != "en" && LANGUAGE_ID != "ru" /*&& file_exists($_SERVER["DOCUMENT_ROOT"].$this->__folder."/lang/en") && is_dir($_SERVER["DOCUMENT_ROOT"].$this->__folder."/lang/en")*/)
					$arLangMessages = $this->__IncludeLangFile($_SERVER["DOCUMENT_ROOT"].$this->__folder."/lang/en/".$templateFileName.".php");

				$arLangMessages = $this->__IncludeLangFile($_SERVER["DOCUMENT_ROOT"].$this->__folder."/lang/".LANGUAGE_ID."/".$templateFileName.".php") + $arLangMessages;
			/*}*/
		}

		return $arLangMessages;
	}

	function __IncludeMutatorFile(&$arResult, &$arParams)
	{
		global $APPLICATION, $USER, $DB;

		if (StrLen($this->__folder) > 0)
		{
			if (file_exists($_SERVER["DOCUMENT_ROOT"].$this->__folder."/result_modifier.php"))
				include($_SERVER["DOCUMENT_ROOT"].$this->__folder."/result_modifier.php");
		}
	}

	function __IncludeCSSFile()
	{
		global $APPLICATION;

		if (StrLen($this->__folder) > 0)
		{
			$fname = $_SERVER["DOCUMENT_ROOT"].$this->__folder."/style.css";
			if (file_exists($fname))
			{
				$bOpera = (strpos($_SERVER["HTTP_USER_AGENT"], "Opera") !== false);
				$APPLICATION->SetAdditionalCSS($this->__folder."/style.css".($bOpera? '':'?'.filemtime($fname)));
			}
		}
	}

	function __IncludeJSFile()
	{
		if (StrLen($this->__folder) > 0)
		{
			$fname = $_SERVER["DOCUMENT_ROOT"].$this->__folder."/script.js";
			if (file_exists($fname))
			{
				$bOpera = (strpos($_SERVER["HTTP_USER_AGENT"], "Opera") !== false);
				echo "<script src=\"".$this->__folder."/script.js".($bOpera? '':'?'.filemtime($fname))."\" type=\"text/javascript\"></script>";
			}
		}
	}

	/***********  UTIL  ***************/
	function __GetTemplateExtension($templateName)
	{
		$templateName = trim($templateName, ". \r\n\t");
		$arTemplateName = explode(".", $templateName);
		return strtolower($arTemplateName[count($arTemplateName) - 1]);
	}

	function __GetTemplateEngine()
	{
		global $arBXRuntimeTemplateEngines;

		if (!$arBXRuntimeTemplateEngines)
			$this->InitTemplateEngines();

		$templateExt = $this->__GetTemplateExtension($this->__file);

		if (array_key_exists($templateExt, $arBXRuntimeTemplateEngines))
			$this->__engineID = $arBXRuntimeTemplateEngines[$templateExt];
		else
			$this->__engineID = "php";
	}
}
?>