63 lines
1.6 KiB
PHP
63 lines
1.6 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../core/db.php';
|
|
require_once __DIR__ . '/../core/redis.php';
|
|
|
|
|
|
class Paste
|
|
{
|
|
private $pdo;
|
|
public function __construct($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]);
|
|
}
|
|
|
|
$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
|
|
])
|
|
);
|
|
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
public function get($id)
|
|
{
|
|
$redis = redisClient();
|
|
|
|
$data = $redis->get("paste:$id");
|
|
if ($data !== false) {
|
|
$json = json_decode($data, true);
|
|
return [
|
|
'encrypted_text' => $json['encrypted_text'],
|
|
'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->execute([$id]);
|
|
return $stmt->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
}
|