Path : /home/vishqocm/pcib.in/
File Upload :
Current File : /home/vishqocm//pcib.in/fix_website.php

<?php
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Start session
session_start();

// Include database configuration
require_once __DIR__ . '/admin/database/db_config.php';

echo "<h1>Website Fix Utility</h1>";

// Check if connection is successful
if (!$conn) {
    echo "<p style='color: red;'>Database connection failed: " . mysqli_connect_error() . "</p>";
    exit;
}

echo "<p style='color: green;'>Database connection successful!</p>";

// Function to check if a table exists
function tableExists($conn, $tableName) {
    $result = mysqli_query($conn, "SHOW TABLES LIKE '$tableName'");
    return mysqli_num_rows($result) > 0;
}

// Function to create the site_settings table and populate it
function createSiteSettingsTable($conn) {
    if (!tableExists($conn, 'site_settings')) {
        $sql = "CREATE TABLE site_settings (
            id INT(11) NOT NULL AUTO_INCREMENT,
            setting_key VARCHAR(100) NOT NULL,
            setting_value TEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            UNIQUE KEY (setting_key)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
        
        if (mysqli_query($conn, $sql)) {
            echo "<p style='color: green;'>Table 'site_settings' created successfully</p>";
            
            // Insert default settings
            $default_settings = [
                ['site_name', 'Popular Computer'],
                ['site_tagline', 'Computer Education Institute'],
                ['site_logo', 'assets/img/logo.png'],
                ['primary_color', '#4e73df'],
                ['enable_animations', 'true'],
                ['contact_email', '[email protected]'],
                ['contact_phone', '+91 9876543210']
            ];
            
            foreach ($default_settings as $setting) {
                $insert_sql = "INSERT INTO site_settings (setting_key, setting_value) VALUES ('$setting[0]', '$setting[1]')";
                if (mysqli_query($conn, $insert_sql)) {
                    echo "<p>Inserted setting: $setting[0]</p>";
                } else {
                    echo "<p style='color: red;'>Error inserting setting $setting[0]: " . mysqli_error($conn) . "</p>";
                }
            }
            return true;
        } else {
            echo "<p style='color: red;'>Error creating table 'site_settings': " . mysqli_error($conn) . "</p>";
            return false;
        }
    } else {
        echo "<p>Table 'site_settings' already exists</p>";
        return true;
    }
}

// Function to create the institute_stats table and populate it
function createInstituteStatsTable($conn) {
    if (!tableExists($conn, 'institute_stats')) {
        $sql = "CREATE TABLE institute_stats (
            id INT(11) NOT NULL AUTO_INCREMENT,
            title VARCHAR(100) NOT NULL,
            value INT(11) NOT NULL DEFAULT 0,
            icon VARCHAR(50) NOT NULL,
            is_visible TINYINT(1) NOT NULL DEFAULT 1,
            display_order INT(11) NOT NULL DEFAULT 0,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
        
        if (mysqli_query($conn, $sql)) {
            echo "<p style='color: green;'>Table 'institute_stats' created successfully</p>";
            
            // Insert default stats
            $default_stats = [
                ['Students Trained', 5000, 'fa-user-graduate', 1, 1],
                ['Courses Offered', 25, 'fa-book', 1, 2],
                ['Certified Trainers', 15, 'fa-chalkboard-teacher', 1, 3],
                ['Years Experience', 18, 'fa-calendar-alt', 1, 4]
            ];
            
            foreach ($default_stats as $stat) {
                $insert_sql = "INSERT INTO institute_stats (title, value, icon, is_visible, display_order) 
                              VALUES ('$stat[0]', $stat[1], '$stat[2]', $stat[3], $stat[4])";
                if (mysqli_query($conn, $insert_sql)) {
                    echo "<p>Inserted stat: $stat[0]</p>";
                } else {
                    echo "<p style='color: red;'>Error inserting stat $stat[0]: " . mysqli_error($conn) . "</p>";
                }
            }
            return true;
        } else {
            echo "<p style='color: red;'>Error creating table 'institute_stats': " . mysqli_error($conn) . "</p>";
            return false;
        }
    } else {
        echo "<p>Table 'institute_stats' already exists</p>";
        return true;
    }
}

// Function to create the students table
function createStudentsTable($conn) {
    if (!tableExists($conn, 'students')) {
        $sql = "CREATE TABLE students (
            id INT(11) NOT NULL AUTO_INCREMENT,
            full_name VARCHAR(100) NOT NULL,
            email VARCHAR(100) NOT NULL,
            phone VARCHAR(20) NOT NULL,
            password VARCHAR(255) NOT NULL,
            profile_image VARCHAR(255) DEFAULT NULL,
            status ENUM('active', 'inactive', 'suspended') NOT NULL DEFAULT 'active',
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            UNIQUE KEY (email)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
        
        if (mysqli_query($conn, $sql)) {
            echo "<p style='color: green;'>Table 'students' created successfully</p>";
            
            // Insert a sample student for testimonials
            $insert_sql = "INSERT INTO students (full_name, email, phone, password, profile_image, status) 
                          VALUES ('John Doe', '[email protected]', '9876543210', '" . password_hash('password', PASSWORD_DEFAULT) . "', 'assets/img/students/john.jpg', 'active')";
            if (mysqli_query($conn, $insert_sql)) {
                echo "<p>Inserted sample student</p>";
            } else {
                echo "<p style='color: red;'>Error inserting sample student: " . mysqli_error($conn) . "</p>";
            }
            
            return true;
        } else {
            echo "<p style='color: red;'>Error creating table 'students': " . mysqli_error($conn) . "</p>";
            return false;
        }
    } else {
        echo "<p>Table 'students' already exists</p>";
        return true;
    }
}

// Function to create the courses table and populate it
function createCoursesTable($conn) {
    if (!tableExists($conn, 'courses')) {
        $sql = "CREATE TABLE courses (
            id INT(11) NOT NULL AUTO_INCREMENT,
            title VARCHAR(255) NOT NULL,
            description TEXT NOT NULL,
            duration VARCHAR(50) NOT NULL,
            price DECIMAL(10,2) NOT NULL,
            image VARCHAR(255) DEFAULT NULL,
            featured TINYINT(1) NOT NULL DEFAULT 0,
            status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
            students_count INT(11) NOT NULL DEFAULT 0,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
        
        if (mysqli_query($conn, $sql)) {
            echo "<p style='color: green;'>Table 'courses' created successfully</p>";
            
            // Insert sample courses
            $sample_courses = [
                ['Web Development', 'Learn HTML, CSS, JavaScript, PHP and MySQL to build dynamic websites.', '3 Months', 15000, 'assets/img/courses/web-development.jpg', 1, 'active', 120],
                ['Graphic Design', 'Master Adobe Photoshop, Illustrator and InDesign for print and digital media.', '2 Months', 12000, 'assets/img/courses/graphic-design.jpg', 1, 'active', 85],
                ['Office Automation', 'Learn Microsoft Office suite including Word, Excel, PowerPoint and Access.', '1 Month', 8000, 'assets/img/courses/office-automation.jpg', 0, 'active', 200]
            ];
            
            foreach ($sample_courses as $course) {
                $insert_sql = "INSERT INTO courses (title, description, duration, price, image, featured, status, students_count) 
                              VALUES ('$course[0]', '$course[1]', '$course[2]', $course[3], '$course[4]', $course[5], '$course[6]', $course[7])";
                if (mysqli_query($conn, $insert_sql)) {
                    echo "<p>Inserted course: $course[0]</p>";
                } else {
                    echo "<p style='color: red;'>Error inserting course $course[0]: " . mysqli_error($conn) . "</p>";
                }
            }
            return true;
        } else {
            echo "<p style='color: red;'>Error creating table 'courses': " . mysqli_error($conn) . "</p>";
            return false;
        }
    } else {
        echo "<p>Table 'courses' already exists</p>";
        return true;
    }
}

// Function to create the testimonials table and populate it
function createTestimonialsTable($conn) {
    if (!tableExists($conn, 'testimonials')) {
        $sql = "CREATE TABLE testimonials (
            id INT(11) NOT NULL AUTO_INCREMENT,
            student_id INT(11) NOT NULL,
            content TEXT NOT NULL,
            course_name VARCHAR(255) NOT NULL,
            rating TINYINT(1) NOT NULL DEFAULT 5,
            status ENUM('pending', 'approved', 'rejected') NOT NULL DEFAULT 'pending',
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
        
        if (mysqli_query($conn, $sql)) {
            echo "<p style='color: green;'>Table 'testimonials' created successfully</p>";
            
            // Get the student ID
            $student_query = "SELECT id FROM students LIMIT 1";
            $student_result = mysqli_query($conn, $student_query);
            
            if ($student_result && mysqli_num_rows($student_result) > 0) {
                $student_id = mysqli_fetch_assoc($student_result)['id'];
                
                // Insert sample testimonials
                $sample_testimonials = [
                    [$student_id, 'The Web Development course was excellent! I learned so much and now I can build my own websites.', 'Web Development', 5, 'approved'],
                    [$student_id, 'Graphic Design course helped me land a job at a design agency. Highly recommended!', 'Graphic Design', 5, 'approved'],
                    [$student_id, 'Office Automation course was very practical and useful for my daily work.', 'Office Automation', 4, 'approved']
                ];
                
                foreach ($sample_testimonials as $testimonial) {
                    $insert_sql = "INSERT INTO testimonials (student_id, content, course_name, rating, status) 
                                  VALUES ($testimonial[0], '$testimonial[1]', '$testimonial[2]', $testimonial[3], '$testimonial[4]')";
                    if (mysqli_query($conn, $insert_sql)) {
                        echo "<p>Inserted testimonial for: $testimonial[2]</p>";
                    } else {
                        echo "<p style='color: red;'>Error inserting testimonial: " . mysqli_error($conn) . "</p>";
                    }
                }
            }
            
            return true;
        } else {
            echo "<p style='color: red;'>Error creating table 'testimonials': " . mysqli_error($conn) . "</p>";
            return false;
        }
    } else {
        echo "<p>Table 'testimonials' already exists</p>";
        return true;
    }
}

// Function to create the dashboard_stats table
function createDashboardStatsTable($conn) {
    if (!tableExists($conn, 'dashboard_stats')) {
        $sql = "CREATE TABLE dashboard_stats (
            id INT(11) NOT NULL AUTO_INCREMENT,
            stat_name VARCHAR(100) NOT NULL,
            stat_value INT(11) NOT NULL DEFAULT 0,
            icon VARCHAR(50) NOT NULL,
            color VARCHAR(20) NOT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
        
        if (mysqli_query($conn, $sql)) {
            echo "<p style='color: green;'>Table 'dashboard_stats' created successfully</p>";
            
            // Insert default dashboard stats
            $default_stats = [
                ['Total Users', 120, 'user', '#4e73df'],
                ['Total Courses', 25, 'book', '#1cc88a'],
                ['Active Users', 85, 'user-check', '#36b9cc'],
                ['Revenue', 250000, 'rupee-sign', '#f6c23e']
            ];
            
            foreach ($default_stats as $stat) {
                $insert_sql = "INSERT INTO dashboard_stats (stat_name, stat_value, icon, color) 
                              VALUES ('$stat[0]', $stat[1], '$stat[2]', '$stat[3]')";
                if (mysqli_query($conn, $insert_sql)) {
                    echo "<p>Inserted dashboard stat: $stat[0]</p>";
                } else {
                    echo "<p style='color: red;'>Error inserting dashboard stat $stat[0]: " . mysqli_error($conn) . "</p>";
                }
            }
            return true;
        } else {
            echo "<p style='color: red;'>Error creating table 'dashboard_stats': " . mysqli_error($conn) . "</p>";
            return false;
        }
    } else {
        echo "<p>Table 'dashboard_stats' already exists</p>";
        return true;
    }
}

// Function to create the activities table
function createActivitiesTable($conn) {
    if (!tableExists($conn, 'activities')) {
        $sql = "CREATE TABLE activities (
            id INT(11) NOT NULL AUTO_INCREMENT,
            user_id INT(11) NOT NULL,
            username VARCHAR(100) NOT NULL,
            full_name VARCHAR(100) NOT NULL,
            description TEXT NOT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
        
        if (mysqli_query($conn, $sql)) {
            echo "<p style='color: green;'>Table 'activities' created successfully</p>";
            
            // Insert sample activities
            $sample_activities = [
                [1, 'admin', 'Administrator', 'Logged into the system'],
                [1, 'admin', 'Administrator', 'Added a new course'],
                [1, 'admin', 'Administrator', 'Updated site settings']
            ];
            
            foreach ($sample_activities as $activity) {
                $insert_sql = "INSERT INTO activities (user_id, username, full_name, description) 
                              VALUES ($activity[0], '$activity[1]', '$activity[2]', '$activity[3]')";
                if (mysqli_query($conn, $insert_sql)) {
                    echo "<p>Inserted activity: $activity[3]</p>";
                } else {
                    echo "<p style='color: red;'>Error inserting activity: " . mysqli_error($conn) . "</p>";
                }
            }
            return true;
        } else {
            echo "<p style='color: red;'>Error creating table 'activities': " . mysqli_error($conn) . "</p>";
            return false;
        }
    } else {
        echo "<p>Table 'activities' already exists</p>";
        return true;
    }
}

// Function to create the users table for admin
function createUsersTable($conn) {
    if (!tableExists($conn, 'users')) {
        $sql = "CREATE TABLE users (
            id INT(11) NOT NULL AUTO_INCREMENT,
            username VARCHAR(50) NOT NULL,
            password VARCHAR(255) NOT NULL,
            full_name VARCHAR(100) NOT NULL,
            email VARCHAR(100) NOT NULL,
            role ENUM('admin', 'editor', 'viewer') NOT NULL DEFAULT 'viewer',
            status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            UNIQUE KEY (username),
            UNIQUE KEY (email)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
        
        if (mysqli_query($conn, $sql)) {
            echo "<p style='color: green;'>Table 'users' created successfully</p>";
            
            // Insert admin user
            $password_hash = password_hash('admin123', PASSWORD_DEFAULT);
            $insert_sql = "INSERT INTO users (username, password, full_name, email, role, status) 
                          VALUES ('admin', '$password_hash', 'Administrator', '[email protected]', 'admin', 'active')";
            if (mysqli_query($conn, $insert_sql)) {
                echo "<p>Inserted admin user</p>";
            } else {
                echo "<p style='color: red;'>Error inserting admin user: " . mysqli_error($conn) . "</p>";
            }
            return true;
        } else {
            echo "<p style='color: red;'>Error creating table 'users': " . mysqli_error($conn) . "</p>";
            return false;
        }
    } else {
        echo "<p>Table 'users' already exists</p>";
        return true;
    }
}

// Check and create all required tables
echo "<h2>Creating Required Tables:</h2>";
$site_settings_created = createSiteSettingsTable($conn);
$institute_stats_created = createInstituteStatsTable($conn);
$students_created = createStudentsTable($conn);
$courses_created = createCoursesTable($conn);
$testimonials_created = createTestimonialsTable($conn);
$dashboard_stats_created = createDashboardStatsTable($conn);
$activities_created = createActivitiesTable($conn);
$users_created = createUsersTable($conn);

// Check if all tables were created successfully
if ($site_settings_created && $institute_stats_created && $students_created && 
    $courses_created && $testimonials_created && $dashboard_stats_created && 
    $activities_created && $users_created) {
    echo "<h2 style='color: green;'>All required tables have been created successfully!</h2>";
    echo "<p>Your website should now be working properly.</p>";
} else {
    echo "<h2 style='color: red;'>Some tables could not be created. Please check the errors above.</h2>";
}

// Check if assets directory exists
echo "<h2>Checking Assets Directory:</h2>";
$assets_dir = __DIR__ . '/assets';
$img_dir = $assets_dir . '/img';
$css_dir = $assets_dir . '/css';
$js_dir = $assets_dir . '/js';

if (!file_exists($assets_dir)) {
    if (mkdir($assets_dir, 0755, true)) {
        echo "<p style='color: green;'>Created assets directory</p>";
    } else {
        echo "<p style='color: red;'>Failed to create assets directory</p>";
    }
}

if (!file_exists($img_dir)) {
    if (mkdir($img_dir, 0755, true)) {
        echo "<p style='color: green;'>Created images directory</p>";
    } else {
        echo "<p style='color: red;'>Failed to create images directory</p>";
    }
}

if (!file_exists($css_dir)) {
    if (mkdir($css_dir, 0755, true)) {
        echo "<p style='color: green;'>Created CSS directory</p>";
    } else {
        echo "<p style='color: red;'>Failed to create CSS directory</p>";
    }
}

if (!file_exists($js_dir)) {
    if (mkdir($js_dir, 0755, true)) {
        echo "<p style='color: green;'>Created JS directory</p>";
    } else {
        echo "<p style='color: red;'>Failed to create JS directory</p>";
    }
}

// Create a placeholder logo if it doesn't exist
$logo_path = $img_dir . '/logo.png';
if (!file_exists($logo_path)) {
    echo "<p>Logo file doesn't exist. You should add a logo image at: $logo_path</p>";
}

// Set session variables for admin panel
$_SESSION['user_id'] = 1;
$_SESSION['username'] = 'admin';
$_SESSION['full_name'] = 'Administrator';
$_SESSION['role'] = 'admin';
$_SESSION['email'] = '[email protected]';

echo "<h2>Navigation:</h2>";
echo "<p><a href='index.php' class='btn' style='background-color: #4e73df; color: white; padding: 10px 15px; text-decoration: none; border-radius: 5px; margin-right: 10px;'>Go to Homepage</a>";
echo "<a href='admin/dashboard.php' class='btn' style='background-color: #1cc88a; color: white; padding: 10px 15px; text-decoration: none; border-radius: 5px;'>Go to Admin Dashboard</a></p>";
?>