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();
456 if ($members{$form->{login}}) {
457 $form->error("$form->{login} " . $locale->text('is already a member!'));
461 # no spaces allowed in directories
462 ($form->{newtemplates}) = split / /, $form->{newtemplates};
464 if ($form->{newtemplates}) {
465 $form->{templates} = $form->{newtemplates};
468 ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
472 if (!-d "$templates") {
473 $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
476 # add base directory to $form->{templates}
477 $form->{templates} =~ s|.*/||;
478 $form->{templates} = "$templates/$form->{templates}";
480 $myconfig = new User($form->{login});
482 $form->isblank("dbname", $locale->text('Dataset missing!'));
483 $form->isblank("dbuser", $locale->text('Database User missing!'));
485 foreach $item (keys %{$form}) {
486 $myconfig->{$item} = $form->{$item};
489 delete $myconfig->{stylesheet};
490 if ($form->{userstylesheet}) {
491 $myconfig->{stylesheet} = $form->{userstylesheet};
494 $myconfig->save_member();
496 if ($auth->can_change_password()
497 && defined $form->{new_password}
498 && ($form->{new_password} ne '********')) {
499 $auth->change_password($form->{login}, $form->{new_password});
504 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
505 foreach $directory (@webdavdirs) {
506 $file = "webdav/" . $directory . "/webdav-user";
507 if ($form->{$directory}) {
508 if (open(HTACCESS, "$file")) {
510 ($login, $password) = split(/:/, $_);
511 if ($login ne $form->{login}) {
517 open(HTACCESS, "> $file") or die "cannot open $file $ERRNO\n";
518 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
519 print(HTACCESS $newfile);
522 $form->{$directory} = 0;
523 if (open(HTACCESS, "$file")) {
525 ($login, $password) = split(/:/, $_);
526 if ($login ne $form->{login}) {
532 open(HTACCESS, "> $file") or die "cannot open $file $ERRNO\n";
533 print(HTACCESS $newfile);
539 $form->{templates} =~ s|.*/||;
540 $form->{templates} = "${templates}/$form->{templates}";
541 $form->{mastertemplates} =~ s|.*/||;
543 # create user template directory and copy master files
544 if (!-d "$form->{templates}") {
547 if (mkdir "$form->{templates}", oct("771")) {
551 # copy templates to the directory
552 opendir TEMPLATEDIR, "$templates/." or $form - error("$templates : $ERRNO");
553 @templates = grep /$form->{mastertemplates}.*?\.(html|tex|sty|xml|txb)$/,
555 closedir TEMPLATEDIR;
557 foreach $file (@templates) {
558 open(TEMP, "$templates/$file")
559 or $form->error("$templates/$file : $ERRNO");
561 $file =~ s/\Q$form->{mastertemplates}\E-//;
562 open(NEW, ">$form->{templates}/$file")
563 or $form->error("$form->{templates}/$file : $ERRNO");
565 while ($line = <TEMP>) {
572 $form->error("$ERRNO: $form->{templates}");
576 $form->redirect($locale->text('User saved!'));
581 my %members = $auth->read_all_users();
582 my $templates = $members{$form->{login}}->{templates};
584 $auth->delete_user($form->{login});
587 my $templates_in_use = 0;
589 foreach $login (keys %members) {
590 next if $form->{login} eq $login;
591 next if $members{$login}->{templates} ne $templates;
592 $templates_in_use = 1;
596 if (!$templates_in_use && -d $templates) {
597 unlink <$templates/*>;
602 $form->redirect($locale->text('User deleted!'));
610 return ($login) ? $login : undef;
617 my ($null, $value) = split(/=/, $line, 2);
620 $value =~ s/\s#.*//g;
622 # remove any trailing whitespace
623 $value =~ s/^\s*(.*?)\s*$/$1/;
628 sub pg_database_administration {
630 $form->{dbdriver} = 'Pg';
635 sub dbselect_source {
636 $form->{dbport} = '5432';
637 $form->{dbuser} = 'postgres';
638 $form->{dbdefault} = 'template1';
639 $form->{dbhost} = 'localhost';
641 $form->{title} = "Lx-Office ERP / " . $locale->text('Database Administration');
643 # Intentionnaly disabled unless fixed to work with the authentication DB.
644 $form->{ALLOW_DBBACKUP} = 0; # "$pg_dump_exe" ne "DISABLED";
647 print $form->parse_html_template("admin/dbadmin");
650 sub test_db_connection {
651 $form->{dbdriver} = 'Pg';
652 User::dbconnect_vars($form, $form->{dbname});
654 my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd});
656 $form->{connection_ok} = $dbh ? 1 : 0;
657 $form->{errstr} = $DBI::errstr;
659 $dbh->disconnect() if ($dbh);
661 $form->{title} = $locale->text('Database Connection Test');
663 print $form->parse_html_template("admin/test_db_connection");
667 call_sub($form->{"nextsub"});
671 call_sub($form->{"back_nextsub"});
677 . $locale->text('Database Administration') . " / "
678 . $locale->text('Update Dataset');
680 my @need_updates = User->dbneedsupdate($form);
681 $form->{NEED_UPDATES} = \@need_updates;
682 $form->{ALL_UPDATED} = !scalar @need_updates;
685 print $form->parse_html_template("admin/update_dataset");
689 $form->{stylesheet} = "lx-office-erp.css";
690 $form->{title} = $locale->text("Dataset upgrade");
693 my $rowcount = $form->{rowcount} * 1;
694 my @update_rows = grep { $form->{"update_$_"} } (1 .. $rowcount);
695 $form->{NOTHING_TO_DO} = !scalar @update_rows;
696 my $saved_form = save_form();
700 print $form->parse_html_template("admin/dbupgrade_all_header");
702 foreach my $i (@update_rows) {
703 restore_form($saved_form);
705 map { $form->{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd);
707 my $controls = parse_dbupdate_controls($form, $form->{dbdriver});
709 print $form->parse_html_template("admin/dbupgrade_header");
711 $form->{dbupdate} = $form->{dbname};
712 $form->{$form->{dbname}} = 1;
714 User->dbupdate($form);
715 User->dbupdate2($form, $controls);
717 print $form->parse_html_template("admin/dbupgrade_footer");
720 print $form->parse_html_template("admin/dbupgrade_all_done");
724 $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources(\%$form);
726 $form->{CHARTS} = [];
728 opendir SQLDIR, "sql/." or $form - error($ERRNO);
729 foreach $item (sort grep /-chart\.sql\z/, readdir SQLDIR) {
730 next if ($item eq 'Default-chart.sql');
731 $item =~ s/-chart\.sql//;
732 push @{ $form->{CHARTS} }, { "name" => $item,
733 "selected" => $item eq "Germany-DATEV-SKR03EU" };
737 my $default_charset = $dbcharset;
738 $default_charset ||= Common::DEFAULT_CHARSET;
740 $form->{DBENCODINGS} = [];
742 foreach my $encoding (@Common::db_encodings) {
743 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
744 "label" => $encoding->{label},
745 "selected" => $encoding->{charset} eq $default_charset };
750 . $locale->text('Database Administration') . " / "
751 . $locale->text('Create Dataset');
754 print $form->parse_html_template("admin/create_dataset");
758 $form->isblank("db", $locale->text('Dataset missing!'));
760 User->dbcreate(\%$form);
764 . $locale->text('Database Administration') . " / "
765 . $locale->text('Create Dataset');
768 print $form->parse_html_template("admin/dbcreate");
772 @dbsources = User->dbsources_unused($form);
773 $form->error($locale->text('Nothing to delete!')) unless @dbsources;
777 . $locale->text('Database Administration') . " / "
778 . $locale->text('Delete Dataset');
779 $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
782 print $form->parse_html_template("admin/delete_dataset");
788 $form->error($locale->text('No Dataset selected!'));
791 User->dbdelete(\%$form);
795 . $locale->text('Database Administration') . " / "
796 . $locale->text('Delete Dataset');
798 print $form->parse_html_template("admin/dbdelete");
804 . $locale->text('Database Administration') . " / "
805 . $locale->text('Backup Dataset');
807 if ("$pg_dump_exe" eq "DISABLED") {
808 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
811 my @dbsources = sort User->dbsources($form);
812 $form->{DATABASES} = [ map { { "dbname" => $_ } } @dbsources ];
813 $form->{NO_DATABASES} = !scalar @dbsources;
815 my $username = getpwuid $UID || "unknown-user";
816 my $hostname = hostname() || "unknown-host";
817 $form->{from} = "Lx-Office Admin <${username}\@${hostname}>";
820 print $form->parse_html_template("admin/backup_dataset");
823 sub backup_dataset_start {
826 . $locale->text('Database Administration') . " / "
827 . $locale->text('Backup Dataset');
829 $pg_dump_exe ||= "pg_dump";
831 if ("$pg_dump_exe" eq "DISABLED") {
832 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
835 $form->isblank("dbname", $locale->text('The dataset name is missing.'));
836 $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
838 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
839 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
841 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
845 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
848 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
851 $ENV{HOME} = $tmpdir;
853 my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
854 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
855 push @args, $form->{dbname};
857 my $cmd = "${pg_dump_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
858 my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar";
860 if ($form->{destination} ne "email") {
861 my $in = IO::File->new("$cmd |");
864 unlink "${tmpdir}/.pgpass";
867 $form->error($locale->text('The pg_dump process could not be started.'));
870 print "content-type: application/x-tar\n";
871 print "content-disposition: attachment; filename=\"${name}\"\n\n";
873 while (my $line = <$in>) {
879 unlink "${tmpdir}/.pgpass";
883 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
885 if (system("$cmd > $tmp") != 0) {
886 unlink "${tmpdir}/.pgpass", $tmp;
889 $form->error($locale->text('The pg_dump process could not be started.'));
892 my $mail = new Mailer;
894 map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
896 $mail->{charset} = $dbcharset ? $dbcharset : Common::DEFAULT_CHARSET;
897 $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
900 unlink "${tmpdir}/.pgpass", $tmp;
905 . $locale->text('Database Administration') . " / "
906 . $locale->text('Backup Dataset');
909 print $form->parse_html_template("admin/backup_dataset_email_done");
913 sub restore_dataset {
916 . $locale->text('Database Administration') . " / "
917 . $locale->text('Restore Dataset');
919 if ("$pg_restore_exe" eq "DISABLED") {
920 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
923 my $default_charset = $dbcharset;
924 $default_charset ||= Common::DEFAULT_CHARSET;
926 $form->{DBENCODINGS} = [];
928 foreach my $encoding (@Common::db_encodings) {
929 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
930 "label" => $encoding->{label},
931 "selected" => $encoding->{charset} eq $default_charset };
935 print $form->parse_html_template("admin/restore_dataset");
938 sub restore_dataset_start {
941 . $locale->text('Database Administration') . " / "
942 . $locale->text('Restore Dataset');
944 $pg_restore_exe ||= "pg_restore";
946 if ("$pg_restore_exe" eq "DISABLED") {
947 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
950 $form->isblank("new_dbname", $locale->text('The dataset name is missing.'));
951 $form->isblank("content", $locale->text('No backup file has been uploaded.'));
953 # Create temporary directories. Write the backup file contents to a temporary
954 # file. Create a .pgpass file with the username and password for the pg_restore
957 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
958 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
960 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
964 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
967 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
970 $ENV{HOME} = $tmpdir;
972 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
975 if (substr($form->{content}, 0, 2) eq "\037\213") {
976 $tmpfile = IO::File->new("| gzip -d > $tmp");
980 $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600);
984 unlink "${tmpdir}/.pgpass";
987 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
990 print $tmpfile $form->{content};
993 delete $form->{content};
995 # Try to connect to the database. Find out if a database with the same name exists.
996 # If yes, then drop the existing database. Create a new one with the name and encoding
999 User::dbconnect_vars($form, "template1");
1001 my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form };
1002 my $dbh = $form->dbconnect(\%myconfig) || $form->dberror();
1006 $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g;
1008 $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|;
1009 my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname});
1011 do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|);
1015 foreach my $item (@Common::db_encodings) {
1016 if ($item->{dbencoding} eq $form->{dbencoding}) {
1021 $form->{dbencoding} = "LATIN9" unless $form->{dbencoding};
1023 do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding});
1027 # Spawn pg_restore on the temporary file.
1029 my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname});
1030 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
1033 my $cmd = "${pg_restore_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
1035 my $in = IO::File->new("$cmd 2>&1 |");
1038 unlink "${tmpdir}/.pgpass", $tmp;
1041 $form->error($locale->text('The pg_restore process could not be started.'));
1047 print $form->parse_html_template("admin/restore_dataset_start_header");
1049 while (my $line = <$in>) {
1054 $form->{retval} = $CHILD_ERROR >> 8;
1055 print $form->parse_html_template("admin/restore_dataset_start_footer");
1057 unlink "${tmpdir}/.pgpass", $tmp;
1063 unlink "$userspath/nologin";
1065 $form->{callback} = "admin.pl?action=list_users";
1067 $form->redirect($locale->text('Lockfile removed!'));
1073 open(FH, ">$userspath/nologin")
1074 or $form->error($locale->text('Cannot create Lock!'));
1077 $form->{callback} = "admin.pl?action=list_users";
1079 $form->redirect($locale->text('Lockfile created!'));
1084 call_sub($form->{yes_nextsub});
1088 call_sub($form->{no_nextsub});
1092 call_sub($form->{add_nextsub});
1096 $form->{edit_nextsub} ||= 'edit_user';
1098 call_sub($form->{edit_nextsub});
1102 $form->{delete_nextsub} ||= 'delete_user';
1104 call_sub($form->{delete_nextsub});
1108 $form->{save_nextsub} ||= 'save_user';
1110 call_sub($form->{save_nextsub});
1114 call_sub($form->{back_nextsub});
1118 foreach my $action (qw(create_standard_group dont_create_standard_group)) {
1119 if ($form->{"action_${action}"}) {
1125 call_sub($form->{default_action}) if ($form->{default_action});
1127 $form->error($locale->text('No action defined.'));