1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
debugger.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Session;
4
5use Bitrix\Main\Application;
6use Bitrix\Main\Context;
7use Bitrix\Main\Diag\Helper;
8use Bitrix\Main\EventManager;
9use Bitrix\Main\Security\Cipher;
10use Bitrix\Main\Security\SecurityException;
11
12final 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 ($this->isInternalSessionLine($item))
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
69 private function isInternalSessionLine(array $item): bool
70 {
71 if (empty($item['class']))
72 {
73 return false;
74 }
75
76 if (strpos($item['file'], 'lib' . DIRECTORY_SEPARATOR . 'session' . DIRECTORY_SEPARATOR))
77 {
78 return true;
79 }
80 if (strpos($item['file'], 'storage' . DIRECTORY_SEPARATOR . 'nativesessionstorage'))
81 {
82 return true;
83 }
84
85 return false;
86 }
87
88 public function logToFile($text): void
89 {
90 if ($this->mode & self::TO_FILE)
91 {
92 $requestUri = Application::getInstance()->getContext()->getServer()->getRequestUri();
93 AddMessage2Log($text . ' ' . $requestUri, 'main', 20);
94 }
95 }
96
97 protected function addHeader(string $category, string $value): void
98 {
99 $context = Context::getCurrent();
100 if (!($context instanceof Context))
101 {
102 return;
103 }
104
105 if ($this->mode & self::TO_HEADER)
106 {
107 if ($this->shouldEncryptValue())
108 {
109 $value = $this->encryptValue($value);
110 }
111
112 $response = $context->getResponse();
113 $response->addHeader("X-Session-{$category}", $value);
114 }
115 }
116
117 protected function getCryptoKey(): ?string
118 {
119 return $this->config['debugKey'] ?? null;
120 }
121
122 protected function shouldEncryptValue(): bool
123 {
124 return !empty($this->getCryptoKey());
125 }
126
127 protected function encryptValue(string $value): string
128 {
129 try
130 {
131 $cipher = new Cipher();
132 $encryptedValue = $cipher->encrypt($value, $this->getCryptoKey());
133
134 return $this->encodeUrlSafeB64($encryptedValue);
135 }
136 catch (SecurityException $securityException)
137 {
138 return '';
139 }
140 }
141
142 private function encodeUrlSafeB64(string $input): string
143 {
144 return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
145 }
146
147 public function storeConfig(array $sessionConfig): void
148 {
149 $this->config = $sessionConfig;
150 }
151}
$type
Определения options.php:106
static getInstance()
Определения application.php:98
static getInstance()
Определения eventmanager.php:31
storeConfig(array $sessionConfig)
Определения debugger.php:147
const TO_FILE
Определения debugger.php:14
logToFile($text)
Определения debugger.php:88
addHeader(string $category, string $value)
Определения debugger.php:97
shouldEncryptValue()
Определения debugger.php:122
detectFirstUsage()
Определения debugger.php:43
const TO_HEADER
Определения debugger.php:15
__construct(int $mode=0)
Определения debugger.php:21
setMode(int $mode)
Определения debugger.php:30
const TO_ALL
Определения debugger.php:16
encryptValue(string $value)
Определения debugger.php:127
logConfiguration(array $config)
Определения debugger.php:35
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$context
Определения csv_new_setup.php:223
$requestUri
Определения urlrewrite.php:51
AddMessage2Log($text, $module='', $traceDepth=6, $showArgs=false)
Определения tools.php:3941
Определения culture.php:9
$text
Определения template_pdf.php:79
$response
Определения result.php:21