36320-vm/admin_upload.php
Flatlogic Bot f1c13ea8d8 1.2
2025-11-26 19:33:57 +00:00

170 lines
6.9 KiB
PHP

<?php
session_start();
// Dummy authentication check - replace with your actual login check
if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) {
// header('Location: admin_login.php');
// die('Access Denied. You must be logged in as an admin.');
}
$message = '';
$error = '';
function parse_m3u_attributes($info_line) {
$attributes = [];
// Extract the part of the string that contains the attributes
preg_match_all('/ (\w+-.+?)="(.+?)"/', $info_line, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$attributes[$match[1]] = $match[2];
}
// Get the title from the last part of the line
$parts = explode(',', $info_line);
$title = trim(end($parts));
if ($title) {
$attributes['title'] = $title;
}
return $attributes;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['channels_file'])) {
$file = $_FILES['channels_file'];
$overwrite = isset($_POST['overwrite']);
if ($file['error'] === UPLOAD_ERR_OK) {
$filename = $file['tmp_name'];
$content = file_get_contents($filename);
$lines = explode(PHP_EOL, $content);
$new_channels = [];
$malformed_entries = 0;
if (strpos(trim($lines[0]), '#EXTM3U') !== 0) {
$error = 'Invalid M3U file. The file must start with #EXTM3U.';
} else {
for ($i = 0; $i < count($lines); $i++) {
$line = trim($lines[$i]);
if (strpos($line, '#EXTINF') === 0) {
$attributes = parse_m3u_attributes($line);
$stream_url = isset($lines[$i + 1]) ? trim($lines[$i + 1]) : '';
if (!empty($stream_url) && !empty($attributes['title'])) {
$new_channels[] = [
'title' => $attributes['title'],
'category' => $attributes['group-title'] ?? 'General',
'poster_url' => $attributes['tvg-logo'] ?? '',
'stream_url' => $stream_url,
];
$i++; // Skip the next line as it's the URL
} else {
$malformed_entries++;
}
}
}
if (!empty($new_channels)) {
$content_file = 'premium_content.json';
$existing_channels = [];
if (!$overwrite && file_exists($content_file)) {
$existing_content = json_decode(file_get_contents($content_file), true);
if (is_array($existing_content)) {
$existing_channels = $existing_content;
}
}
$last_id = 0;
if (!empty($existing_channels)) {
$last_item = end($existing_channels);
$last_id = $last_item['id'] ?? 0;
}
foreach($new_channels as &$channel) {
$last_id++;
$channel['id'] = $last_id;
}
$all_channels = array_merge($existing_channels, $new_channels);
if (json_encode($all_channels, JSON_PRETTY_PRINT)) {
file_put_contents($content_file, json_encode($all_channels, JSON_PRETTY_PRINT));
$message = 'Successfully uploaded and processed ' . count($new_channels) . ' new channels.';
if ($malformed_entries > 0) {
$error = $malformed_entries . ' entries in your file were malformed and could not be imported.';
}
} else {
$error = 'Failed to encode data to JSON. Check for special characters.';
}
} else {
$error = 'No valid channel data found in the uploaded M3U file.';
if ($malformed_entries > 0) {
$error .= ' All entries appear to be malformed.';
}
}
}
} else {
$error = 'File upload failed with error code: ' . $file['error'];
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin - Upload M3U Playlist</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg bg-dark navbar-dark">
<div class="container">
<a class="navbar-brand" href="admin.php">Admin Panel</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" href="admin_users.php">Users</a></li>
<li class="nav-item"><a class="nav-link" href="admin_content.php">Content</a></li>
<li class="nav-item"><a class="nav-link active" href="admin_upload.php">Upload</a></li>
<li class="nav-item"><a class="nav-link" href="admin_settings.php">Settings</a></li>
</ul>
</div>
</div>
</nav>
<div class="container mt-5">
<h1 class="mb-4">Upload Premium Channels</h1>
<?php if ($message): ?>
<div class="alert alert-success"><?php echo $message; ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<div class="card">
<div class="card-body">
<h5 class="card-title">Upload an M3U Playlist File</h5>
<p class="card-text">
The file should be a valid M3U playlist (starting with <code>#EXTM3U</code>). The parser will attempt to extract channel information from <code>#EXTINF</code> lines.
</p>
<form action="admin_upload.php" method="post" enctype="multipart/form-data">
<div class="mb-3">
<label for="channels_file" class="form-label">M3U Playlist File (.m3u, .m3u8)</label>
<input class="form-control" type="file" id="channels_file" name="channels_file" accept=".m3u,.m3u8,.txt" required>
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="overwrite" name="overwrite">
<label class="form-check-label" for="overwrite">Overwrite existing channels</label>
<small class="form-text text-muted d-block">If checked, all current premium channels will be replaced by the ones in this file. If unchecked, new channels will be added to the existing list.</small>
</div>
<button type="submit" class="btn btn-primary">Upload</button>
</form>
</div>
</div>
<div class="mt-4">
<a href="admin.php" class="btn btn-secondary">Back to Admin</a>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>