Skip to content

Commit

Permalink
up to 0.1.33
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfriend committed Jul 5, 2019
1 parent 7b64797 commit e924e4e
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 112 deletions.
121 changes: 69 additions & 52 deletions utf8/dev2fun.multidomain/classes/general/SubDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @package subdomain
* @author darkfriend
* @version 0.1.27
* @version 0.1.33
*/

namespace Dev2fun\MultiDomain;
Expand Down Expand Up @@ -34,6 +34,7 @@ class SubDomain

private $cookieKey = 'subdomain';
private $mainHost;
private $httpHost = '';
/**
* Default value for default site
* @var string
Expand Down Expand Up @@ -68,6 +69,12 @@ public static function getInstance()
return self::$instance;
}

public function getHttpHost() {
if(!$this->httpHost)
$this->httpHost = preg_replace('#(\:\d+)#','',$_SERVER['HTTP_HOST']);
return $this->httpHost;
}

/**
* check subdomain
* @param bool $enable
Expand All @@ -82,11 +89,12 @@ public function check($enable = true, $params = ['cacheTime' => 3600, 'cacheID'
$moduleId = Base::$module_id;
$subHost = '';

$arNames = explode('.', $_SERVER['HTTP_HOST']);
$arNames = explode('.', $this->getHttpHost());
//var_dump($arNames);
$cntNames = count($arNames);
switch ($cntNames) {
case 2 :
$host = $_SERVER['HTTP_HOST'];
$host = $this->getHttpHost();
break;
default :
$arNames = array_reverse($arNames);
Expand All @@ -106,15 +114,23 @@ public function check($enable = true, $params = ['cacheTime' => 3600, 'cacheID'

$hl = HLHelpers::getInstance();
$hlDomain = $config->get('highload_domains');
/*if(isset($_COOKIE['ADMIN'])) {
var_dump($hlDomain);
//die();
}*/
$this->domains = $hl->getElementList($hlDomain, [
'UF_DOMAIN' => $host,
'UF_SUBDOMAIN' => $subHost,
// 'UF_ACTIVE' => 'Y',
// 'UF_ACTIVE' => 'Y',
]);
/*if(isset($_COOKIE['ADMIN'])) {
var_dump($this->domains);
die();
}*/
if (!$this->domains) return;

$this->mainHost = $host;
// $arDomainToLang = [];
// $arDomainToLang = [];
foreach ($this->domains as $key => $domain) {
$subDomain = '';
if ($domain['UF_SUBDOMAIN']) {
Expand All @@ -124,21 +140,21 @@ public function check($enable = true, $params = ['cacheTime' => 3600, 'cacheID'
$this->domains[$subDomain . $domain['UF_DOMAIN']] = $domain;
unset($this->domains[$key]);
}
if (!$this->isSupportHost($_SERVER['HTTP_HOST'])) {
if (!$this->isSupportHost($this->getHttpHost())) {
\CHTTP::SetStatus('404 Not Found');
}

if ($config->get('logic_subdomain') != 'virtual') {
if ($this->domainToLang[$_SERVER['HTTP_HOST']] == 'redirect') {
if ($this->domainToLang[$this->getHttpHost()] == 'redirect') {
$this->redirectDomainProcess();
}
} else {
$this->subdomain = $this->getSubDomain();
}

if (isset($this->domains[$_SERVER['HTTP_HOST']])) {
$this->currentDomain = $this->domains[$_SERVER['HTTP_HOST']];
$this->subdomain = $this->domainToLang[$_SERVER['HTTP_HOST']];
if (isset($this->domains[$this->getHttpHost()])) {
$this->currentDomain = $this->domains[$this->getHttpHost()];
$this->subdomain = $this->domainToLang[$this->getHttpHost()];
}

$GLOBALS[$this->getGlobalKey()] = $this->subdomain;
Expand All @@ -156,25 +172,25 @@ public function check($enable = true, $params = ['cacheTime' => 3600, 'cacheID'
$this->setCookie($lang, $this->globalLangKey);
}

// $cookie = $APPLICATION->get_cookie($this->cookieKey);
// if($cookie) {
// $cookie = mb_strtolower(htmlspecialcharsbx($cookie));
// if($cookie==$this->domainToLang[$_SERVER['HTTP_HOST']]) {
// $this->subdomain = $cookie;
// return $this->subdomain;
// }
// }

// if($enable && $this->subdomain = $this->getCache($params)){
// $GLOBALS[$this->getGlobalKey()] = $this->subdomain; //SUBDOMAIN
// return $this->subdomain;
// }
// $this->subdomain = $this->match();
// if($this->subdomain) {
// $this->setCache($this->subdomain, $params);
// }
// $GLOBALS[$this->getGlobalKey()] = $this->subdomain;
// $this->setLanguage($this->subdomain);
// $cookie = $APPLICATION->get_cookie($this->cookieKey);
// if($cookie) {
// $cookie = mb_strtolower(htmlspecialcharsbx($cookie));
// if($cookie==$this->domainToLang[$_SERVER['HTTP_HOST']]) {
// $this->subdomain = $cookie;
// return $this->subdomain;
// }
// }

// if($enable && $this->subdomain = $this->getCache($params)){
// $GLOBALS[$this->getGlobalKey()] = $this->subdomain; //SUBDOMAIN
// return $this->subdomain;
// }
// $this->subdomain = $this->match();
// if($this->subdomain) {
// $this->setCache($this->subdomain, $params);
// }
// $GLOBALS[$this->getGlobalKey()] = $this->subdomain;
// $this->setLanguage($this->subdomain);
return $this->currentDomain;
}

Expand Down Expand Up @@ -236,7 +252,7 @@ public function setGlobal($key, $subdomain)
*/
public function redirectDomainProcess($redirect = true) {
global $APPLICATION;
// $config = Config::getInstance();
// $config = Config::getInstance();
$currentPage = $APPLICATION->GetCurUri();

$subdomain = $this->searchSubdomain();
Expand All @@ -250,10 +266,10 @@ public function redirectDomainProcess($redirect = true) {
}
}
}
// var_dump($subdomain);die();
// $currentDomain = $this->getSubDomainByList($subdomain);
// var_dump($subdomain);die();
// $currentDomain = $this->getSubDomainByList($subdomain);
$this->setCookie($subdomain);
// $APPLICATION->set_cookie($this->cookieKey,$subdomain,time()+3600*30*12,'/','*.'.$this->mainHost);
// $APPLICATION->set_cookie($this->cookieKey,$subdomain,time()+3600*30*12,'/','*.'.$this->mainHost);
$url = $this->getProtocol() . '://' . $subdomain . '.' . $this->mainHost . $currentPage;
if ($redirect) LocalRedirect($url);
return $url;
Expand All @@ -270,16 +286,16 @@ public function getFullDomain($subDomain,$mainHost=null) {
* @return string
*/
public function getSubDomain() {
// $subdomain = $this->getCookie();
// if (!$subdomain) {
// $subdomain = $this->getCookie();
// if (!$subdomain) {
$subdomain = $this->searchSubdomain();
$fullDomain = $this->getFullDomain($subdomain);
// }
// var_dump($fullDomain, $this->domainToLang);
// $config = Config::getInstance();
// }
// var_dump($fullDomain, $this->domainToLang);
// $config = Config::getInstance();
if (!in_array($fullDomain, $this->domainToLang)) {
return false;
// $fullDomain = $config->get('domain_default');
// $fullDomain = $config->get('domain_default');
}
return $subdomain;
}
Expand Down Expand Up @@ -317,7 +333,7 @@ public function getCurrent() {
* @return boolean
*/
private function getCache($params) {
if (!$params['cacheID']) $params['cacheID'] = md5($_SERVER['HTTP_HOST']);
if (!$params['cacheID']) $params['cacheID'] = md5($this->getHttpHost());
if (!$params['cacheInit']) $params['cacheInit'] = '/dev2fun.multidomain/';
$oCache = new \CPHPCache();
if ($oCache->initCache($params['cacheTime'], $params['cacheID'], $params['cacheInit'])) {
Expand All @@ -333,7 +349,7 @@ private function getCache($params) {
*/
private function setCache($data, $params=[]) {
$oCache = new \CPHPCache();
if (!$params['cacheID']) $params['cacheID'] = md5($_SERVER['HTTP_HOST']);
if (!$params['cacheID']) $params['cacheID'] = md5($this->getHttpHost());
if (!$params['cacheInit']) $params['cacheInit'] = '/dev2fun.multidomain/';
$oCache->StartDataCache($params['cacheTime'], $params['cacheID'], $params['cacheInit']);
$oCache->EndDataCache((array)$data);
Expand All @@ -345,7 +361,7 @@ private function setCache($data, $params=[]) {
*/
public function match()
{
$host = $_SERVER['HTTP_HOST'];
$host = $this->getHttpHost();
$mainHost = $this->getMainHost();
$host = str_replace($mainHost, '', $host);
if (!$host) return $this->defaultVal;
Expand Down Expand Up @@ -502,19 +518,20 @@ public function getLangList()
* Get list other languages and exclude current lang
* @return array
*/
// public static function GetOtherLang(){
// $oLang = new CSubdomain();
// if (!$oLang->otherLangs) {
// $listLang = $oLang->getLangList();
// array_splice($listLang, array_search($GLOBALS['lang'], $listLang), 1);
// $oLang->otherLangs = $listLang;
// }
// return $oLang->otherLangs;
// }
// public static function GetOtherLang(){
// $oLang = new CSubdomain();
// if (!$oLang->otherLangs) {
// $listLang = $oLang->getLangList();
// array_splice($listLang, array_search($GLOBALS['lang'], $listLang), 1);
// $oLang->otherLangs = $listLang;
// }
// return $oLang->otherLangs;
// }

public function getProperties($hlId)
{
$host = $_SERVER['HTTP_HOST'];
$host = $this->getHttpHost();
//$host = $_SERVER['HTTP_HOST'];
$arHost = explode('.', $host);
if (count($arHost) > 2) {
$subHost = $arHost[0];
Expand Down
28 changes: 28 additions & 0 deletions utf8/dev2fun.multidomain/classes/general/TemplateSeo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* @author dev2fun (darkfriend)
* @copyright darkfriend
* @version 0.1.32
*/

namespace Dev2fun\MultiDomain;


class TemplateSeo extends \Bitrix\Iblock\Template\Functions\FunctionBase
{
/**
* Обработчик события на вход получает имя требуемой функции
* @example {=get_city}
* @param \Bitrix\Main\Event $event
* @return string
*/
public static function EventHandler(\Bitrix\Main\Event $event) {
$parameters = $event->getParameters();
$functionName = $parameters[0];
if ($functionName === "get_city") {
return \Dev2fun\MultiDomain\Base::GetCurrentDomain()['UF_NAME'];
} else {
return '';
}
}
}
3 changes: 2 additions & 1 deletion utf8/dev2fun.multidomain/include.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @author dev2fun (darkfriend)
* @copyright darkfriend
* @version 0.1.31
* @version 0.1.32
*/
namespace Dev2fun\MultiDomain;

Expand All @@ -29,6 +29,7 @@
'Dev2fun\MultiDomain\Geo' => 'classes/general/Geo.php',
'Dev2fun\MultiDomain\HLHelpers' => 'lib/HLHelpers.php',
'Dev2fun\MultiDomain\Config' => 'classes/general/Config.php',
'Dev2fun\MultiDomain\TemplateSeo' => 'classes/general/TemplateSeo.php',
)
);

Expand Down
4 changes: 3 additions & 1 deletion utf8/dev2fun.multidomain/install/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* @author dev2fun (darkfriend)
* @copyright darkfriend
* @version 0.1.24
* @version 0.1.32
*/
if(class_exists("dev2fun_multidomain")) return;

Expand Down Expand Up @@ -382,6 +382,7 @@ public function registerEvents() {
$eventManager->registerEventHandler("main", "OnPageStart", $this->MODULE_ID, "Dev2fun\\MultiDomain\\Base", "InitDomains");
$eventManager->registerEventHandler("main", "OnEpilog", $this->MODULE_ID, "Dev2fun\\MultiDomain\\Base", "InitSeoDomains");
$eventManager->registerEventHandler("main", "OnEndBufferContent", $this->MODULE_ID, "Dev2fun\\MultiDomain\\Base", "InitBufferContent");
$eventManager->registerEventHandler("iblock", "OnTemplateGetFunctionClass", $this->MODULE_ID, "Dev2fun\\MultiDomain\\TemplateSeo", "EventHandler");

return true;
}
Expand Down Expand Up @@ -461,6 +462,7 @@ public function unRegisterEvents() {
$eventManager->unRegisterEventHandler('main','OnPageStart',$this->MODULE_ID);
$eventManager->unRegisterEventHandler('main','OnEpilog',$this->MODULE_ID);
$eventManager->unRegisterEventHandler('main','OnEndBufferContent',$this->MODULE_ID);
$eventManager->unRegisterEventHandler('iblock','OnTemplateGetFunctionClass',$this->MODULE_ID);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions utf8/dev2fun.multidomain/install/version.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
$arModuleVersion = array(
"VERSION" => "0.1.31",
"VERSION_DATE" => "2019-03-04 10:00:00",
"VERSION" => "0.1.33",
"VERSION_DATE" => "2019-07-06 10:00:00",
);
?>
Loading

0 comments on commit e924e4e

Please sign in to comment.