353 lines
14 KiB
PHP
353 lines
14 KiB
PHP
<?php
|
||
// install.php
|
||
// Single installation file to set up the project on Windows (XAMPP) or Linux.
|
||
|
||
require_once 'db/config.php';
|
||
|
||
// Disable output buffering to show progress in real-time
|
||
if (function_exists('apache_setenv')) {
|
||
@apache_setenv('no-gzip', 1);
|
||
}
|
||
@ini_set('zlib.output_compression', 0);
|
||
@ini_set('implicit_flush', 1);
|
||
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
|
||
ob_implicit_flush(1);
|
||
|
||
echo "<!DOCTYPE html><html><head><title>Installation</title><style>body{font-family: sans-serif; padding: 20px; line-height: 1.6;} .success{color: green;} .error{color: red;} .step{margin-bottom: 10px;}</style></head><body>";
|
||
echo "<h1>Car Sells in Afghanistan - Installation</h1>";
|
||
|
||
try {
|
||
// 1. Connect to Database Server
|
||
$pdo = db();
|
||
echo "<div class='step success'>✅ Connected to Database Server.</div>";
|
||
|
||
// 2. Create Database (if it doesn't exist)
|
||
// Note: On some hosting/VMs, the user might not have permission to create databases, only tables.
|
||
// We try to create it if we are 'root' or similar, but otherwise assume it exists if connection worked.
|
||
$dbName = DB_NAME;
|
||
try {
|
||
$pdo->exec("CREATE DATABASE IF NOT EXISTS `$dbName` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
||
echo "<div class='step success'>✅ Database `$dbName` checked/created.</div>";
|
||
} catch (PDOException $e) {
|
||
// Ignore if we can't create DB (might already be connected to it)
|
||
echo "<div class='step'>ℹ️ Note: Could not create database (might already exist or permission denied). Proceeding...</div>";
|
||
}
|
||
|
||
// Select the database
|
||
$pdo->exec("USE `$dbName`");
|
||
|
||
// 3. Drop existing tables (Clean Install)
|
||
$pdo->exec("SET FOREIGN_KEY_CHECKS=0");
|
||
$tables = ['reviews', 'bookings', 'cars', 'users'];
|
||
foreach ($tables as $table) {
|
||
$pdo->exec("DROP TABLE IF EXISTS `$table`");
|
||
}
|
||
$pdo->exec("SET FOREIGN_KEY_CHECKS=1");
|
||
echo "<div class='step success'>✅ Existing tables dropped (Clean Install).</div>";
|
||
|
||
// 4. Create Tables
|
||
|
||
// Users Table
|
||
$pdo->exec("CREATE TABLE users (
|
||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||
username VARCHAR(50) UNIQUE NOT NULL,
|
||
email VARCHAR(100) UNIQUE NOT NULL,
|
||
password VARCHAR(255) NOT NULL,
|
||
role VARCHAR(20) DEFAULT 'user',
|
||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||
)");
|
||
echo "<div class='step success'>✅ Table `users` created.</div>";
|
||
|
||
// Cars Table
|
||
$pdo->exec("CREATE TABLE cars (
|
||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||
user_id INT NULL,
|
||
title VARCHAR(255) NULL,
|
||
make VARCHAR(100),
|
||
model VARCHAR(100),
|
||
year INT,
|
||
mileage INT,
|
||
price DECIMAL(10,2),
|
||
description TEXT,
|
||
status VARCHAR(50) NOT NULL DEFAULT 'pending',
|
||
color VARCHAR(50),
|
||
province VARCHAR(100),
|
||
city VARCHAR(100),
|
||
image_url VARCHAR(255) DEFAULT NULL,
|
||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
|
||
)");
|
||
echo "<div class='step success'>✅ Table `cars` created.</div>";
|
||
|
||
// Bookings Table
|
||
$pdo->exec("CREATE TABLE bookings (
|
||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||
user_id INT NOT NULL,
|
||
car_id INT NOT NULL,
|
||
booking_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||
status VARCHAR(50) NOT NULL DEFAULT 'pending',
|
||
bank_province VARCHAR(100) NULL,
|
||
bank_account_number VARCHAR(100) NULL,
|
||
sale_price DECIMAL(10, 2) NULL,
|
||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||
FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE
|
||
)");
|
||
echo "<div class='step success'>✅ Table `bookings` created.</div>";
|
||
|
||
// Reviews Table
|
||
$pdo->exec("CREATE TABLE reviews (
|
||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||
car_id INT NOT NULL,
|
||
user_id INT NOT NULL,
|
||
rating INT NOT NULL CHECK (rating >= 1 AND rating <= 5),
|
||
review TEXT,
|
||
status VARCHAR(20) DEFAULT 'pending',
|
||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||
FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE,
|
||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||
)");
|
||
echo "<div class='step success'>✅ Table `reviews` created.</div>";
|
||
|
||
// 5. Create Default Admin User
|
||
$adminUser = 'admin';
|
||
$adminEmail = 'admin@gmail.com';
|
||
$adminPass = '123'; // As requested
|
||
$adminHash = password_hash($adminPass, PASSWORD_DEFAULT);
|
||
|
||
$stmt = $pdo->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, 'admin')");
|
||
$stmt->execute([$adminUser, $adminEmail, $adminHash]);
|
||
$adminId = $pdo->lastInsertId();
|
||
echo "<div class='step success'>✅ Admin user created.<br> Username: <b>$adminUser</b><br> Email: <b>$adminEmail</b><br> Password: <b>$adminPass</b></div>";
|
||
|
||
// 6. Insert Sample Data (Cars)
|
||
$carsData = [
|
||
[
|
||
'title' => 'Toyota Corolla 2020 Clean',
|
||
'make' => 'Toyota',
|
||
'model' => 'Corolla',
|
||
'year' => 2020,
|
||
'mileage' => 15000,
|
||
'price' => 18500.00,
|
||
'description' => 'Very clean car, no accidents. Perfect for city driving.',
|
||
'status' => 'approved',
|
||
'color' => 'White',
|
||
'province' => 'Kabul',
|
||
'city' => 'Kabul',
|
||
'image_url' => 'https://images.pexels.com/photos/112460/pexels-photo-112460.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Honda Civic 2018 Sport',
|
||
'make' => 'Honda',
|
||
'model' => 'Civic',
|
||
'year' => 2018,
|
||
'mileage' => 45000,
|
||
'price' => 16200.00,
|
||
'description' => 'Sport mode, leather seats, sunroof. Excellent condition.',
|
||
'status' => 'approved',
|
||
'color' => 'Black',
|
||
'province' => 'Herat',
|
||
'city' => 'Herat',
|
||
'image_url' => 'https://images.pexels.com/photos/170811/pexels-photo-170811.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Ford Ranger 2019 4x4',
|
||
'make' => 'Ford',
|
||
'model' => 'Ranger',
|
||
'year' => 2019,
|
||
'mileage' => 30000,
|
||
'price' => 25000.00,
|
||
'description' => 'Strong pickup for tough roads. 4x4 capability.',
|
||
'status' => 'pending',
|
||
'color' => 'Blue',
|
||
'province' => 'Kandahar',
|
||
'city' => 'Kandahar',
|
||
'image_url' => 'https://images.pexels.com/photos/919073/pexels-photo-919073.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Toyota Land Cruiser 2022',
|
||
'make' => 'Toyota',
|
||
'model' => 'Land Cruiser',
|
||
'year' => 2022,
|
||
'mileage' => 5000,
|
||
'price' => 85000.00,
|
||
'description' => 'Luxury SUV, fully loaded, V8 engine.',
|
||
'status' => 'approved',
|
||
'color' => 'White',
|
||
'province' => 'Kabul',
|
||
'city' => 'Kabul',
|
||
'image_url' => 'https://images.pexels.com/photos/205740/pexels-photo-205740.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Toyota Camry 2015 XLE',
|
||
'make' => 'Toyota',
|
||
'model' => 'Camry',
|
||
'year' => 2015,
|
||
'mileage' => 80000,
|
||
'price' => 12500.00,
|
||
'description' => 'Reliable family sedan, fuel efficient.',
|
||
'status' => 'approved',
|
||
'color' => 'Silver',
|
||
'province' => 'Mazar-i-Sharif',
|
||
'city' => 'Mazar',
|
||
'image_url' => 'https://images.pexels.com/photos/244206/pexels-photo-244206.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Hyundai Sonata 2021 Hybrid',
|
||
'make' => 'Hyundai',
|
||
'model' => 'Sonata',
|
||
'year' => 2021,
|
||
'mileage' => 12000,
|
||
'price' => 22000.00,
|
||
'description' => 'Hybrid engine, great mileage, modern tech.',
|
||
'status' => 'approved',
|
||
'color' => 'Grey',
|
||
'province' => 'Kabul',
|
||
'city' => 'Kabul',
|
||
'image_url' => 'https://images.pexels.com/photos/3764984/pexels-photo-3764984.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Mercedes-Benz C-Class 2016',
|
||
'make' => 'Mercedes-Benz',
|
||
'model' => 'C-Class',
|
||
'year' => 2016,
|
||
'mileage' => 55000,
|
||
'price' => 28000.00,
|
||
'description' => 'Luxury interior, smooth ride, imported from Germany.',
|
||
'status' => 'approved',
|
||
'color' => 'Black',
|
||
'province' => 'Herat',
|
||
'city' => 'Herat',
|
||
'image_url' => 'https://images.pexels.com/photos/116675/pexels-photo-116675.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'BMW X5 2019 xDrive',
|
||
'make' => 'BMW',
|
||
'model' => 'X5',
|
||
'year' => 2019,
|
||
'mileage' => 25000,
|
||
'price' => 55000.00,
|
||
'description' => 'Premium SUV, panoramic sunroof, leather interior.',
|
||
'status' => 'approved',
|
||
'color' => 'White',
|
||
'province' => 'Kabul',
|
||
'city' => 'Kabul',
|
||
'image_url' => 'https://images.pexels.com/photos/3752169/pexels-photo-3752169.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Kia Sportage 2020 AWD',
|
||
'make' => 'Kia',
|
||
'model' => 'Sportage',
|
||
'year' => 2020,
|
||
'mileage' => 18000,
|
||
'price' => 21000.00,
|
||
'description' => 'Compact SUV, AWD, apple carplay.',
|
||
'status' => 'approved',
|
||
'color' => 'Red',
|
||
'province' => 'Jalalabad',
|
||
'city' => 'Jalalabad',
|
||
'image_url' => 'https://images.pexels.com/photos/4062468/pexels-photo-4062468.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Nissan Sunny 2017',
|
||
'make' => 'Nissan',
|
||
'model' => 'Sunny',
|
||
'year' => 2017,
|
||
'mileage' => 60000,
|
||
'price' => 9500.00,
|
||
'description' => 'Economic car, cheap maintenance.',
|
||
'status' => 'approved',
|
||
'color' => 'White',
|
||
'province' => 'Kandahar',
|
||
'city' => 'Kandahar',
|
||
'image_url' => 'https://images.pexels.com/photos/4574184/pexels-photo-4574184.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Toyota Hilux 2021 Revo',
|
||
'make' => 'Toyota',
|
||
'model' => 'Hilux',
|
||
'year' => 2021,
|
||
'mileage' => 10000,
|
||
'price' => 42000.00,
|
||
'description' => 'Powerful diesel engine, off-road ready.',
|
||
'status' => 'approved',
|
||
'color' => 'White',
|
||
'province' => 'Kabul',
|
||
'city' => 'Kabul',
|
||
'image_url' => 'https://images.pexels.com/photos/6301931/pexels-photo-6301931.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Lexus LX570 2018',
|
||
'make' => 'Lexus',
|
||
'model' => 'LX570',
|
||
'year' => 2018,
|
||
'mileage' => 40000,
|
||
'price' => 95000.00,
|
||
'description' => 'Top of the line luxury, armored option available.',
|
||
'status' => 'approved',
|
||
'color' => 'Black',
|
||
'province' => 'Kabul',
|
||
'city' => 'Kabul',
|
||
'image_url' => 'https://images.pexels.com/photos/1592384/pexels-photo-1592384.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Suzuki Alto 2022',
|
||
'make' => 'Suzuki',
|
||
'model' => 'Alto',
|
||
'year' => 2022,
|
||
'mileage' => 5000,
|
||
'price' => 7500.00,
|
||
'description' => 'Small city car, very fuel efficient.',
|
||
'status' => 'approved',
|
||
'color' => 'Red',
|
||
'province' => 'Mazar-i-Sharif',
|
||
'city' => 'Mazar',
|
||
'image_url' => 'https://images.pexels.com/photos/35967/mini-cooper-auto-model-vehicle.jpg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Mazda 6 2019',
|
||
'make' => 'Mazda',
|
||
'model' => '6',
|
||
'year' => 2019,
|
||
'mileage' => 28000,
|
||
'price' => 19500.00,
|
||
'description' => 'Stylish sedan, premium interior.',
|
||
'status' => 'approved',
|
||
'color' => 'Blue',
|
||
'province' => 'Herat',
|
||
'city' => 'Herat',
|
||
'image_url' => 'https://images.pexels.com/photos/1007410/pexels-photo-1007410.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
],
|
||
[
|
||
'title' => 'Chevrolet Tahoe 2015',
|
||
'make' => 'Chevrolet',
|
||
'model' => 'Tahoe',
|
||
'year' => 2015,
|
||
'mileage' => 85000,
|
||
'price' => 26000.00,
|
||
'description' => 'Large family SUV, spacious, American muscle.',
|
||
'status' => 'approved',
|
||
'color' => 'Black',
|
||
'province' => 'Kabul',
|
||
'city' => 'Kabul',
|
||
'image_url' => 'https://images.pexels.com/photos/4173163/pexels-photo-4173163.jpeg?auto=compress&cs=tinysrgb&w=600'
|
||
]
|
||
];
|
||
|
||
$insertCar = $pdo->prepare("INSERT INTO cars (user_id, title, make, model, year, mileage, price, description, status, color, province, city, image_url) VALUES (:user_id, :title, :make, :model, :year, :mileage, :price, :description, :status, :color, :province, :city, :image_url)");
|
||
|
||
foreach ($carsData as $car) {
|
||
$car['user_id'] = $adminId;
|
||
$insertCar->execute($car);
|
||
}
|
||
echo "<div class='step success'>✅ Seed data inserted (" . count($carsData) . " cars).</div>";
|
||
|
||
echo "<hr><h2>🎉 Installation Complete!</h2>";
|
||
echo "<p>You can now <a href='login.php'>Login here</a>.</p>";
|
||
echo "<p><b>Credentials:</b><br>Username: <code>admin</code> or <code>admin@gmail.com</code><br>Password: <code>123</code></p>";
|
||
|
||
} catch (PDOException $e) {
|
||
echo "<div class='step error'>❌ Installation Failed: " . htmlspecialchars($e->getMessage()) . "</div>";
|
||
}
|
||
echo "</body></html>";
|
||
?>
|