setup.php 15 KB

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