1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  16 # This program is free software; you can redistribute it and/or modify
 
  17 # it under the terms of the GNU General Public License as published by
 
  18 # the Free Software Foundation; either version 2 of the License, or
 
  19 # (at your option) any later version.
 
  21 # This program is distributed in the hope that it will be useful,
 
  22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  24 # GNU General Public License for more details.
 
  25 # You should have received a copy of the GNU General Public License
 
  26 # along with this program; if not, write to the Free Software
 
  27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  28 #======================================================================
 
  31 # add/edit/delete users
 
  33 #======================================================================
 
  37 use English qw(-no_match_vars);
 
  41 use POSIX qw(strftime);
 
  53 require "bin/mozilla/common.pl";
 
  54 require "bin/mozilla/admin_groups.pl";
 
  64   $::lxdebug->enter_sub;
 
  65   my $session_result = shift;
 
  72   $::auth->set_session_value('rpw', $::form->{rpw}) if $session_result == SL::Auth->SESSION_OK;
 
  74   $form->{stylesheet} = "lx-office-erp.css";
 
  75   $form->{favicon}    = "favicon.ico";
 
  77   if ($form->{action}) {
 
  78     if ($auth->authenticate_root($form->{rpw}, 0) != $auth->OK()) {
 
  79       $form->{error_message} = $locale->text('Incorrect Password!');
 
  82       $auth->create_or_refresh_session() if ($auth->session_tables_present());
 
  83       call_sub($locale->findsub($form->{action}));
 
  85   } elsif ($auth->authenticate_root($form->{rpw}, 0) == $auth->OK()) {
 
  87     $auth->create_or_refresh_session() if ($auth->session_tables_present());
 
  91     # if there are no drivers bail out
 
  92     $form->error($locale->text('No Database Drivers available!'))
 
  93       unless (User->dbdrivers);
 
  97   $::lxdebug->leave_sub;
 
 101   my $form   = $main::form;
 
 102   my $locale = $main::locale;
 
 104   $form->{title} = qq|Lx-Office ERP $form->{version} | . $locale->text('Administration');
 
 107   print $form->parse_html_template('admin/adminlogin');
 
 111   check_auth_db_and_tables();
 
 116   $main::auth->destroy_session();
 
 120 sub check_auth_db_and_tables {
 
 121   my $form   = $main::form;
 
 122   my $locale = $main::locale;
 
 126   map { $params{"db_${_}"} = $main::auth->{DB_config}->{$_} } keys %{ $auth->{DB_config} };
 
 128   if (!$main::auth->check_database()) {
 
 129     $form->{title} = $locale->text('Authentification database creation');
 
 131     print $form->parse_html_template('admin/check_auth_database', \%params);
 
 136   if (!$main::auth->check_tables()) {
 
 137     $form->{title} = $locale->text('Authentification tables creation');
 
 139     print $form->parse_html_template('admin/check_auth_tables', \%params);
 
 144   if (-f $main::memberfile) {
 
 147     if ($main::memberfile =~ m|^.*/|) {
 
 151     my $backupdir = "${memberdir}member-file-migration";
 
 153     $form->{title} = $locale->text('User data migration');
 
 155     print $form->parse_html_template('admin/user_migration', { 'memberfile' => $main::memberfile,
 
 156                                                                'backupdir'  => $backupdir });
 
 163   my $form = $main::form;
 
 165   $main::auth->create_database('superuser'          => $form->{db_superuser},
 
 166                                'superuser_password' => $form->{db_superuser_password},
 
 167                                'template'           => $form->{db_template});
 
 171 sub create_auth_tables {
 
 172   my $form   = $main::form;
 
 173   my $locale = $main::locale;
 
 175   $main::auth->create_tables();
 
 176   $main::auth->set_session_value('rpw', $form->{rpw});
 
 177   $main::auth->create_or_refresh_session();
 
 179   if (!-f $main::memberfile) {
 
 180     # New installation -- create a standard group with full access
 
 183       'name'        => $locale->text('Full Access'),
 
 184       'description' => $locale->text('Full access to all functions'),
 
 185       'rights'      => { map { $_ => 1 } SL::Auth::all_rights() },
 
 186       'members'     => [ map { $_->{id} } values %members ],
 
 189     $main::auth->save_group($group);
 
 196   $main::lxdebug->enter_sub();
 
 198   my $form      = $main::form;
 
 199   my $locale    = $main::locale;
 
 203   if ($main::memberfile =~ m|^.*/|) {
 
 207   my $backupdir = "${memberdir}member-file-migration";
 
 209   if (! -d $backupdir && !mkdir $backupdir, 0700) {
 
 210     $form->error(sprintf($locale->text('The directory "%s" could not be created:\n%s'), $backupdir, $!));
 
 213   copy $main::memberfile, "users/member-file-migration/members";
 
 215   my $in = IO::File->new($main::memberfile, "r");
 
 217   $form->error($locale->text('Could not open the old memberfile.')) if (!$in);
 
 219   my (%members, $login);
 
 228       $login =~ s/(\[|\])//g;
 
 232       $members{$login} = { "login" => $login };
 
 236     if ($login && m/=/) {
 
 237       my ($key, $value) = split m/\s*=\s*/, $_, 2;
 
 242       $value =~ s|\\n|\n|g;
 
 244       $members{$login}->{$key} = $value;
 
 250   delete $members{"root login"};
 
 252   map { $_->{dbpasswd} = unpack 'u', $_->{dbpasswd} } values %members;
 
 254   while (my ($login, $params) = each %members) {
 
 255     $main::auth->save_user($login, %{ $params });
 
 256     $main::auth->change_password($login, $params->{password}, 1);
 
 258     my $conf_file = "${memberdir}${login}.conf";
 
 261       copy   $conf_file, "${backupdir}/${login}.conf";
 
 266   unlink $main::memberfile;
 
 268   my @member_list = sort { lc $a->{login} cmp lc $b->{login} } values %members;
 
 270   $form->{title} = $locale->text('User data migration');
 
 272   print $form->parse_html_template('admin/user_migration_done', { 'MEMBERS' => \@member_list });
 
 274   $main::lxdebug->leave_sub();
 
 277 sub create_standard_group_ask {
 
 278   my $form   = $main::form;
 
 279   my $locale = $main::locale;
 
 281   $form->{title} = $locale->text('Create a standard group');
 
 284   print $form->parse_html_template("admin/create_standard_group_ask");
 
 287 sub create_standard_group {
 
 288   my $form    = $main::form;
 
 289   my $locale  = $main::locale;
 
 291   my %members = $main::auth->read_all_users();
 
 293   my $groups  = $main::auth->read_groups();
 
 295   foreach my $group (values %{$groups}) {
 
 296     if (($form->{group_id} != $group->{id})
 
 297         && ($form->{name} eq $group->{name})) {
 
 298       $form->show_generic_error($locale->text("A group with that name does already exist."));
 
 303     'name'        => $locale->text('Full Access'),
 
 304     'description' => $locale->text('Full access to all functions'),
 
 305     'rights'      => { map { $_ => 1 } SL::Auth::all_rights() },
 
 306     'members'     => [ map { $_->{id} } values %members ],
 
 309   $main::auth->save_group($group);
 
 311   user_migration_complete(1);
 
 314 sub dont_create_standard_group {
 
 315   user_migration_complete(0);
 
 318 sub user_migration_complete {
 
 319   my $standard_group_created = shift;
 
 321   my $form                   = $main::form;
 
 322   my $locale                 = $main::locale;
 
 324   $form->{title} = $locale->text('User migration complete');
 
 327   print $form->parse_html_template('admin/user_migration_complete', { 'standard_group_created' => $standard_group_created });
 
 331   my $form    = $main::form;
 
 332   my $locale  = $main::locale;
 
 334   my %members = $main::auth->read_all_users();
 
 336   delete $members{"root login"};
 
 338   map { $_->{templates} =~ s|.*/||; } values %members;
 
 340   $form->{title}   = "Lx-Office ERP " . $locale->text('Administration');
 
 341   $form->{LOCKED}  = -e "$main::userspath/nologin";
 
 342   $form->{MEMBERS} = [ @members{sort { lc $a cmp lc $b } keys %members} ];
 
 345   print $form->parse_html_template("admin/list_users");
 
 349   my $form         = $main::form;
 
 350   my $locale       = $main::locale;
 
 352   $form->{title}   = "Lx-Office ERP " . $locale->text('Administration') . " / " . $locale->text('Add User');
 
 356     "countrycode"  => "de",
 
 357     "numberformat" => "1.000,00",
 
 358     "dateformat"   => "dd.mm.yy",
 
 359     "stylesheet"   => "lx-office-erp.css",
 
 363   edit_user_form($myconfig);
 
 367   my $form       = $main::form;
 
 368   my $locale     = $main::locale;
 
 370   $form->{title} = "Lx-Office ERP " . $locale->text('Administration') . " / " . $locale->text('Edit User');
 
 373   $form->isblank("login", $locale->text("The login is missing."));
 
 376   my $myconfig = new User($form->{login});
 
 378   # strip basedir from templates directory
 
 379   $myconfig->{templates} =~ s|.*/||;
 
 381   edit_user_form($myconfig);
 
 387   my $form       = $main::form;
 
 388   my $locale     = $main::locale;
 
 390   my @valid_dateformats = qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd);
 
 391   $form->{ALL_DATEFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{dateformat} } } @valid_dateformats ];
 
 393   my @valid_numberformats = ('1,000.00', '1000.00', '1.000,00', '1000,00');
 
 394   $form->{ALL_NUMBERFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{numberformat} } } @valid_numberformats ];
 
 396   my %countrycodes = User->country_codes;
 
 397   $form->{ALL_COUNTRYCODES} = [];
 
 398   foreach my $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
 
 399     push @{ $form->{ALL_COUNTRYCODES} }, { "value"    => $countrycode,
 
 400                                            "name"     => $countrycodes{$countrycode},
 
 401                                            "selected" => $countrycode eq $myconfig->{countrycode} };
 
 404   # is there a templates basedir
 
 405   if (!-d "$main::templates") {
 
 406     $form->error(sprintf($locale->text("The directory %s does not exist."), $main::templates));
 
 409   opendir TEMPLATEDIR, "$main::templates/." or $form->error("$main::templates : $ERRNO");
 
 410   my @all     = readdir(TEMPLATEDIR);
 
 411   my @alldir  = sort grep { -d "$main::templates/$_" && !/^\.\.?$/ } @all;
 
 412   my @allhtml = sort grep { -f "$main::templates/$_" && /\.html$/ } @all;
 
 413   closedir TEMPLATEDIR;
 
 415   @alldir = grep !/\.(html|tex|sty|odt|xml|txb)$/, @alldir;
 
 416   @alldir = grep !/^(webpages|\.svn)$/, @alldir;
 
 418   @allhtml = reverse grep !/Default/, @allhtml;
 
 419   push @allhtml, 'Default';
 
 420   @allhtml = reverse @allhtml;
 
 422   $form->{ALL_TEMPLATES} = [ map { { "name", => $_, "selected" => $_ eq $myconfig->{templates} } } @alldir ];
 
 424   my $lastitem = $allhtml[0];
 
 425   $lastitem =~ s/-.*//g;
 
 426   $form->{ALL_MASTER_TEMPLATES} = [ { "name" => $lastitem, "selected" => $lastitem eq "German" } ];
 
 427   foreach my $item (@allhtml) {
 
 429     next if ($item eq $lastitem);
 
 431     push @{ $form->{ALL_MASTER_TEMPLATES} }, { "name" => $item, "selected" => $item eq "German" };
 
 435   # css dir has styles that are not intended as general layouts.
 
 436   # reverting to hardcoded list
 
 437   $form->{ALL_STYLESHEETS} = [ map { { "name" => $_, "selected" => $_ eq $myconfig->{stylesheet} } } qw(lx-office-erp.css Win2000.css) ];
 
 439   $form->{"menustyle_" . $myconfig->{menustyle} } = 1;
 
 441   map { $form->{"myc_${_}"} = $myconfig->{$_} } keys %{ $myconfig };
 
 446     my $user_id    = $main::auth->get_user_id($form->{login});
 
 447     my $all_groups = $main::auth->read_groups();
 
 449     foreach my $group (values %{ $all_groups }) {
 
 450       push @{ $groups }, $group if (grep { $user_id == $_ } @{ $group->{members} });
 
 453     $groups = [ sort { lc $a->{name} cmp lc $b->{name} } @{ $groups } ];
 
 456   $form->{CAN_CHANGE_PASSWORD} = $main::auth->can_change_password();
 
 459   print $form->parse_html_template("admin/edit_user", { 'GROUPS' => $groups });
 
 463   my $form          = $main::form;
 
 464   my $locale        = $main::locale;
 
 466   $form->{dbdriver} = 'Pg';
 
 468   # no spaces allowed in login name
 
 469   $form->{login} =~ s|\s||g;
 
 470   $form->isblank("login", $locale->text('Login name missing!'));
 
 472   # check for duplicates
 
 473   if (!$form->{edit}) {
 
 474     my %members = $main::auth->read_all_users();
 
 475     if ($members{$form->{login}}) {
 
 476       $form->show_generic_error($locale->text('Another user with the login #1 does already exist.', $form->{login}), 'back_button' => 1);
 
 480   # no spaces allowed in directories
 
 481   ($form->{newtemplates}) = split / /, $form->{newtemplates};
 
 483   if ($form->{newtemplates}) {
 
 484     $form->{templates} = $form->{newtemplates};
 
 487       ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
 
 491   if (!-d "$main::templates") {
 
 492     $form->error(sprintf($locale->text("The directory %s does not exist."), $main::templates));
 
 495   # add base directory to $form->{templates}
 
 496   $form->{templates} =~ s|.*/||;
 
 497   $form->{templates} =  "$main::templates/$form->{templates}";
 
 499   my $myconfig = new User($form->{login});
 
 501   $form->isblank("dbname", $locale->text('Dataset missing!'));
 
 502   $form->isblank("dbuser", $locale->text('Database User missing!'));
 
 504   foreach my $item (keys %{$form}) {
 
 505     $myconfig->{$item} = $form->{$item};
 
 508   delete $myconfig->{stylesheet};
 
 509   if ($form->{userstylesheet}) {
 
 510     $myconfig->{stylesheet} = $form->{userstylesheet};
 
 513   $myconfig->save_member();
 
 515   if ($main::auth->can_change_password()
 
 516       && defined $form->{new_password}
 
 517       && ($form->{new_password} ne '********')) {
 
 518     $main::auth->change_password($form->{login}, $form->{new_password});
 
 521   $form->{templates}       =~ s|.*/||;
 
 522   $form->{templates}       =  "$main::templates/$form->{templates}";
 
 523   $form->{mastertemplates} =~ s|.*/||;
 
 525   # create user template directory and copy master files
 
 526   if (!-d "$form->{templates}") {
 
 529     if (mkdir "$form->{templates}", oct("771")) {
 
 533       # copy templates to the directory
 
 534       opendir TEMPLATEDIR, "$main::templates/." or $form->error("$main::templates : $ERRNO");
 
 535       my @templates = grep /$form->{mastertemplates}.*?\.(html|tex|sty|odt|xml|txb)$/,
 
 537       closedir TEMPLATEDIR;
 
 539       foreach my $file (@templates) {
 
 540         open(TEMP, "$main::templates/$file")
 
 541           or $form->error("$main::templates/$file : $ERRNO");
 
 543         $file =~ s/\Q$form->{mastertemplates}\E-//;
 
 544         open(NEW, ">$form->{templates}/$file")
 
 545           or $form->error("$form->{templates}/$file : $ERRNO");
 
 547         while (my $line = <TEMP>) {
 
 554       $form->error("$ERRNO: $form->{templates}");
 
 558   # Add new user to his groups.
 
 559   if (ref $form->{new_user_group_ids} eq 'ARRAY') {
 
 560     my $all_groups = $main::auth->read_groups();
 
 561     my %user       = $main::auth->read_user($form->{login});
 
 563     foreach my $group_id (@{ $form->{new_user_group_ids} }) {
 
 564       my $group = $all_groups->{$group_id};
 
 568       push @{ $group->{members} }, $user{id};
 
 569       $main::auth->save_group($group);
 
 573   $form->redirect($locale->text('User saved!'));
 
 577 sub save_user_as_new {
 
 578   my $form       = $main::form;
 
 580   $form->{login} = $form->{new_user_login};
 
 581   delete @{$form}{qw(edit new_user_login)};
 
 587   my $form      = $main::form;
 
 588   my $locale    = $main::locale;
 
 590   my %members   = $main::auth->read_all_users();
 
 591   my $templates = $members{$form->{login}}->{templates};
 
 593   $main::auth->delete_user($form->{login});
 
 596     my $templates_in_use = 0;
 
 598     foreach my $login (keys %members) {
 
 599       next if $form->{login} eq $login;
 
 600       next if $members{$login}->{templates} ne $templates;
 
 601       $templates_in_use = 1;
 
 605     if (!$templates_in_use && -d $templates) {
 
 606       unlink <$templates/*>;
 
 611   $form->redirect($locale->text('User deleted!'));
 
 619   return ($login) ? $login : undef;
 
 625   my ($null, $value) = split(/=/, $line, 2);
 
 628   $value =~ s/\s#.*//g;
 
 630   # remove any trailing whitespace
 
 631   $value =~ s/^\s*(.*?)\s*$/$1/;
 
 636 sub pg_database_administration {
 
 637   my $form = $main::form;
 
 639   $form->{dbdriver} = 'Pg';
 
 644 sub dbselect_source {
 
 645   my $form           = $main::form;
 
 646   my $locale         = $main::locale;
 
 648   $form->{dbport}    = '5432';
 
 649   $form->{dbuser}    = 'postgres';
 
 650   $form->{dbdefault} = 'template1';
 
 651   $form->{dbhost}    = 'localhost';
 
 653   $form->{title}     = "Lx-Office ERP / " . $locale->text('Database Administration');
 
 655   # Intentionnaly disabled unless fixed to work with the authentication DB.
 
 656   $form->{ALLOW_DBBACKUP} = 0; # "$pg_dump_exe" ne "DISABLED";
 
 659   print $form->parse_html_template("admin/dbadmin");
 
 662 sub test_db_connection {
 
 663   my $form   = $main::form;
 
 664   my $locale = $main::locale;
 
 666   $form->{dbdriver} = 'Pg';
 
 667   User::dbconnect_vars($form, $form->{dbname});
 
 669   my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd});
 
 671   $form->{connection_ok} = $dbh ? 1 : 0;
 
 672   $form->{errstr}        = $DBI::errstr;
 
 674   $dbh->disconnect() if ($dbh);
 
 676   $form->{title} = $locale->text('Database Connection Test');
 
 678   print $form->parse_html_template("admin/test_db_connection");
 
 682   call_sub($main::form->{"nextsub"});
 
 686   my $form              = $main::form;
 
 687   my $locale            = $main::locale;
 
 689   $form->{title}        = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Update Dataset');
 
 691   my @need_updates      = User->dbneedsupdate($form);
 
 692   $form->{NEED_UPDATES} = \@need_updates;
 
 693   $form->{ALL_UPDATED}  = !scalar @need_updates;
 
 696   print $form->parse_html_template("admin/update_dataset");
 
 700   my $form            = $main::form;
 
 701   my $locale          = $main::locale;
 
 703   $form->{stylesheet} = "lx-office-erp.css";
 
 704   $form->{title}      = $locale->text("Dataset upgrade");
 
 707   my $rowcount           = $form->{rowcount} * 1;
 
 708   my @update_rows        = grep { $form->{"update_$_"} } (1 .. $rowcount);
 
 709   $form->{NOTHING_TO_DO} = !scalar @update_rows;
 
 710   my $saved_form         = save_form();
 
 714   print $form->parse_html_template("admin/dbupgrade_all_header");
 
 716   foreach my $i (@update_rows) {
 
 717     restore_form($saved_form);
 
 719     map { $form->{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd);
 
 721     my $controls = parse_dbupdate_controls($form, $form->{dbdriver});
 
 723     print $form->parse_html_template("admin/dbupgrade_header");
 
 725     $form->{dbupdate}        = $form->{dbname};
 
 726     $form->{$form->{dbname}} = 1;
 
 728     User->dbupdate($form);
 
 729     User->dbupdate2($form, $controls);
 
 731     print $form->parse_html_template("admin/dbupgrade_footer");
 
 734   print $form->parse_html_template("admin/dbupgrade_all_done");
 
 738   my $form           = $main::form;
 
 739   my $locale         = $main::locale;
 
 741   $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources($form);
 
 743   $form->{CHARTS}    = [];
 
 745   opendir SQLDIR, "sql/." or $form->error($ERRNO);
 
 746   foreach my $item (sort grep /-chart\.sql\z/, readdir SQLDIR) {
 
 747     next if ($item eq 'Default-chart.sql');
 
 748     $item =~ s/-chart\.sql//;
 
 749     push @{ $form->{CHARTS} }, { "name"     => $item,
 
 750                                  "selected" => $item eq "Germany-DATEV-SKR03EU" };
 
 754   my $default_charset = $main::dbcharset;
 
 755   $default_charset ||= Common::DEFAULT_CHARSET;
 
 757   my $cluster_encoding = User->dbclusterencoding($form);
 
 758   if ($cluster_encoding && ($cluster_encoding =~ m/^(?:UTF-?8|UNICODE)$/i)) {
 
 759     if ($main::dbcharset !~ m/^UTF-?8$/i) {
 
 760       $form->show_generic_error($locale->text('The selected  PostgreSQL installation uses UTF-8 as its encoding. ' .
 
 761                                               'Therefore you have to configure Lx-Office to use UTF-8 as well.'),
 
 765     $form->{FORCE_DBENCODING} = 'UNICODE';
 
 768     $form->{DBENCODINGS} = [];
 
 770     foreach my $encoding (@Common::db_encodings) {
 
 771       push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
 
 772                                         "label"      => $encoding->{label},
 
 773                                         "selected"   => $encoding->{charset} eq $default_charset };
 
 777   $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
 
 780   print $form->parse_html_template("admin/create_dataset");
 
 784   my $form   = $main::form;
 
 785   my $locale = $main::locale;
 
 787   $form->isblank("db", $locale->text('Dataset missing!'));
 
 789   User->dbcreate(\%$form);
 
 791   $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
 
 794   print $form->parse_html_template("admin/dbcreate");
 
 798   my $form      = $main::form;
 
 799   my $locale    = $main::locale;
 
 801   my @dbsources = User->dbsources_unused($form);
 
 802   $form->error($locale->text('Nothing to delete!')) unless @dbsources;
 
 804   $form->{title}     = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
 
 805   $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
 
 808   print $form->parse_html_template("admin/delete_dataset");
 
 812   my $form   = $main::form;
 
 813   my $locale = $main::locale;
 
 816     $form->error($locale->text('No Dataset selected!'));
 
 819   User->dbdelete(\%$form);
 
 821   $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
 
 823   print $form->parse_html_template("admin/dbdelete");
 
 827   my $form       = $main::form;
 
 828   my $locale     = $main::locale;
 
 830   $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
 
 832   if ("$main::pg_dump_exe" eq "DISABLED") {
 
 833     $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
 
 836   my @dbsources         = sort User->dbsources($form);
 
 837   $form->{DATABASES}    = [ map { { "dbname" => $_ } } @dbsources ];
 
 838   $form->{NO_DATABASES} = !scalar @dbsources;
 
 840   my $username  = getpwuid $UID || "unknown-user";
 
 841   my $hostname  = hostname() || "unknown-host";
 
 842   $form->{from} = "Lx-Office Admin <${username}\@${hostname}>";
 
 845   print $form->parse_html_template("admin/backup_dataset");
 
 848 sub backup_dataset_start {
 
 849   my $form       = $main::form;
 
 850   my $locale     = $main::locale;
 
 852   $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
 
 854   $main::pg_dump_exe ||= "pg_dump";
 
 856   if ("$main::pg_dump_exe" eq "DISABLED") {
 
 857     $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
 
 860   $form->isblank("dbname", $locale->text('The dataset name is missing.'));
 
 861   $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
 
 863   my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
 
 864   mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
 
 866   my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
 
 870     $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
 
 873   print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
 
 876   $ENV{HOME} = $tmpdir;
 
 878   my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
 
 879   push @args, ("-p", $form->{dbport}) if ($form->{dbport});
 
 880   push @args, $form->{dbname};
 
 882   my $cmd  = "$main::pg_dump_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
 
 883   my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar";
 
 885   if ($form->{destination} ne "email") {
 
 886     my $in = IO::File->new("$cmd |");
 
 889       unlink "${tmpdir}/.pgpass";
 
 892       $form->error($locale->text('The pg_dump process could not be started.'));
 
 895     print "content-type: application/x-tar\n";
 
 896     print "content-disposition: attachment; filename=\"${name}\"\n\n";
 
 898     while (my $line = <$in>) {
 
 904     unlink "${tmpdir}/.pgpass";
 
 908     my $tmp = $tmpdir . "/dump_" . Common::unique_id();
 
 910     if (system("$cmd > $tmp") != 0) {
 
 911       unlink "${tmpdir}/.pgpass", $tmp;
 
 914       $form->error($locale->text('The pg_dump process could not be started.'));
 
 917     my $mail = new Mailer;
 
 919     map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
 
 921     $mail->{charset}     = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
 
 922     $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
 
 925     unlink "${tmpdir}/.pgpass", $tmp;
 
 928     $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
 
 931     print $form->parse_html_template("admin/backup_dataset_email_done");
 
 935 sub restore_dataset {
 
 936   my $form       = $main::form;
 
 937   my $locale     = $main::locale;
 
 939   $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset');
 
 941   if ("$main::pg_restore_exe" eq "DISABLED") {
 
 942     $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
 
 945   my $default_charset   = $main::dbcharset;
 
 946   $default_charset    ||= Common::DEFAULT_CHARSET;
 
 948   $form->{DBENCODINGS}  = [];
 
 950   foreach my $encoding (@Common::db_encodings) {
 
 951     push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
 
 952                                       "label"      => $encoding->{label},
 
 953                                       "selected"   => $encoding->{charset} eq $default_charset };
 
 957   print $form->parse_html_template("admin/restore_dataset");
 
 960 sub restore_dataset_start {
 
 961   my $form       = $main::form;
 
 962   my $locale     = $main::locale;
 
 964   $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset');
 
 966   $main::pg_restore_exe ||= "pg_restore";
 
 968   if ("$main::pg_restore_exe" eq "DISABLED") {
 
 969     $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
 
 972   $form->isblank("new_dbname", $locale->text('The dataset name is missing.'));
 
 973   $form->isblank("content", $locale->text('No backup file has been uploaded.'));
 
 975   # Create temporary directories. Write the backup file contents to a temporary
 
 976   # file. Create a .pgpass file with the username and password for the pg_restore
 
 979   my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
 
 980   mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
 
 982   my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
 
 986     $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
 
 989   print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
 
 992   $ENV{HOME} = $tmpdir;
 
 994   my $tmp = $tmpdir . "/dump_" . Common::unique_id();
 
 997   if (substr($form->{content}, 0, 2) eq "\037\213") {
 
 998     $tmpfile = IO::File->new("| gzip -d > $tmp");
 
1002     $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600);
 
1006     unlink "${tmpdir}/.pgpass";
 
1009     $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
 
1012   print $tmpfile $form->{content};
 
1015   delete $form->{content};
 
1017   # Try to connect to the database. Find out if a database with the same name exists.
 
1018   # If yes, then drop the existing database. Create a new one with the name and encoding
 
1019   # given by the user.
 
1021   User::dbconnect_vars($form, "template1");
 
1023   my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form };
 
1024   my $dbh      = $form->dbconnect(\%myconfig) || $form->dberror();
 
1028   $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g;
 
1030   $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|;
 
1031   my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname});
 
1033     do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|);
 
1037   foreach my $item (@Common::db_encodings) {
 
1038     if ($item->{dbencoding} eq $form->{dbencoding}) {
 
1043   $form->{dbencoding} = "LATIN9" unless $form->{dbencoding};
 
1045   do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding});
 
1049   # Spawn pg_restore on the temporary file.
 
1051   my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname});
 
1052   push @args, ("-p", $form->{dbport}) if ($form->{dbport});
 
1055   my $cmd = "$main::pg_restore_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
 
1057   my $in = IO::File->new("$cmd 2>&1 |");
 
1060     unlink "${tmpdir}/.pgpass", $tmp;
 
1063     $form->error($locale->text('The pg_restore process could not be started.'));
 
1066   $English::AUTOFLUSH = 1;
 
1069   print $form->parse_html_template("admin/restore_dataset_start_header");
 
1071   while (my $line = <$in>) {
 
1076   $form->{retval} = $CHILD_ERROR >> 8;
 
1077   print $form->parse_html_template("admin/restore_dataset_start_footer");
 
1079   unlink "${tmpdir}/.pgpass", $tmp;
 
1084   my $form   = $main::form;
 
1085   my $locale = $main::locale;
 
1087   unlink "$main::userspath/nologin";
 
1089   $form->{callback} = "admin.pl?action=list_users";
 
1091   $form->redirect($locale->text('Lockfile removed!'));
 
1096   my $form   = $main::form;
 
1097   my $locale = $main::locale;
 
1099   open(FH, ">$main::userspath/nologin")
 
1100     or $form->error($locale->text('Cannot create Lock!'));
 
1103   $form->{callback} = "admin.pl?action=list_users";
 
1105   $form->redirect($locale->text('Lockfile created!'));
 
1110   call_sub($main::form->{yes_nextsub});
 
1114   call_sub($main::form->{no_nextsub});
 
1118   call_sub($main::form->{add_nextsub});
 
1122   my $form = $main::form;
 
1124   $form->{edit_nextsub} ||= 'edit_user';
 
1126   call_sub($form->{edit_nextsub});
 
1130   my $form     = $main::form;
 
1132   $form->{delete_nextsub} ||= 'delete_user';
 
1134   call_sub($form->{delete_nextsub});
 
1138   my $form = $main::form;
 
1140   $form->{save_nextsub} ||= 'save_user';
 
1142   call_sub($form->{save_nextsub});
 
1146   call_sub($main::form->{back_nextsub});
 
1150   my $form   = $main::form;
 
1151   my $locale = $main::locale;
 
1153   foreach my $action (qw(create_standard_group dont_create_standard_group
 
1154                          save_user delete_user save_user_as_new)) {
 
1155     if ($form->{"action_${action}"}) {
 
1161   call_sub($form->{default_action}) if ($form->{default_action});
 
1163   $form->error($locale->text('No action defined.'));