\n};
print qq{
\n};
print qq{
\n};
@@ -161,372 +121,3 @@ JS
print GnizaWHM::UI::page_footer();
Whostmgr::HTMLInterface::footer();
}
-
-# ── Step 2: Remote Destination ───────────────────────────────
-
-sub handle_step2 {
- my @errors;
- my $key_path = $form->{'key_path'} // '/root/.ssh/id_ed25519';
-
- if ($method eq 'POST') {
- unless (GnizaWHM::UI::verify_csrf_token($form->{'gniza_csrf'})) {
- push @errors, 'Invalid or expired form token. Please try again.';
- }
-
- my $name = $form->{'remote_name'} // '';
- my $name_err = GnizaWHM::Validator::validate_remote_name($name);
- push @errors, $name_err if $name_err;
-
- if (!@errors && -f GnizaWHM::UI::remote_conf_path($name)) {
- push @errors, "A remote named '$name' already exists.";
- }
-
- my %data;
- for my $key (@GnizaWHM::Config::REMOTE_KEYS) {
- $data{$key} = $form->{$key} // '';
- }
-
- if (!@errors) {
- my $validation_errors = GnizaWHM::Validator::validate_remote_config(\%data);
- push @errors, @$validation_errors;
- }
-
- if (!@errors) {
- my ($ssh_ok, $ssh_err) = GnizaWHM::UI::test_ssh_connection(
- $data{REMOTE_HOST},
- $data{REMOTE_PORT} || '22',
- $data{REMOTE_USER} || 'root',
- $data{REMOTE_KEY},
- );
- push @errors, "SSH connection test failed: $ssh_err" unless $ssh_ok;
- }
-
- if (!@errors) {
- my $dest = GnizaWHM::UI::remote_conf_path($name);
- my $example = GnizaWHM::UI::remote_example_path();
- if (-f $example) {
- File::Copy::copy($example, $dest)
- or do { push @errors, "Failed to create remote file: $!"; goto RENDER_STEP2; };
- }
- my ($ok, $err) = GnizaWHM::Config::write($dest, \%data, \@GnizaWHM::Config::REMOTE_KEYS);
- if ($ok) {
- print "Status: 302 Found\r\n";
- print "Location: setup.cgi?step=3&remote_name=" . _uri_escape($name) . "\r\n\r\n";
- exit;
- } else {
- push @errors, "Failed to save remote config: $err";
- }
- }
- }
-
- RENDER_STEP2:
-
- print "Content-Type: text/html\r\n\r\n";
- Whostmgr::HTMLInterface::defheader('gniza Setup Wizard', '', '/cgi/gniza-whm/setup.cgi');
-
- print GnizaWHM::UI::page_header('gniza Setup Wizard');
- render_steps_indicator(2);
-
- if (@errors) {
- print GnizaWHM::UI::render_errors(\@errors);
- }
-
- my $conf = {};
- my $name_val = '';
- if ($method eq 'POST') {
- for my $key (@GnizaWHM::Config::REMOTE_KEYS) {
- $conf->{$key} = $form->{$key} // '';
- }
- $name_val = GnizaWHM::UI::esc($form->{'remote_name'} // '');
- } else {
- $conf->{REMOTE_KEY} = $key_path;
- $conf->{REMOTE_PORT} = '22';
- $conf->{REMOTE_USER} = 'root';
- $conf->{REMOTE_BASE} = '/backups';
- $conf->{RETENTION_COUNT} = '30';
- $conf->{BWLIMIT} = '0';
- }
-
- print qq{
\n};
-
- print <<'JS';
-
-JS
-
- print GnizaWHM::UI::page_footer();
- Whostmgr::HTMLInterface::footer();
-}
-
-# ── Step 3: Schedule (writes to schedules.d/) ────────────────
-
-sub handle_step3 {
- my $remote_name = $form->{'remote_name'} // '';
- my @errors;
-
- my $name_err = GnizaWHM::Validator::validate_remote_name($remote_name);
- if ($name_err) {
- GnizaWHM::UI::set_flash('error', 'Invalid remote name. Please start the wizard again.');
- print "Status: 302 Found\r\n";
- print "Location: setup.cgi\r\n\r\n";
- exit;
- }
-
- my $remote_conf_path = GnizaWHM::UI::remote_conf_path($remote_name);
- unless (-f $remote_conf_path) {
- GnizaWHM::UI::set_flash('error', "Remote '$remote_name' not found. Please start the wizard again.");
- print "Status: 302 Found\r\n";
- print "Location: setup.cgi\r\n\r\n";
- exit;
- }
-
- if ($method eq 'POST') {
- unless (GnizaWHM::UI::verify_csrf_token($form->{'gniza_csrf'})) {
- push @errors, 'Invalid or expired form token. Please try again.';
- }
-
- my $schedule = $form->{SCHEDULE} // '';
-
- if ($schedule ne '' && !@errors) {
- # Create a schedule config in schedules.d/ targeting this remote
- my $sched_name = $remote_name;
- my %data = (
- SCHEDULE => $schedule,
- SCHEDULE_TIME => $form->{SCHEDULE_TIME} // '02:00',
- SCHEDULE_DAY => $form->{SCHEDULE_DAY} // '',
- SCHEDULE_CRON => $form->{SCHEDULE_CRON} // '',
- REMOTES => $remote_name,
- );
-
- my $validation_errors = GnizaWHM::Validator::validate_schedule_config(\%data);
- push @errors, @$validation_errors;
-
- if (!@errors) {
- my $sched_path = GnizaWHM::UI::schedule_conf_path($sched_name);
- my $example = GnizaWHM::UI::schedule_example_path();
- if (-f $example) {
- File::Copy::copy($example, $sched_path)
- or do { push @errors, "Failed to create schedule file: $!"; };
- }
- if (!@errors) {
- my ($ok, $err) = GnizaWHM::Config::write($sched_path, \%data, \@GnizaWHM::Config::SCHEDULE_KEYS);
- push @errors, "Failed to save schedule: $err" unless $ok;
- }
- }
-
- if (!@errors) {
- my ($ok, $stdout, $stderr) = GnizaWHM::Cron::install_schedules();
- if (!$ok) {
- push @errors, "Schedule saved but cron install failed: $stderr";
- }
- }
- }
-
- if (!@errors) {
- GnizaWHM::UI::set_flash('success', 'Setup complete! Your first remote destination is configured.');
- print "Status: 302 Found\r\n";
- print "Location: index.cgi\r\n\r\n";
- exit;
- }
- }
-
- print "Content-Type: text/html\r\n\r\n";
- Whostmgr::HTMLInterface::defheader('gniza Setup Wizard', '', '/cgi/gniza-whm/setup.cgi');
-
- print GnizaWHM::UI::page_header('gniza Setup Wizard');
- render_steps_indicator(3);
-
- if (@errors) {
- print GnizaWHM::UI::render_errors(\@errors);
- }
-
- my $esc_name = GnizaWHM::UI::esc($remote_name);
-
- # Load existing schedule data from schedules.d/ if it exists, else from POST
- my $conf = {};
- if (@errors && $method eq 'POST') {
- for my $key (@GnizaWHM::Config::SCHEDULE_KEYS) {
- $conf->{$key} = $form->{$key} // '';
- }
- } elsif (-f GnizaWHM::UI::schedule_conf_path($remote_name)) {
- $conf = GnizaWHM::Config::parse(GnizaWHM::UI::schedule_conf_path($remote_name), 'schedule');
- }
-
- print qq{
\n};
-
- print <<'JS';
-
-JS
-
- print GnizaWHM::UI::page_footer();
- Whostmgr::HTMLInterface::footer();
-}
-
-# ── Shared Helpers ───────────────────────────────────────────
-
-sub render_steps_indicator {
- my ($current) = @_;
- my @labels = ('SSH Key', 'Remote', 'Schedule');
- print qq{
\n};
- for my $i (1..3) {
- my $class = 'step';
- $class .= ' step-primary' if $i <= $current;
- print qq{ - $labels[$i-1]
\n};
- }
- print qq{
\n};
-}
-
-sub _wiz_field {
- my ($name, $val, $label, $hint) = @_;
- $val = GnizaWHM::UI::esc($val // '');
- my $hint_html = $hint ? qq{
$hint} : '';
- print qq{
\n};
- print qq{ \n};
- print qq{ \n};
- print qq{ $hint_html\n} if $hint;
- print qq{
\n};
-}
-
-sub _wiz_field_conf {
- my ($conf, $key, $label, $hint) = @_;
- _wiz_field($key, $conf->{$key}, $label, $hint);
-}
-
-sub _uri_escape {
- my ($str) = @_;
- $str =~ s/([^A-Za-z0-9._~-])/sprintf("%%%02X", ord($1))/ge;
- return $str;
-}