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

<?php
// Include database connection
require_once 'database/db_config.php';

// Start session if not already started
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// Check if user is logged in and is admin
if (!isset($_SESSION['user_id']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
    header('Location: login.php');
    exit;
}

// Initialize messages
$success_message = '';
$error_message = '';

// Create site_settings table if it doesn't exist
$create_table_query = "CREATE TABLE IF NOT EXISTS site_settings (
    id INT(11) NOT NULL AUTO_INCREMENT,
    setting_key VARCHAR(50) NOT NULL,
    setting_value TEXT,
    setting_group VARCHAR(50) DEFAULT 'general',
    PRIMARY KEY (id),
    UNIQUE KEY (setting_key)
)";
mysqli_query($conn, $create_table_query);

// Get current settings
$settings_query = "SELECT * FROM site_settings";
$settings_result = mysqli_query($conn, $settings_query);
$settings = [];

if ($settings_result && mysqli_num_rows($settings_result) > 0) {
    while ($row = mysqli_fetch_assoc($settings_result)) {
        $settings[$row['setting_key']] = $row['setting_value'];
    }
}

// Set default values if settings not found
$site_name = isset($settings['site_name']) ? $settings['site_name'] : 'Popular Computer Institute';
$site_tagline = isset($settings['site_tagline']) ? $settings['site_tagline'] : 'Computer Education Institute';
$site_logo = isset($settings['site_logo']) ? $settings['site_logo'] : '../assets/img/logo.png';
$institute_name = isset($settings['institute_name']) ? $settings['institute_name'] : 'Popular Computer Institute';
$institute_address = isset($settings['institute_address']) ? $settings['institute_address'] : 'Your Address, City, State, Country';
$contact_email = isset($settings['contact_email']) ? $settings['contact_email'] : '[email protected]';
$contact_phone = isset($settings['contact_phone']) ? $settings['contact_phone'] : '+91 1234567890';
$smtp_host = isset($settings['smtp_host']) ? $settings['smtp_host'] : 'smtp.example.com';
$smtp_port = isset($settings['smtp_port']) ? $settings['smtp_port'] : '587';
$smtp_username = isset($settings['smtp_username']) ? $settings['smtp_username'] : '[email protected]';
$smtp_password = isset($settings['smtp_password']) ? $settings['smtp_password'] : '';
$director_name = isset($settings['director_name']) ? $settings['director_name'] : 'Director Name';
$director_designation = isset($settings['director_designation']) ? $settings['director_designation'] : 'Director';
$default_avatar = isset($settings['default_avatar']) ? $settings['default_avatar'] : '../assets/img/default-avatar.png';
$razorpay_key_id = isset($settings['razorpay_key_id']) ? $settings['razorpay_key_id'] : '';
$razorpay_key_secret = isset($settings['razorpay_key_secret']) ? $settings['razorpay_key_secret'] : '';
$payment_upi_id = isset($settings['payment_upi_id']) ? $settings['payment_upi_id'] : '';
$payment_qr_image = isset($settings['payment_qr_image']) ? $settings['payment_qr_image'] : '';
$certificate_fee = isset($settings['certificate_fee']) ? $settings['certificate_fee'] : '700';
$certificate_secret_key = isset($settings['certificate_secret_key']) ? $settings['certificate_secret_key'] : md5(time());
$show_helper_messages = isset($settings['show_helper_messages']) ? $settings['show_helper_messages'] : 'true';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Process site settings form submission
    $new_site_name = mysqli_real_escape_string($conn, $_POST['site_name']);
    $new_site_tagline = mysqli_real_escape_string($conn, $_POST['site_tagline']);
    $new_institute_name = mysqli_real_escape_string($conn, $_POST['institute_name']);
    $new_institute_address = mysqli_real_escape_string($conn, $_POST['institute_address']);
    $new_contact_email = mysqli_real_escape_string($conn, $_POST['contact_email']);
    $new_contact_phone = mysqli_real_escape_string($conn, $_POST['contact_phone']);
    $new_smtp_host = mysqli_real_escape_string($conn, $_POST['smtp_host']);
    $new_smtp_port = mysqli_real_escape_string($conn, $_POST['smtp_port']);
    $new_smtp_username = mysqli_real_escape_string($conn, $_POST['smtp_username']);
    $new_smtp_password = mysqli_real_escape_string($conn, $_POST['smtp_password']);
    $new_director_name = mysqli_real_escape_string($conn, $_POST['director_name']);
    $new_director_designation = mysqli_real_escape_string($conn, $_POST['director_designation']);
    $new_razorpay_key_id = mysqli_real_escape_string($conn, $_POST['razorpay_key_id']);
    $new_razorpay_key_secret = mysqli_real_escape_string($conn, $_POST['razorpay_key_secret']);
    $new_payment_upi_id = mysqli_real_escape_string($conn, $_POST['payment_upi_id']);
    $new_certificate_fee = mysqli_real_escape_string($conn, $_POST['certificate_fee']);
    
    // Logo upload handling
    $new_site_logo = $site_logo; // Default to current logo
    if (isset($_FILES['site_logo']) && $_FILES['site_logo']['error'] === 0) {
        $allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'];
        $file_type = $_FILES['site_logo']['type'];
        
        if (in_array($file_type, $allowed_types)) {
            $file_name = 'logo_' . time() . '_' . $_FILES['site_logo']['name'];
            $upload_path = '../assets/img/' . $file_name;
            
            if (move_uploaded_file($_FILES['site_logo']['tmp_name'], $upload_path)) {
                $new_site_logo = 'assets/img/' . $file_name;
            } else {
                $error_message = 'Failed to upload logo. Please try again.';
            }
        } else {
            $error_message = 'Invalid file type. Please upload JPEG, PNG, GIF, or SVG.';
        }
    }
    
    // QR Code upload handling
    $new_payment_qr_image = $payment_qr_image; // Default to current QR image
    if (isset($_FILES['payment_qr_image']) && $_FILES['payment_qr_image']['error'] === 0) {
        $allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'];
        $file_type = $_FILES['payment_qr_image']['type'];
        
        if (in_array($file_type, $allowed_types)) {
            $file_name = 'qr_' . time() . '_' . $_FILES['payment_qr_image']['name'];
            $upload_path = '../assets/img/' . $file_name;
            
            if (move_uploaded_file($_FILES['payment_qr_image']['tmp_name'], $upload_path)) {
                $new_payment_qr_image = 'assets/img/' . $file_name;
            } else {
                $error_message = 'Failed to upload QR code. Please try again.';
            }
        } else {
            $error_message = 'Invalid file type. Please upload JPEG, PNG, GIF, or SVG.';
        }
    }
    
    // Default avatar upload handling
    $new_default_avatar = $default_avatar; // Default to current avatar
    if (isset($_FILES['default_avatar']) && $_FILES['default_avatar']['error'] === 0) {
        $allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'];
        $file_type = $_FILES['default_avatar']['type'];
        
        if (in_array($file_type, $allowed_types)) {
            $file_name = 'avatar_' . time() . '_' . $_FILES['default_avatar']['name'];
            $upload_path = '../assets/img/' . $file_name;
            
            if (move_uploaded_file($_FILES['default_avatar']['tmp_name'], $upload_path)) {
                $new_default_avatar = 'assets/img/' . $file_name;
            } else {
                $error_message = 'Failed to upload default avatar. Please try again.';
            }
        } else {
            $error_message = 'Invalid file type. Please upload JPEG, PNG, GIF, or SVG.';
        }
    }
    
    // Generate new certificate secret key if requested
    $new_certificate_secret_key = $certificate_secret_key;
    if (isset($_POST['regenerate_certificate_key']) && $_POST['regenerate_certificate_key'] === 'yes') {
        $new_certificate_secret_key = md5(uniqid(rand(), true));
    }
    
    // Update settings in database
    $settings_to_update = [
        'site_name' => $new_site_name,
        'site_tagline' => $new_site_tagline,
        'site_logo' => $new_site_logo,
        'institute_name' => $new_institute_name,
        'institute_address' => $new_institute_address,
        'contact_email' => $new_contact_email,
        'contact_phone' => $new_contact_phone,
        'smtp_host' => $new_smtp_host,
        'smtp_port' => $new_smtp_port,
        'smtp_username' => $new_smtp_username,
        'director_name' => $new_director_name,
        'director_designation' => $new_director_designation,
        'default_avatar' => $new_default_avatar,
        'razorpay_key_id' => $new_razorpay_key_id,
        'razorpay_key_secret' => $new_razorpay_key_secret,
        'payment_upi_id' => $new_payment_upi_id,
        'payment_qr_image' => $new_payment_qr_image,
        'certificate_fee' => $new_certificate_fee,
        'certificate_secret_key' => $new_certificate_secret_key
    ];
    
    // Only update SMTP password if a new one is provided
    if (!empty($_POST['smtp_password'])) {
        $settings_to_update['smtp_password'] = $new_smtp_password;
    }
    
    foreach ($settings_to_update as $key => $value) {
        // Check if setting exists
        $check_query = "SELECT * FROM site_settings WHERE setting_key = '$key'";
        $check_result = mysqli_query($conn, $check_query);
        
        if (mysqli_num_rows($check_result) > 0) {
            // Update existing setting
            $update_query = "UPDATE site_settings SET setting_value = '$value' WHERE setting_key = '$key'";
            mysqli_query($conn, $update_query);
        } else {
            // Insert new setting
            $insert_query = "INSERT INTO site_settings (setting_key, setting_value) VALUES ('$key', '$value')";
            mysqli_query($conn, $insert_query);
        }
    }
    
    // Show success message
    $success_message = 'Site settings updated successfully.';
    
    // Update local variables to reflect new settings
    $site_name = $new_site_name;
    $site_tagline = $new_site_tagline;
    $site_logo = $new_site_logo;
    $institute_name = $new_institute_name;
    $institute_address = $new_institute_address;
    $contact_email = $new_contact_email;
    $contact_phone = $new_contact_phone;
    $smtp_host = $new_smtp_host;
    $smtp_port = $new_smtp_port;
    $smtp_username = $new_smtp_username;
    if (!empty($_POST['smtp_password'])) {
        $smtp_password = $new_smtp_password;
    }
    $director_name = $new_director_name;
    $director_designation = $new_director_designation;
    $default_avatar = $new_default_avatar;
    $razorpay_key_id = $new_razorpay_key_id;
    $razorpay_key_secret = $new_razorpay_key_secret;
    $payment_upi_id = $new_payment_upi_id;
    $payment_qr_image = $new_payment_qr_image;
    $certificate_fee = $new_certificate_fee;
    $certificate_secret_key = $new_certificate_secret_key;
}

// Include header
include('includes/header.php');
?>

<div class="container-fluid">
    <h1 class="h3 mb-4 text-gray-800">Site Settings</h1>
    
    <?php if ($success_message): ?>
        <div class="alert alert-success" role="alert">
            <?php echo $success_message; ?>
        </div>
    <?php endif; ?>
    
    <?php if ($error_message): ?>
        <div class="alert alert-danger" role="alert">
            <?php echo $error_message; ?>
        </div>
    <?php endif; ?>
    
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Configure Site Settings</h6>
        </div>
        <div class="card-body">
            <form method="post" action="" enctype="multipart/form-data">
                <div class="row">
                    <!-- Settings Navigation -->
                    <div class="col-md-3">
                        <div class="nav flex-column nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
                            <a class="nav-link active" id="general-tab" data-bs-toggle="pill" href="#general" role="tab" aria-controls="general" aria-selected="true">
                                <i class="fas fa-cog"></i> General Settings
                            </a>
                            <a class="nav-link" id="contact-tab" data-bs-toggle="pill" href="#contact" role="tab" aria-controls="contact" aria-selected="false">
                                <i class="fas fa-address-book"></i> Contact Information
                            </a>
                            <a class="nav-link" id="email-tab" data-bs-toggle="pill" href="#email" role="tab" aria-controls="email" aria-selected="false">
                                <i class="fas fa-envelope"></i> Email Settings
                            </a>
                            <a class="nav-link" id="payment-tab" data-bs-toggle="pill" href="#payment" role="tab" aria-controls="payment" aria-selected="false">
                                <i class="fas fa-money-bill-wave"></i> Payment Settings
                            </a>
                            <a class="nav-link" id="certificate-tab" data-bs-toggle="pill" href="#certificate" role="tab" aria-controls="certificate" aria-selected="false">
                                <i class="fas fa-certificate"></i> Certificate Settings
                            </a>
                        </div>
                    </div>
                    
                    <!-- Settings Content -->
                    <div class="col-md-9">
                        <div class="tab-content" id="v-pills-tabContent">
                            <!-- General Settings -->
                            <div class="tab-pane fade show active" id="general" role="tabpanel" aria-labelledby="general-tab">
                                <h4 class="mb-3">General Settings</h4>
                                
                                <div class="mb-3">
                                    <label for="site_name" class="form-label">Site Name</label>
                                    <input type="text" class="form-control" id="site_name" name="site_name" value="<?php echo htmlspecialchars($site_name); ?>" required>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="site_tagline" class="form-label">Site Tagline</label>
                                    <input type="text" class="form-control" id="site_tagline" name="site_tagline" value="<?php echo htmlspecialchars($site_tagline); ?>">
                                </div>
                                
                                <div class="mb-3">
                                    <label for="institute_name" class="form-label">Institute Name</label>
                                    <input type="text" class="form-control" id="institute_name" name="institute_name" value="<?php echo htmlspecialchars($institute_name); ?>" required>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="institute_address" class="form-label">Institute Address</label>
                                    <textarea class="form-control" id="institute_address" name="institute_address" rows="3"><?php echo htmlspecialchars($institute_address); ?></textarea>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="site_logo" class="form-label">Site Logo</label>
                                    <div class="input-group">
                                        <input type="file" class="form-control" id="site_logo" name="site_logo" accept="image/*">
                                    </div>
                                    <?php if ($site_logo): ?>
                                        <div class="mt-2">
                                            <img src="<?php echo formatUrl($site_logo); ?>" alt="Current Logo" style="max-height: 100px;" class="img-thumbnail">
                                        </div>
                                    <?php endif; ?>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="default_avatar" class="form-label">Default User Avatar</label>
                                    <div class="input-group">
                                        <input type="file" class="form-control" id="default_avatar" name="default_avatar" accept="image/*">
                                    </div>
                                    <?php if ($default_avatar): ?>
                                        <div class="mt-2">
                                            <img src="<?php echo formatUrl($default_avatar); ?>" alt="Default Avatar" style="max-height: 100px;" class="img-thumbnail">
                                        </div>
                                    <?php endif; ?>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="director_name" class="form-label">Director Name</label>
                                    <input type="text" class="form-control" id="director_name" name="director_name" value="<?php echo htmlspecialchars($director_name); ?>">
                                </div>
                                
                                <div class="mb-3">
                                    <label for="director_designation" class="form-label">Director Designation</label>
                                    <input type="text" class="form-control" id="director_designation" name="director_designation" value="<?php echo htmlspecialchars($director_designation); ?>">
                                </div>
                            </div>
                            
                            <!-- Contact Information -->
                            <div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">
                                <h4 class="mb-3">Contact Information</h4>
                                
                                <div class="mb-3">
                                    <label for="contact_email" class="form-label">Contact Email</label>
                                    <input type="email" class="form-control" id="contact_email" name="contact_email" value="<?php echo htmlspecialchars($contact_email); ?>" required>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="contact_phone" class="form-label">Contact Phone</label>
                                    <input type="text" class="form-control" id="contact_phone" name="contact_phone" value="<?php echo htmlspecialchars($contact_phone); ?>">
                                </div>
                            </div>
                            
                            <!-- Email Settings -->
                            <div class="tab-pane fade" id="email" role="tabpanel" aria-labelledby="email-tab">
                                <h4 class="mb-3">Email Settings</h4>
                                
                                <div class="mb-3">
                                    <label for="smtp_host" class="form-label">SMTP Host</label>
                                    <input type="text" class="form-control" id="smtp_host" name="smtp_host" value="<?php echo htmlspecialchars($smtp_host); ?>">
                                </div>
                                
                                <div class="mb-3">
                                    <label for="smtp_port" class="form-label">SMTP Port</label>
                                    <input type="text" class="form-control" id="smtp_port" name="smtp_port" value="<?php echo htmlspecialchars($smtp_port); ?>">
                                </div>
                                
                                <div class="mb-3">
                                    <label for="smtp_username" class="form-label">SMTP Username</label>
                                    <input type="text" class="form-control" id="smtp_username" name="smtp_username" value="<?php echo htmlspecialchars($smtp_username); ?>">
                                </div>
                                
                                <div class="mb-3">
                                    <label for="smtp_password" class="form-label">SMTP Password</label>
                                    <input type="password" class="form-control" id="smtp_password" name="smtp_password" placeholder="Leave empty to keep existing password">
                                    <small class="text-muted">Leave this field blank if you don't want to change the password.</small>
                                </div>
                            </div>
                            
                            <!-- Payment Settings -->
                            <div class="tab-pane fade" id="payment" role="tabpanel" aria-labelledby="payment-tab">
                                <h4 class="mb-3">Payment Settings</h4>
                                
                                <div class="mb-3">
                                    <label for="razorpay_key_id" class="form-label">Razorpay Key ID</label>
                                    <input type="text" class="form-control" id="razorpay_key_id" name="razorpay_key_id" value="<?php echo htmlspecialchars($razorpay_key_id); ?>">
                                </div>
                                
                                <div class="mb-3">
                                    <label for="razorpay_key_secret" class="form-label">Razorpay Key Secret</label>
                                    <input type="text" class="form-control" id="razorpay_key_secret" name="razorpay_key_secret" value="<?php echo htmlspecialchars($razorpay_key_secret); ?>">
                                </div>
                                
                                <div class="mb-3">
                                    <label for="payment_upi_id" class="form-label">UPI ID</label>
                                    <input type="text" class="form-control" id="payment_upi_id" name="payment_upi_id" value="<?php echo htmlspecialchars($payment_upi_id); ?>">
                                </div>
                                
                                <div class="mb-3">
                                    <label for="payment_qr_image" class="form-label">Payment QR Code</label>
                                    <div class="input-group">
                                        <input type="file" class="form-control" id="payment_qr_image" name="payment_qr_image" accept="image/*">
                                    </div>
                                    <?php if ($payment_qr_image): ?>
                                        <div class="mt-2">
                                            <img src="<?php echo formatUrl($payment_qr_image); ?>" alt="Payment QR Code" style="max-height: 150px;" class="img-thumbnail">
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                            
                            <!-- Certificate Settings -->
                            <div class="tab-pane fade" id="certificate" role="tabpanel" aria-labelledby="certificate-tab">
                                <h4 class="mb-3">Certificate Settings</h4>
                                
                                <div class="mb-3">
                                    <label for="certificate_fee" class="form-label">Certificate Fee (₹)</label>
                                    <input type="number" class="form-control" id="certificate_fee" name="certificate_fee" value="<?php echo htmlspecialchars($certificate_fee); ?>" min="0">
                                </div>
                                
                                <div class="mb-3">
                                    <label for="certificate_secret_key" class="form-label">Certificate Secret Key</label>
                                    <div class="input-group">
                                        <input type="text" class="form-control" id="certificate_secret_key" value="<?php echo htmlspecialchars($certificate_secret_key); ?>" readonly>
                                        <div class="input-group-append">
                                            <button class="btn btn-outline-secondary" type="button" onclick="document.getElementById('regenerate_certificate_key').value='yes'; this.form.submit();">Regenerate Key</button>
                                        </div>
                                    </div>
                                    <small class="text-muted">This key is used for certificate verification. Changing it will invalidate all existing certificates.</small>
                                    <input type="hidden" name="regenerate_certificate_key" id="regenerate_certificate_key" value="no">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="mt-4 text-end">
                    <button type="submit" class="btn btn-primary">Save Settings</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Initialize Bootstrap 5 tabs
    var triggerTabList = [].slice.call(document.querySelectorAll('#v-pills-tab a'))
    triggerTabList.forEach(function(triggerEl) {
        var tabTrigger = new bootstrap.Tab(triggerEl)
        
        triggerEl.addEventListener('click', function(event) {
            event.preventDefault()
            tabTrigger.show()
        })
    })
    
    // Save active tab to session storage when changed
    triggerTabList.forEach(function(triggerEl) {
        triggerEl.addEventListener('shown.bs.tab', function() {
            sessionStorage.setItem('activeSettingsTab', this.id)
        })
    })
    
    // Restore active tab from session storage
    var activeTab = sessionStorage.getItem('activeSettingsTab')
    if (activeTab) {
        var tabElement = document.getElementById(activeTab)
        if (tabElement) {
            var tab = new bootstrap.Tab(tabElement)
            tab.show()
        }
    }
})
</script>

<?php include('includes/footer.php'); ?>