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 POSIX qw(strftime);
 
  42 use English qw(-no_match_vars);
 
  53 require "bin/mozilla/common.pl";
 
  55 our $cgi = new CGI('');
 
  58 $form->{"root"} = "root login";
 
  60 $locale = new Locale $language, "admin";
 
  63 if (-f "bin/mozilla/custom_$form->{script}") {
 
  64   eval { require "bin/mozilla/custom_$form->{script}"; };
 
  65   $form->error($@) if ($@);
 
  68 $form->{stylesheet} = "lx-office-erp.css";
 
  69 $form->{favicon}    = "favicon.ico";
 
  71 if ($form->{action}) {
 
  74   $subroutine = $locale->findsub($form->{action});
 
  76   if ($subroutine eq 'login') {
 
  78       $form->{rpw} = crypt $form->{rpw}, "ro";
 
  84   call_sub($subroutine);
 
  88   # if there are no drivers bail out
 
  89   $form->error($locale->text('No Database Drivers available!'))
 
  90     unless (User->dbdrivers);
 
  93   if (!-f $memberfile) {
 
  94     open(FH, ">$memberfile") or $form->error("$memberfile : $ERRNO");
 
  95     print FH qq|# SQL-Ledger Accounting members
 
 115     qq|Lx-Office ERP $form->{version} | . $locale->text('Administration');
 
 118   print $form->parse_html_template2('admin/adminlogin');
 
 127   $form->error($locale->text('File locked!')) if (-f "${memberfile}.LCK");
 
 129   open(FH, "$memberfile") or $form->error("$memberfile : $ERRNO");
 
 138       $login =~ s/(\[|\])//g;
 
 140       $members{$login} = { "login" => $login };
 
 143     if (/^([a-z]+)=(.*)/) {
 
 144       $members{$login}->{$1} = $2;
 
 150   delete $members{"root login"};
 
 151   map { $_->{templates} =~ s|.*/||; } values %members;
 
 153   $form->{title}  = "Lx-Office ERP " . $locale->text('Administration');
 
 154   $form->{LOCKED} = -e "$userspath/nologin";
 
 155   $form->{MEMBERS} = [ @members{sort { lc $a cmp lc $b } keys %members} ];
 
 158   print $form->parse_html_template2("admin/list_users");
 
 165     . $locale->text('Administration') . " / "
 
 166     . $locale->text('Add User');
 
 170     "countrycode"  => "de",
 
 171     "numberformat" => "1.000,00",
 
 172     "dateformat"   => "dd.mm.yy",
 
 173     "stylesheet"   => "lx-office-erp.css",
 
 177   edit_user_form($myconfig);
 
 184     . $locale->text('Administration') . " / "
 
 185     . $locale->text('Edit User');
 
 188   $form->isblank("login", $locale->text("The login is missing."));
 
 191   my $myconfig = new User "$memberfile", "$form->{login}";
 
 193   $myconfig->{signature} =~ s/\\n/\r\n/g;
 
 194   $myconfig->{address}   =~ s/\\n/\r\n/g;
 
 196   # strip basedir from templates directory
 
 197   $myconfig->{templates} =~ s|.*/||;
 
 199   edit_user_form($myconfig);
 
 205   my @valid_dateformats = qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd);
 
 206   $form->{ALL_DATEFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{dateformat} } } @valid_dateformats ];
 
 208   my @valid_numberformats = qw(1,000.00 1000.00 1.000,00 1000,00);
 
 209   $form->{ALL_NUMBERFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{numberformat} } } @valid_numberformats ];
 
 211   %countrycodes = User->country_codes;
 
 212   $form->{ALL_COUNTRYCODES} = [];
 
 213   foreach $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
 
 214     push @{ $form->{ALL_COUNTRYCODES} }, { "value"    => $countrycode,
 
 215                                            "name"     => $countrycodes{$countrycode},
 
 216                                            "selected" => $countrycode eq $myconfig->{countrycode} };
 
 219   # is there a templates basedir
 
 220   if (!-d "$templates") {
 
 221     $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
 
 224   opendir TEMPLATEDIR, "$templates/." or $form->error("$templates : $ERRNO");
 
 225   my @all     = readdir(TEMPLATEDIR);
 
 226   my @alldir  = sort grep { -d "$templates/$_" && !/^\.\.?$/ } @all;
 
 227   my @allhtml = sort grep { -f "$templates/$_" && /\.html$/ } @all;
 
 228   closedir TEMPLATEDIR;
 
 230   @alldir = grep !/\.(html|tex|sty|odt|xml|txb)$/, @alldir;
 
 231   @alldir = grep !/^(webpages|\.svn)$/, @alldir;
 
 233   @allhtml = reverse grep !/Default/, @allhtml;
 
 234   push @allhtml, 'Default';
 
 235   @allhtml = reverse @allhtml;
 
 237   $form->{ALL_TEMPLATES} = [ map { { "name", => $_, "selected" => $_ eq $myconfig->{templates} } } @alldir ];
 
 239   $lastitem = $allhtml[0];
 
 240   $lastitem =~ s/-.*//g;
 
 241   $form->{ALL_MASTER_TEMPLATES} = [ { "name" => $lastitem, "selected" => $lastitem eq "German" } ];
 
 242   foreach $item (@allhtml) {
 
 244     next if ($item eq $lastitem);
 
 246     push @{ $form->{ALL_MASTER_TEMPLATES} }, { "name" => $item, "selected" => $item eq "German" };
 
 250   # css dir has styles that are not intended as general layouts.
 
 251   # reverting to hardcoded list
 
 252   $form->{ALL_STYLESHEETS} = [ map { { "name" => $_, "selected" => $_ eq $myconfig->{stylesheet} } } qw(lx-office-erp.css Win2000.css) ];
 
 254   $form->{"menustyle_" . $myconfig->{menustyle} } = 1;
 
 256   map { $form->{"myc_${_}"} = $myconfig->{$_} } keys %{ $myconfig };
 
 262   open(FH, $menufile) or $form->error("$menufile : $ERRNO");
 
 264   while ($item = <FH>) {
 
 265     next unless $item =~ /\[/;
 
 266     next if $item =~ /\#/;
 
 268     $item =~ s/(\[|\])//g;
 
 271     my ($level, $menuitem);
 
 274       ($level, $menuitem) = split /--/, $item, 2;
 
 278       push @acsorder, $item;
 
 282     push @{ $acs{$level} }, $menuitem;
 
 286   foreach $item (split(/;/, $myconfig->{acs})) {
 
 287     ($key, $value) = split /--/, $item, 2;
 
 288     $excl{$key}{$value} = 1;
 
 292   $form->{all_acs} = "";
 
 294   foreach $key (@acsorder) {
 
 295     my $acl = { "checked" => $form->{login} ? !$excl{$key}->{$key} : 1,
 
 296                 "name"    => "${key}--${key}",
 
 299     $form->{all_acs} .= "${key}--${key};";
 
 301     foreach $item (@{ $acs{$key} }) {
 
 302       next if ($key eq $item);
 
 304       my $subacl = { "checked" => $form->{login} ? !$excl{$key}->{$item} : 1,
 
 305                      "name"    => "${key}--${item}",
 
 307       push @{ $acl->{SUBACLS} }, $subacl;
 
 308       $form->{all_acs} .= "${key}--${item};";
 
 310     push @{ $form->{ACLS} }, $acl;
 
 313   chop $form->{all_acs};
 
 316   print $form->parse_html_template2("admin/edit_user");
 
 321   $form->{dbdriver} = 'Pg';
 
 323   # no spaces allowed in login name
 
 324   ($form->{login}) = split / /, $form->{login};
 
 326   $form->isblank("login", $locale->text('Login name missing!'));
 
 328   # check for duplicates
 
 329   if (!$form->{edit}) {
 
 330     $temp = new User "$memberfile", "$form->{login}";
 
 332     if ($temp->{login}) {
 
 333       $form->error("$form->{login} " . $locale->text('is already a member!'));
 
 337   # no spaces allowed in directories
 
 338   ($form->{newtemplates}) = split / /, $form->{newtemplates};
 
 340   if ($form->{newtemplates}) {
 
 341     $form->{templates} = $form->{newtemplates};
 
 344       ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
 
 348   if (!-d "$templates") {
 
 349     $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
 
 352   # add base directory to $form->{templates}
 
 353   $form->{templates} =~ s|.*/||;
 
 354   $form->{templates} =  "$templates/$form->{templates}";
 
 356   $myconfig = new User "$memberfile", "$form->{login}";
 
 358   # redo acs variable and delete all the acs codes
 
 360   foreach $item (split m|;|, $form->{all_acs}) {
 
 361     my $name =  "ACS_${item}";
 
 363     push @acs, $item if !$form->{$name};
 
 364     delete $form->{$name};
 
 366   $form->{acs} = join ";", @acs;
 
 368   $form->isblank("dbname", $locale->text('Dataset missing!'));
 
 369   $form->isblank("dbuser", $locale->text('Database User missing!'));
 
 371   foreach $item (keys %{$form}) {
 
 372     $myconfig->{$item} = $form->{$item};
 
 375   delete $myconfig->{stylesheet};
 
 376   if ($form->{userstylesheet}) {
 
 377     $myconfig->{stylesheet} = $form->{userstylesheet};
 
 380   $myconfig->save_member($memberfile, $userspath);
 
 384       qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
 
 385     foreach $directory (@webdavdirs) {
 
 386       $file = "webdav/" . $directory . "/webdav-user";
 
 387       if ($form->{$directory}) {
 
 388         if (open(HTACCESS, "$file")) {
 
 390             ($login, $password) = split(/:/, $_);
 
 391             if ($login ne $form->{login}) {
 
 397         open(HTACCESS, "> $file") or die "cannot open $file $ERRNO\n";
 
 398         $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
 
 399         print(HTACCESS $newfile);
 
 402         $form->{$directory} = 0;
 
 403         if (open(HTACCESS, "$file")) {
 
 405             ($login, $password) = split(/:/, $_);
 
 406             if ($login ne $form->{login}) {
 
 412         open(HTACCESS, "> $file") or die "cannot open $file $ERRNO\n";
 
 413         print(HTACCESS $newfile);
 
 419   $form->{templates}       =~ s|.*/||;
 
 420   $form->{templates}       =  "${templates}/$form->{templates}";
 
 421   $form->{mastertemplates} =~ s|.*/||;
 
 423   # create user template directory and copy master files
 
 424   if (!-d "$form->{templates}") {
 
 427     if (mkdir "$form->{templates}", oct("771")) {
 
 431       # copy templates to the directory
 
 432       opendir TEMPLATEDIR, "$templates/." or $form - error("$templates : $ERRNO");
 
 433       @templates = grep /$form->{mastertemplates}.*?\.(html|tex|sty|xml|txb)$/,
 
 435       closedir TEMPLATEDIR;
 
 437       foreach $file (@templates) {
 
 438         open(TEMP, "$templates/$file")
 
 439           or $form->error("$templates/$file : $ERRNO");
 
 441         $file =~ s/$form->{mastertemplates}-//;
 
 442         open(NEW, ">$form->{templates}/$file")
 
 443           or $form->error("$form->{templates}/$file : $ERRNO");
 
 445         while ($line = <TEMP>) {
 
 452       $form->error("$ERRNO: $form->{templates}");
 
 456   $form->redirect($locale->text('User saved!'));
 
 461   $form->error($locale->text('File locked!')) if (-f ${memberfile} . LCK);
 
 462   open(FH, ">${memberfile}.LCK") or $form->error("${memberfile}.LCK : $ERRNO");
 
 465   my $members = Inifile->new($memberfile);
 
 466   my $templates = $members->{$form->{login}}->{templates};
 
 467   delete $members->{$form->{login}};
 
 469   unlink "${memberfile}.LCK";
 
 472     my $templates_in_use = 0;
 
 473     foreach $login (keys %{ $members }) {
 
 474       next if $login =~ m/^[A-Z]+$/;
 
 475       next if $members->{$login}->{templates} ne $templates;
 
 476       $templates_in_use = 1;
 
 480     if (!$templates_in_use && -d $templates) {
 
 481       unlink <$templates/*>;
 
 486   # delete config file for user
 
 487   unlink "$userspath/$form->{login}.conf";
 
 489   $form->redirect($locale->text('User deleted!'));
 
 497   return ($login) ? $login : undef;
 
 504   my ($null, $value) = split(/=/, $line, 2);
 
 507   $value =~ s/\s#.*//g;
 
 509   # remove any trailing whitespace
 
 510   $value =~ s/^\s*(.*?)\s*$/$1/;
 
 515 sub change_admin_password {
 
 519     . $locale->text('Administration') . " / "
 
 520     . $locale->text('Change Admin Password');
 
 523   print $form->parse_html_template2("admin/change_admin_password");
 
 526 sub change_password {
 
 527   if ($form->{"password"} ne $form->{"password_again"}) {
 
 530       . $locale->text('Administration') . " / "
 
 531       . $locale->text('Change Admin Password');
 
 534     $form->error($locale->text("The passwords do not match."));
 
 537   $root->{password} = $form->{password};
 
 539   $root->{'root login'} = 1;
 
 540   $root->save_member($memberfile);
 
 543     "$form->{script}?action=list_users&rpw=$root->{password}";
 
 545   $form->redirect($locale->text('Password changed!'));
 
 549   $root = new User "$memberfile", $form->{root};
 
 551   if (!defined($root->{password}) || ($root->{password} ne $form->{rpw})) {
 
 552     $form->error($locale->text('Incorrect Password!'));
 
 557 sub pg_database_administration {
 
 559   $form->{dbdriver} = 'Pg';
 
 564 sub dbselect_source {
 
 565   $form->{dbport}    = '5432';
 
 566   $form->{dbuser}    = 'postgres';
 
 567   $form->{dbdefault} = 'template1';
 
 568   $form->{dbhost}    = 'localhost';
 
 570   $form->{title}     = "Lx-Office ERP / " . $locale->text('Database Administration');
 
 572   $form->{ALLOW_DBBACKUP} = "$pg_dump_exe" ne "DISABLED";
 
 575   print $form->parse_html_template2("admin/dbadmin");
 
 579   call_sub($form->{"nextsub"});
 
 583   call_sub($form->{"back_nextsub"});
 
 589     . $locale->text('Database Administration') . " / "
 
 590     . $locale->text('Update Dataset');
 
 592   my @need_updates      = User->dbneedsupdate($form);
 
 593   $form->{NEED_UPDATES} = \@need_updates;
 
 594   $form->{ALL_UPDATED}  = !scalar @need_updates;
 
 597   print $form->parse_html_template2("admin/update_dataset");
 
 601   $form->{stylesheet} = "lx-office-erp.css";
 
 602   $form->{title}      = $locale->text("Dataset upgrade");
 
 605   my $rowcount           = $form->{rowcount} * 1;
 
 606   my @update_rows        = grep { $form->{"update_$_"} } (1 .. $rowcount);
 
 607   $form->{NOTHING_TO_DO} = !scalar @update_rows;
 
 608   my $saved_form         = save_form();
 
 612   print $form->parse_html_template2("admin/dbupgrade_all_header");
 
 614   foreach my $i (@update_rows) {
 
 615     restore_form($saved_form);
 
 617     map { $form->{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd);
 
 619     my $controls = parse_dbupdate_controls($form, $form->{dbdriver});
 
 621     print $form->parse_html_template2("admin/dbupgrade_header");
 
 623     $form->{dbupdate}        = $form->{dbname};
 
 624     $form->{$form->{dbname}} = 1;
 
 626     User->dbupdate($form);
 
 627     User->dbupdate2($form, $controls);
 
 629     print $form->parse_html_template2("admin/dbupgrade_footer");
 
 632   print $form->parse_html_template2("admin/dbupgrade_all_done");
 
 636   $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources(\%$form);
 
 638   $form->{CHARTS} = [];
 
 640   opendir SQLDIR, "sql/." or $form - error($ERRNO);
 
 641   foreach $item (sort grep /-chart\.sql\z/, readdir SQLDIR) {
 
 642     next if ($item eq 'Default-chart.sql');
 
 643     $item =~ s/-chart\.sql//;
 
 644     push @{ $form->{CHARTS} }, { "name"     => $item,
 
 645                                  "selected" => $item eq "Germany-DATEV-SKR03EU" };
 
 649   my $default_charset = $dbcharset;
 
 650   $default_charset ||= Common::DEFAULT_CHARSET;
 
 652   $form->{DBENCODINGS} = [];
 
 654   foreach my $encoding (@Common::db_encodings) {
 
 655     push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
 
 656                                       "label"      => $encoding->{label},
 
 657                                       "selected"   => $encoding->{charset} eq $default_charset };
 
 662     . $locale->text('Database Administration') . " / "
 
 663     . $locale->text('Create Dataset');
 
 666   print $form->parse_html_template2("admin/create_dataset");
 
 670   $form->isblank("db", $locale->text('Dataset missing!'));
 
 672   User->dbcreate(\%$form);
 
 676     . $locale->text('Database Administration') . " / "
 
 677     . $locale->text('Create Dataset');
 
 680   print $form->parse_html_template2("admin/dbcreate");
 
 684   @dbsources = User->dbsources_unused(\%$form, $memberfile);
 
 685   $form->error($locale->text('Nothing to delete!')) unless @dbsources;
 
 689     . $locale->text('Database Administration') . " / "
 
 690     . $locale->text('Delete Dataset');
 
 691   $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
 
 694   print $form->parse_html_template2("admin/delete_dataset");
 
 700     $form->error($locale->text('No Dataset selected!'));
 
 703   User->dbdelete(\%$form);
 
 707     . $locale->text('Database Administration') . " / "
 
 708     . $locale->text('Delete Dataset');
 
 710   print $form->parse_html_template2("admin/dbdelete");
 
 716     . $locale->text('Database Administration') . " / "
 
 717     . $locale->text('Backup Dataset');
 
 719   if ("$pg_dump_exe" eq "DISABLED") {
 
 720     $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
 
 723   my @dbsources         = sort User->dbsources($form);
 
 724   $form->{DATABASES}    = [ map { { "dbname" => $_ } } @dbsources ];
 
 725   $form->{NO_DATABASES} = !scalar @dbsources;
 
 727   my $username  = getpwuid $UID || "unknown-user";
 
 728   my $hostname  = hostname() || "unknown-host";
 
 729   $form->{from} = "Lx-Office Admin <${username}\@${hostname}>";
 
 732   print $form->parse_html_template2("admin/backup_dataset");
 
 735 sub backup_dataset_start {
 
 738     . $locale->text('Database Administration') . " / "
 
 739     . $locale->text('Backup Dataset');
 
 741   $pg_dump_exe ||= "pg_dump";
 
 743   if ("$pg_dump_exe" eq "DISABLED") {
 
 744     $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
 
 747   $form->isblank("dbname", $locale->text('The dataset name is missing.'));
 
 748   $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
 
 750   my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
 
 751   mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
 
 753   my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
 
 757     $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
 
 760   print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
 
 763   $ENV{HOME} = $tmpdir;
 
 765   my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
 
 766   push @args, ("-p", $form->{dbport}) if ($form->{dbport});
 
 767   push @args, $form->{dbname};
 
 769   my $cmd  = "${pg_dump_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
 
 770   my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar";
 
 772   if ($form->{destination} ne "email") {
 
 773     my $in = IO::File->new("$cmd |");
 
 776       unlink "${tmpdir}/.pgpass";
 
 779       $form->error($locale->text('The pg_dump process could not be started.'));
 
 782     print "content-type: application/x-tar\n";
 
 783     print "content-disposition: attachment; filename=\"${name}\"\n\n";
 
 785     while (my $line = <$in>) {
 
 791     unlink "${tmpdir}/.pgpass";
 
 795     my $tmp = $tmpdir . "/dump_" . Common::unique_id();
 
 797     if (system("$cmd > $tmp") != 0) {
 
 798       unlink "${tmpdir}/.pgpass", $tmp;
 
 801       $form->error($locale->text('The pg_dump process could not be started.'));
 
 804     my $mail = new Mailer;
 
 806     map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
 
 808     $mail->{charset}     = $dbcharset ? $dbcharset : Common::DEFAULT_CHARSET;
 
 809     $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
 
 812     unlink "${tmpdir}/.pgpass", $tmp;
 
 817       . $locale->text('Database Administration') . " / "
 
 818       . $locale->text('Backup Dataset');
 
 821     print $form->parse_html_template2("admin/backup_dataset_email_done");
 
 825 sub restore_dataset {
 
 828     . $locale->text('Database Administration') . " / "
 
 829     . $locale->text('Restore Dataset');
 
 831   if ("$pg_restore_exe" eq "DISABLED") {
 
 832     $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
 
 835   my $default_charset   = $dbcharset;
 
 836   $default_charset    ||= Common::DEFAULT_CHARSET;
 
 838   $form->{DBENCODINGS}  = [];
 
 840   foreach my $encoding (@Common::db_encodings) {
 
 841     push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
 
 842                                       "label"      => $encoding->{label},
 
 843                                       "selected"   => $encoding->{charset} eq $default_charset };
 
 847   print $form->parse_html_template2("admin/restore_dataset");
 
 850 sub restore_dataset_start {
 
 853     . $locale->text('Database Administration') . " / "
 
 854     . $locale->text('Restore Dataset');
 
 856   $pg_restore_exe ||= "pg_restore";
 
 858   if ("$pg_restore_exe" eq "DISABLED") {
 
 859     $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
 
 862   $form->isblank("new_dbname", $locale->text('The dataset name is missing.'));
 
 863   $form->isblank("content", $locale->text('No backup file has been uploaded.'));
 
 865   # Create temporary directories. Write the backup file contents to a temporary
 
 866   # file. Create a .pgpass file with the username and password for the pg_restore
 
 869   my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
 
 870   mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
 
 872   my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
 
 876     $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
 
 879   print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
 
 882   $ENV{HOME} = $tmpdir;
 
 884   my $tmp = $tmpdir . "/dump_" . Common::unique_id();
 
 887   if (substr($form->{content}, 0, 2) eq "\037\213") {
 
 888     $tmpfile = IO::File->new("| gzip -d > $tmp");
 
 892     $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600);
 
 896     unlink "${tmpdir}/.pgpass";
 
 899     $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
 
 902   print $tmpfile $form->{content};
 
 905   delete $form->{content};
 
 907   # Try to connect to the database. Find out if a database with the same name exists.
 
 908   # If yes, then drop the existing database. Create a new one with the name and encoding
 
 911   User::dbconnect_vars($form, "template1");
 
 913   my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form };
 
 914   my $dbh      = $form->dbconnect(\%myconfig) || $form->dberror();
 
 918   $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g;
 
 920   $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|;
 
 921   my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname});
 
 923     do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|);
 
 927   foreach my $item (@Common::db_encodings) {
 
 928     if ($item->{dbencoding} eq $form->{dbencoding}) {
 
 933   $form->{dbencoding} = "LATIN9" unless $form->{dbencoding};
 
 935   do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding});
 
 939   # Spawn pg_restore on the temporary file.
 
 941   my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname});
 
 942   push @args, ("-p", $form->{dbport}) if ($form->{dbport});
 
 945   my $cmd = "${pg_restore_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
 
 947   my $in = IO::File->new("$cmd 2>&1 |");
 
 950     unlink "${tmpdir}/.pgpass", $tmp;
 
 953     $form->error($locale->text('The pg_restore process could not be started.'));
 
 959   print $form->parse_html_template2("admin/restore_dataset_start_header");
 
 961   while (my $line = <$in>) {
 
 966   $form->{retval} = $CHILD_ERROR >> 8;
 
 967   print $form->parse_html_template2("admin/restore_dataset_start_footer");
 
 969   unlink "${tmpdir}/.pgpass", $tmp;
 
 975   unlink "$userspath/nologin";
 
 978     "$form->{script}?action=list_users&rpw=$root->{password}";
 
 980   $form->redirect($locale->text('Lockfile removed!'));
 
 986   open(FH, ">$userspath/nologin")
 
 987     or $form->error($locale->text('Cannot create Lock!'));
 
 991     "$form->{script}?action=list_users&rpw=$root->{password}";
 
 993   $form->redirect($locale->text('Lockfile created!'));