Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| OlzProcessor | |
0.00% |
0 / 31 |
|
0.00% |
0 / 2 |
90 | |
0.00% |
0 / 1 |
| __invoke | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
| protectTokens | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Utils; |
| 4 | |
| 5 | use Monolog\Attribute\AsMonologProcessor; |
| 6 | use Monolog\LogRecord; |
| 7 | use Monolog\Processor\ProcessorInterface; |
| 8 | |
| 9 | #[AsMonologProcessor] |
| 10 | class OlzProcessor implements ProcessorInterface { |
| 11 | use WithUtilsTrait; |
| 12 | |
| 13 | public function __invoke(LogRecord $record): LogRecord { |
| 14 | if ($this->server()) { |
| 15 | $record->extra['url'] = $this->protectTokens($this->server()['REQUEST_URI'] ?? null); |
| 16 | $record->extra['referrer'] = $this->server()['HTTP_REFERER'] ?? null; |
| 17 | $record->extra['user_agent'] = $this->server()['HTTP_USER_AGENT'] ?? null; |
| 18 | } |
| 19 | $record->extra['user'] = $this->session()->get('user'); |
| 20 | $record->extra['auth_user'] = $this->session()->get('auth_user'); |
| 21 | $safe_message = $this->protectTokens($record->message); |
| 22 | if ($record->channel && $record->channel !== 'app') { |
| 23 | return $record->with(message: $safe_message); |
| 24 | } |
| 25 | $trace = debug_backtrace(); |
| 26 | $general_utils = new GeneralUtils(); |
| 27 | $trace_overview = $general_utils->getTraceOverview($trace); |
| 28 | return $record->with( |
| 29 | channel: $trace_overview, |
| 30 | message: $safe_message, |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | /** @var ?array<string, string> */ |
| 35 | protected ?array $protected_tokens = null; |
| 36 | |
| 37 | protected function protectTokens(?string $unsanitized): ?string { |
| 38 | if (!$unsanitized) { |
| 39 | return $unsanitized; |
| 40 | } |
| 41 | if ($this->protected_tokens === null) { |
| 42 | $this->protected_tokens = []; |
| 43 | $app_secret = $this->server()['APP_SECRET'] ?? null; |
| 44 | if ($app_secret) { |
| 45 | $this->protected_tokens[$app_secret] = '***APP_SECRET***'; |
| 46 | } |
| 47 | } |
| 48 | $value = preg_replace( |
| 49 | '/(access\_token\=[a-zA-Z0-9\_\-\+\/]{3})[a-zA-Z0-9\_\-\+\/]*([a-zA-Z0-9\_\-\+\/]{3})/', |
| 50 | '$1***$2', |
| 51 | $unsanitized, |
| 52 | ); |
| 53 | foreach ($this->protected_tokens as $token => $replacement) { |
| 54 | $value = str_replace($token, $replacement, $value ?? ''); |
| 55 | } |
| 56 | return $value; |
| 57 | } |
| 58 | } |