Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
debugger.php
1
<?php
2
3
namespace
Bitrix\Main\Session
;
4
5
use
Bitrix\Main\Application
;
6
use
Bitrix\Main\Context
;
7
use
Bitrix\Main\Diag\Helper
;
8
use
Bitrix\Main\EventManager
;
9
use
Bitrix\Main\Security\Cipher
;
10
use
Bitrix\Main\Security\SecurityException
;
11
12
final
class
Debugger
13
{
14
public
const
TO_FILE
= 0b001;
15
public
const
TO_HEADER
= 0b010;
16
public
const
TO_ALL
= self::TO_FILE |
self::TO_HEADER
;
17
18
protected
int
$mode
= 0;
19
private
array $config = [];
20
21
public
function
__construct
(
int
$mode
= 0)
22
{
23
$this->
setMode
($mode);
24
25
EventManager::getInstance
()->addEventHandlerCompatible(
'main'
,
'OnPageStart'
,
function
(){
26
$this->
logConfiguration
($this->config);
27
});
28
}
29
30
public
function
setMode
(
int
$mode
): void
31
{
32
$this->mode =
$mode
;
33
}
34
35
public
function
logConfiguration
(array $config): void
36
{
37
$mode
= $config[
'mode'
] ??
'unknown'
;
38
$type = $config[
'handlers'
][
'general'
][
'type'
] ??
'unknown'
;
39
40
$this->
addHeader
(
'Conf'
,
"{$mode}:{$type}"
);
41
}
42
43
public
function
detectFirstUsage
(): void
44
{
45
$firstUsage =
null
;
46
$traceItems = Helper::getBackTrace(10, DEBUG_BACKTRACE_IGNORE_ARGS, 4);
47
foreach
($traceItems as $item)
48
{
49
if
(!empty($item[
'class'
]) && strpos($item[
'file'
],
'lib/session/'
))
50
{
51
continue
;
52
}
53
54
$firstUsage =
"{$item['file']}:{$item['line']}"
;
55
break
;
56
}
57
58
if
($firstUsage)
59
{
60
$this->
addHeader
(
"Usage"
, $firstUsage);
61
}
62
}
63
64
public
function
logToFile
($text): void
65
{
66
if
($this->mode & self::TO_FILE)
67
{
68
$requestUri =
Application::getInstance
()->getContext()->getServer()->getRequestUri();
69
AddMessage2Log($text .
' '
. $requestUri,
'main'
, 20);
70
}
71
}
72
73
protected
function
addHeader
(
string
$category,
string
$value): void
74
{
75
$context =
Context::getCurrent
();
76
if
(!($context instanceof
Context
))
77
{
78
return
;
79
}
80
81
if
($this->mode & self::TO_HEADER)
82
{
83
if
($this->
shouldEncryptValue
())
84
{
85
$value = $this->
encryptValue
($value);
86
}
87
88
$response = $context->getResponse();
89
$response->addHeader(
"X-Session-{$category}"
, $value);
90
}
91
}
92
93
protected
function
getCryptoKey
(): ?string
94
{
95
return
$this->config[
'debugKey'
] ??
null
;
96
}
97
98
protected
function
shouldEncryptValue
(): bool
99
{
100
return
!empty($this->
getCryptoKey
());
101
}
102
103
protected
function
encryptValue
(
string
$value): string
104
{
105
try
106
{
107
$cipher =
new
Cipher
();
108
$encryptedValue = $cipher->encrypt($value, $this->
getCryptoKey
());
109
110
return
$this->encodeUrlSafeB64($encryptedValue);
111
}
112
catch
(
SecurityException
$securityException)
113
{
114
return
''
;
115
}
116
}
117
118
private
function
encodeUrlSafeB64(
string
$input): string
119
{
120
return
str_replace(
'='
,
''
, strtr(base64_encode($input),
'+/'
,
'-_'
));
121
}
122
123
public
function
storeConfig
(array $sessionConfig): void
124
{
125
$this->config = $sessionConfig;
126
}
127
}
Bitrix\Main\Application
Definition
application.php:28
Bitrix\Main\Application\getInstance
static getInstance()
Definition
application.php:95
Bitrix\Main\Context
Definition
context.php:18
Bitrix\Main\Context\getCurrent
static getCurrent()
Definition
context.php:241
Bitrix\Main\Diag\Helper
Definition
helper.php:5
Bitrix\Main\EventManager
Definition
eventmanager.php:15
Bitrix\Main\EventManager\getInstance
static getInstance()
Definition
eventmanager.php:33
Bitrix\Main\Security\Cipher
Definition
cipher.php:11
Bitrix\Main\Security\SecurityException
Definition
securityexception.php:5
Bitrix\Main\Session\Debugger
Definition
debugger.php:13
Bitrix\Main\Session\Debugger\storeConfig
storeConfig(array $sessionConfig)
Definition
debugger.php:123
Bitrix\Main\Session\Debugger\TO_FILE
const TO_FILE
Definition
debugger.php:14
Bitrix\Main\Session\Debugger\logToFile
logToFile($text)
Definition
debugger.php:64
Bitrix\Main\Session\Debugger\addHeader
addHeader(string $category, string $value)
Definition
debugger.php:73
Bitrix\Main\Session\Debugger\getCryptoKey
getCryptoKey()
Definition
debugger.php:93
Bitrix\Main\Session\Debugger\shouldEncryptValue
shouldEncryptValue()
Definition
debugger.php:98
Bitrix\Main\Session\Debugger\detectFirstUsage
detectFirstUsage()
Definition
debugger.php:43
Bitrix\Main\Session\Debugger\TO_HEADER
const TO_HEADER
Definition
debugger.php:15
Bitrix\Main\Session\Debugger\$mode
int $mode
Definition
debugger.php:18
Bitrix\Main\Session\Debugger\__construct
__construct(int $mode=0)
Definition
debugger.php:21
Bitrix\Main\Session\Debugger\setMode
setMode(int $mode)
Definition
debugger.php:30
Bitrix\Main\Session\Debugger\TO_ALL
const TO_ALL
Definition
debugger.php:16
Bitrix\Main\Session\Debugger\encryptValue
encryptValue(string $value)
Definition
debugger.php:103
Bitrix\Main\Session\Debugger\logConfiguration
logConfiguration(array $config)
Definition
debugger.php:35
Bitrix\Main\Context
Definition
culture.php:9
Bitrix\Main\Session
Definition
arrayaccesswithreferences.php:3
modules
main
lib
session
debugger.php
Создано системой
1.10.0