setup.php 15 KB

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