Update -> refactor and optimize UI , code ,...
This commit is contained in:
@@ -1,62 +1,58 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../core/db.php';
|
||||
require_once __DIR__ . '/../core/redis.php';
|
||||
|
||||
require_once __DIR__ . '/../core/redis.php';
|
||||
|
||||
class Paste
|
||||
{
|
||||
private $pdo;
|
||||
public function __construct($pdo)
|
||||
private PDO $pdo;
|
||||
|
||||
public function __construct(PDO $pdo)
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
public function save($id, $encrypted_text, $iv, $expire_time, $password_hash)
|
||||
{
|
||||
if ($expire_time === null) {
|
||||
$stmt = $this->pdo->prepare("INSERT INTO pastes(id, encrypted_text, iv, expire_time, password_hash)
|
||||
VALUES (?, ?, ?, NULL, ?)");
|
||||
return $stmt->execute([$id, $encrypted_text, $iv, $password_hash]);
|
||||
public function save(
|
||||
string $id,
|
||||
string $encrypted_text,
|
||||
string $iv,
|
||||
?int $expire_time,
|
||||
?string $password_hash
|
||||
): bool {
|
||||
if ($expire_time !== null) {
|
||||
$ttl = max(1, $expire_time - time());
|
||||
$redis = redisClient();
|
||||
$redis->setex("paste:{$id}", $ttl, json_encode([
|
||||
'encrypted_text' => $encrypted_text,
|
||||
'iv' => $iv,
|
||||
'password_hash' => $password_hash,
|
||||
]));
|
||||
return true;
|
||||
}
|
||||
|
||||
$redis = redisClient();
|
||||
|
||||
$expire_time = (int)$expire_time;
|
||||
$ttl = max(1, $expire_time - time());
|
||||
|
||||
|
||||
$redis->setex(
|
||||
"paste:$id",
|
||||
$ttl,
|
||||
json_encode([
|
||||
'encrypted_text' => $encrypted_text,
|
||||
'iv' => $iv,
|
||||
'password_hash' => $password_hash
|
||||
])
|
||||
$stmt = $this->pdo->prepare(
|
||||
'INSERT INTO pastes (id, encrypted_text, iv, expire_time, password_hash)
|
||||
VALUES (?, ?, ?, NULL, ?)'
|
||||
);
|
||||
|
||||
|
||||
return true;
|
||||
return $stmt->execute([$id, $encrypted_text, $iv, $password_hash]);
|
||||
}
|
||||
|
||||
|
||||
public function get($id)
|
||||
public function get(string $id): array|false
|
||||
{
|
||||
$redis = redisClient();
|
||||
$raw = $redis->get("paste:{$id}");
|
||||
|
||||
$data = $redis->get("paste:$id");
|
||||
if ($data !== false) {
|
||||
$json = json_decode($data, true);
|
||||
if ($raw !== false) {
|
||||
$json = json_decode($raw, true);
|
||||
return [
|
||||
'encrypted_text' => $json['encrypted_text'],
|
||||
'iv' => $json['iv'],
|
||||
'password_hash' => $json['password_hash'],
|
||||
'expire_time' => time() + $redis->ttl("paste:$id")
|
||||
'iv' => $json['iv'],
|
||||
'password_hash' => $json['password_hash'],
|
||||
'expire_time' => time() + $redis->ttl("paste:{$id}"),
|
||||
];
|
||||
}
|
||||
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM pastes WHERE id = ?");
|
||||
$stmt = $this->pdo->prepare('SELECT * FROM pastes WHERE id = ? LIMIT 1');
|
||||
$stmt->execute([$id]);
|
||||
return $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
return $stmt->fetch();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user