Skip to content

Commit 5261014

Browse files
author
Tim Helfensdörfer
committed
Use laravel default logging
1 parent b999079 commit 5261014

File tree

2 files changed

+3
-64
lines changed

2 files changed

+3
-64
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"php": ">= 7.2",
2222
"ext-sodium": "*",
2323
"illuminate/database": ">= 5.7",
24+
"illuminate/encryption" : ">= 5.7",
2425
"illuminate/support": ">= 5.7",
2526
"monolog/monolog": ">= 1"
2627
},

src/DatabaseHandler.php

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace VisualAppeal\DatabaseLogger;
44

5+
use Illuminate\Container\Container;
56
use Illuminate\Support\Facades\DB;
67

78
use Monolog\Handler\AbstractProcessingHandler;
@@ -88,69 +89,6 @@ public function setEncryptionKey(string $encryptionKey): DatabaseHandler
8889
return $this;
8990
}
9091

91-
/**
92-
* Encrypt the context.
93-
*
94-
* @param mixed $data
95-
* @return string
96-
* @throws \Exception
97-
*/
98-
public function encrypt($data): string
99-
{
100-
$nonce = random_bytes(
101-
SODIUM_CRYPTO_SECRETBOX_NONCEBYTES
102-
);
103-
104-
$cipher = base64_encode(
105-
$nonce.
106-
sodium_crypto_secretbox(
107-
$data,
108-
$nonce,
109-
$this->encryptionKey
110-
)
111-
);
112-
sodium_memzero($data);
113-
sodium_memzero($this->encryptionKey);
114-
115-
return $cipher;
116-
}
117-
118-
/**
119-
* Decrypt the context.
120-
*
121-
* @param $data
122-
* @return bool|string
123-
* @throws \Exception
124-
*/
125-
public function decrypt($data)
126-
{
127-
$decoded = base64_decode($data);
128-
129-
if ($decoded === false) {
130-
throw new \Exception('The encoding failed');
131-
}
132-
if (mb_strlen($decoded, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) {
133-
throw new \Exception('The message was truncated');
134-
}
135-
$nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
136-
$ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
137-
138-
$plain = sodium_crypto_secretbox_open(
139-
$ciphertext,
140-
$nonce,
141-
$this->encryptionKey
142-
);
143-
144-
if ($plain === false) {
145-
throw new \Exception('The message was tampered with in transit');
146-
}
147-
148-
sodium_memzero($ciphertext);
149-
sodium_memzero($this->encryptionKey);
150-
151-
return $plain;
152-
}
153-
15492
/**
15593
* {@inheritdoc}
15694
*/
@@ -161,7 +99,7 @@ protected function write(array $record)
16199
} else {
162100
try {
163101
if ($this->encryption) {
164-
$context = $this->encrypt($record['context']);
102+
$context = Container::getInstance()->make('encrypter')->encrypt($record['context']);
165103
} else {
166104
$context = serialize($record['context']);
167105
}

0 commit comments

Comments
 (0)