setup.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Web Publication System Setup Script
  4. * This script will set up the database and create necessary configuration files
  5. */
  6. // Prevent direct access if already installed
  7. if (file_exists('../includes/config.php')) {
  8. die("System appears to be already installed. Please remove includes/config.php to reinstall.");
  9. }
  10. // Database configuration form handling
  11. $db_host = 'localhost';
  12. $db_name = 'webpub';
  13. $db_user = '';
  14. $db_pass = '';
  15. $site_title = 'Web Publication System';
  16. $admin_username = 'admin';
  17. $admin_password = '';
  18. $admin_email = '';
  19. $errors = [];
  20. $success = false;
  21. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  22. // Validate and sanitize input
  23. $db_host = trim($_POST['db_host'] ?? 'localhost');
  24. $db_name = trim($_POST['db_name'] ?? 'webpub');
  25. $db_user = trim($_POST['db_user'] ?? '');
  26. $db_pass = trim($_POST['db_pass'] ?? '');
  27. $site_title = trim($_POST['site_title'] ?? 'Web Publication System');
  28. $admin_username = trim($_POST['admin_username'] ?? 'admin');
  29. $admin_password = trim($_POST['admin_password'] ?? '');
  30. $admin_email = trim($_POST['admin_email'] ?? '');
  31. // Validation
  32. if (empty($db_user)) $errors[] = 'Database username is required';
  33. if (empty($admin_password)) $errors[] = 'Admin password is required';
  34. if (empty($admin_email)) $errors[] = 'Admin email is required';
  35. if (!filter_var($admin_email, FILTER_VALIDATE_EMAIL)) $errors[] = 'Invalid email format';
  36. if (empty($errors)) {
  37. try {
  38. // Test database connection
  39. $pdo = new PDO("mysql:host=$db_host", $db_user, $db_pass);
  40. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  41. // Create database if it doesn't exist
  42. $pdo->exec("CREATE DATABASE IF NOT EXISTS `$db_name` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
  43. $pdo->exec("USE `$db_name`");
  44. // Import SQL schema
  45. $sql = file_get_contents('database.sql');
  46. $pdo->exec($sql);
  47. // Update admin user with provided credentials
  48. $hashed_password = password_hash($admin_password, PASSWORD_DEFAULT);
  49. $stmt = $pdo->prepare("UPDATE users SET username = ?, password = ?, email = ? WHERE username = 'admin'");
  50. $stmt->execute([$admin_username, $hashed_password, $admin_email]);
  51. // Create configuration file
  52. $config_content = "<?php\n";
  53. $config_content .= "// Database configuration\n";
  54. $config_content .= "define('DB_HOST', '$db_host');\n";
  55. $config_content .= "define('DB_NAME', '$db_name');\n";
  56. $config_content .= "define('DB_USER', '$db_user');\n";
  57. $config_content .= "define('DB_PASS', '$db_pass');\n\n";
  58. $config_content .= "// Site configuration\n";
  59. $config_content .= "define('SITE_TITLE', '$site_title');\n";
  60. $config_content .= "define('SITE_URL', '" . ($_SERVER['HTTPS'] ? 'https' : 'http') . "://{$_SERVER['HTTP_HOST']}" . dirname($_SERVER['PHP_SELF']) . "/../');\n";
  61. $config_content .= "define('ADMIN_EMAIL', '$admin_email');\n";
  62. if (file_put_contents('../includes/config.php', $config_content)) {
  63. $success = true;
  64. } else {
  65. $errors[] = 'Failed to create configuration file. Check file permissions.';
  66. }
  67. } catch (PDOException $e) {
  68. $errors[] = 'Database error: ' . $e->getMessage();
  69. }
  70. }
  71. }
  72. ?>
  73. <!DOCTYPE html>
  74. <html lang="en">
  75. <head>
  76. <meta charset="UTF-8">
  77. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  78. <title>Web Publication System Setup</title>
  79. <link rel="stylesheet" href="../css/style.css">
  80. </head>
  81. <body>
  82. <div class="container">
  83. <h1>Web Publication System Setup</h1>
  84. <?php if ($success): ?>
  85. <div class="alert alert-success">
  86. <h3>Installation Complete!</h3>
  87. <p>The system has been successfully installed. You can now:</p>
  88. <ul>
  89. <li><a href="../admin/">Access the admin panel</a></li>
  90. <li><a href="../public/">View the public site</a></li>
  91. </ul>
  92. <p><strong>Important:</strong> Delete the setup directory for security.</p>
  93. </div>
  94. <?php else: ?>
  95. <?php if (!empty($errors)): ?>
  96. <div class="alert alert-error">
  97. <?php foreach ($errors as $error): ?>
  98. <p><?php echo htmlspecialchars($error); ?></p>
  99. <?php endforeach; ?>
  100. </div>
  101. <?php endif; ?>
  102. <form method="post" class="setup-form">
  103. <h2>Database Configuration</h2>
  104. <div class="form-group">
  105. <label for="db_host">Database Host:</label>
  106. <input type="text" id="db_host" name="db_host" value="<?php echo htmlspecialchars($db_host); ?>" required>
  107. </div>
  108. <div class="form-group">
  109. <label for="db_name">Database Name:</label>
  110. <input type="text" id="db_name" name="db_name" value="<?php echo htmlspecialchars($db_name); ?>" required>
  111. </div>
  112. <div class="form-group">
  113. <label for="db_user">Database Username:</label>
  114. <input type="text" id="db_user" name="db_user" value="<?php echo htmlspecialchars($db_user); ?>" required>
  115. </div>
  116. <div class="form-group">
  117. <label for="db_pass">Database Password:</label>
  118. <input type="password" id="db_pass" name="db_pass">
  119. </div>
  120. <h2>Site Configuration</h2>
  121. <div class="form-group">
  122. <label for="site_title">Site Title:</label>
  123. <input type="text" id="site_title" name="site_title" value="<?php echo htmlspecialchars($site_title); ?>" required>
  124. </div>
  125. <h2>Administrator Account</h2>
  126. <div class="form-group">
  127. <label for="admin_username">Admin Username:</label>
  128. <input type="text" id="admin_username" name="admin_username" value="<?php echo htmlspecialchars($admin_username); ?>" required>
  129. </div>
  130. <div class="form-group">
  131. <label for="admin_password">Admin Password:</label>
  132. <input type="password" id="admin_password" name="admin_password" required>
  133. </div>
  134. <div class="form-group">
  135. <label for="admin_email">Admin Email:</label>
  136. <input type="email" id="admin_email" name="admin_email" value="<?php echo htmlspecialchars($admin_email); ?>" required>
  137. </div>
  138. <button type="submit" class="btn btn-primary">Install System</button>
  139. </form>
  140. <?php endif; ?>
  141. </div>
  142. </body>
  143. </html>