sakulmore

php

Aug 16th, 2025
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. <?php
  2. // Created by sakulmore
  3. // https://sakulmore.eu/
  4.  
  5. // --- Nastavenia adresára ---
  6. $dirNameUrl = 'txt_files'; // URL časť (v prehliadači bude /txt_files/...)
  7. $dirFilesystem = __DIR__ . '/txt_files'; // ABSOLÚTNA cesta na disku k priečinku
  8. $filenameLen = 9;
  9. $maxTries = 10;
  10.  
  11. // --- Limit veľkosti súboru ---
  12. $maxSizeMB = 10; // limit v MB
  13. $maxSizeBytes = $maxSizeMB * 1024 * 1024;
  14.  
  15. // --- Expirácia ---
  16. $retentionDays = 3; // po koľkých dňoch sa má súbor zmazať
  17. $retentionSec = $retentionDays * 24 * 60 * 60;
  18.  
  19. $pasteMessage = '';
  20.  
  21. // URL prefix (bez zapečenej domény)
  22. function baseUrlToDir(string $dirNameUrl): string
  23. {
  24. $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
  25. $host = $_SERVER['HTTP_HOST'] ?? 'localhost';
  26. $base = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\');
  27. return $scheme . '://' . $host . ($base ? $base : '') . '/' . trim($dirNameUrl, '/') . '/';
  28. }
  29.  
  30. function randName(int $len): string
  31. {
  32. $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  33. $out = '';
  34. for ($i = 0; $i < $len; $i++) {
  35. $out .= $alphabet[random_int(0, strlen($alphabet) - 1)];
  36. }
  37. return $out;
  38. }
  39.  
  40. function purgeExpiredTxt(string $dirFilesystem, int $olderThanSec): void
  41. {
  42. if (!is_dir($dirFilesystem)) return;
  43. $now = time();
  44. foreach (glob($dirFilesystem . '/*.txt') ?: [] as $path) {
  45. if (!is_file($path)) continue;
  46. $age = $now - @filemtime($path);
  47. if ($age >= $olderThanSec) {
  48. @unlink($path);
  49. }
  50. }
  51. }
  52.  
  53. purgeExpiredTxt($dirFilesystem, $retentionSec);
  54.  
  55. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  56. $content = $_POST['content'] ?? '';
  57. $content = str_replace(["\r\n", "\r"], "\n", $content);
  58.  
  59. $detected = mb_detect_encoding($content, ['UTF-8', 'Windows-1250', 'ISO-8859-2', 'Windows-1252', 'ISO-8859-1'], true);
  60. if ($detected === false) {
  61. $detected = 'UTF-8';
  62. }
  63. $content = iconv($detected, 'UTF-8//IGNORE', $content);
  64.  
  65. // Voliteľný BOM pre starší Windows Notepad
  66. if (!isset($ADD_UTF8_BOM)) {
  67. $ADD_UTF8_BOM = true;
  68. } // fallback ak nie je definované
  69. if ($ADD_UTF8_BOM && !str_starts_with($content, "\xEF\xBB\xBF")) {
  70. $content = "\xEF\xBB\xBF" . $content;
  71. }
  72.  
  73. if (trim($content) === '') {
  74. $pasteMessage = 'Area can\'t be empty!';
  75. } else {
  76. // 1) Skontroluj / vytvor priečinok (ABS FS cesta)
  77. if (!is_dir($dirFilesystem)) {
  78. if (!@mkdir($dirFilesystem, 0755, true) && !is_dir($dirFilesystem)) {
  79. $pasteMessage = 'Failed to create the txt\_files directory.';
  80. }
  81. }
  82.  
  83. // 2) Skontroluj zapisovateľnosť
  84. if ($pasteMessage === '' && !is_writable($dirFilesystem)) {
  85. $pasteMessage = 'The directory is not writable. Check CHMOD/owner.';
  86. }
  87.  
  88. if ($pasteMessage === '') {
  89. // 3) Najdi voľný názov
  90. $filePath = null;
  91. $fileName = null;
  92. for ($i = 0; $i < $maxTries; $i++) {
  93. $fileName = randName($filenameLen) . '.txt';
  94. $candidate = $dirFilesystem . '/' . $fileName;
  95. if (!file_exists($candidate)) {
  96. $filePath = $candidate;
  97. break;
  98. }
  99. }
  100.  
  101. if ($filePath === null) {
  102. $pasteMessage = 'Failed to generate a free filename. Please try again.';
  103. } else {
  104. // 4) Zápis obsahu
  105. $bytes = @file_put_contents($filePath, $content, LOCK_EX);
  106. if ($bytes === false) {
  107. $pasteMessage = 'Writing to the file failed.';
  108. } else {
  109. @touch($filePath, time());
  110.  
  111. // Kontrola veľkosti súboru po zápise
  112. $size = @filesize($filePath);
  113. if ($size === false) {
  114. // nevie zistiť veľkosť – zmaž a vypíš chybu
  115. @unlink($filePath);
  116. $pasteMessage = 'An error occurred while reading the file size.';
  117. } elseif ($size >= $maxSizeBytes) {
  118. // prekročený limit
  119. @unlink($filePath);
  120. $pasteMessage = 'You have exceeded the ' . (int)$maxSizeMB . ' MB limit!';
  121. } else {
  122. $fileUrl = htmlspecialchars(baseUrlToDir($dirNameUrl) . $fileName, ENT_QUOTES, 'UTF-8');
  123. $expiresAt = date('Y-m-d H:i', time() + $retentionSec);
  124. $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>';
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment