403Webshell
Server IP : 127.0.0.1  /  Your IP : 192.168.99.1
Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9
System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64
User : Administrator ( 0)
PHP Version : 7.4.33
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : OFF  |  Perl : OFF  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /laragon4/www/bpkd_/admin/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /laragon4/www/bpkd_/admin/media_manager.php
<?php
$page_title = 'Manajemen Media';
require_once 'config/database.php';
require_once 'includes/init.php';
requireLogin();

// =====================================================
// KONFIGURASI
// =====================================================
$upload_dirs = [
    'articles' => '../uploads/articles/',
    'pages' => '../uploads/pages/',
    'gallery' => '../uploads/gallery/',
    'carousel' => '../uploads/carousel/',
    'settings' => '../uploads/settings/',
    'avatars' => '../uploads/avatars/',
    'testimonials' => '../uploads/testimonials/',
    'download' => '../uploads/download/'
];

$allowed_types = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'mp4', 'pdf', 'doc', 'docx', 'xls', 'xlsx'];

// Buat semua folder jika belum ada
foreach ($upload_dirs as $dir) {
    if (!file_exists($dir)) {
        mkdir($dir, 0777, true);
    }
}

// =====================================================
// HANDLE UPLOAD
// =====================================================
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['upload'])) {
    $target_type = sanitize($_POST['target_type'] ?? 'articles');
    $title = sanitize($_POST['title'] ?? '');
    $alt_text = sanitize($_POST['alt_text'] ?? '');
    $description = sanitize($_POST['description'] ?? '');
    $uploaded_by = $_SESSION['user_id'];
    
    if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
        $file = $_FILES['file'];
        $file_ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
        $file_size = $file['size'];
        $file_type = $file['type'];
        
        // Validasi tipe file
        if (!in_array($file_ext, $allowed_types)) {
            $error = "Tipe file tidak diizinkan. Format yang diizinkan: " . implode(', ', $allowed_types);
        }
        // Validasi ukuran (max 10MB)
        elseif ($file_size > 10 * 1024 * 1024) {
            $error = "Ukuran file maksimal 10MB";
        }
        else {
            // Buat nama file unik
            $new_filename = time() . '_' . uniqid() . '.' . $file_ext;
            $target_dir = $upload_dirs[$target_type];
            $target_file = $target_dir . $new_filename;
            $filepath = 'uploads/' . $target_type . '/' . $new_filename;
            
            if (move_uploaded_file($file['tmp_name'], $target_file)) {
                // Simpan ke database media
                $query = "INSERT INTO media (filename, filepath, filetype, filesize, title, alt_text, description, uploaded_by, created_at) 
                          VALUES ('{$file['name']}', '$filepath', '$file_type', $file_size, '$title', '$alt_text', '$description', $uploaded_by, NOW())";
                
                if ($conn->query($query)) {
                    $success = "File berhasil diupload!";
                } else {
                    $error = "Gagal menyimpan ke database: " . $conn->error;
                }
            } else {
                $error = "Gagal mengupload file";
            }
        }
    } else {
        $error = "Silakan pilih file";
    }
}

// =====================================================
// HANDLE DELETE
// =====================================================
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    
    // Cek apakah file digunakan di artikel
    $check_articles = $conn->query("SELECT id FROM articles WHERE featured_image LIKE '%$id%'");
    $check_pages = $conn->query("SELECT id FROM pages WHERE featured_image LIKE '%$id%'");
    
    if ($check_articles->num_rows > 0 || $check_pages->num_rows > 0) {
        $error = "File tidak dapat dihapus karena sedang digunakan di konten!";
    } else {
        // Ambil data file
        $media = $conn->query("SELECT * FROM media WHERE id = $id")->fetch_assoc();
        
        if ($media) {
            // Hapus file fisik
            $file_path = "../" . $media['filepath'];
            if (file_exists($file_path)) {
                unlink($file_path);
            }
            
            // Hapus dari database
            if ($conn->query("DELETE FROM media WHERE id = $id")) {
                $success = "File berhasil dihapus!";
            } else {
                $error = "Gagal menghapus dari database: " . $conn->error;
            }
        }
    }
}

// =====================================================
// HANDLE EDIT
// =====================================================
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit'])) {
    $id = (int)$_POST['id'];
    $title = sanitize($_POST['title']);
    $alt_text = sanitize($_POST['alt_text']);
    $description = sanitize($_POST['description']);
    
    $query = "UPDATE media SET 
              title = '$title', 
              alt_text = '$alt_text', 
              description = '$description' 
              WHERE id = $id";
    
    if ($conn->query($query)) {
        $success = "Data berhasil diupdate!";
    } else {
        $error = "Gagal mengupdate: " . $conn->error;
    }
}

// =====================================================
// FILTER DAN PAGINATION
// =====================================================
$limit = 20;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

$type_filter = isset($_GET['type']) ? sanitize($_GET['type']) : '';
$search = isset($_GET['search']) ? sanitize($_GET['search']) : '';

$where = ["1=1"];
if (!empty($type_filter)) {
    $where[] = "filetype LIKE '%$type_filter%'";
}
if (!empty($search)) {
    $where[] = "(filename LIKE '%$search%' OR title LIKE '%$search%')";
}

$where_clause = implode(' AND ', $where);

// Hitung total
$total_query = "SELECT COUNT(*) as total FROM media WHERE $where_clause";
$total_result = $conn->query($total_query);
$total_records = $total_result->fetch_assoc()['total'];
$total_pages = ceil($total_records / $limit);

// Ambil data media
$media = $conn->query("
    SELECT m.*, u.full_name as uploader_name 
    FROM media m 
    LEFT JOIN users u ON m.uploaded_by = u.id 
    WHERE $where_clause 
    ORDER BY m.created_at DESC 
    LIMIT $offset, $limit
");

// Include header
require_once 'includes/header.php';
?>

<div class="container-fluid">
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">
            <i class="fas fa-folder-open me-2"></i>Manajemen Media
        </h1>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#uploadModal">
            <i class="fas fa-upload me-2"></i>Upload File Baru
        </button>
    </div>

    <!-- Info Penggunaan Media -->
    <div class="row mb-4">
        <div class="col-xl-3 col-md-6 mb-3">
            <div class="card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
                                Total File</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                <?php 
                                $total = $conn->query("SELECT COUNT(*) as total FROM media")->fetch_assoc()['total'];
                                echo $total;
                                ?>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-files fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-3">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                Total Ukuran</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                <?php
                                $size = $conn->query("SELECT SUM(filesize) as total FROM media")->fetch_assoc()['total'];
                                echo formatBytes($size);
                                ?>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-database fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-3">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-info text-uppercase mb-1">
                                Digunakan di Artikel</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                <?php
                                $used = $conn->query("
                                    SELECT COUNT(DISTINCT a.id) as total 
                                    FROM articles a 
                                    WHERE a.featured_image IS NOT NULL
                                ")->fetch_assoc()['total'];
                                echo $used;
                                ?>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-newspaper fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-3">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                                Digunakan di Halaman</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                <?php
                                $used_pages = $conn->query("
                                    SELECT COUNT(DISTINCT p.id) as total 
                                    FROM pages p 
                                    WHERE p.featured_image IS NOT NULL
                                ")->fetch_assoc()['total'];
                                echo $used_pages;
                                ?>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-file-alt fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Success/Error Messages -->
    <?php if (isset($success)): ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="fas fa-check-circle me-2"></i><?php echo $success; ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <?php if (isset($error)): ?>
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <i class="fas fa-exclamation-circle me-2"></i><?php echo $error; ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <!-- Filter Card -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-white">
                <i class="fas fa-filter me-2"></i>Filter Pencarian
            </h6>
        </div>
        <div class="card-body">
            <form method="GET" class="row g-3">
                <div class="col-md-4">
                    <label class="form-label">Cari File</label>
                    <input type="text" class="form-control" name="search" value="<?php echo htmlspecialchars($search); ?>" placeholder="Nama file atau judul...">
                </div>
                
                <div class="col-md-3">
                    <label class="form-label">Tipe File</label>
                    <select class="form-control" name="type">
                        <option value="">Semua Tipe</option>
                        <option value="image" <?php echo $type_filter == 'image' ? 'selected' : ''; ?>>Gambar</option>
                        <option value="video" <?php echo $type_filter == 'video' ? 'selected' : ''; ?>>Video</option>
                        <option value="pdf" <?php echo $type_filter == 'pdf' ? 'selected' : ''; ?>>PDF</option>
                        <option value="doc" <?php echo $type_filter == 'doc' ? 'selected' : ''; ?>>Word</option>
                        <option value="xls" <?php echo $type_filter == 'xls' ? 'selected' : ''; ?>>Excel</option>
                    </select>
                </div>
                
                <div class="col-md-3">
                    <label class="form-label">Aksi</label>
                    <div class="d-flex">
                        <button type="submit" class="btn btn-primary me-2">
                            <i class="fas fa-search me-2"></i>Cari
                        </button>
                        <a href="media_manager.php" class="btn btn-secondary">
                            <i class="fas fa-sync-alt"></i>
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Media Table -->
    <div class="card shadow mb-4">
        <div class="card-header py-3 d-flex justify-content-between align-items-center">
            <h6 class="m-0 font-weight-bold text-white">
                <i class="fas fa-list me-2"></i>Daftar File
                <span class="badge bg-primary ms-2"><?php echo $total_records; ?> Total</span>
            </h6>
        </div>
        
        <div class="card-body">
            <?php if ($media && $media->num_rows > 0): ?>
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Preview</th>
                                <th>Nama File</th>
                                <th>Tipe</th>
                                <th>Ukuran</th>
                                <th>Digunakan di</th>
                                <th>Upload oleh</th>
                                <th>Tanggal</th>
                                <th>Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php while($file = $media->fetch_assoc()): 
                                $file_ext = strtolower(pathinfo($file['filename'], PATHINFO_EXTENSION));
                                $is_image = in_array($file_ext, ['jpg', 'jpeg', 'png', 'gif', 'webp']);
                                
                                // Cek apakah file digunakan
                                $used_in_articles = $conn->query("SELECT COUNT(*) as total FROM articles WHERE featured_image LIKE '%{$file['filepath']}%'")->fetch_assoc()['total'];
                                $used_in_pages = $conn->query("SELECT COUNT(*) as total FROM pages WHERE featured_image LIKE '%{$file['filepath']}%'")->fetch_assoc()['total'];
                                $used_in_gallery = $conn->query("SELECT COUNT(*) as total FROM gallery WHERE image = '{$file['filepath']}'")->fetch_assoc()['total'];
                                
                                $total_used = $used_in_articles + $used_in_pages + $used_in_gallery;
                            ?>
                            <tr>
                                <td>
                                    <?php if ($is_image): ?>
                                        <img src="../<?php echo $file['filepath']; ?>" alt="<?php echo $file['title']; ?>" style="width: 50px; height: 50px; object-fit: cover; border-radius: 5px;">
                                    <?php else: ?>
                                        <div class="bg-secondary text-white d-flex align-items-center justify-content-center" style="width: 50px; height: 50px; border-radius: 5px;">
                                            <i class="fas fa-file-<?php echo getFileIcon($file_ext); ?>"></i>
                                        </div>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <strong><?php echo $file['title'] ?: $file['filename']; ?></strong><br>
                                    <small class="text-muted"><?php echo $file['filename']; ?></small>
                                </td>
                                <td>
                                    <span class="badge bg-<?php echo getFileBadgeColor($file_ext); ?>">
                                        <?php echo strtoupper($file_ext); ?>
                                    </span>
                                </td>
                                <td><?php echo formatBytes($file['filesize']); ?></td>
                                <td>
                                    <?php if ($total_used > 0): ?>
                                        <span class="badge bg-info"><?php echo $total_used; ?> konten</span>
                                        <button type="button" class="btn btn-sm btn-link" onclick="showUsage(<?php echo $file['id']; ?>)">
                                            <i class="fas fa-info-circle"></i>
                                        </button>
                                    <?php else: ?>
                                        <span class="badge bg-secondary">Tidak digunakan</span>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo $file['uploader_name']; ?></td>
                                <td><?php echo date('d/m/Y H:i', strtotime($file['created_at'])); ?></td>
                                <td>
                                    <div class="btn-group" role="group">
                                        <button type="button" class="btn btn-sm btn-info" onclick="editFile(<?php echo $file['id']; ?>)">
                                            <i class="fas fa-edit"></i>
                                        </button>
                                        <button type="button" class="btn btn-sm btn-success" onclick="copyUrl('<?php echo $file['filepath']; ?>')">
                                            <i class="fas fa-link"></i>
                                        </button>
                                        <?php if ($total_used == 0): ?>
                                            <a href="?delete=<?php echo $file['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin ingin menghapus file ini?')">
                                                <i class="fas fa-trash"></i>
                                            </a>
                                        <?php else: ?>
                                            <button type="button" class="btn btn-sm btn-danger" disabled title="Tidak bisa dihapus karena sedang digunakan">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
                
                <!-- Pagination -->
                <?php if ($total_pages > 1): ?>
                <nav aria-label="Page navigation" class="mt-4">
                    <ul class="pagination justify-content-center">
                        <?php for($i = 1; $i <= $total_pages; $i++): ?>
                        <li class="page-item <?php echo $i == $page ? 'active' : ''; ?>">
                            <a class="page-link" href="?page=<?php echo $i; ?><?php echo buildQueryString(['page']); ?>">
                                <?php echo $i; ?>
                            </a>
                        </li>
                        <?php endfor; ?>
                    </ul>
                </nav>
                <?php endif; ?>
                
            <?php else: ?>
                <div class="text-center py-5">
                    <i class="fas fa-folder-open fa-4x text-muted mb-3"></i>
                    <h5>Belum Ada File</h5>
                    <p class="text-muted">Klik tombol "Upload File Baru" untuk menambahkan file.</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Upload Modal -->
<div class="modal fade" id="uploadModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="POST" enctype="multipart/form-data">
                <div class="modal-header">
                    <h5 class="modal-title">
                        <i class="fas fa-upload me-2"></i>Upload File Baru
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Pilih File <span class="text-danger">*</span></label>
                        <input type="file" class="form-control" name="file" required>
                        <small class="text-muted">
                            Format: JPG, PNG, GIF, WEBP, MP4, PDF, DOC, DOCX, XLS, XLSX. Maks 10MB.
                        </small>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Tujuan Upload</label>
                        <select class="form-control" name="target_type">
                            <option value="articles">Artikel</option>
                            <option value="pages">Halaman</option>
                            <option value="gallery">Galeri</option>
                            <option value="carousel">Carousel</option>
                            <option value="settings">Pengaturan</option>
                            <option value="avatars">Avatar</option>
                            <option value="testimonials">Testimonial</option>
                            <option value="download">Download</option>
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Judul (Opsional)</label>
                        <input type="text" class="form-control" name="title">
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Alt Text (untuk gambar)</label>
                        <input type="text" class="form-control" name="alt_text">
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Deskripsi</label>
                        <textarea class="form-control" name="description" rows="3"></textarea>
                    </div>
                </div>
                
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" name="upload" class="btn btn-primary">
                        <i class="fas fa-upload me-2"></i>Upload
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Edit Modal -->
<div class="modal fade" id="editModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="POST">
                <div class="modal-header">
                    <h5 class="modal-title">
                        <i class="fas fa-edit me-2"></i>Edit File Info
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                
                <div class="modal-body">
                    <input type="hidden" name="id" id="editId">
                    
                    <div class="mb-3">
                        <label class="form-label">Judul</label>
                        <input type="text" class="form-control" name="title" id="editTitle">
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Alt Text</label>
                        <input type="text" class="form-control" name="alt_text" id="editAltText">
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Deskripsi</label>
                        <textarea class="form-control" name="description" id="editDescription" rows="3"></textarea>
                    </div>
                </div>
                
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" name="edit" class="btn btn-primary">
                        <i class="fas fa-save me-2"></i>Simpan
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
// Edit file
function editFile(id) {
    // Fetch data via AJAX
    fetch('get_media_item.php?id=' + id)
        .then(response => response.json())
        .then(data => {
            document.getElementById('editId').value = data.id;
            document.getElementById('editTitle').value = data.title || '';
            document.getElementById('editAltText').value = data.alt_text || '';
            document.getElementById('editDescription').value = data.description || '';
            
            new bootstrap.Modal(document.getElementById('editModal')).show();
        });
}

// Copy URL
function copyUrl(path) {
    const url = window.location.origin + '/bpkd/' + path;
    navigator.clipboard.writeText(url).then(() => {
        alert('URL berhasil disalin!');
    });
}

// Show usage
function showUsage(id) {
    fetch('get_media_usage.php?id=' + id)
        .then(response => response.json())
        .then(data => {
            let message = 'File digunakan di:\n\n';
            if (data.articles > 0) message += '- Artikel: ' + data.articles + ' konten\n';
            if (data.pages > 0) message += '- Halaman: ' + data.pages + ' konten\n';
            if (data.gallery > 0) message += '- Galeri: ' + data.gallery + ' konten\n';
            alert(message);
        });
}
</script>

<?php
// Helper functions
function formatBytes($bytes, $precision = 2) {
    $units = ['B', 'KB', 'MB', 'GB'];
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
    $bytes /= pow(1024, $pow);
    return round($bytes, $precision) . ' ' . $units[$pow];
}

function getFileIcon($ext) {
    $icons = [
        'pdf' => 'pdf',
        'doc' => 'word',
        'docx' => 'word',
        'xls' => 'excel',
        'xlsx' => 'excel',
        'jpg' => 'image',
        'jpeg' => 'image',
        'png' => 'image',
        'gif' => 'image',
        'mp4' => 'video'
    ];
    return $icons[$ext] ?? 'alt';
}

function getFileBadgeColor($ext) {
    $colors = [
        'pdf' => 'danger',
        'doc' => 'primary',
        'docx' => 'primary',
        'xls' => 'success',
        'xlsx' => 'success',
        'jpg' => 'info',
        'jpeg' => 'info',
        'png' => 'info',
        'gif' => 'warning',
        'mp4' => 'secondary'
    ];
    return $colors[$ext] ?? 'dark';
}

function buildQueryString($exclude = []) {
    $params = $_GET;
    foreach($exclude as $key) {
        unset($params[$key]);
    }
    if(empty($params)) return '';
    return '&' . http_build_query($params);
}

require_once 'includes/footer.php';
?>

Youez - 2016 - github.com/yon3zu
LinuXploit