<?php
// 璁剧疆鍏佽鎿嶄綔鐨勭洰褰�
$basePath = "D:/vhostroot/LocalUser/yuda/www/";

// 鑾峰彇褰撳墠璺緞锛屽鏋滄病鏈変紶閫掕矾寰勫弬鏁帮紝鍒欎娇鐢ㄩ粯璁ょ洰褰�
$currentPath = isset($_GET['path']) ? $_GET['path'] : $basePath;
$currentPath = realpath($currentPath);

// 杈撳嚭褰撳墠璺緞锛屾柟渚胯皟璇�
echo "<p>Current Path: $currentPath</p>";

// 纭繚璺緞鍚堟硶锛岄槻姝㈣矾寰勯亶鍘嗘敾鍑�
$basePath = realpath($basePath); // 纭繚 basePath 鏄粷瀵硅矾寰�
if (strpos($currentPath, $basePath) !== 0) {
    die("Invalid directory.");
}

// 鍒楀嚭褰撳墠鐩綍涓嬬殑鏂囦欢
if (isset($_GET['list'])) {
    echo "<h2>Files in '$currentPath'</h2>";

    // 浣跨敤 scandir 鏉ュ垪鍑烘枃浠�
    $files = scandir($currentPath);

    // 濡傛灉娌℃湁鏂囦欢锛屽垯缁欏嚭鎻愮ず
    if ($files === false) {
        echo "<p>Failed to open directory.</p>";
    } else {
        echo "<ul>";
        foreach ($files as $file) {
            if ($file != "." && $file != "..") {
                $filePath = $currentPath . DIRECTORY_SEPARATOR . $file;
                echo "<li>";
                if (is_dir($filePath)) {
                    echo "<strong>[DIR]</strong> <a href='?list&path=$filePath'>$file</a> | 
                          <a href='?createFolder&path=$filePath'>Create Folder</a> | 
                          <a href='?delete&path=$filePath'>Delete</a>";
                } else {
                    echo "<a href='?view&path=$filePath'>$file</a> | 
                          <a href='?edit&path=$filePath'>Edit</a> | 
                          <a href='?delete&path=$filePath'>Delete</a> | 
                          <a href='?rename&path=$filePath'>Rename</a>";
                }
                echo "</li>";
            }
        }
        echo "</ul>";
    }
}

// 鏌ョ湅鏂囦欢鍐呭
if (isset($_GET['view'])) {
    $filePath = $_GET['path'];
    if (file_exists($filePath)) {
        echo "<h2>Content of '$filePath'</h2>";
        echo "<pre>" . htmlspecialchars(file_get_contents($filePath)) . "</pre>";
    } else {
        echo "File does not exist.";
    }
}

// 缂栬緫鏂囦欢鍐呭
if (isset($_GET['edit'])) {
    $filePath = $_GET['path'];
    if (file_exists($filePath)) {
        $content = file_get_contents($filePath);
        echo "<h2>Edit file: '$filePath'</h2>";
        echo "<form method='POST'>
                <textarea name='content' rows='10' cols='50'>" . htmlspecialchars($content) . "</textarea><br>
                <input type='hidden' name='file' value='$filePath'>
                <button type='submit' name='save'>Save</button>
              </form>";
    } else {
        echo "File does not exist.";
    }
}

// 淇濆瓨缂栬緫鐨勬枃浠�
if (isset($_POST['save'])) {
    $filePath = $_POST['file'];
    $content = $_POST['content'];
    if (file_put_contents($filePath, $content) !== false) {
        echo "File '$filePath' saved successfully.";
    } else {
        echo "Failed to save file.";
    }
}

// 鍒犻櫎鏂囦欢鎴栨枃浠跺す
if (isset($_GET['delete'])) {
    $filePath = $_GET['path'];
    if (is_dir($filePath)) {
        rmdir($filePath); // 鍒犻櫎绌烘枃浠跺す
    } else {
        unlink($filePath); // 鍒犻櫎鏂囦欢
    }
    echo "Deleted '$filePath'.";
}

// 閲嶅懡鍚嶆枃浠舵垨鏂囦欢澶�
if (isset($_GET['rename'])) {
    $filePath = $_GET['path'];
    echo "<h2>Rename '$filePath'</h2>";
    echo "<form method='POST'>
            <input type='text' name='newname' placeholder='New name' required><br>
            <input type='hidden' name='file' value='$filePath'>
            <button type='submit' name='renameFile'>Rename</button>
          </form>";
}

// 鎵ц閲嶅懡鍚嶆搷浣�
if (isset($_POST['renameFile'])) {
    $filePath = $_POST['file'];
    $newName = $_POST['newname'];
    $newPath = dirname($filePath) . DIRECTORY_SEPARATOR . $newName;
    if (rename($filePath, $newPath)) {
        echo "File renamed successfully.";
    } else {
        echo "Failed to rename file.";
    }
}

// 鍒涘缓鏂版枃浠跺姛鑳�
if (isset($_POST['create'])) {
    $newFileName = $_POST['filename'];
    $newFileContent = $_POST['filecontent'];
    $newFilePath = $currentPath . DIRECTORY_SEPARATOR . $newFileName;

    if (file_put_contents($newFilePath, $newFileContent) !== false) {
        echo "File '$newFileName' created successfully.";
    } else {
        echo "Failed to create file.";
    }
}

// 鍒涘缓鏂版枃浠跺す鍔熻兘
if (isset($_GET['createFolder'])) {
    $folderPath = $_GET['path'];
    $newFolderName = $folderPath . DIRECTORY_SEPARATOR . "NewFolder";
    if (mkdir($newFolderName)) {
        echo "Folder created successfully.";
    } else {
        echo "Failed to create folder.";
    }
}
?>

<!-- HTML 琛ㄥ崟鏉ユ柊寤烘枃浠� -->
<h2>Create a new file</h2>
<form method="POST">
    <label for="filename">File Name:</label><br>
    <input type="text" name="filename" placeholder="Enter filename" required><br><br>
    <label for="filecontent">File Content:</label><br>
    <textarea name="filecontent" rows="10" cols="50" required></textarea><br><br>
    <button type="submit" name="create">Create File</button>
</form>