BtRepository.php
4.7 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2023/09/18
* Time: 20:56
*/
namespace App\Repositories;
use App\Repositories\Bt\Bt;
/**
* Class BtRepository
* @package App\Repositories
*/
class BtRepository
{
public $instance = [];
/**
* 获取bt对象
* @param string $key
* @param string $panel
* @return Bt
*/
public function getBtObject($key = '', $panel = '')
{
$key = $key ?: env('BT_KEY');
$panel = $panel ?: env('BT_PANEL');
$object_key = md5($key . $panel);
if (empty($this->instance[$object_key])) {
$bt = new Bt($panel, $key);
$this->instance[$object_key] = $bt;
}
return $this->instance[$object_key];
}
/**
* 获取配置前的文件内容
* @param $domain
* @return string|string[]
*/
public function getBeforeNginxConfStr($domain)
{
$bt = $this->getBtObject();
$conf = $bt->GetFileBody($domain, 2);
//如果有变量标识,去掉变量标识
if (FALSE !== strpos($conf['data'], 'map $domain_prefix')) {
$conf['data'] = str_replace($this->domainPrefixConf(), '', $conf['data']);
//去掉自定义配置
$conf['data'] = str_replace($this->customConf($domain), 'root ' . public_path() . ';', $conf['data']);
}
return $conf['data'];
}
/**
* 获取配置后的文件内容
* @param $domain
* @return mixed
*/
public function getAfterNginxConfStr($domain)
{
$bt = $this->getBtObject();
$conf = $bt->GetFileBody($domain, 2);
// 如果没有变量, 添加变量识别
if (FALSE === strpos($conf['data'], 'map $domain_prefix')) {
$conf['data'] = $this->domainPrefixConf() . $conf['data'];
// 替换自定义配置
$conf['data'] = str_replace('root ' . public_path() . ';', $this->customConf($domain), $conf['data']);
}
return $conf['data'];
}
/**
* 需要定制修改的配置
* @param $domain string
* @return string
*/
public function customConf($domain): string
{
$domain_arr = explode('.', $domain);
if ($domain_arr[0] == 'www') {
$string = ' root ' . public_path() . '; # 设置根目录
set $domain_prefix "";
if ($host ~* "^(?<domain_prefix>[^.]+)\.") {
set $domain_prefix $1;
}
location / {
root ' . public_path() . '/' . $domain . '; # 设置根目录
if ($is_prefix_in_string = 1) {
root ' . public_path() . '/' . $domain . '/$domain_prefix;
}
try_files $uri $uri/ /404/index.html =404;
}
location ~ ^/(api|search) {
root ' . public_path() . '; # 设置/api /search /.well-known的根目录
try_files $uri $uri/ /index.php?$query_string;
}
location ~ ^/(well-known) {
root ' . public_path() . ';
try_files $uri $uri/ /404/index.html =404;
}
error_page 403 /404/index.html;';
} else {
$string = ' root ' . public_path() . '; # 设置根目录
set $domain_prefix "";
if ($host ~* "^(?<domain_prefix>[^.]+)\.") {
set $domain_prefix $1;
}
if ($domain_prefix = ' . $domain_arr[0] . '){
set $is_prefix_in_string 0;
}
location / {
root ' . public_path() . '/' . $domain . '; # 设置根目录
if ($is_prefix_in_string = 1) {
root ' . public_path() . '/' . $domain . '/$domain_prefix;
}
try_files $uri $uri/ /404/index.html =404;
}
location ~ ^/(api|search) {
root ' . public_path() . '; # 设置/api /search /.well-known的根目录
try_files $uri $uri/ /index.php?$query_string;
}
location ~ ^/(well-known) {
root ' . public_path() . ';
try_files $uri $uri/ /404/index.html =404;
}
error_page 403 /404/index.html;';
}
return $string;
}
/**
* 配置变量
* @return string
*/
public function domainPrefixConf()
{
return 'map $domain_prefix $is_prefix_in_string {
default 0; en 1; zh 1; fr 1; de 1; ko 1; ja 1; es 1; ar 1; pt 1; ru 1; af 1; sq 1;
am 1; hy 1; az 1; eu 1; be 1; bn 1; bs 1; bg 1; ca 1; ceb 1; zh-CN 1; zh-TW 1; co 1;
hr 1; cs 1; da 1; nl 1; eo 1; et 1; fi 1; fy 1; gl 1; ka 1; el 1; gu 1; ht 1; ha 1; haw 1;
iw 1; hi 1; hmn 1; hu 1; is 1; ig 1; id 1; ga 1; it 1; jw 1; kn 1; kk 1; km 1; rw 1;
ku 1; ky 1; lo 1; la 1; lv 1; lt 1; lb 1; mk 1; mg 1; ms 1; ml 1; mt 1; mi 1; mr 1; mn 1;
my 1; ne 1; no 1; ny 1; or 1; ps 1; fa 1; pl 1; pa 1; ro 1; sm 1; gd 1; sr 1; st 1; sn 1;
sd 1; si 1; sk 1; sl 1; so 1; su 1; sw 1; sv 1; tl 1; tg 1; ta 1; tt 1; te 1; th 1; tr 1;
tk 1; uk 1; ur 1; ug 1; uz 1; vi 1; cy 1; xh 1; yi 1; yo 1; zu 1;
}';
}
}