manage_files.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. session_start();
  3. // Load configuration
  4. $config = include(__DIR__ . '/../../config.php');
  5. // Get the website ID from the request or session
  6. $websiteId = isset($_GET['websiteId']) ? (int)$_GET['websiteId'] : (isset($_SESSION['websiteId']) ? (int)$_SESSION['websiteId'] : 1);
  7. $_SESSION['websiteId'] = $websiteId;
  8. // Find the website configuration
  9. $websiteConfig = array_filter($config, function($site) use ($websiteId) {
  10. return isset($site['id']) && $site['id'] === $websiteId;
  11. });
  12. $websiteConfig = reset($websiteConfig);
  13. // Enable error reporting
  14. error_reporting(E_ALL);
  15. ini_set('display_errors', 1);
  16. $rootDir = realpath(__DIR__ . '/../../' . $websiteConfig['folder']);
  17. $currentDir = isset($_GET['dir']) ? realpath($rootDir . '/' . $_GET['dir']) : $rootDir;
  18. // Ensure the current directory is within the root directory
  19. if (strpos($currentDir, $rootDir) !== 0) {
  20. $currentDir = $rootDir;
  21. }
  22. // Handle file upload
  23. if (isset($_POST['upload'])) {
  24. $targetFile = $currentDir . '/' . basename($_FILES['file']['name']);
  25. if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)) {
  26. $message = "File uploaded successfully.";
  27. } else {
  28. $message = "Failed to upload file.";
  29. }
  30. }
  31. // Handle file deletion
  32. if (isset($_POST['delete'])) {
  33. $fileToDelete = $currentDir . '/' . $_POST['filename'];
  34. if (unlink($fileToDelete)) {
  35. $message = "File deleted successfully.";
  36. } else {
  37. $message = "Failed to delete file.";
  38. }
  39. }
  40. // Handle file renaming
  41. if (isset($_POST['rename'])) {
  42. $oldName = $currentDir . '/' . $_POST['oldname'];
  43. $newName = $currentDir . '/' . $_POST['newname'];
  44. if (rename($oldName, $newName)) {
  45. $message = "File renamed successfully.";
  46. } else {
  47. $message = "Failed to rename file.";
  48. }
  49. }
  50. // Handle folder creation
  51. if (isset($_POST['create_folder'])) {
  52. $newFolder = $currentDir . '/' . $_POST['foldername'];
  53. if (mkdir($newFolder, 0755, true)) {
  54. $message = "Folder created successfully.";
  55. } else {
  56. $message = "Failed to create folder.";
  57. }
  58. }
  59. // Handle folder deletion
  60. if (isset($_POST['delete_folder'])) {
  61. $folderToDelete = $currentDir . '/' . $_POST['foldername'];
  62. if (rmdir($folderToDelete)) {
  63. $message = "Folder deleted successfully.";
  64. } else {
  65. $message = "Failed to delete folder.";
  66. }
  67. }
  68. // Handle folder backup
  69. if (isset($_POST['backup_folder'])) {
  70. $backupDir = realpath(__DIR__ . '/../../' . $websiteConfig['backup_folder']);
  71. $backupFile = $backupDir . '/' . basename($currentDir) . '_' . date('Ymd_His') . '.tar';
  72. $command = "tar -cf $backupFile -C " . escapeshellarg($currentDir) . " .";
  73. exec($command, $output, $returnVar);
  74. if ($returnVar === 0) {
  75. $message = "Folder backed up successfully.";
  76. } else {
  77. $message = "Failed to back up folder.";
  78. }
  79. }
  80. // Get list of files and directories
  81. $items = scandir($currentDir);
  82. // Get relative path for display
  83. $relativePath = str_replace($rootDir, '', $currentDir);
  84. if ($relativePath === '') {
  85. $relativePath = '/';
  86. }
  87. ?>
  88. <!DOCTYPE html>
  89. <html lang="en">
  90. <head>
  91. <meta charset="UTF-8">
  92. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  93. <title>Manage Files</title>
  94. </head>
  95. <body>
  96. <div class="container">
  97. <form method="post" action="../index.php">
  98. <button type="submit">Back to Admin Panel</button>
  99. </form>
  100. <h1>File Manager</h1>
  101. <form method="get" action="manage_files.php">
  102. <label for="websiteId">Select Website:</label>
  103. <select name="websiteId" id="websiteId" onchange="this.form.submit()">
  104. <?php foreach ($config as $site): ?>
  105. <?php if (isset($site['id'])): ?>
  106. <option value="<?php echo $site['id']; ?>" <?php echo ($websiteId == $site['id']) ? 'selected' : ''; ?>>
  107. <?php echo htmlspecialchars($site['name']); ?>
  108. </option>
  109. <?php endif; ?>
  110. <?php endforeach; ?>
  111. </select>
  112. </form>
  113. <?php if (isset($message)): ?>
  114. <p class="message"><?php echo $message; ?></p>
  115. <?php endif; ?>
  116. <h2>Current Directory: <?php echo htmlspecialchars($relativePath); ?></h2>
  117. <form method="post">
  118. <button type="submit" name="refresh">Refresh</button>
  119. </form>
  120. <table border="1">
  121. <thead>
  122. <tr>
  123. <th colspan="2"></th>
  124. </tr>
  125. </thead>
  126. <tbody>
  127. <?php if ($currentDir !== $rootDir): ?>
  128. <tr>
  129. <td colspan="2"><a href="?dir=<?php echo urlencode(dirname($relativePath)); ?>&websiteId=<?php echo $websiteId; ?>">.. (Parent Directory)</a></td>
  130. </tr>
  131. <?php endif; ?>
  132. <tr>
  133. <td colspan="2" style="text-align: center; background-color: yellow;"><strong>Folders</strong></td>
  134. </tr>
  135. <?php foreach ($items as $item): ?>
  136. <?php if ($item === '.' || $item === '..') continue; ?>
  137. <?php if (is_dir($currentDir . '/' . $item)): ?>
  138. <tr>
  139. <td><a href="?dir=<?php echo urlencode(ltrim($relativePath . '/' . $item, '/')); ?>&websiteId=<?php echo $websiteId; ?>"><?php echo htmlspecialchars($item); ?></a></td>
  140. <td class="actions">
  141. <form method="post" style="display:inline;">
  142. <input type="hidden" name="foldername" value="<?php echo htmlspecialchars($item); ?>">
  143. <button type="submit" name="delete_folder">Delete</button>
  144. </form>
  145. <form method="post" style="display:inline;">
  146. <input type="hidden" name="oldname" value="<?php echo htmlspecialchars($item); ?>">
  147. <input type="text" name="newname" placeholder="New name">
  148. <button type="submit" name="rename">Rename</button>
  149. </form>
  150. </td>
  151. </tr>
  152. <?php endif; ?>
  153. <?php endforeach; ?>
  154. <tr>
  155. <td colspan="2" style="text-align: center; background-color: orange;"><strong>Files</strong></td>
  156. </tr>
  157. <?php foreach ($items as $item): ?>
  158. <?php if ($item === '.' || $item === '..') continue; ?>
  159. <?php if (!is_dir($currentDir . '/' . $item)): ?>
  160. <tr>
  161. <td><?php echo htmlspecialchars($item); ?></td>
  162. <td class="actions">
  163. <form method="post" style="display:inline;">
  164. <input type="hidden" name="filename" value="<?php echo htmlspecialchars($item); ?>">
  165. <button type="submit" name="delete">Delete</button>
  166. </form>
  167. <form method="post" style="display:inline;">
  168. <input type="hidden" name="oldname" value="<?php echo htmlspecialchars($item); ?>">
  169. <input type="text" name="newname" placeholder="New name">
  170. <button type="submit" name="rename">Rename</button>
  171. </form>
  172. <a href="../../<?php echo htmlspecialchars($websiteConfig['folder'] . '/' . $relativePath . '/' . $item); ?>" download>Download</a>
  173. </td>
  174. </tr>
  175. <?php endif; ?>
  176. <?php endforeach; ?>
  177. <tr>
  178. <td colspan="2">
  179. <h2>Upload File</h2>
  180. <form method="post" enctype="multipart/form-data">
  181. <input type="file" name="file">
  182. <button type="submit" name="upload">Upload</button>
  183. </form>
  184. </td>
  185. </tr>
  186. <tr>
  187. <td colspan="2">
  188. <h2>Create Folder</h2>
  189. <form method="post">
  190. <input type="text" name="foldername" placeholder="Folder name">
  191. <button type="submit" name="create_folder">Create</button>
  192. </form>
  193. </td>
  194. </tr>
  195. <tr>
  196. <td colspan="2">
  197. <h2>Backup Folder</h2>
  198. <form method="post">
  199. <button type="submit" name="backup_folder">Backup Folder</button>
  200. </form>
  201. </td>
  202. </tr>
  203. </tbody>
  204. </table>
  205. </div>
  206. </body>
  207. </html>