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();
179 if (!-f $memberfile) {
180 # New installation -- create a standard group with full access
182 'name' => $locale->text('Full Access'),
183 'description' => $locale->text('Full access to all functions'),
184 'rights' => { map { $_ => 1 } SL::Auth::all_rights() },
185 'members' => [ map { $_->{id} } values %members ],
188 $auth->save_group($group);
195 $lxdebug->enter_sub();
199 if ($memberfile =~ m|^.*/|) {
203 my $backupdir = "${memberdir}member-file-migration";
205 if (! -d $backupdir && !mkdir $backupdir, 0700) {
206 $form->error(sprintf($locale->text('The directory "%s" could not be created:\n%s'), $backupdir, $!));
209 copy $memberfile, "users/member-file-migration/members";
211 my $in = IO::File->new($memberfile, "r");
213 $form->error($locale->text('Could not open the old memberfile.')) if (!$in);
215 my (%members, $login);
224 $login =~ s/(\[|\])//g;
228 $members{$login} = { "login" => $login };
232 if ($login && m/=/) {
233 my ($key, $value) = split m/\s*=\s*/, $_, 2;
238 $value =~ s|\\n|\n|g;
240 $members{$login}->{$key} = $value;
246 delete $members{"root login"};
248 map { $_->{dbpasswd} = unpack 'u', $_->{dbpasswd} } values %members;
250 while (my ($login, $params) = each %members) {
251 $auth->save_user($login, %{ $params });
252 $auth->change_password($login, $params->{password}, 1);
254 my $conf_file = "${memberdir}${login}.conf";
257 copy $conf_file, "${backupdir}/${login}.conf";
264 my @member_list = sort { lc $a->{login} cmp lc $b->{login} } values %members;
266 $form->{title} = $locale->text('User data migration');
268 print $form->parse_html_template('admin/user_migration_done', { 'MEMBERS' => \@member_list });
270 $lxdebug->leave_sub();
273 sub create_standard_group_ask {
274 $form->{title} = $locale->text('Create a standard group');
277 print $form->parse_html_template("admin/create_standard_group_ask");
280 sub create_standard_group {
281 my %members = $auth->read_all_users();
283 my $groups = $auth->read_groups();
285 foreach my $group (values %{$groups}) {
286 if (($form->{group_id} != $group->{id})
287 && ($form->{name} eq $group->{name})) {
288 $form->show_generic_error($locale->text("A group with that name does already exist."));
293 'name' => $locale->text('Full Access'),
294 'description' => $locale->text('Full access to all functions'),
295 'rights' => { map { $_ => 1 } SL::Auth::all_rights() },
296 'members' => [ map { $_->{id} } values %members ],
299 $auth->save_group($group);
301 user_migration_complete(1);
304 sub dont_create_standard_group {
305 user_migration_complete(0);
308 sub user_migration_complete {
309 my $standard_group_created = shift;
311 $form->{title} = $locale->text('User migration complete');
314 print $form->parse_html_template('admin/user_migration_complete', { 'standard_group_created' => $standard_group_created });
318 my %members = $auth->read_all_users();
320 delete $members{"root login"};
322 map { $_->{templates} =~ s|.*/||; } values %members;
324 $form->{title} = "Lx-Office ERP " . $locale->text('Administration');
325 $form->{LOCKED} = -e "$userspath/nologin";
326 $form->{MEMBERS} = [ @members{sort { lc $a cmp lc $b } keys %members} ];
329 print $form->parse_html_template("admin/list_users");
336 . $locale->text('Administration') . " / "
337 . $locale->text('Add User');
341 "countrycode" => "de",
342 "numberformat" => "1.000,00",
343 "dateformat" => "dd.mm.yy",
344 "stylesheet" => "lx-office-erp.css",
348 edit_user_form($myconfig);
355 . $locale->text('Administration') . " / "
356 . $locale->text('Edit User');
359 $form->isblank("login", $locale->text("The login is missing."));
362 my $myconfig = new User($form->{login});
364 # strip basedir from templates directory
365 $myconfig->{templates} =~ s|.*/||;
367 edit_user_form($myconfig);
373 my @valid_dateformats = qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd);
374 $form->{ALL_DATEFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{dateformat} } } @valid_dateformats ];
376 my @valid_numberformats = qw(1,000.00 1000.00 1.000,00 1000,00);
377 $form->{ALL_NUMBERFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{numberformat} } } @valid_numberformats ];
379 %countrycodes = User->country_codes;
380 $form->{ALL_COUNTRYCODES} = [];
381 foreach $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
382 push @{ $form->{ALL_COUNTRYCODES} }, { "value" => $countrycode,
383 "name" => $countrycodes{$countrycode},
384 "selected" => $countrycode eq $myconfig->{countrycode} };
387 # is there a templates basedir
388 if (!-d "$templates") {
389 $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
392 opendir TEMPLATEDIR, "$templates/." or $form->error("$templates : $ERRNO");
393 my @all = readdir(TEMPLATEDIR);
394 my @alldir = sort grep { -d "$templates/$_" && !/^\.\.?$/ } @all;
395 my @allhtml = sort grep { -f "$templates/$_" && /\.html$/ } @all;
396 closedir TEMPLATEDIR;
398 @alldir = grep !/\.(html|tex|sty|odt|xml|txb)$/, @alldir;
399 @alldir = grep !/^(webpages|\.svn)$/, @alldir;
401 @allhtml = reverse grep !/Default/, @allhtml;
402 push @allhtml, 'Default';
403 @allhtml = reverse @allhtml;
405 $form->{ALL_TEMPLATES} = [ map { { "name", => $_, "selected" => $_ eq $myconfig->{templates} } } @alldir ];
407 $lastitem = $allhtml[0];
408 $lastitem =~ s/-.*//g;
409 $form->{ALL_MASTER_TEMPLATES} = [ { "name" => $lastitem, "selected" => $lastitem eq "German" } ];
410 foreach $item (@allhtml) {
412 next if ($item eq $lastitem);
414 push @{ $form->{ALL_MASTER_TEMPLATES} }, { "name" => $item, "selected" => $item eq "German" };
418 # css dir has styles that are not intended as general layouts.
419 # reverting to hardcoded list
420 $form->{ALL_STYLESHEETS} = [ map { { "name" => $_, "selected" => $_ eq $myconfig->{stylesheet} } } qw(lx-office-erp.css Win2000.css) ];
422 $form->{"menustyle_" . $myconfig->{menustyle} } = 1;
424 map { $form->{"myc_${_}"} = $myconfig->{$_} } keys %{ $myconfig };
429 my $user_id = $auth->get_user_id($form->{login});
430 my $all_groups = $auth->read_groups();
432 foreach my $group (values %{ $all_groups }) {
433 push @{ $groups }, $group if (grep { $user_id == $_ } @{ $group->{members} });
436 $groups = [ sort { lc $a->{name} cmp lc $b->{name} } @{ $groups } ];
439 $form->{CAN_CHANGE_PASSWORD} = $auth->can_change_password();
442 print $form->parse_html_template("admin/edit_user", { 'GROUPS' => $groups });
446 $form->{dbdriver} = 'Pg';
448 # no spaces allowed in login name
449 $form->{login} =~ s|\s||g;
450 $form->isblank("login", $locale->text('Login name missing!'));
452 # check for duplicates
453 if (!$form->{edit}) {
454 my %members = $auth->read_all_users();
455 if ($members{$form->{login}}) {
456 $form->show_generic_error($locale->text('Another user with the login #1 does already exist.', $form->{login}), 'back_button' => 1);
460 # no spaces allowed in directories
461 ($form->{newtemplates}) = split / /, $form->{newtemplates};
463 if ($form->{newtemplates}) {
464 $form->{templates} = $form->{newtemplates};
467 ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
471 if (!-d "$templates") {
472 $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
475 # add base directory to $form->{templates}
476 $form->{templates} =~ s|.*/||;
477 $form->{templates} = "$templates/$form->{templates}";
479 $myconfig = new User($form->{login});
481 $form->isblank("dbname", $locale->text('Dataset missing!'));
482 $form->isblank("dbuser", $locale->text('Database User missing!'));
484 foreach $item (keys %{$form}) {
485 $myconfig->{$item} = $form->{$item};
488 delete $myconfig->{stylesheet};
489 if ($form->{userstylesheet}) {
490 $myconfig->{stylesheet} = $form->{userstylesheet};
493 $myconfig->save_member();
495 if ($auth->can_change_password()
496 && defined $form->{new_password}
497 && ($form->{new_password} ne '********')) {
498 $auth->change_password($form->{login}, $form->{new_password});
503 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
504 foreach $directory (@webdavdirs) {
505 $file = "webdav/" . $directory . "/webdav-user";
506 if ($form->{$directory}) {
507 if (open(HTACCESS, "$file")) {
509 ($login, $password) = split(/:/, $_);
510 if ($login ne $form->{login}) {
516 open(HTACCESS, "> $file") or die "cannot open $file $ERRNO\n";
517 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
518 print(HTACCESS $newfile);
521 $form->{$directory} = 0;
522 if (open(HTACCESS, "$file")) {
524 ($login, $password) = split(/:/, $_);
525 if ($login ne $form->{login}) {
531 open(HTACCESS, "> $file") or die "cannot open $file $ERRNO\n";
532 print(HTACCESS $newfile);
538 $form->{templates} =~ s|.*/||;
539 $form->{templates} = "${templates}/$form->{templates}";
540 $form->{mastertemplates} =~ s|.*/||;
542 # create user template directory and copy master files
543 if (!-d "$form->{templates}") {
546 if (mkdir "$form->{templates}", oct("771")) {
550 # copy templates to the directory
551 opendir TEMPLATEDIR, "$templates/." or $form - error("$templates : $ERRNO");
552 @templates = grep /$form->{mastertemplates}.*?\.(html|tex|sty|odt|xml|txb)$/,
554 closedir TEMPLATEDIR;
556 foreach $file (@templates) {
557 open(TEMP, "$templates/$file")
558 or $form->error("$templates/$file : $ERRNO");
560 $file =~ s/\Q$form->{mastertemplates}\E-//;
561 open(NEW, ">$form->{templates}/$file")
562 or $form->error("$form->{templates}/$file : $ERRNO");
564 while ($line = <TEMP>) {
571 $form->error("$ERRNO: $form->{templates}");
575 # Add new user to his groups.
576 if (ref $form->{new_user_group_ids} eq 'ARRAY') {
577 my $all_groups = $auth->read_groups();
578 my %user = $auth->read_user($form->{login});
580 foreach my $group_id (@{ $form->{new_user_group_ids} }) {
581 my $group = $all_groups->{$group_id};
585 push @{ $group->{members} }, $user{id};
586 $auth->save_group($group);
590 $form->redirect($locale->text('User saved!'));
594 sub save_user_as_new {
595 $form->{login} = $form->{new_user_login};
596 delete @{$form}{qw(edit new_user_login)};
602 my %members = $auth->read_all_users();
603 my $templates = $members{$form->{login}}->{templates};
605 $auth->delete_user($form->{login});
608 my $templates_in_use = 0;
610 foreach $login (keys %members) {
611 next if $form->{login} eq $login;
612 next if $members{$login}->{templates} ne $templates;
613 $templates_in_use = 1;
617 if (!$templates_in_use && -d $templates) {
618 unlink <$templates/*>;
623 $form->redirect($locale->text('User deleted!'));
631 return ($login) ? $login : undef;
638 my ($null, $value) = split(/=/, $line, 2);
641 $value =~ s/\s#.*//g;
643 # remove any trailing whitespace
644 $value =~ s/^\s*(.*?)\s*$/$1/;
649 sub pg_database_administration {
651 $form->{dbdriver} = 'Pg';
656 sub dbselect_source {
657 $form->{dbport} = '5432';
658 $form->{dbuser} = 'postgres';
659 $form->{dbdefault} = 'template1';
660 $form->{dbhost} = 'localhost';
662 $form->{title} = "Lx-Office ERP / " . $locale->text('Database Administration');
664 # Intentionnaly disabled unless fixed to work with the authentication DB.
665 $form->{ALLOW_DBBACKUP} = 0; # "$pg_dump_exe" ne "DISABLED";
668 print $form->parse_html_template("admin/dbadmin");
671 sub test_db_connection {
672 $form->{dbdriver} = 'Pg';
673 User::dbconnect_vars($form, $form->{dbname});
675 my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd});
677 $form->{connection_ok} = $dbh ? 1 : 0;
678 $form->{errstr} = $DBI::errstr;
680 $dbh->disconnect() if ($dbh);
682 $form->{title} = $locale->text('Database Connection Test');
684 print $form->parse_html_template("admin/test_db_connection");
688 call_sub($form->{"nextsub"});
692 call_sub($form->{"back_nextsub"});
698 . $locale->text('Database Administration') . " / "
699 . $locale->text('Update Dataset');
701 my @need_updates = User->dbneedsupdate($form);
702 $form->{NEED_UPDATES} = \@need_updates;
703 $form->{ALL_UPDATED} = !scalar @need_updates;
706 print $form->parse_html_template("admin/update_dataset");
710 $form->{stylesheet} = "lx-office-erp.css";
711 $form->{title} = $locale->text("Dataset upgrade");
714 my $rowcount = $form->{rowcount} * 1;
715 my @update_rows = grep { $form->{"update_$_"} } (1 .. $rowcount);
716 $form->{NOTHING_TO_DO} = !scalar @update_rows;
717 my $saved_form = save_form();
721 print $form->parse_html_template("admin/dbupgrade_all_header");
723 foreach my $i (@update_rows) {
724 restore_form($saved_form);
726 map { $form->{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd);
728 my $controls = parse_dbupdate_controls($form, $form->{dbdriver});
730 print $form->parse_html_template("admin/dbupgrade_header");
732 $form->{dbupdate} = $form->{dbname};
733 $form->{$form->{dbname}} = 1;
735 User->dbupdate($form);
736 User->dbupdate2($form, $controls);
738 print $form->parse_html_template("admin/dbupgrade_footer");
741 print $form->parse_html_template("admin/dbupgrade_all_done");
745 $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources($form);
747 $form->{CHARTS} = [];
749 opendir SQLDIR, "sql/." or $form - error($ERRNO);
750 foreach $item (sort grep /-chart\.sql\z/, readdir SQLDIR) {
751 next if ($item eq 'Default-chart.sql');
752 $item =~ s/-chart\.sql//;
753 push @{ $form->{CHARTS} }, { "name" => $item,
754 "selected" => $item eq "Germany-DATEV-SKR03EU" };
758 my $default_charset = $dbcharset;
759 $default_charset ||= Common::DEFAULT_CHARSET;
761 my $cluster_encoding = User->dbclusterencoding($form);
762 if ($cluster_encoding && ($cluster_encoding =~ m/^(?:UTF-?8|UNICODE)$/i)) {
763 if ($dbcharset !~ m/^UTF-?8$/i) {
764 $form->show_generic_error($locale->text('The selected PostgreSQL installation uses UTF-8 as its encoding. ' .
765 'Therefore you have to configure Lx-Office to use UTF-8 as well.'),
769 $form->{FORCE_DBENCODING} = 'UNICODE';
772 $form->{DBENCODINGS} = [];
774 foreach my $encoding (@Common::db_encodings) {
775 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
776 "label" => $encoding->{label},
777 "selected" => $encoding->{charset} eq $default_charset };
783 . $locale->text('Database Administration') . " / "
784 . $locale->text('Create Dataset');
787 print $form->parse_html_template("admin/create_dataset");
791 $form->isblank("db", $locale->text('Dataset missing!'));
793 User->dbcreate(\%$form);
797 . $locale->text('Database Administration') . " / "
798 . $locale->text('Create Dataset');
801 print $form->parse_html_template("admin/dbcreate");
805 @dbsources = User->dbsources_unused($form);
806 $form->error($locale->text('Nothing to delete!')) unless @dbsources;
810 . $locale->text('Database Administration') . " / "
811 . $locale->text('Delete Dataset');
812 $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
815 print $form->parse_html_template("admin/delete_dataset");
821 $form->error($locale->text('No Dataset selected!'));
824 User->dbdelete(\%$form);
828 . $locale->text('Database Administration') . " / "
829 . $locale->text('Delete Dataset');
831 print $form->parse_html_template("admin/dbdelete");
837 . $locale->text('Database Administration') . " / "
838 . $locale->text('Backup Dataset');
840 if ("$pg_dump_exe" eq "DISABLED") {
841 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
844 my @dbsources = sort User->dbsources($form);
845 $form->{DATABASES} = [ map { { "dbname" => $_ } } @dbsources ];
846 $form->{NO_DATABASES} = !scalar @dbsources;
848 my $username = getpwuid $UID || "unknown-user";
849 my $hostname = hostname() || "unknown-host";
850 $form->{from} = "Lx-Office Admin <${username}\@${hostname}>";
853 print $form->parse_html_template("admin/backup_dataset");
856 sub backup_dataset_start {
859 . $locale->text('Database Administration') . " / "
860 . $locale->text('Backup Dataset');
862 $pg_dump_exe ||= "pg_dump";
864 if ("$pg_dump_exe" eq "DISABLED") {
865 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
868 $form->isblank("dbname", $locale->text('The dataset name is missing.'));
869 $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
871 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
872 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
874 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
878 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
881 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
884 $ENV{HOME} = $tmpdir;
886 my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
887 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
888 push @args, $form->{dbname};
890 my $cmd = "${pg_dump_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
891 my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar";
893 if ($form->{destination} ne "email") {
894 my $in = IO::File->new("$cmd |");
897 unlink "${tmpdir}/.pgpass";
900 $form->error($locale->text('The pg_dump process could not be started.'));
903 print "content-type: application/x-tar\n";
904 print "content-disposition: attachment; filename=\"${name}\"\n\n";
906 while (my $line = <$in>) {
912 unlink "${tmpdir}/.pgpass";
916 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
918 if (system("$cmd > $tmp") != 0) {
919 unlink "${tmpdir}/.pgpass", $tmp;
922 $form->error($locale->text('The pg_dump process could not be started.'));
925 my $mail = new Mailer;
927 map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
929 $mail->{charset} = $dbcharset ? $dbcharset : Common::DEFAULT_CHARSET;
930 $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
933 unlink "${tmpdir}/.pgpass", $tmp;
938 . $locale->text('Database Administration') . " / "
939 . $locale->text('Backup Dataset');
942 print $form->parse_html_template("admin/backup_dataset_email_done");
946 sub restore_dataset {
949 . $locale->text('Database Administration') . " / "
950 . $locale->text('Restore Dataset');
952 if ("$pg_restore_exe" eq "DISABLED") {
953 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
956 my $default_charset = $dbcharset;
957 $default_charset ||= Common::DEFAULT_CHARSET;
959 $form->{DBENCODINGS} = [];
961 foreach my $encoding (@Common::db_encodings) {
962 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
963 "label" => $encoding->{label},
964 "selected" => $encoding->{charset} eq $default_charset };
968 print $form->parse_html_template("admin/restore_dataset");
971 sub restore_dataset_start {
974 . $locale->text('Database Administration') . " / "
975 . $locale->text('Restore Dataset');
977 $pg_restore_exe ||= "pg_restore";
979 if ("$pg_restore_exe" eq "DISABLED") {
980 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
983 $form->isblank("new_dbname", $locale->text('The dataset name is missing.'));
984 $form->isblank("content", $locale->text('No backup file has been uploaded.'));
986 # Create temporary directories. Write the backup file contents to a temporary
987 # file. Create a .pgpass file with the username and password for the pg_restore
990 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
991 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
993 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
997 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
1000 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
1003 $ENV{HOME} = $tmpdir;
1005 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
1008 if (substr($form->{content}, 0, 2) eq "\037\213") {
1009 $tmpfile = IO::File->new("| gzip -d > $tmp");
1013 $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600);
1017 unlink "${tmpdir}/.pgpass";
1020 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
1023 print $tmpfile $form->{content};
1026 delete $form->{content};
1028 # Try to connect to the database. Find out if a database with the same name exists.
1029 # If yes, then drop the existing database. Create a new one with the name and encoding
1030 # given by the user.
1032 User::dbconnect_vars($form, "template1");
1034 my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form };
1035 my $dbh = $form->dbconnect(\%myconfig) || $form->dberror();
1039 $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g;
1041 $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|;
1042 my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname});
1044 do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|);
1048 foreach my $item (@Common::db_encodings) {
1049 if ($item->{dbencoding} eq $form->{dbencoding}) {
1054 $form->{dbencoding} = "LATIN9" unless $form->{dbencoding};
1056 do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding});
1060 # Spawn pg_restore on the temporary file.
1062 my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname});
1063 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
1066 my $cmd = "${pg_restore_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
1068 my $in = IO::File->new("$cmd 2>&1 |");
1071 unlink "${tmpdir}/.pgpass", $tmp;
1074 $form->error($locale->text('The pg_restore process could not be started.'));
1080 print $form->parse_html_template("admin/restore_dataset_start_header");
1082 while (my $line = <$in>) {
1087 $form->{retval} = $CHILD_ERROR >> 8;
1088 print $form->parse_html_template("admin/restore_dataset_start_footer");
1090 unlink "${tmpdir}/.pgpass", $tmp;
1096 unlink "$userspath/nologin";
1098 $form->{callback} = "admin.pl?action=list_users";
1100 $form->redirect($locale->text('Lockfile removed!'));
1106 open(FH, ">$userspath/nologin")
1107 or $form->error($locale->text('Cannot create Lock!'));
1110 $form->{callback} = "admin.pl?action=list_users";
1112 $form->redirect($locale->text('Lockfile created!'));
1117 call_sub($form->{yes_nextsub});
1121 call_sub($form->{no_nextsub});
1125 call_sub($form->{add_nextsub});
1129 $form->{edit_nextsub} ||= 'edit_user';
1131 call_sub($form->{edit_nextsub});
1135 $form->{delete_nextsub} ||= 'delete_user';
1137 call_sub($form->{delete_nextsub});
1141 $form->{save_nextsub} ||= 'save_user';
1143 call_sub($form->{save_nextsub});
1147 call_sub($form->{back_nextsub});
1151 foreach my $action (qw(create_standard_group dont_create_standard_group
1152 save_user delete_user save_user_as_new)) {
1153 if ($form->{"action_${action}"}) {
1159 call_sub($form->{default_action}) if ($form->{default_action});
1161 $form->error($locale->text('No action defined.'));