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 #======================================================================
 
  35 $menufile = "menu.ini";
 
  39 use English qw(-no_match_vars);
 
  43 use POSIX qw(strftime);
 
  55 require "bin/mozilla/common.pl";
 
  56 require "bin/mozilla/admin_groups.pl";
 
  58 our $cgi = new CGI('');
 
  62 $locale = new Locale $language, "admin";
 
  64 our $auth = SL::Auth->new();
 
  65 if ($auth->session_tables_present()) {
 
  66   $auth->expire_sessions();
 
  67   $auth->restore_session();
 
  68   $auth->set_session_value('rpw', $form->{rpw});
 
  72 if (-f "bin/mozilla/custom_$form->{script}") {
 
  73   eval { require "bin/mozilla/custom_$form->{script}"; };
 
  74   $form->error($@) if ($@);
 
  77 $form->{stylesheet} = "lx-office-erp.css";
 
  78 $form->{favicon}    = "favicon.ico";
 
  80 if ($form->{action}) {
 
  81   if ($auth->authenticate_root($form->{rpw}, 0) != Auth::OK) {
 
  82     $form->{error_message} = $locale->text('Incorrect Password!');
 
  87   $auth->create_or_refresh_session() if ($auth->session_tables_present());
 
  89   call_sub($locale->findsub($form->{action}));
 
  91 } elsif ($auth->authenticate_root($form->{rpw}, 0) == Auth::OK) {
 
  93   $auth->create_or_refresh_session() if ($auth->session_tables_present());
 
  98   # if there are no drivers bail out
 
  99   $form->error($locale->text('No Database Drivers available!'))
 
 100     unless (User->dbdrivers);
 
 112   $form->{title} = qq|Lx-Office ERP $form->{version} | . $locale->text('Administration');
 
 115   print $form->parse_html_template('admin/adminlogin');
 
 119   check_auth_db_and_tables();
 
 124   $auth->destroy_session();
 
 128 sub check_auth_db_and_tables {
 
 131   map { $params{"db_${_}"} = $auth->{DB_config}->{$_} } keys %{ $auth->{DB_config} };
 
 133   if (!$auth->check_database()) {
 
 134     $form->{title} = $locale->text('Authentification database creation');
 
 136     print $form->parse_html_template('admin/check_auth_database', \%params);
 
 141   if (!$auth->check_tables()) {
 
 142     $form->{title} = $locale->text('Authentification tables creation');
 
 144     print $form->parse_html_template('admin/check_auth_tables', \%params);
 
 149   if (-f $memberfile) {
 
 152     if ($memberfile =~ m|^.*/|) {
 
 156     my $backupdir = "${memberdir}member-file-migration";
 
 158     $form->{title} = $locale->text('User data migration');
 
 160     print $form->parse_html_template('admin/user_migration', { 'memberfile' => $memberfile,
 
 161                                                                'backupdir'  => $backupdir });
 
 168   $auth->create_database('superuser'          => $form->{db_superuser},
 
 169                          'superuser_password' => $form->{db_superuser_password},
 
 170                          'template'           => $form->{db_template});
 
 174 sub create_auth_tables {
 
 175   $auth->create_tables();
 
 176   $auth->set_session_value('rpw', $form->{rpw});
 
 177   $auth->create_or_refresh_session();
 
 183   $lxdebug->enter_sub();
 
 187   if ($memberfile =~ m|^.*/|) {
 
 191   my $backupdir = "${memberdir}member-file-migration";
 
 193   if (! -d $backupdir && !mkdir $backupdir, 0700) {
 
 194     $form->error(sprintf($locale->text('The directory "%s" could not be created:\n%s'), $backupdir, $!));
 
 197   copy $memberfile, "users/member-file-migration/members";
 
 199   my $in = IO::File->new($memberfile, "r");
 
 201   $form->error($locale->text('Could not open the old memberfile.')) if (!$in);
 
 203   my (%members, $login);
 
 212       $login =~ s/(\[|\])//g;
 
 216       $members{$login} = { "login" => $login };
 
 220     if ($login && m/=/) {
 
 221       my ($key, $value) = split m/\s*=\s*/, $_, 2;
 
 226       $value =~ s|\\n|\n|g;
 
 228       $members{$login}->{$key} = $value;
 
 234   delete $members{"root login"};
 
 236   map { $_->{dbpasswd} = unpack 'u', $_->{dbpasswd} } values %members;
 
 238   while (my ($login, $params) = each %members) {
 
 239     $auth->save_user($login, %{ $params });
 
 240     $auth->change_password($login, $params->{password}, 1);
 
 242     my $conf_file = "${memberdir}${login}.conf";
 
 245       copy   $conf_file, "${backupdir}/${login}.conf";
 
 252   my @member_list = sort { lc $a->{login} cmp lc $b->{login} } values %members;
 
 254   $form->{title} = $locale->text('User data migration');
 
 256   print $form->parse_html_template('admin/user_migration_done', { 'MEMBERS' => \@member_list });
 
 258   $lxdebug->leave_sub();
 
 261 sub create_standard_group_ask {
 
 262   $form->{title} = $locale->text('Create a standard group');
 
 265   print $form->parse_html_template("admin/create_standard_group_ask");
 
 268 sub create_standard_group {
 
 269   my %members = $auth->read_all_users();
 
 271   my $groups = $auth->read_groups();
 
 273   foreach my $group (values %{$groups}) {
 
 274     if (($form->{group_id} != $group->{id})
 
 275         && ($form->{name} eq $group->{name})) {
 
 276       $form->show_generic_error($locale->text("A group with that name does already exist."));
 
 281     'name'        => $locale->text('Full Access'),
 
 282     'description' => $locale->text('Full access to all functions'),
 
 283     'rights'      => { map { $_ => 1 } SL::Auth::all_rights() },
 
 284     'members'     => [ map { $_->{id} } values %members ],
 
 287   $auth->save_group($group);
 
 289   user_migration_complete(1);
 
 292 sub dont_create_standard_group {
 
 293   user_migration_complete(0);
 
 296 sub user_migration_complete {
 
 297   my $standard_group_created = shift;
 
 299   $form->{title} = $locale->text('User migration complete');
 
 302   print $form->parse_html_template('admin/user_migration_complete', { 'standard_group_created' => $standard_group_created });
 
 306   my %members = $auth->read_all_users();
 
 308   delete $members{"root login"};
 
 310   map { $_->{templates} =~ s|.*/||; } values %members;
 
 312   $form->{title}   = "Lx-Office ERP " . $locale->text('Administration');
 
 313   $form->{LOCKED}  = -e "$userspath/nologin";
 
 314   $form->{MEMBERS} = [ @members{sort { lc $a cmp lc $b } keys %members} ];
 
 317   print $form->parse_html_template("admin/list_users");
 
 324     . $locale->text('Administration') . " / "
 
 325     . $locale->text('Add User');
 
 329     "countrycode"  => "de",
 
 330     "numberformat" => "1.000,00",
 
 331     "dateformat"   => "dd.mm.yy",
 
 332     "stylesheet"   => "lx-office-erp.css",
 
 336   edit_user_form($myconfig);
 
 343     . $locale->text('Administration') . " / "
 
 344     . $locale->text('Edit User');
 
 347   $form->isblank("login", $locale->text("The login is missing."));
 
 350   my $myconfig = new User($form->{login});
 
 352   # strip basedir from templates directory
 
 353   $myconfig->{templates} =~ s|.*/||;
 
 355   edit_user_form($myconfig);
 
 361   my @valid_dateformats = qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd);
 
 362   $form->{ALL_DATEFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{dateformat} } } @valid_dateformats ];
 
 364   my @valid_numberformats = qw(1,000.00 1000.00 1.000,00 1000,00);
 
 365   $form->{ALL_NUMBERFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{numberformat} } } @valid_numberformats ];
 
 367   %countrycodes = User->country_codes;
 
 368   $form->{ALL_COUNTRYCODES} = [];
 
 369   foreach $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
 
 370     push @{ $form->{ALL_COUNTRYCODES} }, { "value"    => $countrycode,
 
 371                                            "name"     => $countrycodes{$countrycode},
 
 372                                            "selected" => $countrycode eq $myconfig->{countrycode} };
 
 375   # is there a templates basedir
 
 376   if (!-d "$templates") {
 
 377     $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
 
 380   opendir TEMPLATEDIR, "$templates/." or $form->error("$templates : $ERRNO");
 
 381   my @all     = readdir(TEMPLATEDIR);
 
 382   my @alldir  = sort grep { -d "$templates/$_" && !/^\.\.?$/ } @all;
 
 383   my @allhtml = sort grep { -f "$templates/$_" && /\.html$/ } @all;
 
 384   closedir TEMPLATEDIR;
 
 386   @alldir = grep !/\.(html|tex|sty|odt|xml|txb)$/, @alldir;
 
 387   @alldir = grep !/^(webpages|\.svn)$/, @alldir;
 
 389   @allhtml = reverse grep !/Default/, @allhtml;
 
 390   push @allhtml, 'Default';
 
 391   @allhtml = reverse @allhtml;
 
 393   $form->{ALL_TEMPLATES} = [ map { { "name", => $_, "selected" => $_ eq $myconfig->{templates} } } @alldir ];
 
 395   $lastitem = $allhtml[0];
 
 396   $lastitem =~ s/-.*//g;
 
 397   $form->{ALL_MASTER_TEMPLATES} = [ { "name" => $lastitem, "selected" => $lastitem eq "German" } ];
 
 398   foreach $item (@allhtml) {
 
 400     next if ($item eq $lastitem);
 
 402     push @{ $form->{ALL_MASTER_TEMPLATES} }, { "name" => $item, "selected" => $item eq "German" };
 
 406   # css dir has styles that are not intended as general layouts.
 
 407   # reverting to hardcoded list
 
 408   $form->{ALL_STYLESHEETS} = [ map { { "name" => $_, "selected" => $_ eq $myconfig->{stylesheet} } } qw(lx-office-erp.css Win2000.css) ];
 
 410   $form->{"menustyle_" . $myconfig->{menustyle} } = 1;
 
 412   map { $form->{"myc_${_}"} = $myconfig->{$_} } keys %{ $myconfig };
 
 417     my $user_id    = $auth->get_user_id($form->{login});
 
 418     my $all_groups = $auth->read_groups();
 
 420     foreach my $group (values %{ $all_groups }) {
 
 421       push @{ $groups }, $group if (grep { $user_id == $_ } @{ $group->{members} });
 
 424     $groups = [ sort { lc $a->{name} cmp lc $b->{name} } @{ $groups } ];
 
 427   $form->{CAN_CHANGE_PASSWORD} = $auth->can_change_password();
 
 430   print $form->parse_html_template("admin/edit_user", { 'GROUPS' => $groups });
 
 434   $form->{dbdriver} = 'Pg';
 
 436   # no spaces allowed in login name
 
 437   $form->{login} =~ s|\s||g;
 
 438   $form->isblank("login", $locale->text('Login name missing!'));
 
 440   # check for duplicates
 
 441   if (!$form->{edit}) {
 
 442     my %members = $auth->read_all_users();
 
 444     if ($members{$form->{login}}) {
 
 445       $form->error("$form->{login} " . $locale->text('is already a member!'));
 
 449   # no spaces allowed in directories
 
 450   ($form->{newtemplates}) = split / /, $form->{newtemplates};
 
 452   if ($form->{newtemplates}) {
 
 453     $form->{templates} = $form->{newtemplates};
 
 456       ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
 
 460   if (!-d "$templates") {
 
 461     $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
 
 464   # add base directory to $form->{templates}
 
 465   $form->{templates} =~ s|.*/||;
 
 466   $form->{templates} =  "$templates/$form->{templates}";
 
 468   $myconfig = new User($form->{login});
 
 470   $form->isblank("dbname", $locale->text('Dataset missing!'));
 
 471   $form->isblank("dbuser", $locale->text('Database User missing!'));
 
 473   foreach $item (keys %{$form}) {
 
 474     $myconfig->{$item} = $form->{$item};
 
 477   delete $myconfig->{stylesheet};
 
 478   if ($form->{userstylesheet}) {
 
 479     $myconfig->{stylesheet} = $form->{userstylesheet};
 
 482   $myconfig->save_member();
 
 484   if ($auth->can_change_password()
 
 485       && defined $form->{new_password}
 
 486       && ($form->{new_password} ne '********')) {
 
 487     $auth->change_password($form->{login}, $form->{new_password});
 
 492       qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
 
 493     foreach $directory (@webdavdirs) {
 
 494       $file = "webdav/" . $directory . "/webdav-user";
 
 495       if ($form->{$directory}) {
 
 496         if (open(HTACCESS, "$file")) {
 
 498             ($login, $password) = split(/:/, $_);
 
 499             if ($login ne $form->{login}) {
 
 505         open(HTACCESS, "> $file") or die "cannot open $file $ERRNO\n";
 
 506         $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
 
 507         print(HTACCESS $newfile);
 
 510         $form->{$directory} = 0;
 
 511         if (open(HTACCESS, "$file")) {
 
 513             ($login, $password) = split(/:/, $_);
 
 514             if ($login ne $form->{login}) {
 
 520         open(HTACCESS, "> $file") or die "cannot open $file $ERRNO\n";
 
 521         print(HTACCESS $newfile);
 
 527   $form->{templates}       =~ s|.*/||;
 
 528   $form->{templates}       =  "${templates}/$form->{templates}";
 
 529   $form->{mastertemplates} =~ s|.*/||;
 
 531   # create user template directory and copy master files
 
 532   if (!-d "$form->{templates}") {
 
 535     if (mkdir "$form->{templates}", oct("771")) {
 
 539       # copy templates to the directory
 
 540       opendir TEMPLATEDIR, "$templates/." or $form - error("$templates : $ERRNO");
 
 541       @templates = grep /$form->{mastertemplates}.*?\.(html|tex|sty|xml|txb)$/,
 
 543       closedir TEMPLATEDIR;
 
 545       foreach $file (@templates) {
 
 546         open(TEMP, "$templates/$file")
 
 547           or $form->error("$templates/$file : $ERRNO");
 
 549         $file =~ s/\Q$form->{mastertemplates}\E-//;
 
 550         open(NEW, ">$form->{templates}/$file")
 
 551           or $form->error("$form->{templates}/$file : $ERRNO");
 
 553         while ($line = <TEMP>) {
 
 560       $form->error("$ERRNO: $form->{templates}");
 
 564   $form->redirect($locale->text('User saved!'));
 
 569   my %members   = $auth->read_all_users();
 
 570   my $templates = $members{$form->{login}}->{templates};
 
 572   $auth->delete_user($form->{login});
 
 575     my $templates_in_use = 0;
 
 577     foreach $login (keys %members) {
 
 578       next if $form->{login} eq $login;
 
 579       next if $members{$login}->{templates} ne $templates;
 
 580       $templates_in_use = 1;
 
 584     if (!$templates_in_use && -d $templates) {
 
 585       unlink <$templates/*>;
 
 590   $form->redirect($locale->text('User deleted!'));
 
 598   return ($login) ? $login : undef;
 
 605   my ($null, $value) = split(/=/, $line, 2);
 
 608   $value =~ s/\s#.*//g;
 
 610   # remove any trailing whitespace
 
 611   $value =~ s/^\s*(.*?)\s*$/$1/;
 
 616 sub pg_database_administration {
 
 618   $form->{dbdriver} = 'Pg';
 
 623 sub dbselect_source {
 
 624   $form->{dbport}    = '5432';
 
 625   $form->{dbuser}    = 'postgres';
 
 626   $form->{dbdefault} = 'template1';
 
 627   $form->{dbhost}    = 'localhost';
 
 629   $form->{title}     = "Lx-Office ERP / " . $locale->text('Database Administration');
 
 631   # Intentionnaly disabled unless fixed to work with the authentication DB.
 
 632   $form->{ALLOW_DBBACKUP} = 0; # "$pg_dump_exe" ne "DISABLED";
 
 635   print $form->parse_html_template("admin/dbadmin");
 
 638 sub test_db_connection {
 
 639   $form->{dbdriver} = 'Pg';
 
 640   User::dbconnect_vars($form, $form->{dbname});
 
 642   my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd});
 
 644   $form->{connection_ok} = $dbh ? 1 : 0;
 
 645   $form->{errstr}        = $DBI::errstr;
 
 647   $dbh->disconnect() if ($dbh);
 
 649   $form->{title} = $locale->text('Database Connection Test');
 
 651   print $form->parse_html_template("admin/test_db_connection");
 
 655   call_sub($form->{"nextsub"});
 
 659   call_sub($form->{"back_nextsub"});
 
 665     . $locale->text('Database Administration') . " / "
 
 666     . $locale->text('Update Dataset');
 
 668   my @need_updates      = User->dbneedsupdate($form);
 
 669   $form->{NEED_UPDATES} = \@need_updates;
 
 670   $form->{ALL_UPDATED}  = !scalar @need_updates;
 
 673   print $form->parse_html_template("admin/update_dataset");
 
 677   $form->{stylesheet} = "lx-office-erp.css";
 
 678   $form->{title}      = $locale->text("Dataset upgrade");
 
 681   my $rowcount           = $form->{rowcount} * 1;
 
 682   my @update_rows        = grep { $form->{"update_$_"} } (1 .. $rowcount);
 
 683   $form->{NOTHING_TO_DO} = !scalar @update_rows;
 
 684   my $saved_form         = save_form();
 
 688   print $form->parse_html_template("admin/dbupgrade_all_header");
 
 690   foreach my $i (@update_rows) {
 
 691     restore_form($saved_form);
 
 693     map { $form->{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd);
 
 695     my $controls = parse_dbupdate_controls($form, $form->{dbdriver});
 
 697     print $form->parse_html_template("admin/dbupgrade_header");
 
 699     $form->{dbupdate}        = $form->{dbname};
 
 700     $form->{$form->{dbname}} = 1;
 
 702     User->dbupdate($form);
 
 703     User->dbupdate2($form, $controls);
 
 705     print $form->parse_html_template("admin/dbupgrade_footer");
 
 708   print $form->parse_html_template("admin/dbupgrade_all_done");
 
 712   $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources(\%$form);
 
 714   $form->{CHARTS} = [];
 
 716   opendir SQLDIR, "sql/." or $form - error($ERRNO);
 
 717   foreach $item (sort grep /-chart\.sql\z/, readdir SQLDIR) {
 
 718     next if ($item eq 'Default-chart.sql');
 
 719     $item =~ s/-chart\.sql//;
 
 720     push @{ $form->{CHARTS} }, { "name"     => $item,
 
 721                                  "selected" => $item eq "Germany-DATEV-SKR03EU" };
 
 725   my $default_charset = $dbcharset;
 
 726   $default_charset ||= Common::DEFAULT_CHARSET;
 
 728   $form->{DBENCODINGS} = [];
 
 730   foreach my $encoding (@Common::db_encodings) {
 
 731     push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
 
 732                                       "label"      => $encoding->{label},
 
 733                                       "selected"   => $encoding->{charset} eq $default_charset };
 
 738     . $locale->text('Database Administration') . " / "
 
 739     . $locale->text('Create Dataset');
 
 742   print $form->parse_html_template("admin/create_dataset");
 
 746   $form->isblank("db", $locale->text('Dataset missing!'));
 
 748   User->dbcreate(\%$form);
 
 752     . $locale->text('Database Administration') . " / "
 
 753     . $locale->text('Create Dataset');
 
 756   print $form->parse_html_template("admin/dbcreate");
 
 760   @dbsources = User->dbsources_unused($form);
 
 761   $form->error($locale->text('Nothing to delete!')) unless @dbsources;
 
 765     . $locale->text('Database Administration') . " / "
 
 766     . $locale->text('Delete Dataset');
 
 767   $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
 
 770   print $form->parse_html_template("admin/delete_dataset");
 
 776     $form->error($locale->text('No Dataset selected!'));
 
 779   User->dbdelete(\%$form);
 
 783     . $locale->text('Database Administration') . " / "
 
 784     . $locale->text('Delete Dataset');
 
 786   print $form->parse_html_template("admin/dbdelete");
 
 792     . $locale->text('Database Administration') . " / "
 
 793     . $locale->text('Backup Dataset');
 
 795   if ("$pg_dump_exe" eq "DISABLED") {
 
 796     $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
 
 799   my @dbsources         = sort User->dbsources($form);
 
 800   $form->{DATABASES}    = [ map { { "dbname" => $_ } } @dbsources ];
 
 801   $form->{NO_DATABASES} = !scalar @dbsources;
 
 803   my $username  = getpwuid $UID || "unknown-user";
 
 804   my $hostname  = hostname() || "unknown-host";
 
 805   $form->{from} = "Lx-Office Admin <${username}\@${hostname}>";
 
 808   print $form->parse_html_template("admin/backup_dataset");
 
 811 sub backup_dataset_start {
 
 814     . $locale->text('Database Administration') . " / "
 
 815     . $locale->text('Backup Dataset');
 
 817   $pg_dump_exe ||= "pg_dump";
 
 819   if ("$pg_dump_exe" eq "DISABLED") {
 
 820     $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
 
 823   $form->isblank("dbname", $locale->text('The dataset name is missing.'));
 
 824   $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
 
 826   my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
 
 827   mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
 
 829   my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
 
 833     $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
 
 836   print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
 
 839   $ENV{HOME} = $tmpdir;
 
 841   my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
 
 842   push @args, ("-p", $form->{dbport}) if ($form->{dbport});
 
 843   push @args, $form->{dbname};
 
 845   my $cmd  = "${pg_dump_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
 
 846   my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar";
 
 848   if ($form->{destination} ne "email") {
 
 849     my $in = IO::File->new("$cmd |");
 
 852       unlink "${tmpdir}/.pgpass";
 
 855       $form->error($locale->text('The pg_dump process could not be started.'));
 
 858     print "content-type: application/x-tar\n";
 
 859     print "content-disposition: attachment; filename=\"${name}\"\n\n";
 
 861     while (my $line = <$in>) {
 
 867     unlink "${tmpdir}/.pgpass";
 
 871     my $tmp = $tmpdir . "/dump_" . Common::unique_id();
 
 873     if (system("$cmd > $tmp") != 0) {
 
 874       unlink "${tmpdir}/.pgpass", $tmp;
 
 877       $form->error($locale->text('The pg_dump process could not be started.'));
 
 880     my $mail = new Mailer;
 
 882     map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
 
 884     $mail->{charset}     = $dbcharset ? $dbcharset : Common::DEFAULT_CHARSET;
 
 885     $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
 
 888     unlink "${tmpdir}/.pgpass", $tmp;
 
 893       . $locale->text('Database Administration') . " / "
 
 894       . $locale->text('Backup Dataset');
 
 897     print $form->parse_html_template("admin/backup_dataset_email_done");
 
 901 sub restore_dataset {
 
 904     . $locale->text('Database Administration') . " / "
 
 905     . $locale->text('Restore Dataset');
 
 907   if ("$pg_restore_exe" eq "DISABLED") {
 
 908     $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
 
 911   my $default_charset   = $dbcharset;
 
 912   $default_charset    ||= Common::DEFAULT_CHARSET;
 
 914   $form->{DBENCODINGS}  = [];
 
 916   foreach my $encoding (@Common::db_encodings) {
 
 917     push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
 
 918                                       "label"      => $encoding->{label},
 
 919                                       "selected"   => $encoding->{charset} eq $default_charset };
 
 923   print $form->parse_html_template("admin/restore_dataset");
 
 926 sub restore_dataset_start {
 
 929     . $locale->text('Database Administration') . " / "
 
 930     . $locale->text('Restore Dataset');
 
 932   $pg_restore_exe ||= "pg_restore";
 
 934   if ("$pg_restore_exe" eq "DISABLED") {
 
 935     $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
 
 938   $form->isblank("new_dbname", $locale->text('The dataset name is missing.'));
 
 939   $form->isblank("content", $locale->text('No backup file has been uploaded.'));
 
 941   # Create temporary directories. Write the backup file contents to a temporary
 
 942   # file. Create a .pgpass file with the username and password for the pg_restore
 
 945   my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
 
 946   mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
 
 948   my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
 
 952     $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
 
 955   print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
 
 958   $ENV{HOME} = $tmpdir;
 
 960   my $tmp = $tmpdir . "/dump_" . Common::unique_id();
 
 963   if (substr($form->{content}, 0, 2) eq "\037\213") {
 
 964     $tmpfile = IO::File->new("| gzip -d > $tmp");
 
 968     $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600);
 
 972     unlink "${tmpdir}/.pgpass";
 
 975     $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
 
 978   print $tmpfile $form->{content};
 
 981   delete $form->{content};
 
 983   # Try to connect to the database. Find out if a database with the same name exists.
 
 984   # If yes, then drop the existing database. Create a new one with the name and encoding
 
 987   User::dbconnect_vars($form, "template1");
 
 989   my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form };
 
 990   my $dbh      = $form->dbconnect(\%myconfig) || $form->dberror();
 
 994   $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g;
 
 996   $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|;
 
 997   my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname});
 
 999     do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|);
 
1003   foreach my $item (@Common::db_encodings) {
 
1004     if ($item->{dbencoding} eq $form->{dbencoding}) {
 
1009   $form->{dbencoding} = "LATIN9" unless $form->{dbencoding};
 
1011   do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding});
 
1015   # Spawn pg_restore on the temporary file.
 
1017   my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname});
 
1018   push @args, ("-p", $form->{dbport}) if ($form->{dbport});
 
1021   my $cmd = "${pg_restore_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
 
1023   my $in = IO::File->new("$cmd 2>&1 |");
 
1026     unlink "${tmpdir}/.pgpass", $tmp;
 
1029     $form->error($locale->text('The pg_restore process could not be started.'));
 
1035   print $form->parse_html_template("admin/restore_dataset_start_header");
 
1037   while (my $line = <$in>) {
 
1042   $form->{retval} = $CHILD_ERROR >> 8;
 
1043   print $form->parse_html_template("admin/restore_dataset_start_footer");
 
1045   unlink "${tmpdir}/.pgpass", $tmp;
 
1051   unlink "$userspath/nologin";
 
1053   $form->{callback} = "admin.pl?action=list_users";
 
1055   $form->redirect($locale->text('Lockfile removed!'));
 
1061   open(FH, ">$userspath/nologin")
 
1062     or $form->error($locale->text('Cannot create Lock!'));
 
1065   $form->{callback} = "admin.pl?action=list_users";
 
1067   $form->redirect($locale->text('Lockfile created!'));
 
1072   call_sub($form->{yes_nextsub});
 
1076   call_sub($form->{no_nextsub});
 
1080   call_sub($form->{add_nextsub});
 
1084   $form->{edit_nextsub} ||= 'edit_user';
 
1086   call_sub($form->{edit_nextsub});
 
1090   $form->{delete_nextsub} ||= 'delete_user';
 
1092   call_sub($form->{delete_nextsub});
 
1096   $form->{save_nextsub} ||= 'save_user';
 
1098   call_sub($form->{save_nextsub});
 
1102   call_sub($form->{back_nextsub});
 
1106   foreach my $action (qw(create_standard_group dont_create_standard_group)) {
 
1107     if ($form->{"action_${action}"}) {
 
1113   call_sub($form->{default_action}) if ($form->{default_action});
 
1115   $form->error($locale->text('No action defined.'));