<?php
/**
* Create Default Asset Directories and Files
*
* This script creates the necessary directories and default assets
* for the website to function properly.
*/
// Include database configuration - using absolute path
$root_path = realpath(dirname(__FILE__) . '/../../');
require_once $root_path . '/admin/database/db_config.php';
// Display header
echo "<!DOCTYPE html>
<html>
<head>
<title>Create Default Assets - System Maintenance</title>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; margin: 20px; color: #333; }
h1 { color: #2c3e50; border-bottom: 2px solid #eee; padding-bottom: 10px; }
h2 { color: #3498db; margin-top: 20px; }
.container { max-width: 800px; margin: 0 auto; background: #f9f9f9; padding: 20px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
.success { color: #27ae60; background-color: #d5f5e3; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
.error { color: #c0392b; background-color: #f8d7da; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
.info { color: #2980b9; background-color: #d6eaf8; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
.warning { color: #f39c12; background-color: #fef9e7; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
table { width: 100%; border-collapse: collapse; margin: 20px 0; }
table, th, td { border: 1px solid #ddd; }
th, td { padding: 12px; text-align: left; }
th { background-color: #f2f2f2; }
a { color: #3498db; text-decoration: none; }
a:hover { text-decoration: underline; }
.btn { display: inline-block; background: #3498db; color: white; padding: 8px 16px; border-radius: 4px; text-decoration: none; margin-top: 15px; }
.btn:hover { background: #2980b9; text-decoration: none; }
pre { background: #f8f8f8; padding: 10px; border-radius: 4px; overflow-x: auto; }
code { font-family: monospace; font-size: 14px; }
</style>
</head>
<body>
<div class='container'>
<h1>Create Default Assets</h1>
<p>This script creates necessary directories and default assets for the website.</p>";
try {
// Define required directories
$required_dirs = [
'../assets/img/defaults/' => 'Default images directory',
'../assets/img/defaults/sliders/' => 'Default slider images directory',
'../assets/img/sliders/' => 'Slider images directory',
'../assets/img/courses/' => 'Course images directory',
'../assets/img/instructors/' => 'Instructor profile images directory',
'../assets/img/students/' => 'Student profile images directory',
'../assets/img/avatars/' => 'Avatar images directory',
'../assets/img/uploads/' => 'General uploads directory',
'../assets/css/' => 'CSS directory',
'../assets/js/' => 'JavaScript directory',
'../assets/fonts/' => 'Fonts directory',
];
echo "<h2>Creating Required Directories</h2>";
echo "<table>
<tr>
<th>Directory</th>
<th>Status</th>
</tr>";
foreach ($required_dirs as $dir => $description) {
$full_path = $root_path . '/' . str_replace('../', '', $dir);
// Create directory if it doesn't exist
if (!file_exists($full_path)) {
// Make sure parent directory exists first
$parent_dir = dirname($full_path);
if (!file_exists($parent_dir)) {
mkdir($parent_dir, 0755, true);
}
if (mkdir($full_path, 0755, true)) {
echo "<tr>
<td>{$dir}</td>
<td><span class='success'>Created successfully</span></td>
</tr>";
} else {
echo "<tr>
<td>{$dir}</td>
<td><span class='error'>Failed to create. Error: " . error_get_last()['message'] . "</span></td>
</tr>";
}
} else {
echo "<tr>
<td>{$dir}</td>
<td><span class='info'>Already exists</span></td>
</tr>";
}
}
echo "</table>";
// Create default images if they don't exist
echo "<h2>Creating Default Images</h2>";
$default_images = [
'default-course-image.jpg' => [
'width' => 800,
'height' => 600,
'text' => 'Default Course Image',
'dir' => '../assets/img/defaults/',
'dest_dir' => '../assets/img/courses/'
],
'default-slider-1.jpg' => [
'width' => 1920,
'height' => 600,
'text' => 'Welcome to Popular Computer Institute',
'dir' => '../assets/img/defaults/sliders/',
'dest_dir' => '../assets/img/sliders/'
],
'default-slider-2.jpg' => [
'width' => 1920,
'height' => 600,
'text' => 'Learn Professional Skills',
'dir' => '../assets/img/defaults/sliders/',
'dest_dir' => '../assets/img/sliders/'
],
'default-slider-3.jpg' => [
'width' => 1920,
'height' => 600,
'text' => 'Start Your Career Journey',
'dir' => '../assets/img/defaults/sliders/',
'dest_dir' => '../assets/img/sliders/'
],
'default-avatar.png' => [
'width' => 300,
'height' => 300,
'text' => 'User',
'dir' => '../assets/img/defaults/',
'dest_dir' => '../assets/img/'
],
];
echo "<table>
<tr>
<th>Image</th>
<th>Status</th>
</tr>";
foreach ($default_images as $filename => $properties) {
$source_path = $root_path . '/' . str_replace('../', '', $properties['dir']) . $filename;
$dest_path = $root_path . '/' . str_replace('../', '', $properties['dest_dir']) . $filename;
// Create source directory if it doesn't exist
$source_dir = dirname($source_path);
if (!file_exists($source_dir)) {
if (!mkdir($source_dir, 0755, true)) {
echo "<tr>
<td>{$properties['dir']}</td>
<td><span class='error'>Failed to create directory. Error: " . error_get_last()['message'] . "</span></td>
</tr>";
continue;
}
}
// Create destination directory if it doesn't exist
$dest_dir = dirname($dest_path);
if (!file_exists($dest_dir)) {
if (!mkdir($dest_dir, 0755, true)) {
echo "<tr>
<td>{$properties['dest_dir']}</td>
<td><span class='error'>Failed to create directory. Error: " . error_get_last()['message'] . "</span></td>
</tr>";
continue;
}
}
// Create image if it doesn't exist
if (!file_exists($source_path)) {
// Create a placeholder image using GD
$width = $properties['width'];
$height = $properties['height'];
$image = imagecreatetruecolor($width, $height);
// Colors
$bg_color = imagecolorallocate($image, 240, 240, 240);
$text_color = imagecolorallocate($image, 50, 50, 50);
$accent_color = imagecolorallocate($image, 0, 123, 255);
// Fill background
imagefilledrectangle($image, 0, 0, $width, $height, $bg_color);
// Draw border
imagerectangle($image, 0, 0, $width - 1, $height - 1, $accent_color);
imagerectangle($image, 5, 5, $width - 6, $height - 6, $accent_color);
// Add text
$text = $properties['text'];
$font_size = 5;
$text_width = imagefontwidth($font_size) * strlen($text);
$text_height = imagefontheight($font_size);
// Center the text
$text_x = ($width - $text_width) / 2;
$text_y = ($height - $text_height) / 2;
imagestring($image, $font_size, $text_x, $text_y, $text, $text_color);
// Save the image
if (pathinfo($filename, PATHINFO_EXTENSION) === 'jpg' || pathinfo($filename, PATHINFO_EXTENSION) === 'jpeg') {
imagejpeg($image, $source_path, 90);
} else {
imagepng($image, $source_path, 9);
}
// Set proper file permissions
chmod($source_path, 0644);
imagedestroy($image);
echo "<tr>
<td>{$properties['dir']}{$filename}</td>
<td><span class='success'>Created successfully</span></td>
</tr>";
} else {
echo "<tr>
<td>{$properties['dir']}{$filename}</td>
<td><span class='info'>Already exists</span></td>
</tr>";
}
// Copy image to destination if it doesn't exist there
if (!file_exists($dest_path)) {
if (copy($source_path, $dest_path)) {
// Set proper file permissions
chmod($dest_path, 0644);
echo "<tr>
<td>{$properties['dest_dir']}{$filename}</td>
<td><span class='success'>Copied successfully</span></td>
</tr>";
} else {
echo "<tr>
<td>{$properties['dest_dir']}{$filename}</td>
<td><span class='error'>Failed to copy: " . error_get_last()['message'] . "</span></td>
</tr>";
}
} else {
echo "<tr>
<td>{$properties['dest_dir']}{$filename}</td>
<td><span class='info'>Already exists</span></td>
</tr>";
}
}
echo "</table>";
// Update site settings
echo "<h2>Updating Site Settings</h2>";
// Check if site_settings table exists
$check_query = "SHOW TABLES LIKE 'site_settings'";
$table_exists = $conn->query($check_query);
if ($table_exists && $table_exists->num_rows > 0) {
// Default settings to insert/update
$default_settings = [
['default_course_image', 'assets/img/courses/default-course-image.jpg'],
['default_avatar', 'assets/img/default-avatar.png'],
['default_slider_image', 'assets/img/sliders/default-slider-1.jpg'],
['show_helper_messages', 'false']
];
echo "<table>
<tr>
<th>Setting</th>
<th>Value</th>
<th>Status</th>
</tr>";
foreach ($default_settings as $setting) {
$key = $setting[0];
$value = $setting[1];
try {
// Check if setting already exists
$check_setting = "SELECT * FROM site_settings WHERE setting_key = ?";
$check_stmt = $conn->prepare($check_setting);
$check_stmt->bind_param("s", $key);
$check_stmt->execute();
$result = $check_stmt->get_result();
if ($result->num_rows > 0) {
// Update existing setting
$update_query = "UPDATE site_settings SET setting_value = ? WHERE setting_key = ?";
$stmt = $conn->prepare($update_query);
$stmt->bind_param("ss", $value, $key);
$action = "Updated";
} else {
// Insert new setting
$insert_query = "INSERT INTO site_settings (setting_key, setting_value) VALUES (?, ?)";
$stmt = $conn->prepare($insert_query);
$stmt->bind_param("ss", $key, $value);
$action = "Inserted";
}
if ($stmt->execute()) {
echo "<tr>
<td>{$key}</td>
<td>{$value}</td>
<td><span class='success'>{$action} successfully</span></td>
</tr>";
} else {
echo "<tr>
<td>{$key}</td>
<td>{$value}</td>
<td><span class='error'>Failed to update: {$stmt->error}</span></td>
</tr>";
}
} catch (Exception $e) {
echo "<tr>
<td>{$key}</td>
<td>{$value}</td>
<td><span class='error'>Error: {$e->getMessage()}</span></td>
</tr>";
}
}
echo "</table>";
} else {
echo "<div class='warning'>Site settings table doesn't exist. Run the Create Theme Settings Table tool first.</div>";
}
echo "<div class='success'>✅ Default assets have been created successfully.</div>";
echo "<div class='links'>
<a href='../../index.php' class='btn'>Return to Homepage</a>
<a href='../../admin/index.php' class='btn'>Admin Dashboard</a>
<a href='../../admin/system_utilities.php' class='btn'>System Utilities</a>
<a href='../index.php' class='btn'>Debug Tools</a>
</div>";
} catch (Exception $e) {
echo "<div class='error'>Error: " . $e->getMessage() . "</div>";
echo "<div class='links'>
<a href='../../index.php' class='btn'>Return to Homepage</a>
<a href='../../admin/index.php' class='btn'>Admin Dashboard</a>
<a href='../index.php' class='btn'>Debug Tools</a>
</div>";
}
echo "</div></body></html>";
?>