Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Created by sakulmore
- // https://sakulmore.eu/
- // --- Nastavenia adresára ---
- $dirNameUrl = 'txt_files'; // URL časť (v prehliadači bude /txt_files/...)
- $dirFilesystem = __DIR__ . '/txt_files'; // ABSOLÚTNA cesta na disku k priečinku
- $filenameLen = 9;
- $maxTries = 10;
- // --- Limit veľkosti súboru ---
- $maxSizeMB = 10; // limit v MB
- $maxSizeBytes = $maxSizeMB * 1024 * 1024;
- // --- Expirácia ---
- $retentionDays = 3; // po koľkých dňoch sa má súbor zmazať
- $retentionSec = $retentionDays * 24 * 60 * 60;
- $pasteMessage = '';
- // URL prefix (bez zapečenej domény)
- function baseUrlToDir(string $dirNameUrl): string
- {
- $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
- $host = $_SERVER['HTTP_HOST'] ?? 'localhost';
- $base = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\');
- return $scheme . '://' . $host . ($base ? $base : '') . '/' . trim($dirNameUrl, '/') . '/';
- }
- function randName(int $len): string
- {
- $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
- $out = '';
- for ($i = 0; $i < $len; $i++) {
- $out .= $alphabet[random_int(0, strlen($alphabet) - 1)];
- }
- return $out;
- }
- function purgeExpiredTxt(string $dirFilesystem, int $olderThanSec): void
- {
- if (!is_dir($dirFilesystem)) return;
- $now = time();
- foreach (glob($dirFilesystem . '/*.txt') ?: [] as $path) {
- if (!is_file($path)) continue;
- $age = $now - @filemtime($path);
- if ($age >= $olderThanSec) {
- @unlink($path);
- }
- }
- }
- purgeExpiredTxt($dirFilesystem, $retentionSec);
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $content = $_POST['content'] ?? '';
- $content = str_replace(["\r\n", "\r"], "\n", $content);
- $detected = mb_detect_encoding($content, ['UTF-8', 'Windows-1250', 'ISO-8859-2', 'Windows-1252', 'ISO-8859-1'], true);
- if ($detected === false) {
- $detected = 'UTF-8';
- }
- $content = iconv($detected, 'UTF-8//IGNORE', $content);
- // Voliteľný BOM pre starší Windows Notepad
- if (!isset($ADD_UTF8_BOM)) {
- $ADD_UTF8_BOM = true;
- } // fallback ak nie je definované
- if ($ADD_UTF8_BOM && !str_starts_with($content, "\xEF\xBB\xBF")) {
- $content = "\xEF\xBB\xBF" . $content;
- }
- if (trim($content) === '') {
- $pasteMessage = 'Area can\'t be empty!';
- } else {
- // 1) Skontroluj / vytvor priečinok (ABS FS cesta)
- if (!is_dir($dirFilesystem)) {
- if (!@mkdir($dirFilesystem, 0755, true) && !is_dir($dirFilesystem)) {
- $pasteMessage = 'Failed to create the txt\_files directory.';
- }
- }
- // 2) Skontroluj zapisovateľnosť
- if ($pasteMessage === '' && !is_writable($dirFilesystem)) {
- $pasteMessage = 'The directory is not writable. Check CHMOD/owner.';
- }
- if ($pasteMessage === '') {
- // 3) Najdi voľný názov
- $filePath = null;
- $fileName = null;
- for ($i = 0; $i < $maxTries; $i++) {
- $fileName = randName($filenameLen) . '.txt';
- $candidate = $dirFilesystem . '/' . $fileName;
- if (!file_exists($candidate)) {
- $filePath = $candidate;
- break;
- }
- }
- if ($filePath === null) {
- $pasteMessage = 'Failed to generate a free filename. Please try again.';
- } else {
- // 4) Zápis obsahu
- $bytes = @file_put_contents($filePath, $content, LOCK_EX);
- if ($bytes === false) {
- $pasteMessage = 'Writing to the file failed.';
- } else {
- @touch($filePath, time());
- // Kontrola veľkosti súboru po zápise
- $size = @filesize($filePath);
- if ($size === false) {
- // nevie zistiť veľkosť – zmaž a vypíš chybu
- @unlink($filePath);
- $pasteMessage = 'An error occurred while reading the file size.';
- } elseif ($size >= $maxSizeBytes) {
- // prekročený limit
- @unlink($filePath);
- $pasteMessage = 'You have exceeded the ' . (int)$maxSizeMB . ' MB limit!';
- } else {
- $fileUrl = htmlspecialchars(baseUrlToDir($dirNameUrl) . $fileName, ENT_QUOTES, 'UTF-8');
- $expiresAt = date('Y-m-d H:i', time() + $retentionSec);
- $pasteMessage = 'Done! Your link (expire in: ' . htmlspecialchars($expiresAt, ENT_QUOTES, 'UTF-8') . '):' . '<button type="button" class="form-submit-btn" onclick="navigator.clipboard.writeText(\'' . $fileUrl . '\');">Click to Copy URL</button>';
- }
- }
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment