gamg6688/firmwarefusion-hub
0
1```php2<?php3require_once 'auth.php';4requireAuth();5 6// Configuration7$uploadDir = __DIR__ . '/storage/uploads/';8$maxFileSize = 100 * 1024 * 1024; // 100MB9$allowedMimeTypes = [10 'application/zip',11 'application/x-zip-compressed',12 'application/x-rar-compressed',13 'application/octet-stream', // For .bin files14 'application/x-msdownload' // For .exe files15];16 17// Create upload directory if it doesn't exist18if (!file_exists($uploadDir)) {19 mkdir($uploadDir, 0755, true);20}21 22$errors = [];23$success = false;24 25if ($_SERVER['REQUEST_METHOD'] === 'POST') {26 $csrf_token = $_POST['csrf_token'] ?? '';27 28 // Validate CSRF token29 if (!validateCSRFToken($csrf_token)) {30 $errors[] = "Invalid CSRF token.";31 }32 33 // Check if file was uploaded34 if (!isset($_FILES['firmware_file']) || $_FILES['firmware_file']['error'] === UPLOAD_ERR_NO_FILE) {35 $errors[] = "No file was uploaded.";36 } else {37 $file = $_FILES['firmware_file'];38 39 // Validate the uploaded file40 $validationErrors = validateUploadedFile($file, $maxFileSize, $allowedMimeTypes);41 if (!empty($validationErrors)) {42 $errors = array_merge($errors, $validationErrors);43 }44 45 // Process form data46 $deviceName = trim($_POST['device_name'] ?? '');47 $deviceModel = trim($_POST['device_model'] ?? '');48 $manufacturer = trim($_POST['manufacturer'] ?? '');49 $version = trim($_POST['version'] ?? '');50 $changelog = trim($_POST['changelog'] ?? '');51 $price = intval($_POST['price'] ?? 0);52 53 // Validate form inputs54 if (empty($deviceName)) {55 $errors[] = "Device name is required.";56 }57 if (empty($deviceModel)) {58 $errors[] = "Device model is required.";59 }60 if (empty($manufacturer)) {61 $errors[] = "Manufacturer is required.";62 }63 if (empty($version)) {64 $errors[] = "Version is required.";65 }66 if ($price < 0) {67 $errors[] = "Price cannot be negative.";68 }69 70 // If no errors, proceed with upload71 if (empty($errors)) {72 // Generate secure filename73 $filename = generateSecureFilename($file['name']);74 $destination = $uploadDir . $filename;75 76 // Move uploaded file77 if (move_uploaded_file($file['tmp_name'], $destination)) {78 // Calculate file hashes79 $md5Hash = md5_file($destination);80 $sha1Hash = sha1_file($destination);81 82 // Insert into database83 try {84 $stmt = $pdo->prepare("85 INSERT INTO firmware_files (86 uploader_id, device_name, device_model, manufacturer, 87 file_name, file_path, file_size, version, 88 changelog, price, md5_hash, sha1_hash89 ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)90 ");91 92 $stmt->execute([93 $_SESSION['user_id'],94 $deviceName,95 $deviceModel,96 $manufacturer,97 $file['name'],98 $filename,99 $file['size'],100 $version,101 $changelog,102 $price,103 $md5Hash,104 $sha1Hash105 ]);106 107 $success = true;108 $message = "Firmware uploaded successfully! It will be reviewed by our team.";109 } catch (PDOException $e) {110 // Delete the uploaded file if DB insert fails111 unlink($destination);112 $errors[] = "Database error: " . $e->getMessage();113 }114 } else {115 $errors[] = "Failed to move uploaded file.";116 }117 }118 }119}120 121$csrf_token = generateCSRFToken();122?>123 124<!DOCTYPE html>125<html lang="en">126<head>127 <meta charset="UTF-8">128 <meta name="viewport" content="width=device-width, initial-scale=1.0">129 <title>Upload Firmware | FirmwareFusion</title>130 <link rel="stylesheet" href="../style.css">131 <script src="https://cdn.tailwindcss.com"></script>132</head>133<body class="bg-gray-100">134 <div class="container mx-auto px-4 py-8">135 <div class="max-w-3xl mx-auto bg-white rounded-lg shadow-md p-6">136 <h1 class="text-2xl font-bold text-gray-800 mb-6">Upload Firmware</h1>137 138 <?php if (!empty($errors)): ?>139 <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 mb-6">140 <?php foreach ($errors as $error): ?>141 <p><?php echo htmlspecialchars($error); ?></p>142 <?php endforeach; ?>143 </div>144 <?php endif; ?>145 146 <?php if ($success): ?>147 <div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 mb-6">148 <p><?php echo htmlspecialchars($message); ?></p>149 </div>150 <?php endif; ?>151 152 <form method="POST" enctype="multipart/form-data" class="space-y-6">153 <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token); ?>">154 155 <div class="grid grid-cols-1 md:grid-cols-2 gap-6">156 <div>157 <label for="device_name" class="block text-sm font-medium text-gray-700 mb-1">Device Name *</label>158 <input type="text" id="device_name" name="device_name" required159 class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">160 </div>161 162 <div>163 <label for="device_model" class="block text-sm font-medium text-gray-700 mb-1">Device Model *</label>164 <input type="text" id="device_model" name="device_model" required165 class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">166 </div>167 </div>168 169 <div class="grid grid-cols-1 md:grid-cols-2 gap-6">170 <div>171 <label for="manufacturer" class="block text-sm font-medium text-gray-700 mb-1">Manufacturer *</label>172 <input type="text" id="manufacturer" name="manufacturer" required173 class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">174 </div>175 176 <div>177 <label for="version" class="block text-sm font-medium text-gray-700 mb-1">Version *</label>178 <input type="text" id="version" name="version" required179 class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">180 </div>181 </div>182 183 <div>184 <label for="changelog" class="block text-sm font-medium text-gray-700 mb-1">Changelog</label>185 <textarea id="changelog" name="changelog" rows="3"186 class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"></textarea>187 </div>188 189 <div>190 <label for="price" class="block text-sm font-medium text-gray-700 mb-1">Price (credits)</label>191 <input type="number" id="price" name="price" min="0" value="0"192 class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">193 <p class="mt-1 text-sm text-gray-500">Set to 0 for free downloads</p>194 </div>195 196 <div>197 <label for="firmware_file" class="block text-sm font-medium text-gray-700 mb-1">Firmware File *</label>198 <div class="mt-1 flex items-center">199 <input type="file" id="firmware_file" name="firmware_file" required accept=".zip,.rar,.7z,.bin,.img,.ipsw,.exe"200 class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">201 </div>202 <p class="mt-1 text-sm text-gray-500">203 Max file size: 100MB. Allowed formats: ZIP, RAR, 7Z, BIN, IMG, IPSW, EXE204 </p>205 </div>206 207 <div class="flex justify-end">208 <button type="submit" class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">209 Upload Firmware210 </button>211 </div>212 </form>213 </div>214 </div>215</body>216</html>217```218 2193. Create a download.php file to securely serve files: