meraproject/module/core/site/model.php

373 lines
14 KiB
PHP
Raw Permalink Normal View History

<?
use core\core\module\Installed;
use ms\Portal;
use ms\portal\Section;
use core\site\Theme;
use ms\portal\Domain;
use core\site\structure\HTMLPage;
use ms\portal\Verify;
class Site {
static $URL = [];
static $absPath = '';
static $themeAbsPath = '';
static $themeIndex = '';
static $justContent = false;
static $domen = '';
static $protocol = '';
static $host = '';
static $portal = null;
static $section = null;
static $portal_id = 0;
static $section_id = 0;
// Используется для пути к папке
//static $theme = 'bws.oneway';
//static $theme = 'projectman';
// Отображаемый url темы
//static $themeUrl = '/';
//static $theme = 'msto';
//static $theme = 'bws.sova';
//static $themeUrl = 'bws.sova';
/** @var Theme */
static $theme = null;
static $themeName = '';
static $themeUrl = '';
static $sectionUrl = '/';
/*sova
static $theme = 'bws.sova';
static $themeUrl = '/';
static $sectionUrl = '/';
/**/
static $module_id = null;
static $owner_id = 0;
static function toObject(){
}
static function portalUrl(string $url = '',bool $isFull = false){
return ($isFull?self::$domen:'/').$url;
}
static function sectionUrl(string $url = '',bool $isFull = false){
return ($isFull?self::$domen:'').self::$sectionUrl.$url;
}
static function themeUrl(string $url,bool $isFull = false){
return ($isFull?self::$domen:'').self::$themeAbsPath.$url;
}
static function themeFullPath(string $url){
return Core::$FOLDER.substr(self::$themeAbsPath,1).$url;
}
static function header404(){
header("HTTP/1.0 404 Not Found");
}
static function redirect301($link){
if(\Site::$justContent){
HTMLPage::$redirect = $link;
} else {
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$link);
}
}
static function redirect($link){
if(\Site::$justContent){
HTMLPage::$redirect = $link;
} else {
header("Location: ".$link);
}
}
static function reload(){
header("Refresh:0");
}
static function init() {
$portal = null;
self::$justContent = (isset($_REQUEST['justContent']) && $_REQUEST['justContent'] == 1);
self::$protocol = 'http'.(isset($_SERVER['HTTPS'])?'s':'').'://';
self::$domen = self::$protocol.$_SERVER['HTTP_HOST'];
self::$host = $_SERVER['HTTP_HOST'];
$a = parse_url($_SERVER['REQUEST_URI']);
$b = explode("/", $a['path']);
$arr = [];
foreach ($b as $v) {
if ($v) $arr[] = (string)$v;
}
if (!isset($arr[0]) || $arr[0] === '') {
$arr[0] = '';
}
$x = Installed::getByUrl($arr[0]);
$isModuleSite = false;
/*
if($x){
//
self::$theme = $x[Installed::$THEME];
self::$themeUrl = '/'.$x[Installed::$URL].'/';
self::$module_id = $x[Installed::$ID];
array_shift($arr);
} else
*/
if (count($arr)) {
$C = null;
// Сначала адрес проверяем на регулярку
$a = explode(".",$_SERVER['HTTP_HOST']);
if(count($a)==3){
if(substr($a[0],0,4)=='site'){
$id = intval(substr($a[0],4));
if($id) {
$C = Portal::getByID($id);
$isModuleSite = true;
}
}
}
/*
echo '<pre>';
echo var_dump(\DB::$DB,MYSQL_ADMIN,MYSQL_BASE);
echo '</pre>';
*/
// Потом проверяем, а какой у нас домен.
//if(count($a)==2 and !in_array($_SERVER['HTTP_HOST'],['module.bz','cmsbuilder.in'])){
if(!$C) {
if (count($a) == 2
|| (count($a) == 3 && $a[0] == 'www')
|| (count($a) == 3 )
|| (count($a) == 4 && is_numeric($a[3])) // Типа ip
) {
if ((count($a) == 3 && $a[0] == 'www')) {
array_shift($a);
\Site::redirect301(self::$protocol . implode(".", $a).$_SERVER['REQUEST_URI']);
}
if (count($a) == 3 ) {
$r = Domain::select([
\Query::WHERE => new \Where(\Where::_or([
\Where::_operator(Domain::$DOMAIN, '=', $a[1].'.'.$a[2]),
//\Where::_operator(Domain::$DOMAIN, '=', $a[0].'.'.$a[1].'.'.$a[2]),
])
),
\Query::COUNT => 1,
], true);
if ($r) {
$id = array_pop($r)[Domain::$PORTAL];
if ($id) {
$C = Portal::getByID($id);
}
}
}
// Если это главный портал, и если это админка, то идём в админку
if ((in_array($_SERVER['HTTP_HOST'], ['module.bz', 'module.in']) and $arr[0] == Config::$ADMIN_PAGE) or $arr[0] == Config::$ADMIN_PAGE) {
} else {
$r = Domain::select([
\Query::WHERE => new \Where(\Where::_operator(Domain::$DOMAIN, '=', $_SERVER['HTTP_HOST'])),
\Query::COUNT => 1,
], true);
if ($r) {
$id = array_pop($r)[Domain::$PORTAL];
if ($id) {
$C = Portal::getByID($id);
}
}
}
}
}
if(in_array($_SERVER['HTTP_HOST'],\Core::$CLIENT_DOMAINS) and ($arr[0]!=Config::$ADMIN_PAGE) ){
$C = Portal::getByID($_SERVER['HTTP_HOST']=='module.bz'?10:1);
}
if($C) {
self::$portal = $C;
$portal = $C;
self::$portal_id = $portal_id = $C[Portal::$ID];
// Тут получаем информацию о теме в зависимости от того, в каком мы разделе
$section = null;
if(isset($_POST['_section_path'])){
$section = Section::getByPath($_POST['_section_path'],$portal_id);
}
if(!$section) {
$section = Section::getByPath($arr[0],$portal_id);
}
if(!$section) {
$section = Section::getByPath("",$portal_id);
}
self::$section = $section;
self::$section_id = $section[Section::$ID];
self::$owner_id = $section[Section::$ACCOUNT];
$theme = Theme::init( $section[Section::$THEME] , json_decode( $section[Section::$DATA] ,true ), self::$owner_id );
Profile::init($portal_id);
self::$theme = $theme;
self::$themeName = strtolower($theme::$name);
self::$themeUrl = $theme::$themeUrl;
self::$sectionUrl = '/'.(($c = $section[Section::$PATH])?$c.'/':'');
if($section[Section::$PATH]){
array_shift($arr);
}
$theme::prepare();
//Profile::init($portal_id);
//Theme::prepare();
//$theme::prepare();
}
// Если это не портал
if(! $portal ) {
switch ($arr[0]) {
case 'pm':
//self::$theme = 'pm';
self::$themeUrl = '/pm/';
self::$sectionUrl = '/pm/';
array_shift($arr);
break;
case Config::$ADMIN_PAGE:
//self::$theme = 'admin';
self::$themeUrl = '/admin/';
array_shift($arr);
break;
case 'msto':
//self::$theme = 'msto';
self::$themeUrl = '/msto/';
array_shift($arr);
break;
case 'sova':
//self::$theme = 'bws.sova';
self::$themeUrl = '/sova/';
array_shift($arr);
break;
}
}
}
self::$URL = $arr;
self::$absPath = Core::$FOLDER.'/content/' . self::$themeUrl . '/';
self::$themeAbsPath = '/content/' . self::$themeUrl . '/';
self::$themeIndex = self::$absPath . 'index.php';
if($portal) {
$full_url = self::portalUrl( $_SERVER['REQUEST_URI'] ,true);
$full_path = str_replace("?".$_SERVER['REDIRECT_QUERY_STRING'],"",$full_url);
// Проверяем ссылку на редирект
if($c = $portal[Portal::$REDIRECT]){
$a = explode("\n",$c);
$redirects = [];
foreach ($a as $v){
$v = str_replace("\t"," ",$v);
$v = explode(" ",$v);
$redirects[$v[0]] = $v[1];
}
if($c = $redirects[$full_url]) {\Site::redirect301($c); exit;}
if($c = $redirects[$full_path]) {\Site::redirect301($c); exit;}
}
// Проверяем ссылку
switch (self::$URL[0]){
case 'robots.txt':
header("Content-Type: text/plain");
echo $portal[Portal::$ROBOTS_TXT];
exit;
case 'sitemap.xml':
//*
header("Content-Type: text/xml");
//header("Expires: Thu, 19 Feb 1998 13:24:18 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Cache-Control: post-check=0,pre-check=0");
header("Cache-Control: max-age=0");
header("Pragma: no-cache");
/**/
echo self::$theme->getSitemap();
exit;
case 'yml.xml':
//*
header("Content-Type: text/xml");
//header("Expires: Thu, 19 Feb 1998 13:24:18 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Cache-Control: post-check=0,pre-check=0");
header("Cache-Control: max-age=0");
header("Pragma: no-cache");
/**/
echo self::$theme->getYML();
exit;
case 'merchant.txt':
self::$theme->echoMerchant();
exit;
case 'merchant.xml':
self::$theme->echoMerchantXML();
exit;
case 'turbo.xml':
self::$theme->getTurbo()->echoXml();
exit;
case 'turbo_shop.xml':
header("Content-Type: text/xml");
//header("Expires: Thu, 19 Feb 1998 13:24:18 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Cache-Control: post-check=0,pre-check=0");
header("Cache-Control: max-age=0");
header("Pragma: no-cache");
echo self::$theme->getTurboShop();
exit;
}
// Теперь проверим, вдруг это код верификации
if(count($arr)==1 ) {
$url = $arr[0];
$filename = '';
$v1 = substr($url,-5,5);
if($v1=='.html') $filename = substr($url,0,-5);
$v2 = substr($url,-4,4);
$v3 = substr($url,-3,3);
if($v2=='.htm') $filename = substr($url,0,-4);
if($v2=='.xml') $filename = substr($url,0,-4);
if($v2=='.txt') $filename = substr($url,0,-4);
if($v1=='.json') $filename = substr($url,0,-5);
if($v3=='.js') {
header("Content-Type: application/javascript");
$filename = substr($url,0,-3);
}
$filename = (string)trim($filename);
if($filename){
$r = Verify::select([
\Query::COUNT => 1,
\Query::WHERE => new \Where(\Where::_and([
\Where::_operator(Verify::$PORTAL,'=',self::$portal_id),
\Where::_operator(Verify::$FILENAME,'=',$filename),
]))
],true);
if($r) {
$l = array_pop($r);
echo $l[Verify::$TEXT];
exit;
}
}
}
if($isModuleSite) {
$domains = Domain::getByPortal($C[Portal::$ID]);
if ($domains) {
$domain = array_shift($domains);
\Site::redirect301('http://' . $domain[Domain::$DOMAIN] . $_SERVER['REQUEST_URI']);
exit;
}
}
}
}
static function render(){
if(file_exists(self::$themeIndex)) {
include_once(self::$themeIndex);
}
}
}