32 lines
1.1 KiB
PHP
32 lines
1.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../core/security.php';
|
|
$pdo = require __DIR__ . '/../core/db.php';
|
|
require_once __DIR__ . '/../models/Paste.php';
|
|
$config = require __DIR__ . '/../config/config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('Location: /');
|
|
exit;
|
|
}
|
|
|
|
$text = trim($_POST['text'] ?? '');
|
|
$password = $_POST['password'] ?? '';
|
|
$expire = isset($_POST['expire']) ? (int)$_POST['expire'] : 0;
|
|
|
|
if ($text === '') {
|
|
jsonResponse(['success' => false, 'message' => 'Text cannot be empty.'], 422);
|
|
}
|
|
|
|
$id = generateId();
|
|
$enc = encryptText($text, $config['app']['master_key']);
|
|
$password_hash = $password !== '' ? password_hash($password, PASSWORD_DEFAULT) : null;
|
|
$expire_time = $expire > 0 ? time() + $expire : null;
|
|
|
|
$paste = new Paste($pdo);
|
|
$paste->save($id, $enc['cipher'], $enc['iv'], $expire_time, $password_hash);
|
|
|
|
$base = rtrim($config['app']['base_url'] ?: ('http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST']), '/');
|
|
$url = $base . '/view/' . $id;
|
|
|
|
jsonResponse(['success' => true, 'url' => $url]);
|