Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
helper.php
1
<?
2
namespace
Bitrix\Scale
;
3
4
use \Bitrix\Main\Localization\Loc;
5
Loc::loadMessages
(__FILE__);
6
11
class
Helper
12
{
13
const
BX_ENV_MIN_VERSION
=
"7.2.0"
;
14
15
public
static
function
checkBxEnvVersion
($version =
false
)
16
{
17
if
(!$version)
18
$version = getenv(
'BITRIX_VA_VER'
);
19
20
return
version_compare($version, self::BX_ENV_MIN_VERSION ,
'>='
);
21
}
22
23
public
static
function
nbsp
($str)
24
{
25
return
str_replace(
" "
,
" "
,$str);
26
}
27
28
public
static
function
getAvailabilityPage
($minutes)
29
{
30
if
(intval($minutes) <= 0)
31
throw
new \Bitrix\Main\ArgumentNullException(
"minutes"
);
32
33
$now = time();
34
35
$contents = file_get_contents(\
Bitrix
\Main\
Application::getDocumentRoot
().
'/bitrix/modules/scale/server_off.html'
);
36
37
$contents = str_replace(
38
"##SITE_NAME##"
,
39
\CUtil::JSEscape(\COption::GetOptionString(
"main"
,
"site_name"
, $_SERVER[
"SERVER_NAME"
])),
40
$contents
41
);
42
43
$contents = str_replace(
44
"##CHARSET##"
,
45
LANG_CHARSET,
46
$contents
47
);
48
49
$contents = str_replace(
50
"##AVAILABLE_MESSAGE##"
,
51
Loc::getMessage
(
"SCALE_HLP_AV_MESSAGE"
),
52
$contents
53
);
54
55
$contents = str_replace(
56
"##AVAILABLE_DATETIME##"
,
57
($now+60*$minutes)*1000,
58
$contents
59
);
60
61
$contents = str_replace(
62
"##SERVER_NOW##"
,
63
$now*1000,
64
$contents
65
);
66
67
$contents = str_replace(
68
"##HOURS##"
,
69
Loc::getMessage
(
"SCALE_HLP_AV_HOURS"
).
" "
,
70
$contents
71
);
72
73
$contents = str_replace(
74
"##MINS##"
,
75
Loc::getMessage
(
"SCALE_HLP_AV_MINS"
).
" "
,
76
$contents
77
);
78
79
$contents = str_replace(
80
"##SECS##"
,
81
Loc::getMessage
(
"SCALE_HLP_AV_SECS"
).
" "
,
82
$contents
83
);
84
85
return
$contents;
86
}
87
88
public
static
function
modifyDbconn
($DBHost, $DBName, $DBLogin, $DBPassword)
89
{
90
if
($DBHost ==
''
)
91
throw
new \Bitrix\Main\ArgumentNullException(
"DBHost"
);
92
if
($DBName ==
''
)
93
throw
new \Bitrix\Main\ArgumentNullException(
"DBName"
);
94
if
($DBLogin ==
''
)
95
throw
new \Bitrix\Main\ArgumentNullException(
"DBLogin"
);
96
97
$filename = \Bitrix\Main\Application::getDocumentRoot().
"/bitrix/php_interface/dbconn.php"
;
98
$file = new \Bitrix\Main\IO\File($filename);
99
100
if
(!$file->isExists())
101
return
false
;
102
103
$content = file_get_contents($filename);
104
105
if
($content ==
''
)
106
return
false
;
107
108
file_put_contents(\
Bitrix
\Main\
Application::getDocumentRoot
().
"/bitrix/php_interface/dbconn.php.bak"
, $content);
109
110
$content = preg_replace(
'/(\$DBHost\s*=\s*(\"|\')+)(.*)((\"|\')+;)/'
,
'${1}'
.$DBHost.
'${4}'
,$content);
111
$content = preg_replace(
'/(\$DBName\s*=\s*(\"|\')+)(.*)((\"|\')+;)/'
,
'${1}'
.$DBName.
'${4}'
,$content);
112
$content = preg_replace(
'/(\$DBLogin\s*=\s*(\"|\')+)(.*)((\"|\')+;)/'
,
'${1}'
.$DBLogin.
'${4}'
,$content);
113
$content = preg_replace(
'/(\$DBPassword\s*=\s*(\"|\')+)(.*)((\"|\')+;)/'
,
'${1}'
.$DBPassword.
'${4}'
,$content);
114
115
return
file_put_contents($filename, $content);
116
}
117
118
public
static
function
modifySettings
($DBHost, $DBName, $DBLogin, $DBPassword)
119
{
120
$filename = $_SERVER[
'DOCUMENT_ROOT'
].
"/bitrix/.settings-test.php"
;
121
122
if
(!file_exists($filename))
123
return
true
;
124
125
ob_start();
126
$settings = include($filename);
127
ob_end_clean();
128
129
if
(!is_array($settings))
130
return
false
;
131
132
if
(!isset($settings[
'connections'
][
'value'
][
'default'
]) || !is_array($settings[
'connections'
][
'value'
][
'default'
]))
133
return
true
;
134
135
$settings[
'connections'
][
'value'
][
'default'
][
'host'
] = $DBHost;
136
$settings[
'connections'
][
'value'
][
'default'
][
'database'
] = $DBName;
137
$settings[
'connections'
][
'value'
][
'default'
][
'login'
] = $DBLogin;
138
$settings[
'connections'
][
'value'
][
'default'
][
'password'
] = $DBPassword;
139
140
$data = var_export($settings,
true
);
141
142
rename($filename, $_SERVER[
'DOCUMENT_ROOT'
].
"/bitrix/.settings-test.php.bak"
);
143
file_put_contents($filename,
"<"
.
"?php\nreturn "
.$data.
";\n"
);
144
145
return
true
;
146
}
147
148
public
static
function
generatePass
($length = 20)
149
{
150
$chars=
"abcdefghiknrstyzABCDEFGHKNQRSTYZ1234567890"
;
151
$charsCount = mb_strlen($chars);
152
$result=
""
;
153
154
for
($i=0; $i<$length; $i++)
155
$result .= mb_substr($chars, rand(1, $charsCount) - 1, 1);
156
157
return
$result;
158
}
159
160
public
static
function
isExtraDbExist
($hostname)
161
{
162
$dbList =
ServersData::getDbList
($hostname);
163
$connection = \Bitrix\Main\Application::getConnection();
164
$currentDb = $connection->getDbName();
165
$dbCount = count($dbList);
166
if
($dbCount > 1
167
||($dbCount == 1
168
&& !in_array($currentDb, $dbList)
169
)
170
)
171
{
172
$result =
true
;
173
}
174
else
175
{
176
$result =
false
;
177
}
178
179
return
$result;
180
}
181
182
public
static
function
getNetworkInterfaces
()
183
{
184
$result = array();
185
$shellAdapter =
new
ShellAdapter
();
186
$execRes = $shellAdapter->syncExec(
"sudo -u root /opt/webdir/bin/bx-node -o json"
);
187
$jsonData = $shellAdapter->getLastOutput();
188
189
if
($execRes)
190
{
191
$arData = json_decode($jsonData,
true
);
192
193
if
(isset($arData[
"params"
][
"pool_interfaces"
]))
194
$result = $arData[
"params"
][
"pool_interfaces"
];
195
196
if
(is_array($result))
197
{
198
foreach
($result as $iface => $ip)
199
$result[$iface] = $iface.
" ("
.$ip.
")"
;
200
}
201
}
202
203
return
$result;
204
}
205
206
public
static
function
isScaleCanBeUsed
()
207
{
208
global $DB;
209
210
return
getenv(
'BITRIX_VA_VER'
)
211
&& mb_stristr(php_uname(
's'
),
'linux'
)
212
&& $DB->type ==
'MYSQL'
213
&&
self::checkBxEnvVersion
();
214
}
215
216
public
static
function
getTmpDir
()
217
{
218
$path =
'/home/bitrix/.webdir'
;
219
$permissionsForOwnerOnly = 0700;
220
$res =
true
;
221
222
if
(!file_exists($path))
223
$res = mkdir($path, $permissionsForOwnerOnly,
true
);
224
225
return
$res ? $path :
''
;
226
}
227
}
Bitrix\Main\Application\getDocumentRoot
static getDocumentRoot()
Definition
application.php:717
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Definition
loc.php:64
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Scale\Helper
Definition
helper.php:12
Bitrix\Scale\Helper\nbsp
static nbsp($str)
Definition
helper.php:23
Bitrix\Scale\Helper\isScaleCanBeUsed
static isScaleCanBeUsed()
Definition
helper.php:206
Bitrix\Scale\Helper\getNetworkInterfaces
static getNetworkInterfaces()
Definition
helper.php:182
Bitrix\Scale\Helper\getAvailabilityPage
static getAvailabilityPage($minutes)
Definition
helper.php:28
Bitrix\Scale\Helper\modifySettings
static modifySettings($DBHost, $DBName, $DBLogin, $DBPassword)
Definition
helper.php:118
Bitrix\Scale\Helper\getTmpDir
static getTmpDir()
Definition
helper.php:216
Bitrix\Scale\Helper\generatePass
static generatePass($length=20)
Definition
helper.php:148
Bitrix\Scale\Helper\isExtraDbExist
static isExtraDbExist($hostname)
Definition
helper.php:160
Bitrix\Scale\Helper\checkBxEnvVersion
static checkBxEnvVersion($version=false)
Definition
helper.php:15
Bitrix\Scale\Helper\BX_ENV_MIN_VERSION
const BX_ENV_MIN_VERSION
Definition
helper.php:13
Bitrix\Scale\Helper\modifyDbconn
static modifyDbconn($DBHost, $DBName, $DBLogin, $DBPassword)
Definition
helper.php:88
Bitrix\Scale\ServersData\getDbList
static getDbList($hostname)
Definition
serversdata.php:102
Bitrix\Scale\ShellAdapter
Definition
shelladapter.php:10
Bitrix\Scale
Definition
action.php:2
Bitrix
modules
scale
lib
helper.php
Создано системой
1.10.0