Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
debugger.php
1<?php
2
3namespace Bitrix\Main\Session;
4
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 (!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}
static getCurrent()
Definition context.php:241
storeConfig(array $sessionConfig)
Definition debugger.php:123
addHeader(string $category, string $value)
Definition debugger.php:73
encryptValue(string $value)
Definition debugger.php:103
logConfiguration(array $config)
Definition debugger.php:35