X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=bin%2Fmozilla%2Fadmin.pl;h=5f3e0b19d8f2e95406aa48c8c58a48ba1e65a30a;hb=16455a4f7a2e871d645485ffb5904d197b907217;hp=f3edde8e88588b33cdc700e2359672c0fa3c2cd1;hpb=0b280f98054da74f79cbc9fdb3e9f4c23326be53;p=kivitendo-erp.git diff --git a/bin/mozilla/admin.pl b/bin/mozilla/admin.pl old mode 100644 new mode 100755 index f3edde8e8..5f3e0b19d --- a/bin/mozilla/admin.pl +++ b/bin/mozilla/admin.pl @@ -32,692 +32,498 @@ # #====================================================================== -$menufile = "menu.ini"; - use DBI; -use CGI; - +use Encode; +use English qw(-no_match_vars); +use Fcntl; +use File::Copy; +use File::Find; +use File::Spec; +use Cwd; +use IO::Dir; +use IO::File; +use POSIX qw(strftime); +use Sys::Hostname; + +use SL::Auth; +use SL::Auth::PasswordPolicy; +use SL::DB::AuthClient; +use SL::DB::AuthUser; use SL::Form; +use SL::Iconv; +use SL::Mailer; use SL::User; use SL::Common; use SL::Inifile; use SL::DBUpgrade2; +use SL::DBUtils; +use SL::Template; require "bin/mozilla/common.pl"; -our $cgi = new CGI(''); +use strict; -$form = new Form; -$form->{"root"} = "root login"; +# parserhappy(R): -$locale = new Locale $language, "admin"; +# $locale->text('periodic') +# $locale->text('income') +# $locale->text('perpetual') +# $locale->text('balance') -# customization -if (-f "bin/mozilla/custom_$form->{script}") { - eval { require "bin/mozilla/custom_$form->{script}"; }; - $form->error($@) if ($@); -} +our $cgi; +our $form; +our $locale; +our $auth; -$form->{stylesheet} = "lx-office-erp.css"; -$form->{favicon} = "favicon.ico"; +sub run { + $::lxdebug->enter_sub; + my $session_result = shift; -if ($form->{action}) { + $form = $::form; + $locale = $::locale; + $auth = $::auth; + $::request->{layout} = SL::Layout::Dispatcher->new(style => 'admin'); + $::request->{layout}->use_stylesheet("lx-office-erp.css"); + $form->{favicon} = "favicon.ico"; - $subroutine = $locale->findsub($form->{action}); + if ($form->{action}) { + if ($auth->authenticate_root($form->{'{AUTH}admin_password'}) != $auth->OK()) { + $auth->punish_wrong_login; + $form->{error} = $locale->text('Incorrect password!'); + $auth->delete_session_value('admin_password'); + adminlogin(); + } else { + if ($auth->session_tables_present()) { + delete $::form->{'{AUTH}admin_password'}; + } - if ($subroutine eq 'login') { - if ($form->{rpw}) { - $form->{rpw} = crypt $form->{rpw}, "ro"; + call_sub($locale->findsub($form->{action})); } + } else { + adminlogin(); } - - check_password(); - - call_sub($subroutine); - -} else { - - # if there are no drivers bail out - $form->error($locale->text('No Database Drivers available!')) - unless (User->dbdrivers); - - # create memberfile - if (!-f $memberfile) { - open(FH, ">$memberfile") or $form->error("$memberfile : $!"); - print FH qq|# SQL-Ledger Accounting members - -[root login] -password= - -|; - close FH; - } - - adminlogin(); - + $::lxdebug->leave_sub; } -1; - -# end - sub adminlogin { - - $form->{title} = - qq|Lx-Office ERP $form->{version} | . $locale->text('Administration'); - - $form->header(); - print $form->parse_html_template('admin/adminlogin'); + print $::request->cgi->redirect('controller.pl?action=Admin/login'); } -sub login { - list_users(); +sub pg_database_administration { + my $form = $main::form; + dbselect_source(); } -sub list_users { - - $form->error($locale->text('File locked!')) if (-f "${memberfile}.LCK"); - - open(FH, "$memberfile") or $form->error("$memberfile : $!"); - - my %members; - - while () { - chomp; - - if (/^\[.*\]/) { - $login = $_; - $login =~ s/(\[|\])//g; - - $members{$login} = { "login" => $login }; - } - - if (/^([a-z]+)=(.*)/) { - $members{$login}->{$1} = $2; - } - } +sub dbselect_source { + my $form = $main::form; + my $locale = $main::locale; - close(FH); + $form->{dbport} = $::auth->{DB_config}->{port} || 5432; + $form->{dbuser} = $::auth->{DB_config}->{user} || 'lxoffice'; + $form->{dbdefault} = 'template1'; + $form->{dbhost} = $::auth->{DB_config}->{host} || 'localhost'; - delete $members{"root login"}; - map { $_->{templates} =~ s|.*/||; } values %members; + $form->{title} = "kivitendo / " . $locale->text('Database Administration'); - $form->{title} = "Lx-Office ERP " . $locale->text('Administration'); - $form->{LOCKED} = -e "$userspath/nologin"; - $form->{MEMBERS} = [ @members{sort { lc $a cmp lc $b } keys %members} ]; + # Intentionnaly disabled unless fixed to work with the authentication DB. + $form->{ALLOW_DBBACKUP} = 0; # "$pg_dump_exe" ne "DISABLED"; $form->header(); - print $form->parse_html_template("admin/list_users"); + print $form->parse_html_template("admin/dbadmin"); } -sub add_user { - - $form->{title} = - "Lx-Office ERP " - . $locale->text('Administration') . " / " - . $locale->text('Add User'); - - my $myconfig = { - "vclimit" => 200, - "countrycode" => "de", - "numberformat" => "1000,00", - "dateformat" => "dd.mm.yy", - "stylesheet" => "lx-office-erp.css", - "menustyle" => "v3", - }; - - edit_user_form($myconfig); +sub continue { + call_sub($main::form->{"nextsub"}); } -sub edit { - - $form->{title} = - "Lx-Office ERP " - . $locale->text('Administration') . " / " - . $locale->text('Edit User'); - $form->{edit} = 1; - - $form->isblank("login", $locale->text("The login is missing.")); - - # get user - my $myconfig = new User "$memberfile", "$form->{login}"; +sub update_dataset { + my $form = $main::form; + my $locale = $main::locale; - $myconfig->{signature} =~ s/\\n/\r\n/g; - $myconfig->{address} =~ s/\\n/\r\n/g; + $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Update Dataset'); - # strip basedir from templates directory - $myconfig->{templates} =~ s|.*/||; + my @need_updates = User->dbneedsupdate($form); + $form->{NEED_UPDATES} = \@need_updates; + $form->{ALL_UPDATED} = !scalar @need_updates; - edit_user_form($myconfig); + $form->header(); + print $form->parse_html_template("admin/update_dataset"); } -sub edit_user_form { - my ($myconfig) = @_; - - my @valid_dateformats = qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd); - $form->{ALL_DATEFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{dateformat} } } @valid_dateformats ]; +sub dbupdate { + my $form = $main::form; + my $locale = $main::locale; - my @valid_numberformats = qw(1,000.00 1000.00 1.000,00 1000,00); - $form->{ALL_NUMBERFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{numberformat} } } @valid_numberformats ]; + $::request->{layout}->use_stylesheet("lx-office-erp.css"); + $form->{title} = $locale->text("Dataset upgrade"); + $form->header(); - %countrycodes = User->country_codes; - $form->{ALL_COUNTRYCODES} = []; - foreach $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) { - push @{ $form->{ALL_COUNTRYCODES} }, { "value" => $countrycode, - "name" => $countrycodes{$countrycode}, - "selected" => $countrycode eq $myconfig->{countrycode} }; - } + my $rowcount = $form->{rowcount} * 1; + my @update_rows = grep { $form->{"update_$_"} } (1 .. $rowcount); + $form->{NOTHING_TO_DO} = !scalar @update_rows; + my $saved_form = save_form(); - # is there a templates basedir - if (!-d "$templates") { - $form->error(sprintf($locale->text("The directory %s does not exist."), $templates)); - } + $| = 1; - opendir TEMPLATEDIR, "$templates/." or $form->error("$templates : $!"); - my @all = readdir(TEMPLATEDIR); - my @alldir = sort grep { -d "$templates/$_" && !/^\.\.?$/ } @all; - my @allhtml = sort grep { -f "$templates/$_" && /\.html$/ } @all; - closedir TEMPLATEDIR; + print $form->parse_html_template("admin/dbupgrade_all_header"); - @alldir = grep !/\.(html|tex|sty|odt|xml|txb)$/, @alldir; - @alldir = grep !/^(webpages|\.svn)$/, @alldir; + foreach my $i (@update_rows) { + restore_form($saved_form); - @allhtml = reverse grep !/Default/, @allhtml; - push @allhtml, 'Default'; - @allhtml = reverse @allhtml; + %::myconfig = (); + map { $form->{$_} = $::myconfig{$_} = $form->{"${_}_${i}"} } qw(dbname dbhost dbport dbuser dbpasswd); - $form->{ALL_TEMPLATES} = [ map { { "name", => $_, "selected" => $_ eq $myconfig->{templates} } } @alldir ]; + print $form->parse_html_template("admin/dbupgrade_header"); - $lastitem = $allhtml[0]; - $lastitem =~ s/-.*//g; - $form->{ALL_MASTER_TEMPLATES} = [ { "name" => $lastitem, "selected" => $lastitem eq "German" } ]; - foreach $item (@allhtml) { - $item =~ s/-.*//g; - next if ($item eq $lastitem); + User->dbupdate($form); + User->dbupdate2(form => $form, updater => SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls, database => $form->{dbname}); - push @{ $form->{ALL_MASTER_TEMPLATES} }, { "name" => $item, "selected" => $item eq "German" }; - $lastitem = $item; + print $form->parse_html_template("admin/dbupgrade_footer"); } - # css dir has styles that are not intended as general layouts. - # reverting to hardcoded list - $form->{ALL_STYLESHEETS} = [ map { { "name" => $_, "selected" => $_ eq $myconfig->{stylesheet} } } qw(lx-office-erp.css Win2000.css) ]; + print $form->parse_html_template("admin/dbupgrade_all_done"); +} - $form->{"menustyle_" . $myconfig->{menustyle} } = 1; +sub create_dataset { + my $form = $main::form; + my $locale = $main::locale; - map { $form->{"myc_${_}"} = $myconfig->{$_} } keys %{ $myconfig }; + $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources($form); - # access control - my @acsorder = (); - my %acs = (); - my %excl = (); - open(FH, $menufile) or $form->error("$menufile : $!"); + $form->{CHARTS} = []; - while ($item = ) { - next unless $item =~ /\[/; - next if $item =~ /\#/; + tie my %dir_h, 'IO::Dir', 'sql/'; + foreach my $item (map { s/-chart\.sql$//; $_ } sort grep { /-chart\.sql\z/ && !/Default-chart.sql\z/ } keys %dir_h) { + push @{ $form->{CHARTS} }, { name => $item, + selected => $item eq "Germany-DATEV-SKR03EU" }; + } - $item =~ s/(\[|\])//g; - chomp $item; + $form->{ACCOUNTING_METHODS} = [ map { { name => $_, selected => $_ eq 'cash' } } qw(accrual cash) ]; + $form->{INVENTORY_SYSTEMS} = [ map { { name => $_, selected => $_ eq 'periodic' } } qw(perpetual periodic) ]; + $form->{PROFIT_DETERMINATIONS} = [ map { { name => $_, selected => $_ eq 'income' } } qw(balance income) ]; - my ($level, $menuitem); + my $default_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET; - if ($item =~ /--/) { - ($level, $menuitem) = split /--/, $item, 2; - } else { - $level = $item; - $menuitem = $item; - push @acsorder, $item; + my $cluster_encoding = User->dbclusterencoding($form); + if ($cluster_encoding && ($cluster_encoding =~ m/^(?:UTF-?8|UNICODE)$/i)) { + if ($::lx_office_conf{system}->{dbcharset} !~ m/^UTF-?8$/i) { + $form->show_generic_error($locale->text('The selected PostgreSQL installation uses UTF-8 as its encoding. ' . + 'Therefore you have to configure kivitendo to use UTF-8 as well.'), + 'back_button' => 1); } - $acs{$level} ||= []; - push @{ $acs{$level} }, $menuitem; + $form->{FORCE_DBENCODING} = 'UNICODE'; + } else { + $form->{DBENCODINGS} = [ map { { %{$_}, selected => $_->{charset} eq $default_charset } } @Common::db_encodings ]; } - foreach $item (split(/;/, $myconfig->{acs})) { - ($key, $value) = split /--/, $item, 2; - $excl{$key}{$value} = 1; - } + $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset'); - $form->{ACLS} = []; - $form->{all_acs} = ""; + $form->header(); + print $form->parse_html_template("admin/create_dataset"); +} - foreach $key (@acsorder) { - my $acl = { "checked" => $form->{login} ? !$excl{$key}->{$key} : 1, - "name" => "${key}--${key}", - "title" => $key, - "SUBACLS" => [], }; - $form->{all_acs} .= "${key}--${key};"; +sub dbcreate { + my $form = $main::form; + my $locale = $main::locale; - foreach $item (@{ $acs{$key} }) { - next if ($key eq $item); + $form->isblank("db", $locale->text('Dataset missing!')); + $form->isblank("defaultcurrency", $locale->text('Default currency missing!')); - my $subacl = { "checked" => $form->{login} ? !$excl{$key}->{$item} : 1, - "name" => "${key}--${item}", - "title" => $item }; - push @{ $acl->{SUBACLS} }, $subacl; - $form->{all_acs} .= "${key}--${item};"; - } - push @{ $form->{ACLS} }, $acl; - } + User->dbcreate(\%$form); - chop $form->{all_acs}; + $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset'); $form->header(); - print $form->parse_html_template("admin/edit_user"); + print $form->parse_html_template("admin/dbcreate"); } -sub save { +sub delete_dataset { + my $form = $main::form; + my $locale = $main::locale; - $form->{dbdriver} = 'Pg'; + my @dbsources = User->dbsources_unused($form); + $form->error($locale->text('Nothing to delete!')) unless @dbsources; - # no spaces allowed in login name - ($form->{login}) = split / /, $form->{login}; + $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset'); + $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ]; - $form->isblank("login", $locale->text('Login name missing!')); + $form->header(); + print $form->parse_html_template("admin/delete_dataset"); +} - # check for duplicates - if (!$form->{edit}) { - $temp = new User "$memberfile", "$form->{login}"; +sub dbdelete { + my $form = $main::form; + my $locale = $main::locale; - if ($temp->{login}) { - $form->error("$form->{login} " . $locale->text('is already a member!')); - } + if (!$form->{db}) { + $form->error($locale->text('No Dataset selected!')); } - # no spaces allowed in directories - ($form->{newtemplates}) = split / /, $form->{newtemplates}; - - if ($form->{newtemplates}) { - $form->{templates} = $form->{newtemplates}; - } else { - $form->{templates} = - ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login}; - } + User->dbdelete(\%$form); - # is there a basedir - if (!-d "$templates") { - $form->error(sprintf($locale->text("The directory %s does not exist."), $templates)); - } + $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset'); + $form->header(); + print $form->parse_html_template("admin/dbdelete"); +} - # add base directory to $form->{templates} - $form->{templates} =~ s|.*/||; - $form->{templates} = "$templates/$form->{templates}"; +sub backup_dataset { + my $form = $main::form; + my $locale = $main::locale; - $myconfig = new User "$memberfile", "$form->{login}"; + $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset'); - # redo acs variable and delete all the acs codes - my @acs; - foreach $item (split m|;|, $form->{all_acs}) { - my $name = "ACS_${item}"; - $name =~ s| |+|g; - push @acs, $item if !$form->{$name}; - delete $form->{$name}; + if ($::lx_office_conf{applications}->{pg_dump} eq "DISABLED") { + $form->error($locale->text('Database backups and restorations are disabled in the configuration.')); } - $form->{acs} = join ";", @acs; - $form->isblank("dbname", $locale->text('Dataset missing!')); - $form->isblank("dbuser", $locale->text('Database User missing!')); + my @dbsources = sort User->dbsources($form); + $form->{DATABASES} = [ map { { "dbname" => $_ } } @dbsources ]; + $form->{NO_DATABASES} = !scalar @dbsources; - foreach $item (keys %{$form}) { - $myconfig->{$item} = $form->{$item}; - } + my $username = getpwuid $UID || "unknown-user"; + my $hostname = hostname() || "unknown-host"; + $form->{from} = "kivitendo Admin <${username}\@${hostname}>"; - delete $myconfig->{stylesheet}; - if ($form->{userstylesheet}) { - $myconfig->{stylesheet} = $form->{userstylesheet}; - } - - $myconfig->save_member($memberfile, $userspath); - - if ($webdav) { - @webdavdirs = - qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen); - foreach $directory (@webdavdirs) { - $file = "webdav/" . $directory . "/webdav-user"; - if ($form->{$directory}) { - if (open(HTACCESS, "$file")) { - while () { - ($login, $password) = split(/:/, $_); - if ($login ne $form->{login}) { - $newfile .= $_; - } - } - close(HTACCESS); - } - open(HTACCESS, "> $file") or die "cannot open $file $!\n"; - $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n"; - print(HTACCESS $newfile); - close(HTACCESS); - } else { - $form->{$directory} = 0; - if (open(HTACCESS, "$file")) { - while () { - ($login, $password) = split(/:/, $_); - if ($login ne $form->{login}) { - $newfile .= $_; - } - } - close(HTACCESS); - } - open(HTACCESS, "> $file") or die "cannot open $file $!\n"; - print(HTACCESS $newfile); - close(HTACCESS); - } - } - } + $form->header(); + print $form->parse_html_template("admin/backup_dataset"); +} - $form->{templates} =~ s|.*/||; - $form->{templates} = "${templates}/$form->{templates}"; - $form->{mastertemplates} =~ s|.*/||; +sub backup_dataset_start { + my $form = $main::form; + my $locale = $main::locale; - # create user template directory and copy master files - if (!-d "$form->{templates}") { - umask(002); + $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset'); - if (mkdir "$form->{templates}", oct("771")) { + my $pg_dump_exe = $::lx_office_conf{applications}->{pg_dump} || "pg_dump"; - umask(007); + if ("$pg_dump_exe" eq "DISABLED") { + $form->error($locale->text('Database backups and restorations are disabled in the configuration.')); + } - # copy templates to the directory - opendir TEMPLATEDIR, "$templates/." or $form - error("$templates : $!"); - @templates = grep /$form->{mastertemplates}.*?\.(html|tex|sty|xml|txb)$/, - readdir TEMPLATEDIR; - closedir TEMPLATEDIR; + $form->isblank("dbname", $locale->text('The dataset name is missing.')); + $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email"; - foreach $file (@templates) { - open(TEMP, "$templates/$file") - or $form->error("$templates/$file : $!"); + my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id(); + mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO"); - $file =~ s/$form->{mastertemplates}-//; - open(NEW, ">$form->{templates}/$file") - or $form->error("$form->{templates}/$file : $!"); + my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600); - while ($line = ) { - print NEW $line; - } - close(TEMP); - close(NEW); - } - } else { - $form->error("$!: $form->{templates}"); - } + if (!$pgpass) { + unlink $tmpdir; + $form->error($locale->text('A temporary file could not be created:') . " $ERRNO"); } - $form->redirect($locale->text('User saved!')); + print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n"; + $pgpass->close(); -} + $ENV{HOME} = $tmpdir; -sub delete { - $form->error($locale->text('File locked!')) if (-f ${memberfile} . LCK); - open(FH, ">${memberfile}.LCK") or $form->error("${memberfile}.LCK : $!"); - close(FH); - - my $members = Inifile->new($memberfile); - my $templates = $members->{$form->{login}}->{templates}; - delete $members->{$form->{login}}; - $members->write(); - unlink "${memberfile}.LCK"; - - if ($templates) { - my $templates_in_use = 0; - foreach $login (keys %{ $members }) { - next if $login =~ m/^[A-Z]+$/; - next if $members->{$login}->{templates} ne $templates; - $templates_in_use = 1; - last; - } + my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser}); + push @args, ("-p", $form->{dbport}) if ($form->{dbport}); + push @args, $form->{dbname}; - if (!$templates_in_use && -d $templates) { - unlink <$templates/*>; - rmdir $templates; - } - } + my $cmd = "$pg_dump_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args); + my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar"; - # delete config file for user - unlink "$userspath/$form->{login}.conf"; + if ($form->{destination} ne "email") { + my $in = IO::File->new("$cmd |"); - $form->redirect($locale->text('User deleted!')); + if (!$in) { + unlink "${tmpdir}/.pgpass"; + rmdir $tmpdir; -} + $form->error($locale->text('The pg_dump process could not be started.')); + } -sub login_name { - my $login = shift; + print "content-type: application/x-tar\n"; + print "content-disposition: attachment; filename=\"${name}\"\n\n"; - $login =~ s/\[\]//g; - return ($login) ? $login : undef; + while (my $line = <$in>) { + print $line; + } -} + $in->close(); -sub get_value { - my $line = shift; + unlink "${tmpdir}/.pgpass"; + rmdir $tmpdir; - my ($null, $value) = split(/=/, $line, 2); + } else { + my $tmp = $tmpdir . "/dump_" . Common::unique_id(); - # remove comments - $value =~ s/\s#.*//g; + if (system("$cmd > $tmp") != 0) { + unlink "${tmpdir}/.pgpass", $tmp; + rmdir $tmpdir; - # remove any trailing whitespace - $value =~ s/^\s*(.*?)\s*$/$1/; + $form->error($locale->text('The pg_dump process could not be started.')); + } - $value; -} + my $mail = new Mailer; -sub change_admin_password { + map { $mail->{$_} = $form->{$_} } qw(from to cc subject message); - $form->{title} = - qq|Lx-Office ERP | - . $locale->text('Administration') . " / " - . $locale->text('Change Admin Password'); + $mail->{charset} = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET; + $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ]; + $mail->send(); - $form->header(); - print $form->parse_html_template("admin/change_admin_password"); -} + unlink "${tmpdir}/.pgpass", $tmp; + rmdir $tmpdir; -sub change_password { - if ($form->{"password"} ne $form->{"password_again"}) { - $form->{title} = - qq|Lx-Office ERP | - . $locale->text('Administration') . " / " - . $locale->text('Change Admin Password'); + $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset'); $form->header(); - $form->error($locale->text("The passwords do not match.")); + print $form->parse_html_template("admin/backup_dataset_email_done"); } +} - $root->{password} = $form->{password}; +sub restore_dataset { + my $form = $main::form; + my $locale = $main::locale; - $root->{'root login'} = 1; - $root->save_member($memberfile); + $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset'); - $form->{callback} = - "$form->{script}?action=list_users&rpw=$root->{password}"; + if ($::lx_office_conf{applications}->{pg_restore} eq "DISABLED") { + $form->error($locale->text('Database backups and restorations are disabled in the configuration.')); + } - $form->redirect($locale->text('Password changed!')); -} + my $default_charset = $::lx_office_conf{system}->{dbcharset}; + $default_charset ||= Common::DEFAULT_CHARSET; -sub check_password { - $root = new User "$memberfile", $form->{root}; + $form->{DBENCODINGS} = []; - if (!defined($root->{password}) || ($root->{password} ne $form->{rpw})) { - $form->error($locale->text('Incorrect Password!')); + foreach my $encoding (@Common::db_encodings) { + push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding}, + "label" => $encoding->{label}, + "selected" => $encoding->{charset} eq $default_charset }; } + $form->header(); + print $form->parse_html_template("admin/restore_dataset"); } -sub pg_database_administration { +sub restore_dataset_start { + my $form = $main::form; + my $locale = $main::locale; - $form->{dbdriver} = 'Pg'; - dbselect_source(); + $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset'); -} - -sub dbselect_source { - $form->{dbport} = '5432'; - $form->{dbuser} = 'postgres'; - $form->{dbdefault} = 'template1'; - $form->{dbhost} = 'localhost'; + my $pg_restore_exe = $::lx_office_conf{applications}->{pg_restore} || "pg_restore"; - $form->{title} = "Lx-Office ERP / " . $locale->text('Database Administration'); + if ("$pg_restore_exe" eq "DISABLED") { + $form->error($locale->text('Database backups and restorations are disabled in the configuration.')); + } - $form->header(); - print $form->parse_html_template("admin/dbadmin"); -} + $form->isblank("new_dbname", $locale->text('The dataset name is missing.')); + $form->isblank("content", $locale->text('No backup file has been uploaded.')); -sub continue { - call_sub($form->{"nextsub"}); -} + # Create temporary directories. Write the backup file contents to a temporary + # file. Create a .pgpass file with the username and password for the pg_restore + # utility. -sub update_dataset { - $form->{title} = - "Lx-Office ERP " - . $locale->text('Database Administration') . " / " - . $locale->text('Update Dataset'); + my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id(); + mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO"); - my @need_updates = User->dbneedsupdate($form); - $form->{NEED_UPDATES} = \@need_updates; - $form->{ALL_UPDATED} = !scalar @need_updates; + my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600); - $form->header(); - print $form->parse_html_template("admin/update_dataset"); -} + if (!$pgpass) { + unlink $tmpdir; + $form->error($locale->text('A temporary file could not be created:') . " $ERRNO"); + } -sub dbupdate { - $form->{stylesheet} = "lx-office-erp.css"; - $form->{title} = $locale->text("Dataset upgrade"); - $form->header(); + print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n"; + $pgpass->close(); - my $rowcount = $form->{rowcount} * 1; - my @update_rows = grep { $form->{"update_$_"} } (1 .. $rowcount); - $form->{NOTHING_TO_DO} = !scalar @update_rows; - my $saved_form = save_form(); + $ENV{HOME} = $tmpdir; - $| = 1; + my $tmp = $tmpdir . "/dump_" . Common::unique_id(); + my $tmpfile; - print $form->parse_html_template("admin/dbupgrade_all_header"); + if (substr($form->{content}, 0, 2) eq "\037\213") { + $tmpfile = IO::File->new("| gzip -d > $tmp"); + $tmpfile->binary(); - foreach my $i (@update_rows) { - restore_form($saved_form); + } else { + $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600); + } - map { $form->{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd); + if (!$tmpfile) { + unlink "${tmpdir}/.pgpass"; + rmdir $tmpdir; - my $controls = parse_dbupdate_controls($form, $form->{dbdriver}); + $form->error($locale->text('A temporary file could not be created:') . " $ERRNO"); + } - print $form->parse_html_template("admin/dbupgrade_header"); + print $tmpfile $form->{content}; + $tmpfile->close(); - $form->{dbupdate} = $form->{dbname}; - $form->{$form->{dbname}} = 1; + delete $form->{content}; - User->dbupdate($form); - User->dbupdate2($form, $controls); + # Try to connect to the database. Find out if a database with the same name exists. + # If yes, then drop the existing database. Create a new one with the name and encoding + # given by the user. - print $form->parse_html_template("admin/dbupgrade_footer"); - } + User::dbconnect_vars($form, "template1"); - print $form->parse_html_template("admin/dbupgrade_all_done"); -} + my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form }; + my $dbh = $form->dbconnect(\%myconfig) || $form->dberror(); -sub create_dataset { - $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources(\%$form); + my ($query, $sth); - $form->{CHARTS} = []; + $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g; - opendir SQLDIR, "sql/." or $form - error($!); - foreach $item (sort grep /-chart\.sql\z/, readdir SQLDIR) { - next if ($item eq 'Default-chart.sql'); - $item =~ s/-chart\.sql//; - push @{ $form->{CHARTS} }, { "name" => $item, - "selected" => $item eq "Germany-DATEV-SKR03EU" }; + $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|; + my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname}); + if ($count) { + do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|); } - closedir SQLDIR; - - my $default_charset = $dbcharset; - $default_charset ||= Common::DEFAULT_CHARSET; - - $form->{DBENCODINGS} = []; - foreach my $encoding (@Common::db_encodings) { - push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding}, - "label" => $encoding->{label}, - "selected" => $encoding->{charset} eq $default_charset }; + my $found = 0; + foreach my $item (@Common::db_encodings) { + if ($item->{dbencoding} eq $form->{dbencoding}) { + $found = 1; + last; + } } + $form->{dbencoding} = "LATIN9" unless $form->{dbencoding}; - $form->{title} = - "Lx-Office ERP " - . $locale->text('Database Administration') . " / " - . $locale->text('Create Dataset'); - - $form->header(); - print $form->parse_html_template("admin/create_dataset"); -} - -sub dbcreate { - $form->isblank("db", $locale->text('Dataset missing!')); - - User->dbcreate(\%$form); + do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding}); - $form->{title} = - "Lx-Office ERP " - . $locale->text('Database Administration') . " / " - . $locale->text('Create Dataset'); + $dbh->disconnect(); - $form->header(); - print $form->parse_html_template("admin/dbcreate"); -} + # Spawn pg_restore on the temporary file. -sub delete_dataset { - @dbsources = User->dbsources_unused(\%$form, $memberfile); - $form->error($locale->text('Nothing to delete!')) unless @dbsources; + my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname}); + push @args, ("-p", $form->{dbport}) if ($form->{dbport}); + push @args, $tmp; - $form->{title} = - "Lx-Office ERP " - . $locale->text('Database Administration') . " / " - . $locale->text('Delete Dataset'); - $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ]; + my $cmd = "$pg_restore_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args); - $form->header(); - print $form->parse_html_template("admin/delete_dataset"); -} + my $in = IO::File->new("$cmd 2>&1 |"); -sub dbdelete { + if (!$in) { + unlink "${tmpdir}/.pgpass", $tmp; + rmdir $tmpdir; - if (!$form->{db}) { - $form->error($locale->text('No Dataset selected!')); + $form->error($locale->text('The pg_restore process could not be started.')); } - User->dbdelete(\%$form); - - $form->{title} = - "Lx-Office ERP " - . $locale->text('Database Administration') . " / " - . $locale->text('Delete Dataset'); + $English::AUTOFLUSH = 1; $form->header(); - print $form->parse_html_template("admin/dbdelete"); -} + print $form->parse_html_template("admin/restore_dataset_start_header"); -sub unlock_system { - - unlink "$userspath/nologin"; - - $form->{callback} = - "$form->{script}?action=list_users&rpw=$root->{password}"; + while (my $line = <$in>) { + print $line; + } + $in->close(); - $form->redirect($locale->text('Lockfile removed!')); + $form->{retval} = $CHILD_ERROR >> 8; + print $form->parse_html_template("admin/restore_dataset_start_footer"); + unlink "${tmpdir}/.pgpass", $tmp; + rmdir $tmpdir; } -sub lock_system { - - open(FH, ">$userspath/nologin") - or $form->error($locale->text('Cannot create Lock!')); - close(FH); - - $form->{callback} = - "$form->{script}?action=list_users&rpw=$root->{password}"; - - $form->redirect($locale->text('Lockfile created!')); - -} +1;