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 my $cluster_encoding = User->dbclusterencoding($form);
741 if ($cluster_encoding && ($cluster_encoding =~ m/^(?:UTF-?8|UNICODE)$/i)) {
742 if ($dbcharset !~ m/^UTF-?8$/i) {
743 $form->show_generic_error($locale->text('The selected PostgreSQL installation uses UTF-8 as its encoding. ' .
744 'Therefore you have to configure Lx-Office to use UTF-8 as well.'),
748 $form->{FORCE_DBENCODING} = 'UNICODE';
751 $form->{DBENCODINGS} = [];
753 foreach my $encoding (@Common::db_encodings) {
754 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
755 "label" => $encoding->{label},
756 "selected" => $encoding->{charset} eq $default_charset };
762 . $locale->text('Database Administration') . " / "
763 . $locale->text('Create Dataset');
766 print $form->parse_html_template("admin/create_dataset");
770 $form->isblank("db", $locale->text('Dataset missing!'));
772 User->dbcreate(\%$form);
776 . $locale->text('Database Administration') . " / "
777 . $locale->text('Create Dataset');
780 print $form->parse_html_template("admin/dbcreate");
784 @dbsources = User->dbsources_unused($form);
785 $form->error($locale->text('Nothing to delete!')) unless @dbsources;
789 . $locale->text('Database Administration') . " / "
790 . $locale->text('Delete Dataset');
791 $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
794 print $form->parse_html_template("admin/delete_dataset");
800 $form->error($locale->text('No Dataset selected!'));
803 User->dbdelete(\%$form);
807 . $locale->text('Database Administration') . " / "
808 . $locale->text('Delete Dataset');
810 print $form->parse_html_template("admin/dbdelete");
816 . $locale->text('Database Administration') . " / "
817 . $locale->text('Backup Dataset');
819 if ("$pg_dump_exe" eq "DISABLED") {
820 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
823 my @dbsources = sort User->dbsources($form);
824 $form->{DATABASES} = [ map { { "dbname" => $_ } } @dbsources ];
825 $form->{NO_DATABASES} = !scalar @dbsources;
827 my $username = getpwuid $UID || "unknown-user";
828 my $hostname = hostname() || "unknown-host";
829 $form->{from} = "Lx-Office Admin <${username}\@${hostname}>";
832 print $form->parse_html_template("admin/backup_dataset");
835 sub backup_dataset_start {
838 . $locale->text('Database Administration') . " / "
839 . $locale->text('Backup Dataset');
841 $pg_dump_exe ||= "pg_dump";
843 if ("$pg_dump_exe" eq "DISABLED") {
844 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
847 $form->isblank("dbname", $locale->text('The dataset name is missing.'));
848 $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
850 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
851 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
853 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
857 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
860 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
863 $ENV{HOME} = $tmpdir;
865 my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
866 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
867 push @args, $form->{dbname};
869 my $cmd = "${pg_dump_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
870 my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar";
872 if ($form->{destination} ne "email") {
873 my $in = IO::File->new("$cmd |");
876 unlink "${tmpdir}/.pgpass";
879 $form->error($locale->text('The pg_dump process could not be started.'));
882 print "content-type: application/x-tar\n";
883 print "content-disposition: attachment; filename=\"${name}\"\n\n";
885 while (my $line = <$in>) {
891 unlink "${tmpdir}/.pgpass";
895 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
897 if (system("$cmd > $tmp") != 0) {
898 unlink "${tmpdir}/.pgpass", $tmp;
901 $form->error($locale->text('The pg_dump process could not be started.'));
904 my $mail = new Mailer;
906 map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
908 $mail->{charset} = $dbcharset ? $dbcharset : Common::DEFAULT_CHARSET;
909 $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
912 unlink "${tmpdir}/.pgpass", $tmp;
917 . $locale->text('Database Administration') . " / "
918 . $locale->text('Backup Dataset');
921 print $form->parse_html_template("admin/backup_dataset_email_done");
925 sub restore_dataset {
928 . $locale->text('Database Administration') . " / "
929 . $locale->text('Restore Dataset');
931 if ("$pg_restore_exe" eq "DISABLED") {
932 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
935 my $default_charset = $dbcharset;
936 $default_charset ||= Common::DEFAULT_CHARSET;
938 $form->{DBENCODINGS} = [];
940 foreach my $encoding (@Common::db_encodings) {
941 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
942 "label" => $encoding->{label},
943 "selected" => $encoding->{charset} eq $default_charset };
947 print $form->parse_html_template("admin/restore_dataset");
950 sub restore_dataset_start {
953 . $locale->text('Database Administration') . " / "
954 . $locale->text('Restore Dataset');
956 $pg_restore_exe ||= "pg_restore";
958 if ("$pg_restore_exe" eq "DISABLED") {
959 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
962 $form->isblank("new_dbname", $locale->text('The dataset name is missing.'));
963 $form->isblank("content", $locale->text('No backup file has been uploaded.'));
965 # Create temporary directories. Write the backup file contents to a temporary
966 # file. Create a .pgpass file with the username and password for the pg_restore
969 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
970 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
972 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
976 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
979 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
982 $ENV{HOME} = $tmpdir;
984 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
987 if (substr($form->{content}, 0, 2) eq "\037\213") {
988 $tmpfile = IO::File->new("| gzip -d > $tmp");
992 $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600);
996 unlink "${tmpdir}/.pgpass";
999 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
1002 print $tmpfile $form->{content};
1005 delete $form->{content};
1007 # Try to connect to the database. Find out if a database with the same name exists.
1008 # If yes, then drop the existing database. Create a new one with the name and encoding
1009 # given by the user.
1011 User::dbconnect_vars($form, "template1");
1013 my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form };
1014 my $dbh = $form->dbconnect(\%myconfig) || $form->dberror();
1018 $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g;
1020 $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|;
1021 my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname});
1023 do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|);
1027 foreach my $item (@Common::db_encodings) {
1028 if ($item->{dbencoding} eq $form->{dbencoding}) {
1033 $form->{dbencoding} = "LATIN9" unless $form->{dbencoding};
1035 do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding});
1039 # Spawn pg_restore on the temporary file.
1041 my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname});
1042 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
1045 my $cmd = "${pg_restore_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
1047 my $in = IO::File->new("$cmd 2>&1 |");
1050 unlink "${tmpdir}/.pgpass", $tmp;
1053 $form->error($locale->text('The pg_restore process could not be started.'));
1059 print $form->parse_html_template("admin/restore_dataset_start_header");
1061 while (my $line = <$in>) {
1066 $form->{retval} = $CHILD_ERROR >> 8;
1067 print $form->parse_html_template("admin/restore_dataset_start_footer");
1069 unlink "${tmpdir}/.pgpass", $tmp;
1075 unlink "$userspath/nologin";
1077 $form->{callback} = "admin.pl?action=list_users";
1079 $form->redirect($locale->text('Lockfile removed!'));
1085 open(FH, ">$userspath/nologin")
1086 or $form->error($locale->text('Cannot create Lock!'));
1089 $form->{callback} = "admin.pl?action=list_users";
1091 $form->redirect($locale->text('Lockfile created!'));
1096 call_sub($form->{yes_nextsub});
1100 call_sub($form->{no_nextsub});
1104 call_sub($form->{add_nextsub});
1108 $form->{edit_nextsub} ||= 'edit_user';
1110 call_sub($form->{edit_nextsub});
1114 $form->{delete_nextsub} ||= 'delete_user';
1116 call_sub($form->{delete_nextsub});
1120 $form->{save_nextsub} ||= 'save_user';
1122 call_sub($form->{save_nextsub});
1126 call_sub($form->{back_nextsub});
1130 foreach my $action (qw(create_standard_group dont_create_standard_group)) {
1131 if ($form->{"action_${action}"}) {
1137 call_sub($form->{default_action}) if ($form->{default_action});
1139 $form->error($locale->text('No action defined.'));