This repository has been archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Route.php
108 lines (105 loc) · 2.88 KB
/
Route.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace RedCat\Localize;
class Route{
function byAcceptLanguage($_map=[],$default='en',$url=null,$http_accept_language = ''){
if(!isset($url))
$url = isset($_SERVER['PATH_INFO'])?$_SERVER['PATH_INFO']:'';
if(isset($_COOKIE['language'])||trim($url,'/'))
return;
$map = [];
foreach($_map as $k=>$v){
if(is_integer($k))
$map[$v] = $this->getSubdomainHref($v);
else
$map[$k] = $v;
}
if($default){
if(is_array($default)){
$defaultUrl = $default[1];
$default = $default[0];
$map[$default] = $defaultUrl;
}
else{
$map[$default] = $this->getSubdomainHref();
}
}
$language = $default;
$redirect = AcceptLanguage::detect(function($lang)use(&$map,&$language){
$k = implode('_',$lang);
if(isset($map[$k])){
$language = $k;
return $map[$k];
}
},$default,$http_accept_language);
$lg = $this->getSubdomainLang();
$domain = $this->getServerHref();
if($lg)
$domain = substr($domain,strlen($lg)+1);
setcookie('language',$language,0,'/',$domain);
$current = $this->getBaseHref();
if($current!=$redirect){
header('Location: '.$redirect,false,302);
exit;
}
}
protected $baseHref;
protected $suffixHref;
function setBaseHref($href){
$this->baseHref = $href;
}
function getProtocolHref(){
return 'http'.(@$_SERVER["HTTPS"]=="on"?'s':'').'://';
}
function getServerHref(){
return $_SERVER['SERVER_NAME'];
}
function getPortHref(){
$ssl = @$_SERVER["HTTPS"]=="on";
return @$_SERVER['SERVER_PORT']&&((!$ssl&&(int)$_SERVER['SERVER_PORT']!=80)||($ssl&&(int)$_SERVER['SERVER_PORT']!=443))?':'.$_SERVER['SERVER_PORT']:'';
}
function getBaseHref(){
if(!isset($this->baseHref)){
$this->setBaseHref($this->getProtocolHref().$this->getServerHref().$this->getPortHref().'/');
}
return $this->baseHref.$this->getSuffixHref();
}
function setSuffixHref($href){
$this->suffixHref = $href;
}
function getSuffixHref(){
if(!isset($this->suffixHref)){
if(isset($_SERVER['REDCAT_URI'])){
$this->suffixHref = ltrim($_SERVER['REDCAT_URI'],'/');
}
else{
$docRoot = $_SERVER['DOCUMENT_ROOT'].'/';
//$docRoot = dirname($_SERVER['SCRIPT_FILENAME']).'/';
if(defined('REDCAT_CWD'))
$cwd = REDCAT_CWD;
else
$cwd = getcwd();
if($docRoot!=$cwd&&strpos($cwd,$docRoot)===0)
$this->suffixHref = substr($cwd,strlen($docRoot));
}
}
return $this->suffixHref;
}
function getSubdomainHref($sub=''){
$lg = $this->getSubdomainLang();
$server = $this->getServerHref();
if($lg)
$server = substr($server,strlen($lg)+1);
if($sub)
$sub .= '.';
return $this->getProtocolHref().$sub.$server.$this->getPortHref().'/'.$this->getSuffixHref();
}
function getSubdomainLang($domain=null){
if(!isset($domain))
$domain = $this->getServerHref();
$urlParts = explode('.', $domain);
if(count($urlParts)>2&&strlen($urlParts[0])==2)
return $urlParts[0];
else
return null;
}
}