Skip to content

Commit

Permalink
up to 0.1.35
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfriend committed Jul 9, 2019
1 parent eea1e9c commit e3af9d7
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 178 deletions.
172 changes: 85 additions & 87 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.34
* @version 0.1.35
*/

namespace Dev2fun\MultiDomain;
Expand Down Expand Up @@ -63,8 +63,7 @@ class SubDomain
* Singleton instance.
* @return self
*/
public static function getInstance()
{
public static function getInstance() {
if (!self::$instance) {
self::$instance = new self();
}
Expand Down Expand Up @@ -92,7 +91,6 @@ public function check($enable = true, $params = ['cacheTime' => 3600, 'cacheID'
$subHost = '';

$arNames = explode('.', $this->getHttpHost());
//var_dump($arNames);
$cntNames = count($arNames);
switch ($cntNames) {
case 2 :
Expand All @@ -116,19 +114,11 @@ 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',
]);
/*if(isset($_COOKIE['ADMIN'])) {
var_dump($this->domains);
die();
}*/
if (!$this->domains) return;

$this->mainHost = $host;
Expand All @@ -142,15 +132,17 @@ public function check($enable = true, $params = ['cacheTime' => 3600, 'cacheID'
$this->domains[$subDomain . $domain['UF_DOMAIN']] = $domain;
unset($this->domains[$key]);
}
if (!$this->isSupportHost($this->getHttpHost())) {
\CHTTP::SetStatus('404 Not Found');
}
// if (!$this->isSupportHost($this->getHttpHost())) {
// \CHTTP::SetStatus('404 Not Found');
// }

if ($config->get('logic_subdomain') != 'virtual') {
if ($this->domainToLang[$this->getHttpHost()] == 'redirect') {
$u = $this->redirectDomainProcess();
if(!$u) return;
}
} elseif($config->get('logic_subdomain') == 'virtual' && $this->domainToLang[$this->getHttpHost()] == 'redirect') {
return $this->virtualDomainProcess();
} else {
$this->subdomain = $this->getSubDomain();
}
Expand All @@ -161,49 +153,42 @@ public function check($enable = true, $params = ['cacheTime' => 3600, 'cacheID'
}

$GLOBALS[$this->getGlobalKey()] = $this->subdomain;

$this->setCookie($this->subdomain);
$this->checkLang();

// if ($config->get('enable_multilang') == 'Y') {
// if ($this->currentDomain['UF_LANG']) {
// $lang = $this->currentDomain['UF_LANG'];
// } else {
// $lang = $config->get('lang_default');
// }
// $this->setLanguage($lang);
// $GLOBALS[$this->globalLangKey] = $lang;
// $this->setCookie($lang, $this->globalLangKey);
// }

if ($config->get('enable_multilang') == 'Y') {
return $this->currentDomain;
}

public function checkLang() {
if (Config::getInstance()->get('enable_multilang') == 'Y') {
if ($this->currentDomain['UF_LANG']) {
$lang = $this->currentDomain['UF_LANG'];
} else {
$lang = $config->get('lang_default');
$lang = Config::getInstance()->get('lang_default');
}
$this->setLanguage($lang);
$GLOBALS[$this->globalLangKey] = $lang;
$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);
return $this->currentDomain;
}

/**
* Записывает куку
* @param string $cookieKey
* @return void
*/
public function setCookie($subdomain, $cookieKey = null)
{
public function setCookie($subdomain, $cookieKey = null) {
global $APPLICATION;
if (!$cookieKey) $cookieKey = $this->cookieKey;
$APPLICATION->set_cookie($cookieKey, $subdomain, time() + 3600 * 30 * 12, '/', '*.' . $this->mainHost);
Expand All @@ -214,8 +199,7 @@ public function setCookie($subdomain, $cookieKey = null)
* @param string $cookieKey
* @return string
*/
public function getCookie($cookieKey = null)
{
public function getCookie($cookieKey = null) {
global $APPLICATION;
if (!$cookieKey) $cookieKey = $this->cookieKey;
return $APPLICATION->get_cookie($cookieKey);
Expand All @@ -226,25 +210,22 @@ public function getCookie($cookieKey = null)
* @param string $url
* @return string
*/
public function getParentHost($url)
{
public function getParentHost($url) {
$host = '';
if (preg_match('#(\w+\.\w+)$#', $url, $match)) {
$host = $match[1];
}
return $host;
}

public function getProtocol()
{
public function getProtocol() {
if (\CMain::IsHTTPS()) {
return 'https';
}
return 'http';
}

public function setGlobal($key, $subdomain)
{
public function setGlobal($key, $subdomain) {
$GLOBALS[$key] = $subdomain;
}

Expand Down Expand Up @@ -295,6 +276,50 @@ public function redirectDomainProcess($redirect = true) {
return $url;
}

public function virtualDomainProcess() {
global $APPLICATION;
// $config = Config::getInstance();
$currentPage = $APPLICATION->GetCurUri();

$subdomain = $this->searchSubdomain();
$subDomainMaps = Config::getInstance()->get('mapping_list');
if($subDomainMaps) {
$subDomainMaps = unserialize($subDomainMaps);
foreach ($subDomainMaps as $subDomainMap) {
if($subDomainMap['KEY']==$subdomain) {
$subdomain = $subDomainMap['SUBNAME'];
break;
}
}
}

$isSupport = false;
$redirectHost = "$subdomain.{$this->mainHost}";
$supportDomains = $this->getDomainList();
if($supportDomains) {
foreach ($supportDomains as $sValue) {
$host = '';
if($sValue['UF_SUBDOMAIN'])
$host .= "{$sValue['UF_SUBDOMAIN']}.";
if($sValue['UF_DOMAIN'])
$host .= $sValue['UF_DOMAIN'];
if($host==$redirectHost) {
$isSupport = true;
$this->domains = array($host=>$sValue);
$this->currentDomain = $sValue;
$this->subdomain = $sValue['UF_LANG'];
$this->setCookie($subdomain);
$this->checkLang();
break;
}
}
}

if ($this->strictMode && !$isSupport) return;

return $isSupport;
}

public function getFullDomain($subDomain,$mainHost=null) {
if(!$mainHost) $mainHost = $this->mainHost;
if($subDomain) $subDomain = $subDomain . '.';
Expand Down Expand Up @@ -379,8 +404,7 @@ private function setCache($data, $params=[]) {
* regexp subdomain
* @return string|false
*/
public function match()
{
public function match() {
$host = $this->getHttpHost();
$mainHost = $this->getMainHost();
$host = str_replace($mainHost, '', $host);
Expand All @@ -394,8 +418,7 @@ public function match()
* Get main server host from bitrix setting
* @return string
*/
public function getMainHost()
{
public function getMainHost() {
if ($this->csite) {
$host = $this->csite['SERVER_NAME'];
} else {
Expand Down Expand Up @@ -458,44 +481,39 @@ public function isSupportHost($host, $checkFile = false)
* get name subdomain
* @return string|false
*/
public function get()
{
public function get() {
return $this->subdomain;
}

/**
* get name subdomain
* @return string|false
*/
public static function GetDomain()
{
public static function GetDomain() {
return $GLOBALS[(new SubDomain())->getGlobalKey()];
}

/**
* set name subdomain
* @param string $subdomain
*/
public function set($subdomain)
{
public function set($subdomain) {
$GLOBALS[$this->getGlobalKey()] = $this->subdomain = $subdomain;
}

/**
* Get key for $GLOBALS
* @return string
*/
public function getGlobalKey()
{
public function getGlobalKey() {
return $this->globalKey;
}

/**
* Set language
* @param string $lang
*/
public function setLanguage($lang)
{
public function setLanguage($lang) {
if ($lang) {
Loc::setCurrentLang($lang);
$application = \Bitrix\Main\Application::getInstance();
Expand All @@ -508,46 +526,26 @@ public function setLanguage($lang)
* Get default lang
* @return string
*/
public function getDefaultLang()
{
public function getDefaultLang() {
return $this->defaultVal;
}

/**
* Get default lang
* @return string
*/
public static function DefaultLang()
{
public static function DefaultLang() {
return (new SubDomain())->defaultVal;
}

/**
* Get active lang list
* @return array
*/
public function getLangList()
{
return [
'en',
'ru',
];
public function getLangList() {
return ['en', 'ru'];
}

/**
* 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 function getProperties($hlId)
{
$host = $this->getHttpHost();
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.34",
"VERSION_DATE" => "2019-07-09 10:00:00",
"VERSION" => "0.1.35",
"VERSION_DATE" => "2019-07-11 10:00:00",
);
?>
Loading

0 comments on commit e3af9d7

Please sign in to comment.