Update -> refactor and optimize UI , code ,...
This commit is contained in:
@@ -1,13 +1,21 @@
|
||||
<?php
|
||||
$config = require __DIR__ . '/../config/config.php';
|
||||
|
||||
try {
|
||||
$pdo = new PDO(
|
||||
"mysql:host={$config['db']['host']};dbname={$config['db']['name']};charset=utf8mb4",
|
||||
$config['db']['user'],
|
||||
$config['db']['pass'],
|
||||
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
|
||||
$dsn = sprintf(
|
||||
'mysql:host=%s;dbname=%s;charset=%s',
|
||||
$config['db']['host'],
|
||||
$config['db']['name'],
|
||||
$config['db']['charset']
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
die('Database connection error');
|
||||
$pdo = new PDO($dsn, $config['db']['user'], $config['db']['pass'], [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]);
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(503);
|
||||
die(json_encode(['success' => false, 'message' => 'Database connection error.']));
|
||||
}
|
||||
|
||||
return $pdo;
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
<?php
|
||||
|
||||
function redisClient()
|
||||
function redisClient(): Redis
|
||||
{
|
||||
static $redis = null;
|
||||
if ($redis === null) {
|
||||
$config = require __DIR__ . '/../config/config.php';
|
||||
|
||||
$redis = new Redis();
|
||||
$redis->connect($config['redis']['host'], $config['redis']['port']);
|
||||
$redis = new Redis();
|
||||
if (!@$redis->connect($config['redis']['host'], $config['redis']['port'])) {
|
||||
throw new RuntimeException('Redis connection failed.');
|
||||
}
|
||||
}
|
||||
return $redis;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,32 @@
|
||||
<?php
|
||||
function encryptText($text, $key) {
|
||||
$iv = random_bytes(16);
|
||||
|
||||
function encryptText(string $text, string $key): array
|
||||
{
|
||||
$iv = random_bytes(16);
|
||||
$cipher = openssl_encrypt($text, 'AES-256-CBC', $key, 0, $iv);
|
||||
if ($cipher === false) {
|
||||
throw new RuntimeException('Encryption failed.');
|
||||
}
|
||||
return [
|
||||
'cipher' => $cipher,
|
||||
'iv' => base64_encode($iv)
|
||||
'iv' => base64_encode($iv),
|
||||
];
|
||||
}
|
||||
function decryptText($cipher, $iv, $key) {
|
||||
|
||||
function decryptText(string $cipher, string $iv, string $key): string|false
|
||||
{
|
||||
return openssl_decrypt($cipher, 'AES-256-CBC', $key, 0, base64_decode($iv));
|
||||
}
|
||||
function generateId() {
|
||||
|
||||
function generateId(): string
|
||||
{
|
||||
return bin2hex(random_bytes(16));
|
||||
}
|
||||
|
||||
function jsonResponse(array $data, int $status = 200): void
|
||||
{
|
||||
http_response_code($status);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($data);
|
||||
exit;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user