-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
152 lines (133 loc) · 5.44 KB
/
setup.php
File metadata and controls
152 lines (133 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
declare(strict_types=1);
// Prevent caching
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
/**
* WebyMail – First-run setup wizard
* Access this file directly (setup.php) before the application is configured.
* Once setup is complete, this file can be deleted or renamed.
*/
define('WEBYMAIL_ROOT', __DIR__);
spl_autoload_register(function (string $class): void {
$file = __DIR__ . '/src/' . $class . '.php';
if (file_exists($file)) {
require_once $file;
}
});
require_once __DIR__ . '/src/Config.php';
// If already set up, redirect to main app unless force setup is requested
if (Config::get('setup_complete') && ($_GET['force'] ?? '') !== '1' && ($_GET['action'] ?? '') !== 'setup') {
header('Location: index.php');
exit;
}
$step = 'welcome';
$error = null;
$requirements = [];
$securityChecks = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$step = $_POST['step'] ?? 'welcome';
if ($step === 'requirements') {
$needed = ['imap', 'pdo_sqlite', 'openssl', 'mbstring', 'iconv'];
$allOk = true;
foreach ($needed as $ext) {
$ok = extension_loaded($ext);
$requirements[$ext] = $ok;
if (!$ok) $allOk = false;
}
if (!$allOk && !isset($_POST['ignore_requirements'])) {
$step = 'requirements';
} else {
$step = 'server';
}
} elseif ($step === 'server') {
// Just show server settings form
} elseif ($step === 'save') {
// Validate and save
$appName = trim($_POST['app_name'] ?? 'WebyMail');
$imapHost = trim($_POST['imap_host'] ?? '');
$imapPort = (int) ($_POST['imap_port'] ?? 993);
$imapSsl = !empty($_POST['imap_ssl']);
$smtpHost = trim($_POST['smtp_host'] ?? '');
$smtpPort = (int) ($_POST['smtp_port'] ?? 587);
$smtpSsl = !empty($_POST['smtp_ssl']);
$smtpTls = !empty($_POST['smtp_starttls']);
$captchaOn = !empty($_POST['captcha_enabled']);
$timezone = trim($_POST['timezone'] ?? 'Europe/Rome');
$hideServer = !empty($_POST['hide_server_on_login']);
$removeFavicon = !empty($_POST['remove_favicon']);
Config::set('app_name', $appName);
Config::set('imap_host', $imapHost);
Config::set('imap_port', $imapPort);
Config::set('imap_ssl', $imapSsl);
Config::set('smtp_host', $smtpHost);
Config::set('smtp_port', $smtpPort);
Config::set('smtp_ssl', $smtpSsl);
Config::set('smtp_starttls', $smtpTls);
Config::set('captcha_enabled', $captchaOn);
Config::set('2fa_enabled', true); // Always enabled by default, can be disabled in config.php manually if needed
Config::set('timezone', $timezone);
Config::set('hide_server_on_login', $hideServer);
Config::set('setup_complete', true);
Config::set('version', null); // Ensure version is removed from config.php
Config::set('update_url', Config::UPDATE_URL);
// Cleanup obsolete keys
Config::set('altcha_hmac_key', null);
Config::set('altcha_enabled', null);
if ($removeFavicon) {
Config::set('favicon_path', null);
}
// Handle Favicon upload
if (!empty($_FILES['favicon']['name']) && $_FILES['favicon']['error'] === UPLOAD_ERR_OK) {
$ext = strtolower(pathinfo($_FILES['favicon']['name'], PATHINFO_EXTENSION));
if (in_array($ext, ['ico', 'png', 'svg'])) {
$imgDir = __DIR__ . '/assets/img';
if (!is_dir($imgDir)) {
mkdir($imgDir, 0755, true);
}
$faviconPath = 'assets/img/favicon.' . $ext;
if (move_uploaded_file($_FILES['favicon']['tmp_name'], __DIR__ . '/' . $faviconPath)) {
Config::set('favicon_path', $faviconPath);
}
}
}
// Ensure data directory is writable
$dataDir = __DIR__ . '/data';
if (!is_dir($dataDir) && !mkdir($dataDir, 0750, true)) {
$error = 'Cannot create data/ directory. Please create it manually and make it writable.';
$step = 'server';
} else {
Config::save();
// Initialise the database (creates the SQLite file + schema)
try {
Database::getInstance();
} catch (Exception $e) {
$error = 'Database initialisation failed: ' . $e->getMessage();
$step = 'server';
}
if ($error === null) {
$step = 'security';
$sys = Config::checkSystem();
$securityChecks = $sys['security'];
}
}
} elseif ($step === 'finish') {
$step = 'done';
// Auto-rename setup.php to prevent accidental re-runs
@rename(__FILE__, __FILE__ . '.bak');
} elseif ($step === 'fix_permissions') {
Config::fixPermissions();
$step = 'security';
$sys = Config::checkSystem();
$securityChecks = $sys['security'];
}
}
// Render
ob_start();
include __DIR__ . '/templates/setup.php';
$content = ob_get_clean();
$pageTitle = Config::get('app_name', 'WebyMail') . ' Setup';
$shellLayout = false;
include __DIR__ . '/templates/layout.php';