setup.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // LDAP configuration
  20. $ldap_enabled = false;
  21. $ldap_host = '';
  22. $ldap_port = '389';
  23. $ldap_base_dn = '';
  24. $ldap_user_filter = '(uid={username})';
  25. $ldap_bind_dn = '';
  26. $ldap_bind_password = '';
  27. $ldap_email_attribute = 'mail';
  28. $ldap_name_attribute = 'cn';
  29. $errors = [];
  30. $success = false;
  31. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  32. // Validate and sanitize input
  33. $db_host = trim($_POST['db_host'] ?? 'localhost');
  34. $db_name = trim($_POST['db_name'] ?? 'webpub');
  35. $db_user = trim($_POST['db_user'] ?? '');
  36. $db_pass = trim($_POST['db_pass'] ?? '');
  37. $site_title = trim($_POST['site_title'] ?? 'Web Publication System');
  38. $admin_username = trim($_POST['admin_username'] ?? 'admin');
  39. $admin_password = trim($_POST['admin_password'] ?? '');
  40. $admin_email = trim($_POST['admin_email'] ?? '');
  41. // LDAP configuration
  42. $ldap_enabled = isset($_POST['ldap_enabled']);
  43. $ldap_host = trim($_POST['ldap_host'] ?? '');
  44. $ldap_port = trim($_POST['ldap_port'] ?? '389');
  45. $ldap_base_dn = trim($_POST['ldap_base_dn'] ?? '');
  46. $ldap_user_filter = trim($_POST['ldap_user_filter'] ?? '(uid={username})');
  47. $ldap_bind_dn = trim($_POST['ldap_bind_dn'] ?? '');
  48. $ldap_bind_password = trim($_POST['ldap_bind_password'] ?? '');
  49. $ldap_email_attribute = trim($_POST['ldap_email_attribute'] ?? 'mail');
  50. $ldap_name_attribute = trim($_POST['ldap_name_attribute'] ?? 'cn');
  51. // Validation
  52. if (empty($db_user)) $errors[] = 'Database username is required';
  53. if (empty($admin_email)) $errors[] = 'Admin email is required';
  54. if (!filter_var($admin_email, FILTER_VALIDATE_EMAIL)) $errors[] = 'Invalid email format';
  55. // If LDAP is disabled, require admin password
  56. if (!$ldap_enabled && empty($admin_password)) {
  57. $errors[] = 'Admin password is required when LDAP is disabled';
  58. }
  59. // LDAP validation
  60. if ($ldap_enabled) {
  61. if (empty($ldap_host)) $errors[] = 'LDAP host is required when LDAP is enabled';
  62. if (empty($ldap_base_dn)) $errors[] = 'LDAP base DN is required when LDAP is enabled';
  63. }
  64. if (empty($errors)) {
  65. try {
  66. // Test database connection
  67. $pdo = new PDO("mysql:host=$db_host", $db_user, $db_pass);
  68. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  69. // Create database if it doesn't exist
  70. $pdo->exec("CREATE DATABASE IF NOT EXISTS `$db_name` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
  71. $pdo->exec("USE `$db_name`");
  72. // Import SQL schema
  73. $sql = file_get_contents('database.sql');
  74. $pdo->exec($sql);
  75. // Update admin user with provided credentials
  76. if ($ldap_enabled) {
  77. // For LDAP, don't require local password
  78. $stmt = $pdo->prepare("UPDATE users SET username = ?, email = ?, auth_type = 'ldap' WHERE username = 'admin'");
  79. $stmt->execute([$admin_username, $admin_email]);
  80. } else {
  81. $hashed_password = password_hash($admin_password, PASSWORD_DEFAULT);
  82. $stmt = $pdo->prepare("UPDATE users SET username = ?, password = ?, email = ?, auth_type = 'local' WHERE username = 'admin'");
  83. $stmt->execute([$admin_username, $hashed_password, $admin_email]);
  84. }
  85. // Create configuration file
  86. $config_content = "<?php\n";
  87. $config_content .= "// Database configuration\n";
  88. $config_content .= "define('DB_HOST', '$db_host');\n";
  89. $config_content .= "define('DB_NAME', '$db_name');\n";
  90. $config_content .= "define('DB_USER', '$db_user');\n";
  91. $config_content .= "define('DB_PASS', '$db_pass');\n\n";
  92. $config_content .= "// Site configuration\n";
  93. $config_content .= "define('SITE_TITLE', '$site_title');\n";
  94. $config_content .= "define('SITE_URL', '" . ($_SERVER['HTTPS'] ? 'https' : 'http') . "://{$_SERVER['HTTP_HOST']}" . dirname($_SERVER['PHP_SELF']) . "/../');\n";
  95. $config_content .= "define('ADMIN_EMAIL', '$admin_email');\n\n";
  96. $config_content .= "// LDAP configuration\n";
  97. $config_content .= "define('LDAP_ENABLED', " . ($ldap_enabled ? 'true' : 'false') . ");\n";
  98. $config_content .= "define('LDAP_HOST', '$ldap_host');\n";
  99. $config_content .= "define('LDAP_PORT', '$ldap_port');\n";
  100. $config_content .= "define('LDAP_BASE_DN', '$ldap_base_dn');\n";
  101. $config_content .= "define('LDAP_USER_FILTER', '$ldap_user_filter');\n";
  102. $config_content .= "define('LDAP_BIND_DN', '$ldap_bind_dn');\n";
  103. $config_content .= "define('LDAP_BIND_PASSWORD', '$ldap_bind_password');\n";
  104. $config_content .= "define('LDAP_EMAIL_ATTRIBUTE', '$ldap_email_attribute');\n";
  105. $config_content .= "define('LDAP_NAME_ATTRIBUTE', '$ldap_name_attribute');\n";
  106. if (file_put_contents('../includes/config.php', $config_content)) {
  107. $success = true;
  108. } else {
  109. $errors[] = 'Failed to create configuration file. Check file permissions.';
  110. }
  111. } catch (PDOException $e) {
  112. $errors[] = 'Database error: ' . $e->getMessage();
  113. }
  114. }
  115. }
  116. ?>
  117. <!DOCTYPE html>
  118. <html lang="en">
  119. <head>
  120. <meta charset="UTF-8">
  121. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  122. <title>Web Publication System Setup</title>
  123. <link rel="stylesheet" href="../css/style.css">
  124. </head>
  125. <body>
  126. <div class="container">
  127. <h1>Web Publication System Setup</h1>
  128. <?php if ($success): ?>
  129. <div class="alert alert-success">
  130. <h3>Installation Complete!</h3>
  131. <p>The system has been successfully installed. You can now:</p>
  132. <ul>
  133. <li><a href="../admin/">Access the admin panel</a></li>
  134. <li><a href="../public/">View the public site</a></li>
  135. </ul>
  136. <p><strong>Important:</strong> Delete the setup directory for security.</p>
  137. </div>
  138. <?php else: ?>
  139. <?php if (!empty($errors)): ?>
  140. <div class="alert alert-error">
  141. <?php foreach ($errors as $error): ?>
  142. <p><?php echo htmlspecialchars($error); ?></p>
  143. <?php endforeach; ?>
  144. </div>
  145. <?php endif; ?>
  146. <form method="post" class="setup-form">
  147. <h2>Database Configuration</h2>
  148. <div class="form-group">
  149. <label for="db_host">Database Host:</label>
  150. <input type="text" id="db_host" name="db_host" value="<?php echo htmlspecialchars($db_host); ?>" required>
  151. </div>
  152. <div class="form-group">
  153. <label for="db_name">Database Name:</label>
  154. <input type="text" id="db_name" name="db_name" value="<?php echo htmlspecialchars($db_name); ?>" required>
  155. </div>
  156. <div class="form-group">
  157. <label for="db_user">Database Username:</label>
  158. <input type="text" id="db_user" name="db_user" value="<?php echo htmlspecialchars($db_user); ?>" required>
  159. </div>
  160. <div class="form-group">
  161. <label for="db_pass">Database Password:</label>
  162. <input type="password" id="db_pass" name="db_pass">
  163. </div>
  164. <h2>Site Configuration</h2>
  165. <div class="form-group">
  166. <label for="site_title">Site Title:</label>
  167. <input type="text" id="site_title" name="site_title" value="<?php echo htmlspecialchars($site_title); ?>" required>
  168. </div>
  169. <h2>Authentication Configuration</h2>
  170. <div class="form-group">
  171. <label>
  172. <input type="checkbox" id="ldap_enabled" name="ldap_enabled" <?php echo $ldap_enabled ? 'checked' : ''; ?> onchange="toggleLdapFields()">
  173. Enable LDAP Authentication
  174. </label>
  175. <p class="help-text">When enabled, users will authenticate against LDAP directory instead of local database.</p>
  176. </div>
  177. <div id="ldap_fields" style="display: <?php echo $ldap_enabled ? 'block' : 'none'; ?>;">
  178. <h3>LDAP Configuration</h3>
  179. <div class="form-group">
  180. <label for="ldap_host">LDAP Server Host:</label>
  181. <input type="text" id="ldap_host" name="ldap_host" value="<?php echo htmlspecialchars($ldap_host); ?>" <?php echo $ldap_enabled ? 'required' : ''; ?>>
  182. </div>
  183. <div class="form-group">
  184. <label for="ldap_port">LDAP Port:</label>
  185. <input type="text" id="ldap_port" name="ldap_port" value="<?php echo htmlspecialchars($ldap_port); ?>">
  186. </div>
  187. <div class="form-group">
  188. <label for="ldap_base_dn">LDAP Base DN:</label>
  189. <input type="text" id="ldap_base_dn" name="ldap_base_dn" value="<?php echo htmlspecialchars($ldap_base_dn); ?>" <?php echo $ldap_enabled ? 'required' : ''; ?> placeholder="Example: dc=example,dc=com">
  190. </div>
  191. <div class="form-group">
  192. <label for="ldap_user_filter">User Search Filter:</label>
  193. <input type="text" id="ldap_user_filter" name="ldap_user_filter" value="<?php echo htmlspecialchars($ldap_user_filter); ?>" placeholder="Example: (uid={username})">
  194. </div>
  195. <div class="form-group">
  196. <label for="ldap_bind_dn">Bind DN (optional):</label>
  197. <input type="text" id="ldap_bind_dn" name="ldap_bind_dn" value="<?php echo htmlspecialchars($ldap_bind_dn); ?>" placeholder="Leave empty for anonymous bind">
  198. </div>
  199. <div class="form-group">
  200. <label for="ldap_bind_password">Bind Password (optional):</label>
  201. <input type="password" id="ldap_bind_password" name="ldap_bind_password" value="<?php echo htmlspecialchars($ldap_bind_password); ?>">
  202. </div>
  203. <div class="form-group">
  204. <label for="ldap_email_attribute">Email Attribute:</label>
  205. <input type="text" id="ldap_email_attribute" name="ldap_email_attribute" value="<?php echo htmlspecialchars($ldap_email_attribute); ?>">
  206. </div>
  207. <div class="form-group">
  208. <label for="ldap_name_attribute">Name Attribute:</label>
  209. <input type="text" id="ldap_name_attribute" name="ldap_name_attribute" value="<?php echo htmlspecialchars($ldap_name_attribute); ?>">
  210. </div>
  211. </div>
  212. <div id="local_auth_fields" style="display: <?php echo $ldap_enabled ? 'none' : 'block'; ?>;">
  213. <h3>Local Administrator Account</h3>
  214. <div class="form-group">
  215. <label for="admin_username">Admin Username:</label>
  216. <input type="text" id="admin_username" name="admin_username" value="<?php echo htmlspecialchars($admin_username); ?>" <?php echo !$ldap_enabled ? 'required' : ''; ?>>
  217. </div>
  218. <div class="form-group">
  219. <label for="admin_password">Admin Password:</label>
  220. <input type="password" id="admin_password" name="admin_password" <?php echo !$ldap_enabled ? 'required' : ''; ?>>
  221. </div>
  222. </div>
  223. <div class="form-group">
  224. <label for="admin_email">Admin Email:</label>
  225. <input type="email" id="admin_email" name="admin_email" value="<?php echo htmlspecialchars($admin_email); ?>" required>
  226. </div>
  227. <button type="submit" class="btn btn-primary">Install System</button>
  228. </form>
  229. <?php endif; ?>
  230. </div>
  231. <script>
  232. function toggleLdapFields() {
  233. const ldapEnabled = document.getElementById('ldap_enabled').checked;
  234. const ldapFields = document.getElementById('ldap_fields');
  235. const localAuthFields = document.getElementById('local_auth_fields');
  236. const adminUsername = document.getElementById('admin_username');
  237. const adminPassword = document.getElementById('admin_password');
  238. const ldapHost = document.getElementById('ldap_host');
  239. const ldapBaseDn = document.getElementById('ldap_base_dn');
  240. if (ldapEnabled) {
  241. ldapFields.style.display = 'block';
  242. localAuthFields.style.display = 'none';
  243. adminUsername.removeAttribute('required');
  244. adminPassword.removeAttribute('required');
  245. ldapHost.setAttribute('required', 'required');
  246. ldapBaseDn.setAttribute('required', 'required');
  247. } else {
  248. ldapFields.style.display = 'none';
  249. localAuthFields.style.display = 'block';
  250. adminUsername.setAttribute('required', 'required');
  251. adminPassword.setAttribute('required', 'required');
  252. ldapHost.removeAttribute('required');
  253. ldapBaseDn.removeAttribute('required');
  254. }
  255. }
  256. </script>
  257. </body>
  258. </html>